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

Steps

  1. Create a cluster called demo. It takes a minute or two the first time:

    bash
    kind create cluster --name demo
  2. Check kubectl is talking to it:

    bash
    kubectl get nodes
  3. Save this as web.yaml. It asks for a Deployment (keep 2 copies of nginx running) and a Service (one address in front of them):

    web.yaml
    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: 80
  4. Apply it, then watch the pods start:

    bash
    kubectl apply -f web.yaml
    kubectl get pods
  5. Forward a port to the Service and open http://localhost:8080:

    bash
    kubectl port-forward service/web 8080:80
  6. In another terminal, scale to three copies and see the new pod appear:

    bash
    kubectl scale deployment web --replicas=3
    kubectl get pods
  7. Delete the cluster when you're done:

    bash
    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.

Next steps