Kubeconfig
- Check with the kubernetes API using
curl:curl https://my-kube-playground:6443/api/v1/pods --key admin.key --cert admin.crt --cacert ca.crt - The response you receive would be the following:
{ "kind": "PodList", "apiVersion": "v1", "metadata": { "selfLink": "/api/v1/pods", }, "items": [] } - The above is then validated by the
kube-apiserverto authenticate the user. - How do you do that using the
kubectlcommand? - To do the same as the above with the
kubectlcommand:kubectl get pods --server my-kube-playground:6443 --client-key admin.key --client-certificate admin.crt --certificate-authority ca.crt - Performing the above every time is tedious. It is better to move it all to a configuration file called
KubeConfig. KubeConfig File:--server my-kube-playground:6443 --client-key admin.key --client-certificate admin.crt --certificate-authority ca.crt- Then specify the file using the
--kubeconfigoption like so:kubectl get pods --kubeconfig config - By default, the
kubectlcommand looks for the fileconfigunder$HOME/.kube/config - If you create the
KubeConfigfile under$HOME/.kube, you only need to run this part of the command:kubectl get pods - The
KubeConfigfile is in a special format. It has three sections:Clusters- various Kubernetes clusters you need access to. Can have different environment such asDevelopment,Productionor Cloud Providers such asGCP.Contexts- marriesClustersandUserstogether. They define which user account can access which cluster.Admin@Productionbeing a good example of mergingProductionandAdmin- With this, not configuring any users or authorising any access within the cluster. Using existing users with existing privileges.
Users- user accounts for which you have access to the aboveClusters. Examples areAdmin,Dev UserandProd User. The users can have different privileges on different clusters.
- How does this command fit in?
--server my-kube-playground:6443 --client-key admin.key --client-certificate admin.crt --certificate-authority ca.crt - Clusters:
--server my-kube-playground:6443 --certificate-authority ca.crt - Users:
--client-key admin.key --client-certificate admin.crt - Then create a
Contextto allow theMyKubeAdminto access themy-kube-playgroundcluster. - A real
KubeConfigfile: ``` apiVersion: v1 kind: Config
clusters:
- name: my-kube-playground cluster: certificate-authority: ca.crt server: https://my-kube-playground:6443
contexts:
- name: my-kube-admin@my-kube-playground context: cluster: user:
users:
- name: my-kube-admin user: client-certificate: admin.crt client-key: admin.key ```
- The above sections
clusters,contextsandusersis in an array format.- Can then specify multiple
clusters,usersetc in the same file with the help of arrays.
- Can then specify multiple
- Another good real
KubeConfigexample: ``` apiVersion: v1 kind: Config
clusters:
- name: my-kube-playground cluster: certificate-authority: ca.crt server: https://my-kube-playground:6443
- name: development
- name: production
- name: google
contexts:
- name: dev-user@google
- name: prod-user@pr
- name: my-kube-admin@my-kube-playground context: cluster: user:
users:
- name: my-kube-admin user: client-certificate: admin.crt client-key: admin.key
- name: admin
- name: dev-user
- name: prod-user ```
- When the file is ready, no need to run
kubectl create -for similar, just leave it and it will automatically be read by thekube api-server. - How does
kubectlknow which context to choose from?- Can add the following option:
current-context: dev-user@google
- Can add the following option:
- What this looks like when added to the file: ``` apiVersion: v1 kind: Config
current-context: dev-user@google
clusters:
- name: my-kube-playground cluster: certificate-authority: ca.crt server: https://my-kube-playground:6443
- name: development
- name: production
- name: google
contexts:
- name: dev-user@google
- name: prod-user@pr
- name: my-kube-admin@my-kube-playground context: cluster: user:
users:
- name: my-kube-admin user: client-certificate: admin.crt client-key: admin.key
- name: admin
- name: dev-user
- name: prod-user ```
- To view the current file being used:
kubectl config view - Displays the above KubeConfig output.
- If you don’t specify the KubeConfig file, the default under
$HOME/.kube/configis used. - Then to view a specific KubeConfig file, do:
kubectl config view --kubeconfig=my-custom-config - How do you change the
Context? - To change the
current-contextfield, runkubectl config use-context prod-user@productionand that will change theContextin the file. - You can make other changes, other options that can be listed are:
kubectl config -h - How about Namespaces?
- Can you configure a
Contextto switch to a particularNamespace? - Add the
namespaceoption like so: ``` apiVersion: v1 kind: Config
clusters:
- name: production cluster: certificate-authority: ca.crt server: https://172.17.0.51:6443
contexts:
- name: admin@production context: cluster: production user: admin namespace: finance
users:
- name: admin user: client-certificate: admin.crt client-key: admin.key ```
- Then when you swittch to
admin@production, you will be automatically added to thefinancenamespace. - For certificates in the KubeConfig file, it is better to list the full path: ``` apiVersion: v1 kind: Config
clusters:
- name: production cluster: certificate-authority: /etc/kubernetes/pki/ca.crt server: https://172.17.0.51:6443
contexts:
- name: admin@production context: cluster: production user: admin namespace: finance
users:
- name: admin user: client-certificate: /etc/kubernetes/pki/users/admin.crt client-key: /etc/kubernetes/pki/users/admin.key ```
- There is another way to specify the certificate credentials.
- Instead of the
certificate-authority:field underclustersand a path to the file, can just add thecertificate-authrority-data:instead and provide the contents of the certificate itself: ``` apiVersion: v1 kind: Config
clusters:
- name: production cluster: certificate-authority-data: <CERTIFICATE_IN_BASE64_FORMAT_EXAMPLE: ca.crt | base64> server: https://172.17.0.51:6443
contexts:
- name: admin@production context: cluster: production user: admin namespace: finance
users:
- name: admin user: client-certificate: /etc/kubernetes/pki/users/admin.crt client-key: /etc/kubernetes/pki/users/admin.key ```
- Decode the certificate again using this option:
echo "LS0...bnJ(base64 encoded certificate)" | base64 --decode