Monday, October 15, 2018

Differences among Port TargetPort nodePort containerPort in Kubernetes

We use below below yaml  to explain:

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  labels:
    app: mysql
spec:
    selector:
      app: mysql
    ports:
      - port: 3309
        targetPort:  3306
        nodePort:  30301
    clusterIP: None


This specification will create a Service which targets TCP port 3306 on any Pod with the app: mysql label, and expose it on an abstracted Service port 3309 (targetPort 3306: is the port the container accepts traffic on, port 3309: is the abstracted Service port, which can be any port other pods use to access the Service). nodePort 30301 is to expose service outside kubernete cluster via kube-proxy.

  • The port is 3309 which represents that order-service can be accessed by other services in the cluster at port 3306(it is advised to be same as targetPort). However when type is LoadBalancer, the port 3309 would be on different scope. It is the service port on LoadBalancer. It is the port which LoadBalancer is listening on. Because type is not clusterIP any more. 
  • The targetPort is 3306 which represents the order-service is actually running on port 3306 on pods
  • The nodePort is 30301 which represents that order-service can be accessed via kube-proxy on port 30301.
  • containerPort which is similar as targetPort , it is used in pod defination yaml

No comments:

Post a Comment