Just a quick reminder how ports definition in k8s services works.
Service definition example below, just forgot all but port definition and take a closer look.
NodePort
apiVersion: v1 kind: Service metadata: name: client-node-port spec: type: NodePort ports: - port: 3050 targetPort: 3000 nodePort: 31515 selector: component: web
We can see port definition 3 times (it’s array and you can use as many of them as open ports you need): port
, targetPort
and nodePort
. Why 3 ports? If you are familiar with Docker (Swarm) you would expect only 2, right…?
NodePort
port
: other pod/service inside our cluster can access our pod through this one
targetPort
: defines which port inside our pod should be open (all incomming traffic should go on this port). Equals to the port defined in Pod configuration.
nodePort
: (value between 30000 – 32767) allows us to access the pod from the outside on this port (myapp.com:31515), if not specified it is auto assigned
Please note NodePort (as a Service) should not be used in production but rather for testing purposes. You should use ClusterIP for prod env instead of NodePort
ClusterIP
(please note I and P are both capital-letters!)
ClusterIP exposes the service on the cluster-internal IP, so it is reachable from the cluster only. That is what we want (most of the times), because services in our cluster should be reachable only through the LoadBalancer (old way) or Ingress. The port definition is now more straight-forward, so I used 2 similar ports to keep things more simple. 🙂 If you want to keep port
and targetPort
same, you can omit targetPort
and it will be set to the same value as port
. The port definition is the same as using NodePort.
apiVersion: v1 kind: Service metadata: name: some-cluster-ip-service spec: type: ClusterIP selector: component: web ports: - port: 3000 targetPort: 3000
You can also find explanation using very useful kubectl explain
:
kubectl explain services.spec.port
Leave a Reply