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

====

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

=========================================


Single Kubeconfig File with Multiple Contexts

List Contexts:

kubectl config get-contexts

Switch to 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:

export KUBECONFIG=/path/to/kubeconfig-dev

On Windows (PowerShell):

$env:KUBECONFIG="C:\path\to\kubeconfig-dev"

kubectl --kubeconfig ~/.kube/config-dev get pods

kubectl --kubeconfig ~/.kube/config-prod get services

Set Environment Variable for Development:

export KUBECONFIG=~/.kube/config-dev
kubectl get nodes

kubectl config get-contexts


export KUBECONFIG=~/.kube/config-prod
kubectl get deployments

kubectl config get-contexts
kubectl config use-context staging-context


alias k8prod='export KUBECONFIG=~/.kube/config-prod'


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):

function k8prod {
  export KUBECONFIG=~/.kube/config-prod
}

or 

function k8prod {
  export KUBECONFIG=~/.kube/config-prod
  kubectl config get-contexts
}

source ~/.bashrc


For Zsh (~/.zshrc):

function k8prod {
  export KUBECONFIG=~/.kube/config-prod
}

or 

function k8prod {
  export KUBECONFIG=~/.kube/config-prod
  kubectl config get-contexts
}

source ~/.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

Popular posts from this blog

Kube-Proxy : Configure Production Grade Cluster

Networking : How is the Kubernetes networking done CNI is after cluster is running

Laptop : Configure your laptop to connect to AKS - Azure