1. Introduction & use cases
Kubernetes (K8s) is an open-source system for running containers across a cluster of machines. You describe the desired state in YAML – “run 3 copies of this image, expose them on port 80” – and Kubernetes keeps it true: it schedules containers (Pods), restarts failed ones, rolls out new versions gradually and scales up or down.
Key objects: Deployment (manages Pods), Service (stable network address), ConfigMap/Secret (configuration), and Ingress or the newer Gateway API (HTTP routing from outside). In production most teams use a managed cluster: Amazon EKS, Google GKE or Azure AKS.
Where it is used
- Running many microservices reliably
- Zero-downtime rolling deployments and rollbacks
- Automatic scaling with traffic
- Portable infrastructure across clouds
2. Prerequisites
| You need | Version / notes |
|---|---|
| Docker | To build images and run kind – see the Docker guide |
| kubectl | Kubernetes command-line tool |
| kind or minikube | Local single-machine cluster |
| Hardware | 8 GB RAM recommended for local clusters |
3. Local setup
1Install kubectl
Follow kubernetes.io for your OS (Homebrew, winget or the Linux binary).
BASH# macOS brew install kubectl # Windows winget install Kubernetes.kubectl kubectl version --client2Install kind and create a cluster
kind runs Kubernetes nodes as Docker containers.
BASH# macOS: brew install kind · Windows: winget install Kubernetes.kind kind create cluster --name dev kubectl cluster-info --context kind-dev kubectl get nodes
4. Create & run a project
1Create a Deployment
Runs 2 replicas of a container with health checks and resource limits.
k8s/deployment.yamlapiVersion: apps/v1 kind: Deployment metadata: name: web spec: replicas: 2 selector: matchLabels: { app: web } template: metadata: labels: { app: web } spec: containers: - name: web image: nginxdemos/hello:latest ports: - containerPort: 80 readinessProbe: httpGet: { path: /, port: 80 } resources: requests: { cpu: 100m, memory: 64Mi } limits: { cpu: 250m, memory: 128Mi }2Expose it with a Service
A ClusterIP Service gives the Pods one stable address inside the cluster.
k8s/service.yamlapiVersion: v1 kind: Service metadata: name: web spec: selector: { app: web } ports: - port: 80 targetPort: 803Apply and open it
Port-forward to your machine to test.
BASHkubectl apply -f k8s/ kubectl get pods,svc kubectl port-forward svc/web 8080:80 # open http://localhost:80804Add configuration and secrets
Create them from literals (or files) and reference them as environment variables.
BASHkubectl create configmap web-config --from-literal=APP_MODE=demo kubectl create secret generic web-secret --from-literal=API_KEY=change-me # in the container spec: # envFrom: # - configMapRef: { name: web-config } # - secretRef: { name: web-secret }5Update and roll back
Changing the image starts a rolling update;
undoreturns to the previous version.BASHkubectl set image deployment/web web=nginxdemos/hello:plain-text kubectl rollout status deployment/web kubectl rollout undo deployment/web
✅ When it works you should see output like Figure 1.
5. Development configuration
Environment variables
Use ConfigMaps for normal settings and Secrets for sensitive values. Secrets are only Base64-encoded by default – enable encryption at rest, restrict RBAC, or use an external secret manager (AWS Secrets Manager, Vault) with the External Secrets Operator.
apiVersion: v1
kind: Secret
metadata:
name: web-secret
type: Opaque
stringData:
DATABASE_URL: postgres://app:CHANGE@db:5432/app
JWT_SECRET: CHANGEKey dependencies
| Package / tool | Used for |
|---|---|
kubectl | CLI to manage clusters |
kind / minikube | Local clusters |
Helm | Package manager for Kubernetes apps |
Kustomize (built into kubectl) | Environment-specific overlays |
metrics-server | CPU/memory metrics for autoscaling |
cert-manager | Automatic TLS certificates (Let’s Encrypt) |
Folder structure
k8s/
├── base/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
└── overlays/
├── staging/
└── production/ # replicas, image tag, ingressEssential commands
| Command | What it does |
|---|---|
kubectl apply -f k8s/ | Create/update resources |
kubectl get pods -w | Watch Pods |
kubectl describe pod NAME | Events and reasons for failures |
kubectl logs -f deploy/web | Logs |
kubectl exec -it POD -- sh | Shell in a Pod |
kubectl rollout undo deploy/web | Roll back |
6. Production deployment
Production clusters are usually managed (EKS, GKE, AKS) so the cloud provider runs the control plane. You add an HTTP entry point – an Ingress controller or a Gateway API implementation – plus cert-manager for TLS. Note: the community ingress-nginx controller was announced for retirement in November 2025; for new clusters prefer your cloud provider’s load-balancer controller or a Gateway API implementation.
Server & hosting requirements
| Requirement | Recommendation |
|---|---|
| Cluster | Managed EKS / GKE / AKS (3+ nodes across zones) |
| Registry | ECR / Artifact Registry / ACR / GHCR |
| Entry point | Ingress controller or Gateway API + cloud load balancer |
| TLS | cert-manager with Let’s Encrypt, or cloud certificates |
| Observability | Metrics, logs, alerts (Prometheus/Grafana or cloud tools) |
Deployment steps
1Create a managed cluster
Example with
eksctlon AWS (GKE:gcloud container clusters create-auto; AKS:az aks create).BASHeksctl create cluster --name prod --region ap-south-1 --nodes 3 --node-type t3.medium kubectl get nodes2Push your image to a registry
Build, tag with a version and push.
BASHdocker build -t <account>.dkr.ecr.ap-south-1.amazonaws.com/web:1.0.0 . docker push <account>.dkr.ecr.ap-south-1.amazonaws.com/web:1.0.03Install an HTTP entry point and cert-manager
Install the ingress/Gateway controller recommended for your cluster (for example the AWS Load Balancer Controller on EKS) and cert-manager, following their official docs, then create a Let’s Encrypt issuer.
k8s/cluster-issuer.yamlapiVersion: cert-manager.io/v1 kind: ClusterIssuer metadata: name: letsencrypt spec: acme: server: https://acme-v02.api.letsencrypt.org/directory email: you@example.com privateKeySecretRef: { name: letsencrypt-key } solvers: - http01: ingress: { ingressClassName: YOUR_INGRESS_CLASS }4Route your domain with TLS
An Ingress (or HTTPRoute with Gateway API) maps the host to the Service; cert-manager stores the certificate in the named secret.
k8s/ingress.yamlapiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: web annotations: cert-manager.io/cluster-issuer: letsencrypt spec: ingressClassName: YOUR_INGRESS_CLASS tls: - hosts: [example.com] secretName: web-tls rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: { name: web, port: { number: 80 } }5Point DNS to the load balancer
Get the external address and create a CNAME/ALIAS record for your domain.
BASHkubectl get ingress web # ADDRESS: k8s-default-web-xxxx.ap-south-1.elb.amazonaws.com # DNS: example.com ALIAS/CNAME → that address6Autoscale
With metrics-server installed, scale between 2 and 10 replicas on CPU.
BASHkubectl autoscale deployment web --cpu-percent=70 --min=2 --max=10 kubectl get hpa
🗺 The whole path from your code to the live site is drawn in the deployment diagram.
7. Images & screenshots
Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.
8. Common errors & solutions
Pod stuck in ImagePullBackOff
Wrong image name/tag or no access to a private registry. Check kubectl describe pod and add an imagePullSecret or node IAM permissions.
CrashLoopBackOff
The container starts and exits. Read kubectl logs POD --previous; usually missing env vars or a failing command.
Pod Pending
Not enough CPU/memory on nodes for the requests, or an unbound PersistentVolumeClaim. See Events in kubectl describe pod.
Service returns nothing / 503
Service selector labels do not match Pod labels, or readiness probes fail. Compare labels and check kubectl get endpoints.
Certificate not issued
Check kubectl describe certificate and the challenge; DNS must already point to the load balancer for HTTP-01.
Best practices
- Always set resource requests/limits and readiness/liveness probes.
- Pin image tags (no
latest) and roll out with versioned manifests. - Keep manifests in Git and deploy via CI (GitOps with Argo CD/Flux).
- Use namespaces and RBAC to separate teams and environments.
- Run at least 2 replicas across zones for anything important.