nigelpoulton / TheK8sBook

YAML files for use with The Kubernetes Book
822 stars 665 forks source link

Chapter 7: Services, port definitions do not match #47

Open martingregoire opened 5 months ago

martingregoire commented 5 months ago

Hi @nigelpoulton, while enjoying your K8s book, I noticed that a port definition in one of the examples does not match another file.

In the Service definition it says the target port is 9090: https://github.com/nigelpoulton/TheK8sBook/blob/0b36032f3ef5ee8a22c8c232dedc0c025880246c/services/svc.yml#L16

The Pods that this Service refers to are defined like this, with port 8080: https://github.com/nigelpoulton/TheK8sBook/blob/0b36032f3ef5ee8a22c8c232dedc0c025880246c/services/deploy.yml#L19

And sure enough, following this chapter and applying both files to K8s will show this error message (please note that I'm using the "KinD" setup, and because of my local Docker setup have to explicitly create a port forwarding):

$ kubectl port-forward service/svc-test 30001:8080
Forwarding from 127.0.0.1:30001 -> 9090
Forwarding from [::1]:30001 -> 9090
Handling connection for 30001
E0625 13:52:21.097232   41027 portforward.go:409] an error occurred forwarding 30001 -> 9090: error forwarding port 9090 to pod ..., uid : failed to execute portforward in network namespace "/var/run/netns/cni-...": failed to connect to localhost:9090 inside namespace "...", IPv4: dial tcp4 127.0.0.1:9090: connect: connection refused IPv6 dial tcp6 [::1]:9090: connect: connection refused

After updating the services/svc.yml file and changing the targetPortfrom 9090 to 8080, it works as expected.

For reference, in the printed book (2023 edition) the services/svc.yml file is shown, and there it also says targetPort: 8080.