Deploy your first app to Kubernetes
Create a local cluster with kind, deploy a web server, reach it, scale it to three copies and clean up.
· Updated
Kubernetes runs containers across a group of machines and keeps them running: if one stops, it starts another. This guide uses kind, which runs a whole cluster inside Docker on your computer, so nothing costs money.
Before you start
- Docker running, and
kubectlinstalled (see Set up your DevOps workstation). - kind installed, following its quick start.
Steps
Create a cluster called
demo. It takes a minute or two the first time:kind create cluster --name demoCheck
kubectlis talking to it:kubectl get nodesSave this as
web.yaml. It asks for a Deployment (keep 2 copies of nginx running) and a Service (one address in front of them):apiVersion: apps/v1 kind: Deployment metadata: name: web spec: replicas: 2 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx image: nginx:1.27 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: web spec: selector: app: web ports: - port: 80 targetPort: 80Apply it, then watch the pods start:
kubectl apply -f web.yaml kubectl get podsForward a port to the Service and open http://localhost:8080:
kubectl port-forward service/web 8080:80In another terminal, scale to three copies and see the new pod appear:
kubectl scale deployment web --replicas=3 kubectl get podsDelete the cluster when you're done:
kind delete cluster --name demo
What happens next
Delete one of the pods with kubectl delete pod <name> and run kubectl get pods again: Kubernetes starts a replacement straight away, because the Deployment asks for three.
Tip
Keep files like web.yaml in Git. Then the cluster's setup is written down, reviewed and easy to recreate.