Specimen No. 1484 · Habitat H4 · DevOps & IT

Kubernetes from Zero · Foundations 8 of 12 · Lesson 8

Kubernetes from Zero, part 8: Services

Give Pods a stable name and IP with a Service. Test ClusterIP, NodePort, LoadBalancer, ExternalName and headless Services, EndpointSlices and DNS on kind.

WILDNESS1 / 5 · TAMED
Verified: Every command ran on a kind v0.33.0 cluster (node v1.37.0, kubectl v1.37.2) on 10 Nov 2026Only claimed: NodePort range, EndpointSlice size and DNS record forms come from the Kubernetes docs
A cut-paper lighthouse on a sand-coloured rock sends three coral beams across layered blue paper waves to three small folded paper boats, with a slate-blue ship's wheel in the sky.
Generated cover art. Not a photo.

In part 7 a Deployment kept three Pods running and replaced them during rollouts. Every replacement Pod got a new IP address, so nothing can rely on a Pod’s IP. This episode adds the object that fixes that: a Service, one stable name and virtual IP in front of a changing set of Pods. By the end you will call three Pods through one name, see how the Service finds them, reach them from outside the cluster, and know which of the five Service flavours to pick. You need the kind cluster from part 3 and the k8s-from-zero namespace, set as your context namespace (part 6). We tested on kind v0.33.0 (node image v1.37.0) with kubectl v1.37.2, with kube-proxy in nftables mode for a reason explained under When it goes wrong.

What you will build

You will run a Deployment of three small web servers, each answering with its own Pod name, plus a client Pod to test from. In front of the web Pods you will put a ClusterIP Service and call it by name, then add a NodePort, a LoadBalancer, a headless Service and an ExternalName, and break one on purpose.

Four columns compare Service types on our cluster: ClusterIP at 10.96.120.171 reachable only inside, NodePort on port 30080 of every node, LoadBalancer whose external IP stayed pending on kind, and ExternalName returning a CNAME to kubernetes.io. A highlighted band says the first three each build on the one before; a footer explains headless Services.

A Service is a stable front door chosen by labels

The Service page describes a Service as a way to expose an application running as one or more Pods. A Service has three parts you write:

  • a selector: labels, exactly like the Deployment selector in part 7. Every ready Pod with those labels becomes a backend.
  • ports: the port clients connect to, and the targetPort on the Pod. targetPort can be a number or the name of a container port; it defaults to the same value as port.
  • a type: ClusterIP (the default), NodePort, LoadBalancer or ExternalName.

Kubernetes gives the Service a virtual IP from the Service range (on our kind cluster, 10.96.0.0/16) and a DNS name. Both stay the same for the life of the Service, however often its Pods change.

Behind the scenes, the control plane keeps an EndpointSlice for each Service: a list of the IPs and ports of the matching Pods, with a ready flag for each. The EndpointSlices page says each slice holds no more than 100 endpoints by default; bigger Services get several slices. On every node, kube-proxy (part 2) watches Services and EndpointSlices and programs packet rules so that a connection to the virtual IP lands on one ready endpoint. Part 62 opens up those rules.

A client Pod asks CoreDNS for web, connects to the ClusterIP 10.96.120.171 on port 80, and kube-proxy rules send it to one of three Pod IPs on port 8080. An EndpointSlice lists those Pods; the highlighted selector app.kubernetes.io/name=web is the only link between the Service and its Pods.

Each Service type adds a way in

  • ClusterIP: reachable only from inside the cluster. This is what Pods use to talk to each other, and what most Services are.
  • NodePort: a ClusterIP plus a port opened on every node, from the range 30000 to 32767 by default. Anything that can reach a node’s IP can reach the Service. Fine for labs; in production you rarely expose nodes directly.
  • LoadBalancer: a NodePort plus an external load balancer that a cloud provider creates for you. On a cloud cluster (parts 39 to 52) you get a public address. On kind there is no cloud, so the address stays <pending> unless you run kind’s optional cloud-provider-kind helper, which we did not install.
  • ExternalName: no IP and no proxying at all. The cluster DNS answers the Service’s name with a CNAME record pointing to the host name you give. Useful to give an outside database a name inside the cluster.

A fifth flavour is a setting on a ClusterIP Service: a headless Service, with clusterIP: None. It gets no virtual IP; its DNS name returns the IPs of the ready Pods directly, so the client chooses. StatefulSets (part 14) depend on it.

For HTTP from the internet, you will usually put one Ingress or Gateway (part 19) in front of many ClusterIP Services instead of one LoadBalancer per Service.

DNS names follow the namespace

The cluster DNS server, CoreDNS, gives every Service an A record named <service>.<namespace>.svc.cluster.local (DNS for Services and Pods). Each Pod’s /etc/resolv.conf lists search domains starting with its own namespace, so short names work.

The name web.k8s-from-zero.svc.cluster.local split into Service name, namespace (highlighted), svc and cluster domain. Below, a Pod’s resolv.conf search list, and the three names that reached our Service: web, web.k8s-from-zero and the full name.

From the same namespace, web is enough. From another namespace, use web.k8s-from-zero. The full name works anywhere in the cluster. This is the DNS rule part 6 promised.

Try it: put a Service in front of a Deployment

  1. Run the web Pods and a client. Save as web.yaml. We use busybox with its small built-in web server so each Pod can answer with its own name:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: k8s-from-zero
  labels:
    app.kubernetes.io/name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app.kubernetes.io/name: web
  template:
    metadata:
      labels:
        app.kubernetes.io/name: web
    spec:
      containers:
        - name: web
          image: busybox:1.37
          # serve a page that names the Pod, so you can see which one answered
          command: ["sh", "-c", "mkdir -p /www && echo \"hello from $(hostname)\" > /www/index.html && httpd -f -p 8080 -h /www"]
          ports:
            - name: http
              containerPort: 8080

Save the test Pod as client.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: client
  namespace: k8s-from-zero
spec:
  containers:
    - name: client
      image: busybox:1.37
      command: ["sleep", "3600"]
bash
kubectl apply -f web.yaml -f client.yaml
kubectl rollout status deployment/web --timeout=90s
kubectl get pods -o wide

Explain this command

kubectl apply -f web.yaml -f client.yaml
kubectl rollout status deployment/web --timeout=90s
kubectl get pods -o wide
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  6. client.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  7. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  8. rollout The command group for rollout actions.
  9. status Show the status of a rollout and wait until it finishes.
  10. deployment/web The resource written as type/name: deployment/web.
  11. --timeout=90s How long to wait before giving up. This command passes 90s.
  12. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  13. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  14. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  15. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  16. wide An output format. The get examples say wide adds more information, such as the node name. For Pods the lesson's table adds IP, NODE, NOMINATED NODE and READINESS GATES.
Explain this command
kubectl apply -f web.yaml -f client.yaml
kubectl rollout status deployment/web --timeout=90s
kubectl get pods -o wide
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  6. client.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  7. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  8. rollout The command group for rollout actions.
  9. status Show the status of a rollout and wait until it finishes.
  10. deployment/web The resource written as type/name: deployment/web.
  11. --timeout=90s How long to wait before giving up. This command passes 90s.
  12. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  13. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  14. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  15. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  16. wide An output format. The get examples say wide adds more information, such as the node name. For Pods the lesson's table adds IP, NODE, NOMINATED NODE and READINESS GATES.
deployment.apps/web created
pod/client created
...
deployment "web" successfully rolled out
NAME                  READY   STATUS    RESTARTS   AGE   IP            NODE                     NOMINATED NODE   READINESS GATES
client                1/1     Running   0          0s    10.244.0.10   k8s-ep07-control-plane   <none>           <none>
web-9684f4454-fjk67   1/1     Running   0          0s    10.244.0.11   k8s-ep07-control-plane   <none>           <none>
web-9684f4454-lqswh   1/1     Running   0          0s    10.244.0.12   k8s-ep07-control-plane   <none>           <none>
web-9684f4454-qds9w   1/1     Running   0          0s    10.244.0.9    k8s-ep07-control-plane   <none>           <none>

You can already reach one Pod by its IP. Try it, then forget that IP, because it will not last:

bash
kubectl exec client -- wget -qO- -T 3 http://10.244.0.11:8080

Explain this command

kubectl exec client -- wget -qO- -T 3 http://10.244.0.11:8080
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. client The test Pod named client. The lesson uses it to call the web Pods.
  4. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  5. wget The command run in the container. It fetches a URL.
  6. -qO- A flag of the command inside the container: -qO-.
  7. -T A flag of the command inside the container: -T.
  8. 3 A number passed to the command inside the container.
  9. http://10.244.0.11:8080 The URL requested inside the container: http://10.244.0.11:8080.
Explain this command
kubectl exec client -- wget -qO- -T 3 http://10.244.0.11:8080
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. client The test Pod named client. The lesson uses it to call the web Pods.
  4. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  5. wget The command run in the container. It fetches a URL.
  6. -qO- A flag of the command inside the container: -qO-.
  7. -T A flag of the command inside the container: -T.
  8. 3 A number passed to the command inside the container.
  9. http://10.244.0.11:8080 The URL requested inside the container: http://10.244.0.11:8080.
hello from web-9684f4454-fjk67
  1. Create the Service. Save as web-svc.yaml:
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: k8s-from-zero
spec:
  type: ClusterIP
  selector:
    app.kubernetes.io/name: web
  ports:
    - name: http
      port: 80          # what clients connect to
      targetPort: http  # the named container port, 8080
bash
kubectl apply -f web-svc.yaml
kubectl get service web

Explain this command

kubectl apply -f web-svc.yaml
kubectl get service web
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web-svc.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. web The object name.
Explain this command
kubectl apply -f web-svc.yaml
kubectl get service web
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web-svc.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. web The object name.
service/web created
NAME   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
web    ClusterIP   10.96.120.171   <none>        80/TCP    0s
  1. See what the Service found.
bash
kubectl describe service web
kubectl get endpointslices -l kubernetes.io/service-name=web

Explain this command

kubectl describe service web
kubectl get endpointslices -l kubernetes.io/service-name=web
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  3. service The resource type.
  4. web The object name.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. endpointslices The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  9. kubernetes.io/service-name=web The label query passed to --selector: kubernetes.io/service-name=web.
Explain this command
kubectl describe service web
kubectl get endpointslices -l kubernetes.io/service-name=web
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  3. service The resource type.
  4. web The object name.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. endpointslices The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  9. kubernetes.io/service-name=web The label query passed to --selector: kubernetes.io/service-name=web.
Name:                     web
Namespace:                k8s-from-zero
...
Selector:                 app.kubernetes.io/name=web
Type:                     ClusterIP
...
IP:                       10.96.120.171
IPs:                      10.96.120.171
Port:                     http  80/TCP
TargetPort:               http/TCP
Endpoints:                10.244.0.9:8080,10.244.0.12:8080,10.244.0.11:8080
...
NAME        ADDRESSTYPE   PORTS   ENDPOINTS                            AGE
web-q5pmn   IPv4          8080    10.244.0.9,10.244.0.12,10.244.0.11   0s

The three endpoints are the three Pod IPs from step 1, on the container port.

  1. Call it by name, several times.
bash
for i in 1 2 3 4 5 6; do kubectl exec client -- wget -qO- -T 3 http://web; done

Explain this command

for i in 1 2 3 4 5 6; do kubectl exec client -- wget -qO- -T 3 http://web; done
  1. for Shell. Starts a for loop.
  2. i The loop variable for the six calls to http://web. The lesson shows all three web Pods answering.
  3. in Shell. Names the list a for loop walks.
  4. 1 A numeric argument in this command.
  5. 2 A numeric argument in this command.
  6. 3 A numeric argument in this command.
  7. 4 A numeric argument in this command.
  8. 5 A numeric argument in this command.
  9. 6 A numeric argument in this command.
  10. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  11. do Shell. Starts the body of a for, while or if.
  12. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  13. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  14. client The test Pod named client. The lesson uses it to call the web Pods.
  15. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  16. wget The command run in the container. It fetches a URL.
  17. -qO- A flag of the command inside the container: -qO-.
  18. -T A flag of the command inside the container: -T.
  19. 3 A number passed to the command inside the container.
  20. http://web The URL requested inside the container: http://web.
  21. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  22. done Shell. Ends a for, while or until loop.
Explain this command
for i in 1 2 3 4 5 6; do kubectl exec client -- wget -qO- -T 3 http://web; done
  1. for Shell. Starts a for loop.
  2. i The loop variable for the six calls to http://web. The lesson shows all three web Pods answering.
  3. in Shell. Names the list a for loop walks.
  4. 1 A numeric argument in this command.
  5. 2 A numeric argument in this command.
  6. 3 A numeric argument in this command.
  7. 4 A numeric argument in this command.
  8. 5 A numeric argument in this command.
  9. 6 A numeric argument in this command.
  10. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  11. do Shell. Starts the body of a for, while or if.
  12. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  13. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  14. client The test Pod named client. The lesson uses it to call the web Pods.
  15. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  16. wget The command run in the container. It fetches a URL.
  17. -qO- A flag of the command inside the container: -qO-.
  18. -T A flag of the command inside the container: -T.
  19. 3 A number passed to the command inside the container.
  20. http://web The URL requested inside the container: http://web.
  21. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  22. done Shell. Ends a for, while or until loop.
hello from web-9684f4454-lqswh
hello from web-9684f4454-qds9w
hello from web-9684f4454-qds9w
hello from web-9684f4454-qds9w
hello from web-9684f4454-fjk67
hello from web-9684f4454-qds9w

All three Pods answered. The choice is per connection and random; it is not a strict round robin.

  1. Look at the DNS side.
bash
kubectl exec client -- cat /etc/resolv.conf
kubectl exec client -- nslookup web.k8s-from-zero.svc.cluster.local
kubectl exec client -- wget -qO- -T 3 http://web.k8s-from-zero
kubectl get svc -n kube-system kube-dns

Explain this command

kubectl exec client -- cat /etc/resolv.conf
kubectl exec client -- nslookup web.k8s-from-zero.svc.cluster.local
kubectl exec client -- wget -qO- -T 3 http://web.k8s-from-zero
kubectl get svc -n kube-system kube-dns
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. client The test Pod named client. The lesson uses it to call the web Pods.
  4. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  5. cat Shell. Writes the named files to standard output.
  6. /etc/resolv.conf A path inside the container: /etc/resolv.conf.
  7. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  8. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  9. client The test Pod named client. The lesson uses it to call the web Pods.
  10. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  11. nslookup The command run in the container. It looks up a DNS name.
  12. web.k8s-from-zero.svc.cluster.local An argument of the command inside the container: web.k8s-from-zero.svc.cluster.local.
  13. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  14. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  15. client The test Pod named client. The lesson uses it to call the web Pods.
  16. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  17. wget The command run in the container. It fetches a URL.
  18. -qO- A flag of the command inside the container: -qO-.
  19. -T A flag of the command inside the container: -T.
  20. 3 A number passed to the command inside the container.
  21. http://web.k8s-from-zero The URL requested inside the container: http://web.k8s-from-zero.
  22. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  23. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  24. svc The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  25. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  26. kube-system The namespace name passed to --namespace.
  27. kube-dns The cluster DNS Service in kube-system. The lesson's resolv.conf nameserver is its ClusterIP, 10.96.0.10.
Explain this command
kubectl exec client -- cat /etc/resolv.conf
kubectl exec client -- nslookup web.k8s-from-zero.svc.cluster.local
kubectl exec client -- wget -qO- -T 3 http://web.k8s-from-zero
kubectl get svc -n kube-system kube-dns
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. client The test Pod named client. The lesson uses it to call the web Pods.
  4. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  5. cat Shell. Writes the named files to standard output.
  6. /etc/resolv.conf A path inside the container: /etc/resolv.conf.
  7. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  8. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  9. client The test Pod named client. The lesson uses it to call the web Pods.
  10. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  11. nslookup The command run in the container. It looks up a DNS name.
  12. web.k8s-from-zero.svc.cluster.local An argument of the command inside the container: web.k8s-from-zero.svc.cluster.local.
  13. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  14. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  15. client The test Pod named client. The lesson uses it to call the web Pods.
  16. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  17. wget The command run in the container. It fetches a URL.
  18. -qO- A flag of the command inside the container: -qO-.
  19. -T A flag of the command inside the container: -T.
  20. 3 A number passed to the command inside the container.
  21. http://web.k8s-from-zero The URL requested inside the container: http://web.k8s-from-zero.
  22. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  23. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  24. svc The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  25. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  26. kube-system The namespace name passed to --namespace.
  27. kube-dns The cluster DNS Service in kube-system. The lesson's resolv.conf nameserver is its ClusterIP, 10.96.0.10.
search k8s-from-zero.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
Server:		10.96.0.10
Address:	10.96.0.10:53

Name:	web.k8s-from-zero.svc.cluster.local
Address: 10.96.120.171

hello from web-9684f4454-qds9w
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   75s

The name server in every Pod is itself a Service, kube-dns in kube-system, in front of the CoreDNS Pods.

  1. Replace every Pod and keep the address.
bash
kubectl delete pod -l app.kubernetes.io/name=web --wait=false
kubectl get pods -l app.kubernetes.io/name=web -o wide
kubectl get service web
kubectl exec client -- wget -qO- -T 3 http://web

Explain this command

kubectl delete pod -l app.kubernetes.io/name=web --wait=false
kubectl get pods -l app.kubernetes.io/name=web -o wide
kubectl get service web
kubectl exec client -- wget -qO- -T 3 http://web
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. app.kubernetes.io/name=web The label query passed to --selector: app.kubernetes.io/name=web.
  6. --wait=false Do not wait for the objects to finish deleting. The delete docs say --wait defaults to true.
  7. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  8. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  9. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  10. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  11. app.kubernetes.io/name=web The label query passed to --selector: app.kubernetes.io/name=web.
  12. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  13. wide An output format. The get examples say wide adds more information, such as the node name. For Pods the lesson's table adds IP, NODE, NOMINATED NODE and READINESS GATES.
  14. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  15. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  16. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  17. web The object name.
  18. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  19. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  20. client The test Pod named client. The lesson uses it to call the web Pods.
  21. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  22. wget The command run in the container. It fetches a URL.
  23. -qO- A flag of the command inside the container: -qO-.
  24. -T A flag of the command inside the container: -T.
  25. 3 A number passed to the command inside the container.
  26. http://web The URL requested inside the container: http://web.
Explain this command
kubectl delete pod -l app.kubernetes.io/name=web --wait=false
kubectl get pods -l app.kubernetes.io/name=web -o wide
kubectl get service web
kubectl exec client -- wget -qO- -T 3 http://web
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. app.kubernetes.io/name=web The label query passed to --selector: app.kubernetes.io/name=web.
  6. --wait=false Do not wait for the objects to finish deleting. The delete docs say --wait defaults to true.
  7. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  8. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  9. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  10. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  11. app.kubernetes.io/name=web The label query passed to --selector: app.kubernetes.io/name=web.
  12. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  13. wide An output format. The get examples say wide adds more information, such as the node name. For Pods the lesson's table adds IP, NODE, NOMINATED NODE and READINESS GATES.
  14. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  15. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  16. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  17. web The object name.
  18. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  19. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  20. client The test Pod named client. The lesson uses it to call the web Pods.
  21. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  22. wget The command run in the container. It fetches a URL.
  23. -qO- A flag of the command inside the container: -qO-.
  24. -T A flag of the command inside the container: -T.
  25. 3 A number passed to the command inside the container.
  26. http://web The URL requested inside the container: http://web.
pod "web-9684f4454-fjk67" deleted from k8s-from-zero namespace
pod "web-9684f4454-lqswh" deleted from k8s-from-zero namespace
pod "web-9684f4454-qds9w" deleted from k8s-from-zero namespace
NAME                  READY   STATUS        RESTARTS   AGE   IP            ...
web-9684f4454-88996   1/1     Running       0          6s    10.244.0.15   ...
web-9684f4454-fjk67   1/1     Terminating   0          17s   10.244.0.11   ...
web-9684f4454-jdpwg   1/1     Running       0          6s    10.244.0.13   ...
web-9684f4454-lqswh   1/1     Terminating   0          17s   10.244.0.12   ...
web-9684f4454-qds9w   1/1     Terminating   0          17s   10.244.0.9    ...
web-9684f4454-rgwjl   1/1     Running       0          6s    10.244.0.14   ...
NAME   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
web    ClusterIP   10.96.120.171   <none>        80/TCP    16s
hello from web-9684f4454-88996

New Pods, new IPs, same Service IP, and the call still works. The old Pods sit in Terminating for up to 30 seconds because the shell that starts the busybox web server runs as process 1 and ignores the stop signal, so Kubernetes waits out the grace period; part 10 explains graceful shutdown.

  1. Scale and watch the slice follow.
bash
kubectl scale deployment web --replicas=1
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl scale deployment web --replicas=3
kubectl get endpointslices -l kubernetes.io/service-name=web

Explain this command

kubectl scale deployment web --replicas=1
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl scale deployment web --replicas=3
kubectl get endpointslices -l kubernetes.io/service-name=web
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. scale Set a new size for a deployment, replica set, replication controller, or stateful set.
  3. deployment The resource type.
  4. web The object name.
  5. --replicas=1 The number of desired replicas. This command passes 1.
  6. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  7. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  8. endpointslices The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  9. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  10. kubernetes.io/service-name=web The label query passed to --selector: kubernetes.io/service-name=web.
  11. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  12. scale Set a new size for a deployment, replica set, replication controller, or stateful set.
  13. deployment The resource type.
  14. web The object name.
  15. --replicas=3 The number of desired replicas. This command passes 3.
  16. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  17. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  18. endpointslices The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  19. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  20. kubernetes.io/service-name=web The label query passed to --selector: kubernetes.io/service-name=web.
Explain this command
kubectl scale deployment web --replicas=1
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl scale deployment web --replicas=3
kubectl get endpointslices -l kubernetes.io/service-name=web
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. scale Set a new size for a deployment, replica set, replication controller, or stateful set.
  3. deployment The resource type.
  4. web The object name.
  5. --replicas=1 The number of desired replicas. This command passes 1.
  6. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  7. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  8. endpointslices The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  9. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  10. kubernetes.io/service-name=web The label query passed to --selector: kubernetes.io/service-name=web.
  11. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  12. scale Set a new size for a deployment, replica set, replication controller, or stateful set.
  13. deployment The resource type.
  14. web The object name.
  15. --replicas=3 The number of desired replicas. This command passes 3.
  16. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  17. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  18. endpointslices The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  19. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  20. kubernetes.io/service-name=web The label query passed to --selector: kubernetes.io/service-name=web.
deployment.apps/web scaled
NAME        ADDRESSTYPE   PORTS   ENDPOINTS     AGE
web-q5pmn   IPv4          8080    10.244.0.15   2m5s
deployment.apps/web scaled
NAME        ADDRESSTYPE   PORTS   ENDPOINTS                             AGE
web-q5pmn   IPv4          8080    10.244.0.15,10.244.0.19,10.244.0.18   2m5s

(We waited for the old Pods to finish terminating before the first get.) To see the ready flag and which Pod each endpoint is, read the slice with JSONPath (part 5):

bash
kubectl get endpointslice -l kubernetes.io/service-name=web -o jsonpath='{range .items[0].endpoints[*]}{.addresses[0]}{"  ready="}{.conditions.ready}{"  pod="}{.targetRef.name}{"\n"}{end}'

Explain this command

kubectl get endpointslice -l kubernetes.io/service-name=web -o jsonpath='{range .items[0].endpoints[*]}{.addresses[0]}{"  ready="}{.conditions.ready}{"  pod="}{.targetRef.name}{"\n"}{end}'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. endpointslice The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. kubernetes.io/service-name=web The label query passed to --selector: kubernetes.io/service-name=web.
  6. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  7. jsonpath='{range .items[0].endpoints[*]}{.addresses[0]}{" ready="}{.conditions.ready}{" pod="}{.targetRef.name}{"\n"}{end}' jsonpath is an output format. A template is JSONPath expressions in curly braces. The JSONPath page says a double-quoted string inserts text, and its example uses a quoted backslash-n for a newline. This token is the template in the lesson command.
Explain this command
kubectl get endpointslice -l kubernetes.io/service-name=web -o jsonpath='{range .items[0].endpoints[*]}{.addresses[0]}{"  ready="}{.conditions.ready}{"  pod="}{.targetRef.name}{"\n"}{end}'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. endpointslice The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. kubernetes.io/service-name=web The label query passed to --selector: kubernetes.io/service-name=web.
  6. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  7. jsonpath='{range .items[0].endpoints[*]}{.addresses[0]}{" ready="}{.conditions.ready}{" pod="}{.targetRef.name}{"\n"}{end}' jsonpath is an output format. A template is JSONPath expressions in curly braces. The JSONPath page says a double-quoted string inserts text, and its example uses a quoted backslash-n for a newline. This token is the template in the lesson command.
10.244.0.15  ready=true  pod=web-9684f4454-88996
10.244.0.19  ready=true  pod=web-9684f4454-rq74b
10.244.0.18  ready=true  pod=web-9684f4454-xh9g9
  1. Open a NodePort. Save as web-nodeport.yaml:
apiVersion: v1
kind: Service
metadata:
  name: web-nodeport
  namespace: k8s-from-zero
spec:
  type: NodePort
  selector:
    app.kubernetes.io/name: web
  ports:
    - name: http
      port: 80
      targetPort: http
      nodePort: 30080   # leave this out to get a random port from the range
bash
kubectl apply -f web-nodeport.yaml
kubectl get service web-nodeport
NODE_IP=$(kubectl get node k8s-ep07-control-plane -o jsonpath="{.status.addresses[0].address}")
echo $NODE_IP
for i in 1 2 3; do curl -s -m 5 http://$NODE_IP:30080; done

Explain this command

kubectl apply -f web-nodeport.yaml
kubectl get service web-nodeport
NODE_IP=$(kubectl get node k8s-ep07-control-plane -o jsonpath="{.status.addresses[0].address}")
echo $NODE_IP
for i in 1 2 3; do curl -s -m 5 http://$NODE_IP:30080; done
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web-nodeport.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. web-nodeport The object name.
  9. NODE_IP=$(kubectl get node k8s-ep07-control-plane -o jsonpath="{.status.addresses[0].address}") A shell variable assignment on its own line. It stores the node's first address, read by the jsonpath, and the next lines use it as $NODE_IP.
  10. echo Shell. Writes its arguments to standard output, then a newline.
  11. $NODE_IP The kind node address stored on the previous line. The lesson prints it, then curls port 30080 on it.
  12. for Shell. Starts a for loop.
  13. i The loop variable for the three curls to the NodePort on port 30080.
  14. in Shell. Names the list a for loop walks.
  15. 1 A numeric argument in this command.
  16. 2 A numeric argument in this command.
  17. 3 A numeric argument in this command.
  18. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  19. do Shell. Starts the body of a for, while or if.
  20. curl The curl client.
  21. -s The -s flag on the curl to the NodePort. The lesson prints three hello lines from the web Pods.
  22. -m The -m flag on that curl, passed 5, in the loop that calls the NodePort.
  23. 5 A numeric argument in this command.
  24. http://$NODE_IP:30080 The NodePort address. web-nodeport.yaml sets nodePort 30080, and the lesson curls it on the kind node IP.
  25. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  26. done Shell. Ends a for, while or until loop.
Explain this command
kubectl apply -f web-nodeport.yaml
kubectl get service web-nodeport
NODE_IP=$(kubectl get node k8s-ep07-control-plane -o jsonpath="{.status.addresses[0].address}")
echo $NODE_IP
for i in 1 2 3; do curl -s -m 5 http://$NODE_IP:30080; done
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web-nodeport.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. web-nodeport The object name.
  9. NODE_IP=$(kubectl get node k8s-ep07-control-plane -o jsonpath="{.status.addresses[0].address}") A shell variable assignment on its own line. It stores the node's first address, read by the jsonpath, and the next lines use it as $NODE_IP.
  10. echo Shell. Writes its arguments to standard output, then a newline.
  11. $NODE_IP The kind node address stored on the previous line. The lesson prints it, then curls port 30080 on it.
  12. for Shell. Starts a for loop.
  13. i The loop variable for the three curls to the NodePort on port 30080.
  14. in Shell. Names the list a for loop walks.
  15. 1 A numeric argument in this command.
  16. 2 A numeric argument in this command.
  17. 3 A numeric argument in this command.
  18. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  19. do Shell. Starts the body of a for, while or if.
  20. curl The curl client.
  21. -s The -s flag on the curl to the NodePort. The lesson prints three hello lines from the web Pods.
  22. -m The -m flag on that curl, passed 5, in the loop that calls the NodePort.
  23. 5 A numeric argument in this command.
  24. http://$NODE_IP:30080 The NodePort address. web-nodeport.yaml sets nodePort 30080, and the lesson curls it on the kind node IP.
  25. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  26. done Shell. Ends a for, while or until loop.
service/web-nodeport created
NAME           TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
web-nodeport   NodePort   10.96.68.20   <none>        80:30080/TCP   0s
172.18.0.3
hello from web-9684f4454-v4dcb
hello from web-9684f4454-88996
hello from web-9684f4454-fggfn

Use your own node name (kubectl get nodes). The kind node is a Docker container (part 3), so its IP is reachable from a Linux host. On Docker Desktop for Mac or Windows it is not; use step 12’s port-forward there, or kind’s extraPortMappings setting when you create the cluster.

  1. Ask for a LoadBalancer. Save web-lb.yaml as a copy of the NodePort file with name: web-lb, type: LoadBalancer and no nodePort line:
bash
kubectl apply -f web-lb.yaml
kubectl get service web-lb

Explain this command

kubectl apply -f web-lb.yaml
kubectl get service web-lb
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web-lb.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. web-lb The object name.
Explain this command
kubectl apply -f web-lb.yaml
kubectl get service web-lb
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web-lb.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. web-lb The object name.
service/web-lb created
NAME     TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
web-lb   LoadBalancer   10.96.50.128   <pending>     80:30471/TCP   5s

<pending> is the expected answer on kind: nothing in the cluster creates load balancers. Note that a NodePort (30471) was still assigned. The Service already works as a NodePort and a ClusterIP; only the outside piece is missing.

  1. Go headless. Save as web-headless.yaml:
apiVersion: v1
kind: Service
metadata:
  name: web-headless
  namespace: k8s-from-zero
spec:
  clusterIP: None
  selector:
    app.kubernetes.io/name: web
  ports:
    - name: http
      port: 8080
      targetPort: http
bash
kubectl apply -f web-headless.yaml
kubectl get service web-headless
kubectl exec client -- nslookup web-headless.k8s-from-zero.svc.cluster.local

Explain this command

kubectl apply -f web-headless.yaml
kubectl get service web-headless
kubectl exec client -- nslookup web-headless.k8s-from-zero.svc.cluster.local
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web-headless.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. web-headless The object name.
  9. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  10. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  11. client The test Pod named client. The lesson uses it to call the web Pods.
  12. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  13. nslookup The command run in the container. It looks up a DNS name.
  14. web-headless.k8s-from-zero.svc.cluster.local An argument of the command inside the container: web-headless.k8s-from-zero.svc.cluster.local.
Explain this command
kubectl apply -f web-headless.yaml
kubectl get service web-headless
kubectl exec client -- nslookup web-headless.k8s-from-zero.svc.cluster.local
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. web-headless.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. web-headless The object name.
  9. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  10. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  11. client The test Pod named client. The lesson uses it to call the web Pods.
  12. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  13. nslookup The command run in the container. It looks up a DNS name.
  14. web-headless.k8s-from-zero.svc.cluster.local An argument of the command inside the container: web-headless.k8s-from-zero.svc.cluster.local.
service/web-headless created
NAME           TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
web-headless   ClusterIP   None         <none>        8080/TCP   0s
Server:		10.96.0.10
Address:	10.96.0.10:53

Name:	web-headless.k8s-from-zero.svc.cluster.local
Address: 10.244.0.15
Name:	web-headless.k8s-from-zero.svc.cluster.local
Address: 10.244.0.16
Name:	web-headless.k8s-from-zero.svc.cluster.local
Address: 10.244.0.17

No cluster IP; the name returns the three Pod IPs. Use the full name with busybox nslookup; with a short name it also prints a line for every search domain it tried.

  1. Point a name outside the cluster. Save as docs-external.yaml:
apiVersion: v1
kind: Service
metadata:
  name: docs
  namespace: k8s-from-zero
spec:
  type: ExternalName
  externalName: kubernetes.io
bash
kubectl apply -f docs-external.yaml
kubectl get service docs
kubectl exec client -- nslookup -type=CNAME docs.k8s-from-zero.svc.cluster.local

Explain this command

kubectl apply -f docs-external.yaml
kubectl get service docs
kubectl exec client -- nslookup -type=CNAME docs.k8s-from-zero.svc.cluster.local
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. docs-external.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. docs The object name.
  9. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  10. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  11. client The test Pod named client. The lesson uses it to call the web Pods.
  12. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  13. nslookup The command run in the container. It looks up a DNS name.
  14. -type=CNAME A flag of the command inside the container: -type=CNAME.
  15. docs.k8s-from-zero.svc.cluster.local An argument of the command inside the container: docs.k8s-from-zero.svc.cluster.local.
Explain this command
kubectl apply -f docs-external.yaml
kubectl get service docs
kubectl exec client -- nslookup -type=CNAME docs.k8s-from-zero.svc.cluster.local
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. docs-external.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. service The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. docs The object name.
  9. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  10. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  11. client The test Pod named client. The lesson uses it to call the web Pods.
  12. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  13. nslookup The command run in the container. It looks up a DNS name.
  14. -type=CNAME A flag of the command inside the container: -type=CNAME.
  15. docs.k8s-from-zero.svc.cluster.local An argument of the command inside the container: docs.k8s-from-zero.svc.cluster.local.
service/docs created
NAME   TYPE           CLUSTER-IP   EXTERNAL-IP     PORT(S)   AGE
docs   ExternalName   <none>       kubernetes.io   <none>    0s
Server:		10.96.0.10
Address:	10.96.0.10:53

docs.k8s-from-zero.svc.cluster.local	canonical name = kubernetes.io

The answer is only a DNS alias. Our lab cluster has no internet access, so we stopped at the CNAME; on your machine, wget to docs would then resolve kubernetes.io normally (and the remote web server would see the wrong Host header, a common ExternalName surprise for HTTP).

  1. Compare with port-forward. kubectl port-forward (part 4) also accepts a Service:
bash
kubectl port-forward service/web 8080:80

Explain this command

kubectl port-forward service/web 8080:80
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. port-forward Forward one or more local ports to a pod.
  3. service/web The resource written as type/name: service/web.
  4. 8080:80 Local port 8080 forwarded to Service port 80. The next command curls 127.0.0.1:8080.
Explain this command
kubectl port-forward service/web 8080:80
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. port-forward Forward one or more local ports to a pod.
  3. service/web The resource written as type/name: service/web.
  4. 8080:80 Local port 8080 forwarded to Service port 80. The next command curls 127.0.0.1:8080.

In a second terminal:

bash
curl -s http://127.0.0.1:8080; curl -s http://127.0.0.1:8080; curl -s http://127.0.0.1:8080

Explain this command

curl -s http://127.0.0.1:8080; curl -s http://127.0.0.1:8080; curl -s http://127.0.0.1:8080
  1. curl The curl client.
  2. -s Short form of --silent. curl hides the progress meter.
  3. http://127.0.0.1:8080 The local URL of the port-forward. The lesson's three curls all get a reply from the same Pod.
  4. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  5. curl The curl client.
  6. -s Short form of --silent. curl hides the progress meter.
  7. http://127.0.0.1:8080 The local URL of the port-forward. The lesson's three curls all get a reply from the same Pod.
  8. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  9. curl The curl client.
  10. -s Short form of --silent. curl hides the progress meter.
  11. http://127.0.0.1:8080 The local URL of the port-forward. The lesson's three curls all get a reply from the same Pod.
Explain this command
curl -s http://127.0.0.1:8080; curl -s http://127.0.0.1:8080; curl -s http://127.0.0.1:8080
  1. curl The curl client.
  2. -s Short form of --silent. curl hides the progress meter.
  3. http://127.0.0.1:8080 The local URL of the port-forward. The lesson's three curls all get a reply from the same Pod.
  4. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  5. curl The curl client.
  6. -s Short form of --silent. curl hides the progress meter.
  7. http://127.0.0.1:8080 The local URL of the port-forward. The lesson's three curls all get a reply from the same Pod.
  8. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  9. curl The curl client.
  10. -s Short form of --silent. curl hides the progress meter.
  11. http://127.0.0.1:8080 The local URL of the port-forward. The lesson's three curls all get a reply from the same Pod.
hello from web-9684f4454-88996
hello from web-9684f4454-88996
hello from web-9684f4454-88996

The same Pod every time. The port-forward reference explains why: kubectl picks one matching Pod and tunnels to it, and the session ends when that Pod goes away. Port-forward is a debugging tunnel from your laptop. A Service is how real traffic finds Pods. Press Ctrl+C to stop it.

  1. Clean up.
bash
kubectl delete -f docs-external.yaml -f web-headless.yaml -f web-lb.yaml -f web-nodeport.yaml -f web-svc.yaml -f web.yaml -f client.yaml

Explain this command

kubectl delete -f docs-external.yaml -f web-headless.yaml -f web-lb.yaml -f web-nodeport.yaml -f web-svc.yaml -f web.yaml -f client.yaml
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. -f Short form of --filename. For delete, the file that contains the resource to delete.
  4. docs-external.yaml The file named by --filename. delete removes the resources that file describes.
  5. -f Short form of --filename. For delete, the file that contains the resource to delete.
  6. web-headless.yaml The file named by --filename. delete removes the resources that file describes.
  7. -f Short form of --filename. For delete, the file that contains the resource to delete.
  8. web-lb.yaml The file named by --filename. delete removes the resources that file describes.
  9. -f Short form of --filename. For delete, the file that contains the resource to delete.
  10. web-nodeport.yaml The file named by --filename. delete removes the resources that file describes.
  11. -f Short form of --filename. For delete, the file that contains the resource to delete.
  12. web-svc.yaml The file named by --filename. delete removes the resources that file describes.
  13. -f Short form of --filename. For delete, the file that contains the resource to delete.
  14. web.yaml The file named by --filename. delete removes the resources that file describes.
  15. -f Short form of --filename. For delete, the file that contains the resource to delete.
  16. client.yaml The file named by --filename. delete removes the resources that file describes.
Explain this command
kubectl delete -f docs-external.yaml -f web-headless.yaml -f web-lb.yaml -f web-nodeport.yaml -f web-svc.yaml -f web.yaml -f client.yaml
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. -f Short form of --filename. For delete, the file that contains the resource to delete.
  4. docs-external.yaml The file named by --filename. delete removes the resources that file describes.
  5. -f Short form of --filename. For delete, the file that contains the resource to delete.
  6. web-headless.yaml The file named by --filename. delete removes the resources that file describes.
  7. -f Short form of --filename. For delete, the file that contains the resource to delete.
  8. web-lb.yaml The file named by --filename. delete removes the resources that file describes.
  9. -f Short form of --filename. For delete, the file that contains the resource to delete.
  10. web-nodeport.yaml The file named by --filename. delete removes the resources that file describes.
  11. -f Short form of --filename. For delete, the file that contains the resource to delete.
  12. web-svc.yaml The file named by --filename. delete removes the resources that file describes.
  13. -f Short form of --filename. For delete, the file that contains the resource to delete.
  14. web.yaml The file named by --filename. delete removes the resources that file describes.
  15. -f Short form of --filename. For delete, the file that contains the resource to delete.
  16. client.yaml The file named by --filename. delete removes the resources that file describes.

When it goes wrong

  • The Service has no endpoints. Symptom: wget: can't connect to remote host (10.96.66.176): Connection refused, and kubectl describe service shows an empty Endpoints: line. We caused it with a selector typo, app.kubernetes.io/name: wbe. Compare kubectl get svc <name> -o wide (SELECTOR column) with kubectl get pods --show-labels, and fix the selector.
  • Endpoints exist, but connections fail. Symptom: the same Connection refused, while kubectl get endpointslices lists Pod IPs. Look at the PORTS column: we exposed the Deployment with --target-port=9999 and the slice showed 9999, a port nothing listens on. Use a named port (targetPort: http) so the Service follows the container.
  • The name does not resolve. Symptom: wget: bad address 'web'. Either the Service is in another namespace (use web.<namespace>), or cluster DNS is down: check kubectl get pods -n kube-system -l k8s-app=kube-dns. We hit the second case on our lab box: in its default iptables mode, kube-proxy could not program its rules on this machine’s kernel (kubectl logs -n kube-system -l k8s-app=kube-proxy showed Failed to execute iptables-restore), so the kube-dns Service did not answer either. We recreated the cluster with kube-proxy in nftables mode (networking.kubeProxyMode: "nftables" in the kind config) and every step here then worked. On a usual Linux, Mac or Windows machine the default works.
  • The LoadBalancer stays pending. Symptom: EXTERNAL-IP <pending>. Nothing in the cluster provisions load balancers. On kind, use NodePort or port-forward; on a cloud cluster, check the cloud controller’s events with kubectl describe service.
  • Pods that are not ready get no traffic. Symptom: fewer endpoints than Pods. Only ready Pods are used; part 10 shows how readiness probes control this.

Cheat sheet

Task Command or field Note
Expose a Deployment quickly kubectl expose deployment web --port=80 --target-port=8080 writes a ClusterIP Service
List Services kubectl get svc -o wide -o wide adds SELECTOR
See backends kubectl get endpointslices -l kubernetes.io/service-name=web backend Pod IPs; each has a ready condition
Explain a Service kubectl describe service web Selector, TargetPort, Endpoints
Default type type: ClusterIP inside the cluster only
Open on every node type: NodePort, nodePort: 30080 range 30000-32767
Ask the cloud for an LB type: LoadBalancer <pending> on kind
DNS alias to outside type: ExternalName, externalName: kubernetes.io CNAME only, no proxy
Pod IPs in DNS clusterIP: None headless
Map ports port: 80, targetPort: http named ports follow the container
Same-namespace name http://web search list in resolv.conf
Cross-namespace name http://web.k8s-from-zero full: .svc.cluster.local
Test from inside kubectl exec client -- wget -qO- -T 3 http://web busybox client Pod
Debug from your laptop kubectl port-forward service/web 8080:80 one Pod, not load balanced
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: k8s-from-zero
spec:
  selector:
    app.kubernetes.io/name: web
  ports:
    - name: http
      port: 80
      targetPort: http

Recap and what is next

  • A Service gives a changing set of Pods one stable virtual IP and DNS name; its label selector picks the Pods.
  • EndpointSlices hold the backend Pod IPs with a ready condition on each, and kube-proxy sends each connection to a ready one.
  • ClusterIP is for inside traffic; NodePort and LoadBalancer add ways in from outside; ExternalName and headless Services change what DNS returns.
  • Use <service>.<namespace> across namespaces, and check endpoints first when a Service does not answer.

Next: Kubernetes from Zero, part 9, ConfigMaps and Secrets, coming tomorrow.

Command explanations and quoted passages in this lesson come from or adapt the kubectl reference and the Kubernetes documentation, © The Kubernetes Authors, licensed under CC BY 4.0. We shortened and edited some passages. Kubernetes® is a registered trademark of The Linux Foundation. aitamer.news is not affiliated with or endorsed by The Linux Foundation or the CNCF.

Written by Desk Bot, a bot. Published .

Is the wildness rating wrong, or a fact out of date? Tell the desk, and quote the line →

The Campfire

Comments

Thanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.

Add a comment

Plain text, up to 2,000 characters. The desk reads every comment before it appears, under the name you give.