Services

A Service is one stable address in front of changing pods

api poda clientweb pod10.244.1.7web pod10.244.2.4web pod10.244.3.9Service web10.96.0.20an address that never changeshttp://webselector: app=webweb pod, replaced10.244.2.11a new address: the Service updates its listPods come and go. A Service gives them one name and one address that stay.A Service watches for ready pods that match its selector and keeps a list of their addresses.Clients call the Service and each connection is passed to one pod on the list. Pods can bereplaced, added or removed, and no client has to be told.

Clients call the Service. It finds the pods by label.

Services · Going deeper

Lab: a Service in front of the web pods

TypeLook forkubectl expose deployment web --port=80kubectl get service webkubectl get endpointsliceskubectl run tmp -it --rm --image=busybox -- shwget -qO- webnslookup webservice/web exposeda CLUSTER-IP, and no EXTERNAL-IPa slice for web that lists the pod addressesa shell inside a temporary podthe nginx welcome page, found by namethe Service's cluster address; then type exitLab: put a Service in front of the web pods and call it by name.Use the web Deployment from the earlier labs. Lines 5 and 6 are typed inside the temporary pod.From your own machine, "kubectl port-forward service/web 8080:80" opens it at localhost:8080.

Expose the Deployment, then call it by name from another pod.

Service types

Three Service types, each built on the one before

podsapp=webClusterIP10.96.0.20:80for other pods:inside the cluster onlyNodePortevery node, port 30080for anyone who canreach a nodeLoadBalancer203.0.113.10:80for the internet:the cloud's load balanceroutsideinsideThree Service types, each built on the one before it.ClusterIP is the default and is all that most Services need. NodePort also opens the same porton every node. LoadBalancer also asks the cloud for a load balancer with a public address thatforwards to those node ports. Each one costs money, so they are used sparingly.

ClusterIP inside, NodePort on every node, LoadBalancer for the internet.

Service ports · Going deeper

port, targetPort, nodePort: three numbers in three places

apiVersion: v1kind: Servicemetadata: name: webspec: type: NodePort selector: app: web ports: - name: http port: 80 targetPort: 8080 nodePort: 30080any node:30080Service web10.96.0.20:80a web pod:8080ClusterIP if you leave this line outsend to ready pods that carry this labelthe port of the Service itself: http://web:80the port the program listens on; if left out, the same as portone of 2,768 ports, 30000 to 32767; picked for you if left outThree ports in three places: the node, the Service and the container. Only targetPort must match the program.A wrong targetPort gives "connection refused" although the Service looks fine. A selector that matches no ready pod gives aService with no endpoints: kubectl get endpointslices -l kubernetes.io/service-name=web lists none. One command writes thismanifest: kubectl expose deployment web --type=NodePort --port=80 --target-port=8080.

port is the Service. targetPort is the container. nodePort is every node.

DNS

Every Service has a name; programs use names, not addresses

api podin the namespace shopCoreDNSthe cluster's DNSwhat is the address of "web"?10.96.0.20the Service, not a podThe name you useWhat it findswebthe Service web in your own namespace, shopweb.toolsthe Service web in the namespace toolsweb.tools.svc.cluster.localthe same, written in fullEvery Service gets a DNS name, so programs are configured with names, not addresses.The cluster runs its own DNS server and every pod is set up to ask it. A short name is lookedup in the pod's own namespace first, which lets the same configuration work unchanged in a dev,a staging and a production namespace.

web here, web.tools elsewhere. The cluster's DNS answers.

The search list · Going deeper

A short name works because the pod tries its own namespace first

Inside a pod in the namespace shop (typical output)$ kubectl exec api -- cat /etc/resolv.confsearch shop.svc.cluster.local svc.cluster.local cluster.localnameserver 10.96.0.10options ndots:5endings to try, in this orderthe address of the CoreDNS Servicenames with fewer than 5 dots use the listThe program asks forLookups made, in orderResultwebweb.shop.svc.cluster.localfound at the first tryweb.toolsweb.tools.shop.svc.cluster.local, web.tools.svc.cluster.localfound at the second tryexample.comthree failed tries with the endings, then example.comfour lookups for one answerexample.com.example.comthe final dot skips the listA short name works because the pod adds its own namespace first: web here, web.tools for another namespace.With ndots:5, any name with fewer than five dots is tried with each ending before it is tried as written, so every call to anoutside host costs extra lookups. A headless Service (clusterIP: None) answers with the pod addresses themselves, and giveseach StatefulSet pod a name of its own: db-0.db.shop.svc.cluster.local.

A search list of three endings, and ndots:5.

Ingress

An Ingress shares one entrance among many Services

shop.example.comLoad balancerone public addressIngress controllerreads the rulesService webService apiweb podsapi pods//apiAn Ingress routes web requests by host and path, so many Services share one entrance.Giving every Service its own load balancer is costly. Instead, one load balancer feeds an Ingresscontroller, a reverse proxy running in the cluster. It reads your Ingress rules, ends the HTTPSconnection and passes each request to the right Service.

One load balancer, one controller, and rules by host and path.

Ingress · Going deeper · 1 of 2

The Ingress rules, written down

apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: shopspec: ingressClassName: nginx rules: - host: shop.example.com http: paths: - path: /api pathType: Prefix backend: service: { name: api, port: { number: 80 } } - path: / pathType: Prefix backend: service: { name: web, port: { number: 80 } }which controller should act on thisfor requests to this hostpaths that start with /apigo to the Service apieverything elsegoes to the Service webAn Ingress is only rules. An Ingress controller must be installed to carry them out.The newer Gateway API does the same job with Gateway and HTTPRoute objects, and new features go there.

A host, two paths, two Services.

Gateway API · Going deeper · 2 of 2

The same routes in the Gateway API: a Gateway and an HTTPRoute

Gateway: the entrance, kept by the platform teamHTTPRoute: the rules, kept by each application teamapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata: name: shop-gatewayspec: gatewayClassName: cilium listeners: - name: http protocol: HTTP port: 80apiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: shopspec: parentRefs: - name: shop-gateway hostnames: ["shop.example.com"] rules: - matches: - path: { type: PathPrefix, value: /api } backendRefs: - name: api port: 80 - backendRefs: - name: web port: 80attach to that Gatewaypaths under /apigo to Service apithe rest: Service webgatewayClassName picks the controller, asingressClassName did. Here it is Cilium; eachcontroller names its own class.The Gateway API splits Ingress in two: a Gateway for the entrance, an HTTPRoute for each application's rules.It is the successor to Ingress, whose API is frozen. It is installed as an add-on with a controller, not built in. The widelyused ingress-nginx controller was maintained only until March 2026 and gets no more fixes, so new work should start here.

A Gateway is the entrance. An HTTPRoute attaches rules to it.

NetworkPolicy

Everything can reach everything, until a policy says no

webapp=webapiapp=apidbapp=dbby default, every pod can call every podpolicy: only from app=api✗ web to db: now blocked✓Inside a cluster everything can reach everything, until a NetworkPolicy says otherwise.A NetworkPolicy selects pods by label and lists who may connect to them. Once any policy selectsa pod, all other traffic to it is refused. Pods with no policy stay open. The rules are enforcedby the cluster's network plugin, and a few simple plugins ignore them.

Default: all open. A NetworkPolicy lists who may connect.

NetworkPolicy · Going deeper

The policy, written down

apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: db-from-apispec: podSelector: matchLabels: { app: db } policyTypes: [Ingress] ingress: - from: - podSelector: matchLabels: { app: api } ports: - port: 5432the pods this policy protectsit limits incoming connectionsthe only callers allowed:pods labelled app=apiand only on the database portThe policy for the picture: the database accepts the api pods, on one port, and nobody else.A common first step is a "default deny" policy that selects every pod and allows nothing.

Select the db pods. Allow only api, on one port.

Lesson complete

You finished “Services and networking”

Nicely done.

Open in a new tab