Back to DevOps

Kubernetes Tutorial

Master Kubernetes fundamentals with our comprehensive tutorial. Learn container orchestration, deployment strategies, and cluster management through practical examples.

Video Tutorial

Introduction to Kubernetes

Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It groups containers into logical units for easy management and discovery.

Examples:

kubectl version

Check your Kubernetes client and server version

kubectl cluster-info

Display cluster information

kubectl get nodes

List all nodes in the cluster

Pods - The Smallest Deployable Units

Pods are the smallest deployable units in Kubernetes. A Pod represents a single instance of a running process in your cluster and can contain one or more containers.

Examples:

kubectl run nginx --image=nginx

Create a simple pod running nginx

kubectl get pods

List all pods in the current namespace

kubectl get pods -o wide

List pods with additional information (IP, Node, etc.)

kubectl describe pod nginx

Get detailed information about a specific pod

kubectl logs nginx

View logs from a pod

kubectl exec -it nginx -- /bin/bash

Execute interactive shell inside a pod

kubectl delete pod nginx

Delete a pod

Deployments - Managing Application Lifecycle

Deployments provide declarative updates for Pods and ReplicaSets. They manage the rollout of new versions and can automatically roll back if something goes wrong.

Examples:

kubectl create deployment nginx --image=nginx --replicas=3

Create a deployment with 3 replicas

kubectl get deployments

List all deployments

kubectl scale deployment nginx --replicas=5

Scale a deployment to 5 replicas

kubectl set image deployment/nginx nginx=nginx:1.21

Update the image of a deployment

kubectl rollout status deployment/nginx

Check the rollout status of a deployment

kubectl rollout undo deployment/nginx

Rollback to the previous deployment version

Services - Exposing Applications

Services provide a stable network endpoint to access a set of Pods. They enable load balancing and service discovery within the cluster.

Examples:

kubectl expose deployment nginx --port=80 --type=ClusterIP

Expose a deployment as a ClusterIP service (internal only)

kubectl expose deployment nginx --port=80 --type=NodePort

Expose a deployment as a NodePort service (external access)

kubectl expose deployment nginx --port=80 --type=LoadBalancer

Expose a deployment as a LoadBalancer service (cloud provider)

kubectl get services

List all services

kubectl describe service nginx

Get detailed information about a service

ConfigMaps and Secrets

ConfigMaps store non-confidential configuration data, while Secrets store sensitive information like passwords and API keys.

Examples:

kubectl create configmap app-config --from-literal=APP_ENV=production

Create a ConfigMap from literal values

kubectl create configmap app-config --from-file=config.properties

Create a ConfigMap from a file

kubectl get configmaps

List all ConfigMaps

kubectl create secret generic db-password --from-literal=password=mypassword

Create a Secret from literal values

kubectl get secrets

List all Secrets

Namespaces - Organizing Resources

Namespaces provide a way to divide cluster resources between multiple users or teams. They're ideal for environments with many users spread across multiple teams or projects.

Examples:

kubectl get namespaces

List all namespaces

kubectl create namespace development

Create a new namespace

kubectl get pods --namespace=development

List pods in a specific namespace

kubectl config set-context --current --namespace=development

Set default namespace for current context

kubectl delete namespace development

Delete a namespace (and all resources in it)

YAML Manifests - Declarative Configuration

Kubernetes resources are typically defined using YAML files. This allows for version control and reproducible deployments.

Examples:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
        - containerPort: 80

A basic Deployment manifest apiVersion: API version kind: Resource type metadata: Resource metadata spec: Desired state specification

kubectl apply -f deployment.yaml

Apply a YAML manifest to create/update resources

kubectl delete -f deployment.yaml

Delete resources defined in a YAML file

kubectl get deployment nginx-deployment -o yaml

Export a resource as YAML

Monitoring and Debugging

Kubernetes provides various commands to monitor and debug your applications running in the cluster.

Examples:

kubectl top nodes

Display resource usage (CPU/Memory) of nodes

kubectl top pods

Display resource usage of pods

kubectl get events

List cluster events

kubectl logs -f nginx

Stream logs from a pod in real-time -f: Follow log output

kubectl port-forward pod/nginx 8080:80

Forward local port 8080 to pod port 80 for testing

kubectl describe pod nginx

Get detailed information and events for a pod

Quick Reference

Essential Commands

  • kubectl get pods - List pods
  • kubectl apply -f - Apply config
  • kubectl logs - View logs
  • kubectl describe - Get details

Best Practices

  • ✓ Use namespaces for organization
  • ✓ Define resource limits
  • ✓ Use liveness/readiness probes
  • ✓ Version control your manifests