Skip to content

Kubernetes

Cluster & Context

bash
kubectl cluster-info                    # View cluster information
kubectl config current-context          # Show current context
kubectl config use-context <context>    # Switch context
kubectl get nodes                       # List all nodes

Viewing Resources

bash
kubectl get pods                        # List all pods
kubectl get pods -n <namespace>         # List pods in specific namespace
kubectl get services                    # List all services
kubectl get deployments                 # List deployments
kubectl describe pod <pod-name>         # Show detailed info about a pod
kubectl get all                         # List all resources

Creating & Applying

bash
kubectl apply -f deployment.yaml        # Apply/create resources from file
kubectl create deployment nginx --image=nginx  # Create deployment imperatively
kubectl expose deployment nginx --port=80 --type=LoadBalancer  # Expose service

Updating & Scaling

bash
kubectl scale deployment nginx --replicas=3  # Scale to 3 replicas
kubectl set image deployment/nginx nginx=nginx:1.19  # Update image
kubectl rollout status deployment/nginx  # Check rollout progress
kubectl rollout undo deployment/nginx    # Rollback to previous version

Debugging & Logs

bash
kubectl delete pod <pod-name>           # Delete a pod
kubectl delete deployment <name>        # Delete a deployment
kubectl delete -f deployment.yaml       # Delete resources from file

Advanced

bash
kubectl explain pods                    # Show resource documentation
kubectl top nodes                       # View resource usage
kubectl get events                      # View cluster events

YAML Example

Pod

yaml
apiVersion: v1
kind: Pod
metadata:
  name: simple-pod
  labels:
    app: myapp
spec:
  containers:
  - name: nginx-container
    image: nginx:latest
    ports:
    - containerPort: 80
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

Key fields:

  • apiVersion - API version for the resource (v1 for Pod)
  • kind - Resource type (Pod)
  • metadata.name - Name of the pod
  • metadata.labels - Labels for organizing and selecting pods
  • spec.containers - List of containers in the pod
    • name - Container name
    • image - Docker image to use
    • ports - Exposed ports
    • resources - CPU and memory requests/limits

To create this pod:

bash
kubectl apply -f pod.yaml

Deployment

A Deployment manages multiple Pod replicas and provides rolling updates:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: nginx-container
        image: nginx:latest
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "500m"

Key fields:

  • spec.replicas - Number of Pod replicas to maintain
  • spec.selector - Selects pods by labels (matches template labels)
  • spec.template - Pod template for creating replicas

To create this deployment:

bash
kubectl apply -f deployment.yaml

Service

A Service exposes Pods to the network using a stable IP and load balancing:

yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: myapp
spec:
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    app: myapp

Key fields:

  • spec.type - Service type (ClusterIP, NodePort, LoadBalancer)
  • spec.ports - Port mappings
    • port - Service port
    • targetPort - Pod container port
  • spec.selector - Routes traffic to pods matching these labels

To create this service:

bash
kubectl apply -f service.yaml

How They Work Together

Service (nginx-service)
    ↓ (selector: app: myapp)
Deployment (nginx-deployment)
    ↓ (creates 3 replicas)
Pods (with label: app: myapp)
    ├─ nginx-deployment-xxxxx (Port 80)
    ├─ nginx-deployment-yyyyy (Port 80)
    └─ nginx-deployment-zzzzz (Port 80)
  1. Deployment creates 3 nginx Pod replicas with label app: myapp
  2. Service uses selector app: myapp to find and route traffic to these Pods
  3. Clients access the Service, which load-balances requests across the Pods

Contact: M_Bergmann AT gmx.at