kubernetes-solutions

🚀 Kubernetes Solutions 🚀

Kubernetes solutions

Common issues and solutions related to Kubernetes

Kubernetes Architecture

Kubernetes Architecture


Kubernetes Versions:

Kubernetes solutions


🕸️ Kubernetes Sample Templates Examples 🚀

🕸️ Kubernetes Sample Templates  🚀


Common Issues & Solutions

Issue 1: Ingress not getting removed

Even after running the command kubectl delete ingress <ingress_name> --force --grace-period=0, the Ingress is not getting removed.

Solution:

You can patch the Ingress using the following command:

kubectl patch ingress <name-of-the-ingress> -n <your-namespace> -p '{"metadata":{"finalizers":[]}}' --type=merge

Issue 2: Debugging the Kubernetes Pod

Solution:

You can create a new Pod with the curl image and open a shell in it using the following command:

kubectl run curlpod --image=curlimages/curl -i --tty -- sh

Once you’ve finished testing, you can press Ctrl+D to escape the terminal session in the Pod. The Pod will continue running afterwards. You can check the status of the Pod using:

$ kubectl get pod curlpod
NAME        READY   STATUS    RESTARTS   AGE
curlpod   1/1     Running   1          72s

The Pod is still there!

You can re-enter the Pod again using the kubectl exec command:

kubectl exec -it curlpod sh
kubectl attach curlpod -c curlpod -i -t

Or, you can delete the Pod with the kubectl delete pod command:

kubectl delete po curlpod

Issue 3: Drain the pods and delete the node

Solution:

You can patch the Ingress using the following command:

kubectl cordon <node_name>
kubectl drain <node_name>
kubectl delete node <node_name>

Issue 4: Deschedule or cordon the nodes in kuberentes

Solution:

kubectl cordon <node-name>
kubectl get nodes

Schedule the node back so that the pods can be scheduled

kubectl uncordon <node-name>

Issue 5: Execute the command inside the container

Solution:

kubectl exec -i -t <pod_name> -- cat /etc/resolv.conf
kubectl exec -i -t <pod_name> -- nslookup kubernetes.default

Issue 6: Track and sort by pod memory usage

Solution:

kubectl top pods -a --sort-by=memory
kubectl top pods -n <namespace> --selector=app=<app-lablename> --sort-by=memory

Issue 7: Scale down/Up the replicas for deployments and statefulset

Solution:

kubectl scale deployments <deployment-name> --replicas=<new-replicas>
kubectl scale statefulsets <stateful-set-name> --replicas=<new-replicas>

Issue 8: Sort the pods based on the creation timestamp

Solution:

kubectl get po -A --sort-by=.metadata.creationTimestamp

Issue 9: Debugging Pods with Ephemeral Containers

Solution:

kubectl alpha debug -it <podname> --image=busybox --target=containername

Issue 9: Kubectl Debug for Direct Container Debugging

Solution:

kubectl debug provides a way to create a temporary duplicate of a pod and replace its containers with debug versions or add new troubleshooting tools without affecting the original pod. This is incredibly useful for debugging issues in a live environment without impacting the running state of your application

kubectl debug pod/myapp-pod -it --copy-to=myapp-debug --container=myapp-container --image=busybox

Issue 10: Kubernetes API for Dynamic Interaction and Automation

Solution:

curl -X GET https://<kubernetes-api-server>/api/v1/namespaces/default/pods \
  -H "Authorization: Bearer <your-access-token>" \
  -H 'Accept: application/json'

Issue 11: Create the configmap pointing to a file

Solution:

value.txt

Hello world
kubectl create configmap test-cm --from-file=greeting=value.txt --namespace test

image

Issue 12: Create the Deployment template

Solution:

kubectl create deployment sample-deployment --image=busybox -n test --replicas=5 --dry-run=client -o yaml > sample-deployment.yaml

image

Issue 13: Create the nginx pod template

Solution:

kubectl run nginx --image=nginx --restart=Never  --port=80 --dry-run=client -o yaml

image

Issue 14: Set the image to particular version without editing the image

Solution:

kubectl set image pod/nginx nginx=nginx:1.15-alpine

Issue 15: Check the image version without describe command

Solution:

kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'

Issue 16: Create a busybox pod with command sleep 3600

Solution:

kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600"

Issue 17: Create a busybox pod and echo message ‘How are you’ and have it deleted immediately

Solution:

Use the --rm flag

kubectl run busybox --image=nginx --restart=Never -it --rm -- echo "How are you"

Issue 18: Different level of verbosity

Solution:

Use the --v flag

kubectl get po nginx --v=7
kubectl get po nginx --v=8
kubectl get po nginx --v=9

Issue 19: List the pod with custom columns POD_NAME and POD_STATUS

Solution:

kubectl get po -o=custom-columns="POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state"

Issue 20: Check the previous logs of the container

Solution:

kubectl logs busybox -c busybox2 --previous

Issue 21: Node with a specific GPU and you want to reserve it for running only GPU-intensive workloads

Solution:

kubectl taint nodes <node-name> gpu=true:NoSchedule

This taint would prevent any new pods from being scheduled on this node unless they tolerate the “gpu=true” taint.

Tolerations are applied to pods and indicate that the pod can be scheduled on nodes with specific taints. A pod with toleration will only be scheduled on nodes that have a matching taint. By setting tolerations, you can make sure that certain pods are placed on nodes with specific attributes or restrictions, even if those nodes are tainted.

apiVersion: v1
kind: Pod
metadata:
  name: gpu-pod
spec:
  containers:
  - name: my-app
    image: my-app-image
  tolerations:
  - key: "gpu"
    operator: "Exists"
    effect: "NoSchedule"

Issue 22: Check the resource usage per container inside the pod

Solution:

kubectl top pod --containers

Issue 23: To clean up old resources while applying new ones (super useful for selective cleanups)

Solution:

kubectl apply --prune -l app=<label>