I introduce you in this article a set of very useful commands in the Kubernetes world, everyone use a K8s must know them
Contents
To start with anything on command line: To understand the meaning of yaml files use
1 2 3 |
kubectl explain kubectl explain pods kubectl explain pods.spec |
To see the logs of a container
1 2 3 |
docker logs kubectl logs kubectl logs -c |
Container logs are automatically rotated daily and every time the log file reaches 10MB in size. The kubectl logs command only shows the log entries from the last rotation.
Know more about your kubectl and cluster
1 |
kubectl version<br>kubectl cluster-info<br>kubectl config current-context<br>kubectl get componentstatus |
generic commands
1 |
kubectl describe<br>kubectl edit<br>kubectl delete |
Creating any object in kubernetes
1 |
kubectl create -f |
modifying an object
1 |
kubectl apply -f |
Get the service acccounts
1 |
kubectl get serviceaccounts |
K8s api server details
1 |
kubectl get pod/kube-apiserver-kubemaster-01 -n kube-system -o json |
get deployment reated info
1 |
kubectl get deploy/<deplyment-name> -o json</deplyment-name> |
Rollback
Rollback -> First look at the revision names of the deployments:
1 |
kubectl rollback history deploy/ |
Rollback -> Pick a version from the past and rollback
1 |
kubectl rollback |
Get info about other objects
secrets
1 2 3 |
kubectl get secrets kubectl describe secret/default-token-3dj6w kubectl get secret/default-token-3dj6w -o json |
Role
1 2 |
kubectl get role/<role-name> -o json kubectl get rolebinding/<rolebinding-name> -o json</rolebinding-name></role-name> |
Pods
1 2 3 4 |
kubectl get pods --all-namespaces kubectl get pods kubectl describe pod/<podname> kubectl describe pod/<podname> -o json|wide </podname></podname> |
Entering into a container running inside a pod :
1 2 |
kubectl exec <podname> -c <containername> -i -t -- bash kubectl exec -it <containername> -- /bin/bash</containername></containername></podname> |
labels
1 2 3 4 5 |
kubectl get pods --show--labels kubectl get pods --selector <key-label>=<value-label> kubectl get pods -l <key-label>=<value-label> kubectl get pods -l '<label-name> in (label-value1, label-value2)' e.g. : kubectl get pods -l 'env in (dev, prod)'</label-name></value-label></key-label></value-label></key-label> |
Investigating the logs:
Controller Deployment
1 |
logs kubectl get deploy/ -o json |
pod logs kubectl logs pod/
To get more columns in the result use -L
1 2 3 4 5 |
kubectl get po -L creation_method,env NAME READY STATUS RESTARTS AGE CREATION_METHOD ENV kubia-manual 1/1 Running 0 16m <none> <none> kubia-manual-v2 1/1 Running 0 2m manual prod kubia-zxzij 1/1 Running 0 1d <none> <none> |
To get more all the pods that have label creation_method:manual
1 |
kubectl get po -l creation_method=manual |
To list all pods that include the env label, whatever its value is:
1 2 3 |
kubectl get po -l env NAME READY STATUS RESTARTS AGE kubia-manual-v2 1/1 Running 0 37m |
namespaces
1 |
kubectl config set-context current-context --name-space name-space-name |
When you want to figure out why the previous container terminated, you’ll want to see those logs instead of the current container’s logs. This can be done by using the –previous option:
1 |
kubectl logs mypod --previous |
Instead of using the kubectl scale command, you’re going to scale it in a declarative way by editing the ReplicationController’s definition:
1 |
kubectl edit rc kubia |
When deploying a pod, you don’t need to constantly poll the list of pods by repeatedly executing kubectl get pods. Instead, you can use the –watch flag and be notified of each creation, modification, or deletion of a pod,
1 |
kubectl get pods --watch |
Both the Control Plane components and the Kubelet emit events to the API server as they perform these actions. They do this by creating Event resources, which are like any other Kubernetes resource.
1 |
kubectl get events --watch |
Remove the taints on the master so that you can schedule pods on master as well.
1 |
kubectl taint nodes --all node-role.kubernetes.io/master- |
Run Kubectl with better logging to see what kubectl is doing.
1 |
kubectl get pods -v=8 |
Conclusion
In this post, i have learned a usefull command in kubernetes field, these command will help you to manage and debug your cluster easily, if you have any questions or feedback, feel free to leave a comment.
As always, if you found this post useful, then click like and share it 🙂