Avatar

Vinod Kanneganti's Blog

Learn Kubernetes

· 451 words · 3 minutes ·

A concise walkthrough of Kubernetes—the de facto container orchestration system.*

Introduction

Kubernetes (K8s) automates deployment, scaling, and management of containerized applications. It abstracts infrastructure and offers primitives to build cloud-native systems.


1. Set Up Kubernetes Locally

kubectl version --client          # Check kubectl version (CLI tool for Kubernetes)
minikube start                    # Start a local single-node Kubernetes cluster

kubectl is the command-line interface to communicate with Kubernetes. minikube is a tool to run Kubernetes locally for learning and testing.


2. Basic kubectl Commands

kubectl cluster-info             # Show cluster master and services
kubectl get nodes                # List all nodes in the cluster
kubectl get pods                 # List all pods in the default namespace

These commands help you explore and verify the cluster’s health and components.


3. Create a Deployment

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

A Deployment ensures a specified number of pods (replica sets) are running and up to date. Here, we deploy an Nginx container.


4. Expose the Deployment

kubectl expose deployment nginx --port=80 --type=NodePort  # Expose it on a network-accessible port

This creates a Service that exposes the deployment to traffic from outside the cluster.


5. View Services

kubectl get services              # List services and their cluster IPs/ports
minikube service nginx --url     # Open the exposed service in the browser (Minikube only)

kubectl get services shows the available services. minikube service gives you the public URL to access them.


6. View Logs and Status

kubectl get pods                  # Check pods running
kubectl logs <pod-name>          # View logs for a specific pod
kubectl describe pod <pod-name>  # Detailed status of a pod

Useful for debugging and observing what’s happening in a pod.


7. Update the Deployment

kubectl set image deployment/nginx nginx=nginx:1.25.2   # Update container image version

This triggers a rolling update of pods in the deployment with the new image.


8. Scale the Deployment

kubectl scale deployment nginx --replicas=3  # Scale up to 3 replicas

This increases the number of pod replicas to handle more traffic or ensure redundancy.


9. Delete Resources

kubectl delete service nginx     # Delete the service
kubectl delete deployment nginx  # Delete the deployment

Use these to clean up your resources after testing.


10. Use YAML Manifests

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
kubectl apply -f deployment.yaml     # Apply configuration from file
kubectl delete -f deployment.yaml    # Delete resources from file

YAML manifests are the preferred way to define resources in a declarative format.


Conclusion

Kubernetes empowers you to manage and scale containerized applications in a declarative and fault-tolerant way. It abstracts away infrastructure concerns and provides powerful orchestration primitives.

Start building cloud-native apps with Kubernetes! 🚀