kubectl | Connecting to EKS cluster using single kubeconfig file multiple kubeconfig file
Kubeconfig
apiVersion: v1
clusters:
- cluster:
server: https://dev-cluster.example.com
certificate-authority: /path/to/dev-cluster/ca.crt
name: dev-cluster
- cluster:
server: https://staging-cluster.example.com
certificate-authority: /path/to/staging-cluster/ca.crt
name: staging-cluster
- cluster:
server: https://prod-cluster.example.com
certificate-authority: /path/to/prod-cluster/ca.crt
name: prod-cluster
contexts:
- context:
cluster: dev-cluster
user: dev-user
name: dev-context
- context:
cluster: staging-cluster
user: staging-user
name: staging-context
- context:
cluster: prod-cluster
user: prod-user
name: prod-context
current-context: dev-context
users:
- name: dev-user
user:
client-certificate: /path/to/dev-user/client.crt
client-key: /path/to/dev-user/client.key
- name: staging-user
user:
client-certificate: /path/to/staging-user/client.crt
client-key: /path/to/staging-user/client.key
- name: prod-user
user:
client-certificate: /path/to/prod-user/client.crt
client-key: /path/to/prod-user/client.key
=========================================
Single Kubeconfig File with Multiple Contexts
List Contexts:
kubectl config get-contexts
staging-context:kubectl config use-context staging-context
kubectl get pods
Connecting to different Kubernetes clusters via kubectl when using separate kubeconfig files or multiple contexts within a single kubeconfig file is straightforward. Here’s how to manage and connect to different clusters:
1. Using Separate Kubeconfig Files
If you have different kubeconfig files for each environment, you can specify which kubeconfig file kubectl should use with the --kubeconfig flag or by setting the KUBECONFIG environment variable.
Using --kubeconfig Flag
Specify the kubeconfig file directly with kubectl commands:
kubectl --kubeconfig /path/to/kubeconfig-dev get pods
This command will use the kubeconfig-dev file to connect to the Kubernetes cluster.
Setting the KUBECONFIG Environment Variable
You can set the KUBECONFIG environment variable to point to the desired kubeconfig file. This affects all kubectl commands in the current session.
On Linux/macOS:
1. Define a Shell Function
You should define a function in your shell configuration file to set the KUBECONFIG environment variable. For example:
For Bash (
~/.bashrc):
or
~/.zshrc):Use the Function
Once you’ve defined the function and reloaded your shell configuration, you can use the k8prod command to set the KUBECONFIG variable:
Comments
Post a Comment