Skip to content

Commit

Permalink
Add
Browse files Browse the repository at this point in the history
  • Loading branch information
guntiss committed Aug 29, 2024
1 parent feea9cc commit 3f58068
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions _posts/2024-08-20-using-multiple-kubeconfigs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "Using multiple kubeconfig files"
date: 2024-08-20
---

On a daily basis I'm working with multiple Kuberetes cluster. I also use [Kubernetes tools VSCode extension](https://marketplace.visualstudio.com/items?itemName=ms-kubernetes-tools.vscode-kubernetes-tools). Extension allows to easily change current context from within VSCode.

## Generating common kubeconfig

In order to switch contexts you need to merge multiple kubeconfig files into single yaml file. To do that I have created a script at `/home/guntis/.kube/merge.sh` that looks something like:
```sh
KUBECONFIG=\
~/.kube/abc-test.yaml:\
~/.kube/abc-staging.yaml:\
~/.kube/abc-prod.yaml:\
kubectl config view --flatten > ~/.kube/config
```

When you run this script it will merge all listed files into one.

Important rule is that each kubeconfig must have unique keys. For example `abc-test.yaml` file would look like this:
```yaml
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JS...ZJQ0FURS0tLS0tCg==
server: https://kubernetes.docker.internal:6443
name: abc-test
contexts:
- context:
cluster: abc-test
user: abc-test
name: abc-test
current-context: abc-test
kind: Config
preferences: {}
users:
- name: abc-test
user:
client-certificate-data: LS0tLS...S0tCg==
client-key-data: LS0tL..0tCg==
```
## Switch contexts using VSCode
![VScode Kubernetes extension](/assets/k8s-contexts.png)
## Switch contexts using k9s
K9s is super convenvient utility that also allows switching contexts, by typing `:ctx` command.
![K9s contexts](/assets/k9s-contexts.png)
Binary file added assets/k8s-contexts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/k9s-contexts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3f58068

Please sign in to comment.