Service

Understand why changing Pod addresses cause problems and use a Service to provide stable internal access to the Node.js order service.

🎯 Introduction

In the previous article, we used a Deployment to run multiple Node.js order service Pods and verified that a deleted Pod is automatically replaced.

Now consider a payment service that needs to call the order service. If it stores a specific order Pod's IP, replacing that Pod may change the IP, leaving the payment service unable to reach it.

This article introduces Services, which provide a stable access point for a group of order Pods so callers do not depend on an individual Pod address. We will create a Service, inspect its backends, and access the order endpoint from a local computer.


🧱 Why you should not hard-code Pod IPs

Suppose the payment service initially calls an order Pod directly:

http://10.244.0.5:3000

After an update, the old Pod is deleted and the replacement has a different IP:

http://10.244.0.8:3000

If the payment service continues using the old IP, requests may fail. These two addresses are illustrative, not actual query results from the course cluster.

What we need is an address that remains as stable as possible for the payment service, even when order Pods are recreated.

Kubernetes addresses this with a Service. A Service finds backends through Pod labels and provides a stable internal name and address.

courseComponents.mermaid.loading

The two backend connections mean a request can be forwarded to one eligible, available Pod, not that every request is sent to both Pods simultaneously.

Keep these responsibilities separate:

  • A Deployment maintains the order Pod replica count.
  • A Service provides a stable access point for those Pods; it does not create them.

🛠 Creating a Service for the order Deployment

Continue using the node-course cluster, course Namespace, and orders Deployment. First, check the Deployment:

kubectl get deployment orders -n course

Simulated output:

NAME     READY   UP-TO-DATE   AVAILABLE   AGE
orders   2/2     2            2           5m

If you scaled to three replicas in the previous exercise, you may see 3/3 instead. That is fine: you need at least one available order Pod. Actual output depends on the environment and readiness state.

1. Write the Service YAML

Create service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: orders
  namespace: course
spec:
  type: ClusterIP
  selector:
    app: orders
  ports:
    - port: 80
      targetPort: 3000

You have already seen apiVersion, kind, and metadata. Focus on spec:

FieldPurpose
type: ClusterIPProvides internal cluster access; this is the default Service type
selector.app: ordersFinds Pods labeled app: orders
port: 80The port on which the Service receives requests
targetPort: 3000The backend Pod port to which requests are forwarded

This selector corresponds to the Pod template labels in the previous Deployment:

# Pod labels in the Deployment (excerpt)
template:
  metadata:
    labels:
      app: orders

Only Pods with matching labels become candidate backends for the Service.

2. Understand port and targetPort

courseComponents.mermaid.loading

Within the cluster, a request to http://orders uses the Service's port 80, which forwards it to the application's port 3000. These ports do not need to match.

ClusterIP primarily provides internal cluster access. It does not mean your computer's browser can access the virtual IP directly. We will test locally with kubectl port-forward.


🔎 Checking backends and access

1. Create the Service

Run this in the directory containing service.yaml:

kubectl apply -f service.yaml

Simulated output after successful initial creation:

service/orders created

If the Service already exists and its configuration is unchanged, the output may be unchanged.

2. Inspect the Service address

kubectl get service orders -n course

Simulated output:

NAME     TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
orders   ClusterIP   10.96.120.50   <none>        80/TCP    20s
FieldMeaning
TYPEService type, here ClusterIP
CLUSTER-IPVirtual IP used inside the cluster
EXTERNAL-IPExternal address; none is assigned here, so it shows <none>
PORT(S)Service ports and protocols

10.96.120.50 is a simulated address; your assigned IP may differ. Finding the Service does not prove that it has available order Pods. Check its backends next.

3. Check which Pods the Service found

For now, think of an EndpointSlice as a Kubernetes resource that records a group of backend network addresses for a Service. For a Service using a label selector, as here, a controller updates the associated EndpointSlices based on matching Pods.

Run:

kubectl get endpointslices -n course -l kubernetes.io/service-name=orders

Simulated output; names and address counts vary by environment:

NAME           ADDRESSTYPE   PORTS   ENDPOINTS                   AGE
orders-abc12   IPv4          3000    10.244.0.5,10.244.0.8     25s

Focus on two fields:

  • PORTS: The backend port, here 3000.
  • ENDPOINTS: The recorded backend addresses. These two addresses correspond to two order Pods.

If you scaled the Deployment to three replicas, you may see three addresses. An address appearing in an EndpointSlice does not necessarily mean that backend is ready. Here we mainly use it to check that the Service found backends; readiness conditions are covered in the health probe article.

4. Access the order service locally

In terminal A, run:

kubectl port-forward -n course service/orders 8080:80

Simulated output:

Forwarding from 127.0.0.1:8080 -> 3000
Forwarding from [::1]:8080 -> 3000

8080:80 forwards local port 8080 to the Service's specified port 80. The terminal remains occupied, so leave it open.

Note: When forwarding a Service with kubectl port-forward, kubectl selects one matching Pod and establishes a forwarding connection. This is for local debugging; it is not equivalent to load balancing through the Service virtual IP inside the cluster, and it cannot verify requests being distributed across Pods.

In terminal B, run:

curl http://127.0.0.1:8080/api/info

Based on the Express example in the Pod article, the simulated response is:

{"service":"orders","version":"v1"}

This confirms that local port forwarding reached the Express application in an order Pod. The JSON matches the /api/info code from the earlier article.

After testing, press Ctrl+C in terminal A to stop forwarding. If local port 8080 is occupied, use 8081:80 and request http://127.0.0.1:8081/api/info.


🔄 Which address should other services use?

Inside the Kubernetes cluster, other Pods can also access a Service by name.

Suppose the payment service runs in another Pod in the same course Namespace as the order Service. It can request:

http://orders/api/info

If it runs in a different Namespace, use a name that includes the Namespace:

http://orders.course.svc.cluster.local/api/info

orders is the Service name and course is its Namespace. svc.cluster.local is a common default cluster domain suffix; a custom cluster domain changes the address accordingly.

Cluster DNS resolves these names. Callers do not need to hard-code an order Pod IP.

Remember: these names are for communication between Pods inside the cluster. Do not assume they resolve directly in your computer's terminal or browser. We used port-forward earlier to test the endpoint locally.


❓ Questions and answers

Q: Does a Service create order service Pods?

No. A Deployment maintains Pod replicas, while a Service provides access to them. A Service can exist without any available backends.

Q: Does a stable Service address guarantee that requests always succeed?

No. Requests may still fail if no matching Pods are ready or the application fails. A Service solves stable addressing, not every application failure.

Q: Must a Service's port match the Node.js listening port?

No. This Service uses port 80 while Node.js listens on 3000. targetPort: 3000 connects the two.


🧪 Exercise

Deliberately use an incorrect label to verify that an existing Service does not necessarily have available backends.

  1. Temporarily change selector.app in service.yaml from orders to nonexistent.
  2. Run kubectl apply -f service.yaml, then query the Service and EndpointSlices again.
  3. Try kubectl port-forward -n course service/orders 8080:80 and observe the error or failure to establish forwarding.
  4. Restore app: orders, apply service.yaml again, and access /api/info after the backends reappear.

With an incorrect selector, kubectl get service orders -n course still finds the Service, but it normally cannot find the original order Pods. EndpointSlices may show no backend addresses, or the corresponding resource may temporarily be absent. Exact output depends on cluster state and controller synchronization.

When running kubectl port-forward service/orders, a Service without matching Pods may cause the forwarding command to fail immediately, rather than establish forwarding and fail only when curl runs. No single error message is the only valid result.

Success criteria: Explain how Service label selection works, distinguish port from targetPort, and explain why an existing Service may not provide access to the application. Restore the correct selector afterward for the next article.


🎉 You now understand why fixed Pod IPs are unreliable and can use a Service to provide stable internal access to a Node.js order service.