DevOps & Deployment

Kubernetes Setup & Deployment Guide

Run and scale containers across machines – local cluster with kind, Deployments, Services, config and secrets, then a managed cluster with HTTPS.

Updated Official documentation ↗

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 needVersion / notes
DockerTo build images and run kind – see the Docker guide
kubectlKubernetes command-line tool
kind or minikubeLocal single-machine cluster
Hardware8 GB RAM recommended for local clusters

3. Local setup

  1. 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 --client
  2. 2Install 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

  1. 1Create a Deployment

    Runs 2 replicas of a container with health checks and resource limits.

    k8s/deployment.yaml
    apiVersion: 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 }
  2. 2Expose it with a Service

    A ClusterIP Service gives the Pods one stable address inside the cluster.

    k8s/service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: web
    spec:
      selector: { app: web }
      ports:
        - port: 80
          targetPort: 80
  3. 3Apply and open it

    Port-forward to your machine to test.

    BASH
    kubectl apply -f k8s/
    kubectl get pods,svc
    kubectl port-forward svc/web 8080:80
    # open http://localhost:8080
  4. 4Add configuration and secrets

    Create them from literals (or files) and reference them as environment variables.

    BASH
    kubectl 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 }
  5. 5Update and roll back

    Changing the image starts a rolling update; undo returns to the previous version.

    BASH
    kubectl 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.

k8s/secret.yaml (do not commit real values)
apiVersion: v1
kind: Secret
metadata:
  name: web-secret
type: Opaque
stringData:
  DATABASE_URL: postgres://app:CHANGE@db:5432/app
  JWT_SECRET: CHANGE

Key dependencies

Package / toolUsed for
kubectlCLI to manage clusters
kind / minikubeLocal clusters
HelmPackage manager for Kubernetes apps
Kustomize (built into kubectl)Environment-specific overlays
metrics-serverCPU/memory metrics for autoscaling
cert-managerAutomatic TLS certificates (Let’s Encrypt)

Folder structure

project structure
k8s/
├── base/
│   ├── deployment.yaml
│   ├── service.yaml
│   └── kustomization.yaml
└── overlays/
    ├── staging/
    └── production/   # replicas, image tag, ingress

Essential commands

CommandWhat it does
kubectl apply -f k8s/Create/update resources
kubectl get pods -wWatch Pods
kubectl describe pod NAMEEvents and reasons for failures
kubectl logs -f deploy/webLogs
kubectl exec -it POD -- shShell in a Pod
kubectl rollout undo deploy/webRoll 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

RequirementRecommendation
ClusterManaged EKS / GKE / AKS (3+ nodes across zones)
RegistryECR / Artifact Registry / ACR / GHCR
Entry pointIngress controller or Gateway API + cloud load balancer
TLScert-manager with Let’s Encrypt, or cloud certificates
ObservabilityMetrics, logs, alerts (Prometheus/Grafana or cloud tools)

Deployment steps

  1. 1Create a managed cluster

    Example with eksctl on AWS (GKE: gcloud container clusters create-auto; AKS: az aks create).

    BASH
    eksctl create cluster --name prod --region ap-south-1 --nodes 3 --node-type t3.medium
    kubectl get nodes
  2. 2Push your image to a registry

    Build, tag with a version and push.

    BASH
    docker 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.0
  3. 3Install 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.yaml
    apiVersion: 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 }
  4. 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.yaml
    apiVersion: 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 } }
  5. 5Point DNS to the load balancer

    Get the external address and create a CNAME/ALIAS record for your domain.

    BASH
    kubectl get ingress web
    # ADDRESS: k8s-default-web-xxxx.ap-south-1.elb.amazonaws.com
    # DNS: example.com  ALIAS/CNAME  → that address
  6. 6Autoscale

    With metrics-server installed, scale between 2 and 10 replicas on CPU.

    BASH
    kubectl 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.

terminal$ kubectl apply -f k8s/deployment.apps/web createdservice/web created$ kubectl get pods,svcNAME READY STATUS RESTARTS AGEpod/web-6d9f8c7b5d-7kq2x 1/1 Running 0 21spod/web-6d9f8c7b5d-zr4mp 1/1 Running 0 21sNAME TYPE CLUSTER-IP PORT(S) AGEservice/web ClusterIP 10.96.142.17 80/TCP 21s
Figure 1: terminal output of the sample Kubernetes project (section 4).
1Build + push image2kubectl applymanifests3Deployment +Service4Ingress/Gateway +TLS5DNS → loadbalancer
Figure 2: production deployment flow (section 6).
k8s/├── base/│ ├── deployment.yaml│ ├── service.yaml│ └── kustomization.yaml└── overlays/ ├── staging/ └── production/ # replicas, image tag, ingress
Figure 3: project folder structure (section 5).

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.