Kubernetes User Manual

Basic Concepts

Pod

Pod is the smallest deployable unit in Kubernetes, representing one or more containers running in the cluster. Typically a Pod contains a single container, but can also contain multiple tightly coupled containers.

Deployment

Deployments are used to manage stateless applications, providing declarative update capabilities for easy deployment, scaling, and updates.

Service

Services provide stable network endpoints for a set of Pods, maintaining stability even when Pod IP addresses change.

Namespace

Namespaces divide cluster resources into multiple logical partitions, suitable for multiple teams or projects sharing the same cluster.

Common Commands

Cluster Management

# View cluster information
kubectl cluster-info

# View nodes
kubectl get nodes

# View all Pods
kubectl get pods --all-namespaces

# View cluster resource usage
kubectl top nodes
kubectl top pods

Application Deployment

# Create a Deployment
kubectl create deployment nginx --image=nginx

# View Deployments
kubectl get deployments

# Scale
kubectl scale deployment nginx --replicas=3

# Update image
kubectl set image deployment/nginx nginx=nginx:1.21

# Rollback
kubectl rollout undo deployment/nginx

# View rollout status
kubectl rollout status deployment/nginx

Service Management

# Expose a Service
kubectl expose deployment nginx --port=80 --type=LoadBalancer

# View Services
kubectl get services

# Delete Service
kubectl delete service <service-name>

Debugging

# View Pod logs
kubectl logs <pod-name>

# Execute command in container
kubectl exec -it <pod-name> -- /bin/bash

# Describe resource details
kubectl describe pod <pod-name>

# Port forward for debugging
kubectl port-forward <pod-name> 8080:80

Resource Manifests

Kubernetes supports declarative resource management using YAML files. Here are common resource definitions:

Deployment Example

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

Service Example

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
  type: ClusterIP

ConfigMap Example

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  database_url: "postgres://db:5432/myapp"
  cache_enabled: "true"

Secret Example

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
stringData:
  username: admin
  password: changeme

Application Lifecycle Management

Deploying Applications

# Deploy using YAML file
kubectl apply -f deployment.yaml

# View deployment status
kubectl get pods -l app=my-app

Updating Applications

# Update image version
kubectl set image deployment/my-app my-app=my-app:v2

# View update progress
kubectl rollout status deployment/my-app

Rolling Back

# Rollback to previous version
kubectl rollout undo deployment/my-app

# Rollback to specific revision
kubectl rollout undo deployment/my-app --to-revision=2

# View revision history
kubectl rollout history deployment/my-app

Scaling

# Manual scaling
kubectl scale deployment my-app --replicas=5

# Auto-scaling (HPA)
kubectl autoscale deployment my-app --min=2 --max=10 --cpu-percent=80

Storage Management

Creating PersistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: standard
  hostPath:
    path: /data/my-app

Creating PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

Using PVC in Pod

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-app
    image: my-app
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: my-pvc

Configuration Management

Using ConfigMap

# Create ConfigMap from file
kubectl create configmap my-config --from-file=config.properties

# Create from env file
kubectl create configmap my-config --from-env-file=env.properties

# Use in Pod
kubectl apply -f pod-with-configmap.yaml

Using Secret

# Create Secret from file
kubectl create secret generic my-secret --from-file=credentials.json

# Use in Pod
kubectl apply -f pod-with-secret.yaml

Resource Quotas and Limits

ResourceQuota

apiVersion: v1
kind: ResourceQuota
metadata:
  name: my-quota
spec:
  hard:
    requests.cpu: "4"
    requests.memory: 8Gi
    limits.cpu: "8"
    limits.memory: 16Gi
    pods: "20"

LimitRange

apiVersion: v1
kind: LimitRange
metadata:
  name: my-limits
spec:
  limits:
  - max:
      cpu: "2"
      memory: 1Gi
    min:
      cpu: 100m
      memory: 128Mi
    default:
      cpu: 500m
      memory: 512Mi
    defaultRequest:
      cpu: 200m
      memory: 256Mi
    type: Container

Monitoring and Logging

Monitoring

# View resource usage with kubectl top
kubectl top nodes
kubectl top pods

# View detailed status
kubectl describe node <node-name>
kubectl describe pod <pod-name>

Logging

# View Pod logs
kubectl logs my-app-pod

# Follow logs in real-time
kubectl logs -f my-app-pod

# View logs of previous container instance
kubectl logs --previous my-app-pod

Best Practices

  1. Use declarative configuration to manage infrastructure
  2. Set appropriate resource limits and requests
  3. Organize resources using labels
  4. Regularly clean up unused resources
  5. Use namespaces to isolate different environments
  6. Configure health check probes
  7. Use RBAC to control permissions
  8. Regularly backup etcd data