Skip to main content

How to deploy an application to Kubernetes with Deployment and Service

Deploy to Kubernetes by defining a Deployment that maintains replica pods and a Service that exposes them with a stable name. Scale with kubectl scale and ship updates with rolling rollouts.

Difficulty
Beginner
Duration
40 minutes
Steps
6

Deploy an application to Kubernetes

Kubernetes runs containers as pods managed by controllers. A Deployment keeps a desired number of identical pods running and handles rolling updates. A Service gives those pods a stable network identity. Together they form the basic unit of a deployed app.

Prerequisites

  • A Kubernetes cluster you can reach (kubectl get nodes works).
  • A container image pushed to a registry the cluster can pull from.

Steps

1. Push your image to a registry

docker tag myapp:latest registry.example.com/myapp:1.0.0
docker push registry.example.com/myapp:1.0.0

Use an immutable tag like a version or digest rather than latest.

2. Write a Deployment manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: registry.example.com/myapp:1.0.0
          ports:
            - containerPort: 3000

3. Apply and inspect pods

kubectl apply -f deployment.yaml
kubectl get pods -l app=myapp
kubectl describe deployment myapp

4. Expose with a Service

apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  selector:
    app: myapp
  ports:
    - port: 80
      targetPort: 3000

Apply it, then reach the app from inside the cluster at myapp:80.

5. Scale the Deployment

kubectl scale deployment myapp --replicas=5

The Deployment creates or removes pods to match.

6. Roll out a new version

Update the image and watch the rollout:

kubectl set image deployment/myapp myapp=registry.example.com/myapp:1.1.0
kubectl rollout status deployment/myapp

Roll back if needed with kubectl rollout undo deployment/myapp.

Verification

Confirm pods are Running with kubectl get pods. Port-forward to test: kubectl port-forward svc/myapp 8080:80 and curl localhost:8080. After a rollout, kubectl rollout history deployment/myapp shows revisions.

Next Steps

Add liveness and readiness probes, expose the Service externally with an Ingress, externalize configuration into ConfigMaps and Secrets, and add a HorizontalPodAutoscaler to scale on load.

Prerequisites

  • A running Kubernetes cluster
  • kubectl configured
  • A container image in a registry

Steps

  • 1
    Push your image to a registry
  • 2
    Write a Deployment manifest
  • 3
    Apply and inspect pods
  • 4
    Expose with a Service
  • 5
    Scale the Deployment
  • 6
    Roll out a new version

Category

Containers