Kubernetes Tips and Tricks

Categories: Kubernetes

Show cluster status

Check deployment status:
kubectl cluster-info

kubectl get namespace

kubectl get deployments # Show deployments in default namespace
kubectl get deployments -n <namespace> # Show deployment status in specific namespace

kubectl get pods
kubectl get pods -n <namespace>
kubectl get pods --all-namespaces -o wide # Shows all pods accross all namespaces

kubectl get service
kubectl get service -n <namespace>

kubeadm token create --print-join-command # Show the join command for adding worker nodes
kubectl scale --replicas=0 deployment/<your-deployment>

kubectl label node <nodename> <labelname>=allow # Add label to node
kubectl label node <nodename> <labelname>- # Remove label from node

Delete a pod and namespace

Method 1:

To delete everything from the current namespace (which is normally the default namespace) using kubectl delete:

kubectl delete all --all
all refers to all resource types such as pods, deployments, services, etc. –all is used to delete every object of that resource type instead of specifying it using its name or label.

To delete everything from a certain namespace you use the -n flag:

kubectl delete all --all -n <namespace>

Method 2:

You can also delete a namespace and re-create it. This will delete everything that belongs to it:

kubectl delete namespace <namespace>
kubectl create namespace <namespace>

Edit CoreDNS

kubectl -n kube-system edit configmap/coredns
Delete current coredns pod and let a new respawn
kubectl get pod -A
kubectl delete pod -n kube-system core-dns-#########

Delete multiple pods

kubectl delete --all pods --namespace=<namespace>

Updating Pod image

You need image: app-name:latest and imagePullPolicy: Always in your deployment and kubernetes 1.15 or newer

kubectl rollout restart deployment/DEPLOYMENTNAME