2.6. Services and Networking
In this lab, you make an application reachable with a Service: inside the cluster, from your machine and with two versions to switch between.
Step 1: Create a Service
Create a directory for the files of this lab and change into it:
Create the file web-blue.yaml in ~/labs/2.6. The application whoami replies with the name of the Pod
that answers, so you can see where each request ends up:
Create the file web-service.yaml with a Service for these Pods:
Create the Deployment:
Create the Service:
List the Services:
Show the details of the Service:
If Endpoints is empty, the Pods are still starting. Wait a moment and repeat the command.
Explanation
A Service gives a group of Pods a stable IP address and name. It finds its Pods with the
selector, like kubectl get pods -l in 2.3: all Pods with the labels app=web and version=blue. Endpoints
lists the IP addresses of the two Pods it found. Pods come and go with new IP addresses, but the IP of the Service
stays. The second label version lets you tell two versions apart in Step 5.
port is the port of the Service, targetPort the port of the containers. The type ClusterIP (the default, as
your manifest sets no type) makes the Service reachable inside the cluster only. The Service kubernetes belongs
to the API server.
Step 2: Call the Service by Name
Start a temporary Pod and call the Service six times from inside the cluster:
kubectl may also print a few more lines, e.g. If you don't see a command prompt, try pressing enter. or
Session ended, resume using 'kubectl attach …' command. They come from connecting your terminal to the temporary
Pod. You can ignore them. Now and then, kubectl can’t connect in time: it then prints couldn't attach to pod/client, falling back to streaming logs and shows the output a second time.
Explanation
kubectl run with --rm -it starts a Pod, shows you its output and deletes it afterwards. --restart=Never keeps
it from starting again after the command ended (the restartPolicy from 2.3). Everything after -- is the command
of the container.
The Pod reached the Service by its name: CoreDNS, the DNS server of the cluster, resolves web-service to the IP of
the Service. The Service spreads the requests over both Pods.
Step 3: Forward a Port to Your Machine
Forward the port 8080 of your machine to the Service. The command keeps running:
If the port 8080 is already in use on your machine, use another one, e.g. 8081:80, and call localhost:8081.
Open http://localhost:8080 in your browser, or call it in a second terminal:
For every request, the first terminal shows Handling connection for 8080. Stop the port forwarding with Ctrl+C.
Explanation
kubectl port-forward opens a tunnel from your machine through the API server to one Pod of the Service. All
requests go to this Pod, and the Service doesn’t spread them. It’s the quickest way to look at an application while you
develop or debug it, but it only works while the command runs. The port listens on your machine only, which makes
it a safe way to reach a database that shouldn’t be exposed.
Step 4: Expose the Service with a NodePort
Change the type of the Service in web-service.yaml to NodePort and add a fixed nodePort:
Apply the manifest:
List the Services:
Call the application four times from your machine:
Explanation
A Service of type NodePort opens its nodePort on every node of the cluster. 80:30080/TCP in the column
PORT(S) means that port 80 of the Service is also reachable on port 30080 of every node. In 2.1, k3d forwarded the
ports 30080 to 30089 of your machine to the server node. That’s why localhost:30080 reaches the Service.
A Service of type LoadBalancer would get its own external IP from a load balancer. Your cluster has none, so the
EXTERNAL-IP of such a Service would stay <pending>.
Step 5: Switch Between Two Versions
Create the file web-green.yaml with a second version of the application:
Create the Deployment:
Call the application twice:
Both versions run now, but the Service still sends all requests to blue. Change the selector in
web-service.yaml to the green version:
Apply the manifest:
Wait a moment, then call the application again:
Explanation
This is a blue/green release: the new version runs next to the old one, and the selector of the Service decides which of them gets the requests. After a second or two, the switch applies to all requests. Switching back is just as quick, because blue keeps running until you delete it. Both versions run the same image here, so only the Pod names show the switch. In a real release, green runs the new image.
Optional Tasks
Finished early? Try these tasks. The following labs don’t depend on them. Each task lists the commands you need. Open the solution if you get stuck.
Task 1: Canary
Send most requests to blue and only a few to green. Hint: the Service sends requests to all Pods that match its selector, evenly per Pod.
| Command | Effect |
|---|---|
<command> | sort | uniq -c | Counts how often each line of output occurs |
Task 2: Long Distance Call
Call the Service from a Pod in another namespace. Does the short name web-service still work?
| Command | Effect |
|---|---|
kubectl run <name> -n <namespace> ... -- <command> | Starts the temporary Pod in another namespace |
http://<service>.<namespace> | Address of a Service in another namespace |
Task 3: Nobody Home
Change the selector of the Service to version: purple, a version that doesn’t exist. What happens when you call
the application, and how can you find the cause?
You know all the commands you need.
Clean Up
Delete all resources of this lab, since the following labs don’t need them:
If you did Task 2 and the namespace other still exists, delete it with kubectl delete namespace other.