Skip to content
docktionary

Snippets / Kubernetes

Deployment + Service

The standard pair for a stateless web app: a Deployment to run it, a Service to reach it.

deploymentservicestateless
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: docktionary-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: docktionary
  strategy:
    type: RollingUpdate
    rollingUpdate:
      # At most 1 pod down and 1 extra pod up at a time during a
      # rollout -- keeps capacity close to steady-state the whole time.
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: docktionary
    spec:
      containers:
        - name: app
          image: docktionary/app:1.4.0
          ports:
            - containerPort: 3000
          # readinessProbe controls traffic: a pod that fails this is
          # pulled out of the Service's endpoints but left running.
          readinessProbe:
            httpGet:
              path: /health
              port: 3000
          # livenessProbe controls the pod's lifecycle: failing this
          # gets the container killed and restarted.
          livenessProbe:
            httpGet:
              path: /health
              port: 3000
            periodSeconds: 10
          resources:
            # requests: what the scheduler reserves -- guaranteed to
            # this pod and used to decide which node it fits on.
            requests:
              cpu: "100m"
              memory: "128Mi"
            # limits: the hard ceiling -- exceed memory and the kernel
            # OOM-kills the container; exceed CPU and it's throttled.
            limits:
              cpu: "500m"
              memory: "256Mi"
---
apiVersion: v1
kind: Service
metadata:
  name: docktionary-app
spec:
  # ClusterIP (the default) load-balances across every pod matching this
  # selector -- no need to track individual pod IPs as they come and go.
  selector:
    app: docktionary
  ports:
    - port: 80
      targetPort: 3000