Skip to content

Kubernetes Operations

Prerequisites

  • K3s installed and running
  • kubectl available

Procedure: Apply a Manifest

When to use: Creating or updating Kubernetes resources (Pods, Deployments, Services).

Steps:

  1. Apply a YAML file:

    kubectl apply -f manifest.yaml
    

  2. Apply all files in a directory:

    kubectl apply -f ./k8s/
    

Troubleshooting:

  • "The connection to the server was refused": Check if K3s/K8s is running (systemctl status k3s) or if KUBECONFIG is set.

Procedure: Check Pod Status

When to use: Monitoring deployment rollout and health.

Steps:

  1. List all pods in default namespace:

    kubectl get pods
    

  2. List pods in all namespaces:

    kubectl get pods -A
    

  3. Watch for changes (live update):

    kubectl get pods -w
    

Troubleshooting:

  • "ImagePullBackOff": Check image name and registry credentials. If using a local image, ensure imagePullPolicy is set to Never or IfNotPresent and the image exists on the node.
  • "CrashLoopBackOff": Application is crashing. Check logs. Use kubectl logs <pod> --previous to see the logs of the last crashed instance.
  • "Pending": The pod cannot be scheduled. Run kubectl describe pod <name> to check for resource limits (CPU/Memory) or Node Selectors/Taints.

Procedure: View Pod Logs

When to use: Debugging application errors.

Steps:

  1. View logs:

    kubectl logs <pod_name>
    

  2. View logs of a specific container (in multi-container pod):

    kubectl logs <pod_name> -c <container_name>
    

  3. Follow logs:

    kubectl logs -f <pod_name>
    

Troubleshooting:

  • "Pod not found": Check if you are in the correct namespace (-n <namespace>).

Procedure: Create a Namespace

When to use: Isolating resources for different environments or projects.

Steps:

  1. Create namespace:

    kubectl create namespace my-app
    

  2. Switch context to use it by default:

    kubectl config set-context --current --namespace=my-app
    

Troubleshooting:

  • "Already exists": No action needed, or check spelling.

Procedure: Expose a Service via NodePort

When to use: Making an internal application accessible from outside the cluster.

Steps:

  1. Expose a deployment:

    kubectl expose deployment my-dep --type=NodePort --port=80 --name=my-svc
    

  2. Find the assigned NodePort:

    kubectl get svc my-svc
    

Troubleshooting:

  • Cannot access port: Ensure firewall allows traffic on the assigned NodePort (30000-32767).

Procedure: Create an Ingress

When to use: Exposing HTTP/HTTPS services via a domain name.

Steps:

  1. Create ingress.yaml:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: my-ingress
    spec:
      rules:
      - host: app.example.com
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: my-svc
                port:
                  number: 80
    

  2. Apply it:

    kubectl apply -f ingress.yaml
    

Troubleshooting:

  • 404 Not Found: Ensure the Ingress Controller (Traefik/Nginx) is running and DNS points to the cluster IP.

Procedure: Debug a Failing Pod

When to use: Detailed investigation of why a pod is pending or failing.

Steps:

  1. Describe the pod events and status:

    kubectl describe pod <pod_name>
    
    Look at the "Events" section at the bottom.

  2. Exec into the pod (if running):

    kubectl exec -it <pod_name> -- /bin/sh
    

Troubleshooting:

  • "SchedulingFailed": Insufficient CPU/Memory resources on nodes.

Quick Reference

Action Command
Get Pods kubectl get pods
Get All kubectl get all
Describe kubectl describe <res> <name>
Logs kubectl logs <pod>
Apply kubectl apply -f <file>
Delete kubectl delete -f <file>
Exec kubectl exec -it <pod> -- sh
  • Technologies: Kubernetes
  • Concepts: Container Orchestration