The objective of this post series helps you for taking the Certified Kubernetes Administrator (CKA) exam using Kubernetes Official Documentation. You don't need to install Kubernetes Cluster for the test that we performed on these posts. You can practice a lot with kubectl API using Katacoda.
Overview Of kubectl Command
Kubectl is a command-line interface for running commands against Kubernetes clusters. You can manage your apps and cluster and cluster resources by kubectl command. If you have already installed Kubernetes Cluster and you want to use kubectl command to manage it then check this link to install and configure kubectl command. But at this post, I will basically describe how to use it over Katacoda.
Kubectl commands use Kubeconfigfile to store Kubernetes cluster information. Basically this config file stores every information that you need to connect and run commands against the Kubernetes cluster. I want to add some information about the Kubeconfig file that helps you to understand how it works.
Kubeconfig YAML file has three objects that Clusters, Contexts, Users.
- Clusters: This is the object that you will define your cluster information like production, test, disaster, etc.
- Users: User information that will be used to manage the resources.
- Contexts: A context is a group of access parameters. Each context contains a Kubernetes cluster, a user, and a namespace. It's important that namespaces parameters must be defined in contexts.
Basic kubeconfigfile example;
apiVersion: v1 kind: Config clusters: - name: Production cluster: certificate-authority: /etc/kubernetes/pki/ca.crt server: https://172.17.0.35:6443 contexts: - name: admin@Production context: cluster: Production user: admin namespace: Billing users: - name: admin user: client-certificate: /etc/kubernetes/pki/users/test-user/test-user.crt client-key: /etc/kubernetes/pki/users/test-user/test-user.key current-context: admin@Production
You can get this information from config file located under ".kube" directory and also with kubectl command.
#cat ~/.kube/config OR: #kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: REDACTED server: https://172.17.0.35:6443 name: kubernetes contexts: - context: cluster: kubernetes user: kubernetes-admin name: kubernetes-admin@kubernetes current-context: kubernetes-admin@kubernetes kind: Config preferences: {} users: - name: kubernetes-admin user: client-certificate-data: REDACTED client-key-data: REDACTED
Time to test your skills. Use Katacoda and find out the answers.
Question 1: How to get current-context and configuration?
#kubectl config current-context kubernetes-admin@kubernetes # kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: REDACTED server: https://172.17.0.35:6443 name: kubernetes contexts: - context: cluster: kubernetes user: kubernetes-admin name: kubernetes-admin@kubernetes current-context: kubernetes-admin@kubernetes kind: Config preferences: {} users: - name: kubernetes-admin user: client-certificate-data: REDACTED client-key-data: REDACTED
Question 2: How to change current-context?
#kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE * kubernetes-admin@kubernetes kubernetes kubernetes-admin #kubectl config use-context kubernetes-admin@kubernetes Switched to context "kubernetes-admin@kubernetes".
Question 3: How to get Kubernetes cluster information?
#kubectl cluster-info Kubernetes master is running at https://172.17.0.11:8443 KubeDNS is running at https://172.17.0.11:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy #kubectl version Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:20:10Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:12:17Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"} -- To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. #kubectl cluster-info dump
Question 4: How to get running node information?
-- To further information about node and running software versions. #kubectl get nodes -o yaml #kubectl get node -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME minikube Ready master 23m v1.17.0 172.17.0.11 <none> Ubuntu 18.04.3 LTS 4.15.0-72-generic docker://18.9.7
Question 5: How to get running pods?
#kubectl get pods --all-namespaces