liveBook · Manning

Kubernetes Deployment

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A Kubernetes Deployment is a controller that manages replicated application pods and replaces failed instances to maintain the declared state.

A Kubernetes Deployment keeps the pods for a microservice running. Unlike a standalone pod, it monitors the application and replaces an instance if it crashes or stops responding. A Service has a different job: it exposes the microservice through DNS and, when configured to do so, outside the cluster. section 6.4.3 section 6.15

Listing 6.2 Deploying our microservice to Kubernetes on Azure
apiVersion: apps/v1
kind: Deployment
metadata:
  name: video-streaming
spec:
  replicas: 1
  selector:
    matchLabels:
      app: video-streaming
  template:
    metadata:
      labels:
        app: video-streaming
    spec:
      containers: 
      - name: video-streaming
        image: bmdk1.azurecr.io/video-streaming:1   #1
        imagePullPolicy: IfNotPresent               #2
        env:
        - name: PORT
          value: "4000"
---
apiVersion: v1
kind: Service
metadata:
  name: video-streaming
spec:
  selector:
    app: video-streaming
  type: LoadBalancer                                #3
  ports:
    - protocol: TCP
      port: 80
      targetPort: 4000

The YAML declares one replica, matches the Deployment’s selector to its pod-template label, and runs a versioned container image. A separate Service selects the same label and routes port 80 to the container’s port 4000. section 6.11.4 section 6.8.3

To deploy and check a microservice:

  • Build and publish a versioned image, then set the image URL in deploy.yaml to your container registry. For a cloud cluster, connect the registry so the cluster can pull the image. section 7.12 section 6.15
  • Run kubectl apply -f scripts/deploy.yaml. Check the result with kubectl get pods, kubectl get deployments, and kubectl get services; the single-replica example should have one application pod. section 6.11.5 section 6.8.5
  • When testing is finished, run kubectl delete -f scripts/deploy.yaml and check that the objects created from the file are gone. section 6.11.7

Local and cloud configurations differ. The local example uses an image already available to the cluster; the Azure example names a registry image and uses IfNotPresent to pull it when needed. Its LoadBalancer Service provides an external IP for testing, but also exposes the microservice to attacks and abuse. Multiple replicas can provide redundancy and load balancing; this working configuration starts with one. section 6.11.4 section 6.4.3 section 6.8.3

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Works through Deployment YAML, pod templates, labels, image settings, and apply-and-check commands; defers detailed scaling and redundancy.
Examples
Uses a single-replica video-streaming microservice, local and Azure YAML configurations, and kubectl output showing its pod, Deployment, and Service.
What it assumes
Assumes a built container image and a connected Kubernetes cluster; introduces the Deployment, pod, and Service arrangement before applying it.
What you can do after
Operate/configure a Deployment with kubectl and YAML, verify its pod and Service, then remove the created objects.
How it connects
Connects Deployments to pods, containers, Services, DNS, labels, container registries, and load balancing.

Also explained in

Compare how 10 books explain Kubernetes Deployment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.