1

This is the simplest config straight from the docs, but when I create the service, kubectl lists the target port as something random. Setting the target port to 1337 in the YAML:

apiVersion: v1
kind: Service
metadata:
  name: sails-svc
spec:
  selector:
    app: sails
  ports:
    - port: 1337
      targetPort: 1337
  type: LoadBalancer

And this is what k8s sets up for services:

kubectl get services
NAME           TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
kubernetes     ClusterIP      <X.X.X.X>      <none>          443/TCP          23h
sails          LoadBalancer   <X.X.X.X>      <X.X.X.X>       1337:30203/TCP   3m6s
svc-postgres   ClusterIP      <X.X.X.X>      <none>          5432/TCP         3m7s

Why is k8s setting the target port to 30203, when I'm specifying 1337? It does the same thing if I try other port numbers, 80 gets 31887. I've read the docs but disabling those attributes did nothing in GCP. What am I not configuring correctly?

Colby Blair
  • 396
  • 4
  • 15
  • [The GCP doc](https://cloud.google.com/kubernetes-engine/docs/quickstart) where I originally was creating this via the cli commands and was having the same problem, and switched to the YAML. No joy. – Colby Blair Sep 24 '21 at 01:42

2 Answers2

1

Kubectl get services output includes Port:NodePort:Protocol information.By default and for convenience, the Kubernetes control plane will allocate a port from a range default: 30000-32767(Refer the example in this documentation)

To get the TargetPort information try using

kubectl get service <your service name> --output yaml

This command shows all ports details and stable external IP address under loadBalancer:ingress:

Refer this documentation from more details on creating a service type loadbalancer

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
Goli Nikitha
  • 858
  • 3
  • 9
0

Maybe this was tripping me up more that it should have due to some redirects I didn't realize that were happening, but ironing out some things with my internal container and this worked.

Yields:

NAME           TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)          AGE
kubernetes     ClusterIP      10.3.240.1    <none>          443/TCP          28h
sails          LoadBalancer   10.3.253.83   <X.X.X.X>       1337:30766/TCP   9m59s
svc-postgres   ClusterIP      10.3.248.7    <none>          5432/TCP         12m

I can curl against the EXTERNAL-IP:1337. The internal target port was what was tripping me up. I thought that meant my pod needed to open up to that port and pod applications were supposed to bind to that port (i.e. 30766), but that's not the case. That port is some internal port mapping to the pod I still don't fully understand yet, but the pod still gets external traffic on port 1337 to the pod's 1337 port. I'd like to understand what's going on there better, as I get more into the k8s Networking section of the docs, or if anyone can enlighten me.

Colby Blair
  • 396
  • 4
  • 15
  • if you hit the my-service:1337(port) the traffic is routed to 1337 of the container(targetPort).Targetport:This is the actual port on which your application is running inside the container.([source](https://stackoverflow.com/questions/49981601/difference-between-targetport-and-port-in-kubernetes-service-definition)) – Goli Nikitha Sep 24 '21 at 06:06
  • But this my-service:1337 is internal to the kubernetes cluster and can be used when one application wants to communicate with another application. So to hit the service from outside the cluster someone needs to expose the port on the host machine on which kubernetes is running so that the traffic is redirected to a port of the container. This is node port(port exposed on the host machine). From the above example, you can hit the service from outside the cluster(Postman or any rest-client) by host_ip:nodePort ex: x:x:x:x:30203 – Goli Nikitha Sep 24 '21 at 06:08