liveBook · Manning

Service

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

A Kubernetes abstraction that exposes a set of pods through a stable network endpoint and service-discovery identity.

A Kubernetes service gives a microservice a network identity. It creates a DNS record that exposes the application within the cluster; its configuration can also make the application reachable from outside. The application runs in a pod, while a deployment manages that pod and replaces it if it fails. Labels connect the deployment, pod, and service. section 6.15 section 6.4.3 section 6.8.3

Start with a YAML file containing both a Deployment and a Service. The deployment’s pod template labels the pod; the service’s selector uses the same label to expose the microservice to HTTP requests. Keeping both resources in one file is convenient, but they can be split into separate files. section 6.8.3

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

In this Azure configuration, the service selects pods labeled app: video-streaming and maps TCP port 80 to the application’s port 4000. Type LoadBalancer requests an external load balancer and IP address. section 6.11.4

Deploy and check the service alongside the pod and deployment:

  • Run `kubectl apply -f scripts/deploy.yaml` to create the resources described by the file. section 6.11.5
  • Run `kubectl get pods`, `kubectl get deployments`, and `kubectl get services`. The services output shows where the service is exposed; one local NodePort example reports `80:30000/TCP`. section 6.8.5
  • After testing, run `kubectl delete -f scripts/deploy.yaml` and check the same resource lists to confirm the service, deployment, and pod are gone. section 6.11.7

Choose external exposure deliberately. A LoadBalancer makes HTTP testing from a development computer possible, but it also exposes the microservice to attacks and abuse. Remove an experimental deployment when testing is finished. section 6.11.4 section 6.11.7

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

How deep it goes
Works through YAML selectors, ports, deployment checks, and cleanup; defers detailed scaling and redundancy.
Examples
Uses a video-streaming microservice, an Azure LoadBalancer manifest, and kubectl output showing a local NodePort on port 30000.
What it assumes
Assumes a built Docker image and Kubernetes cluster; requires a published image and connected container registry for the Azure deployment.
What you can do after
Operate/configure it with YAML and kubectl to deploy, inspect, and remove a microservice’s service.
How it connects
Connects services to pods, deployments, labels, DNS, NodePort, and LoadBalancer.

Also explained in

Compare how 12 books explain service →

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.