“`html
How to Deploy a Docker Container to Kubernetes: A Step-by-Step Guide
Deploying a Docker container to Kubernetes moves your app from a local environment to a resilient, clustered architecture. Kubernetes handles scaling, health checks, and routing, but it doesn’t build images—it runs them. The workflow: build, push, and define your desired state with YAML.
Verify Docker and kubectl are installed and reachable—use Minikube locally or a managed service like EKS, GKE, or AKS. Check with kubectl config current-context.
1. Build and Push the Docker Image
Package your app: run docker build -t yourname/my-app:v1 ., then push it: docker push yourname/my-app:v1. Use a public registry or set up imagePullSecrets so the cluster can pull it.
2. Create a Deployment Manifest
A Deployment manages your pods. Create deployment.yaml with apiVersion: apps/v1 and kind: Deployment, including:
- replicas: desired pod count
- selector: matches pod labels
- template.spec.containers: image and port
Apply with kubectl apply -f deployment.yaml. Failed pods restart automatically.
3. Expose the App with a Service
Pods are ephemeral, so add a Service. Create service.yaml with type: LoadBalancer (or ClusterIP) targeting your container port. Apply it, then find the external address via kubectl get svc.
4. Verify and Scale
Verify with kubectl get pods and kubectl logs <pod-name>. Scale with kubectl scale deployment my-app --replicas=5.
That’s the core loop—build, push, deploy, expose, scale. With a few YAML files, you turn one container into a production-grade, self-healing service.
“`