More default named port in kubernetes?

10/30/2019

In kubernetes, I always see the service's definition like this:

---
apiVersion: v1
kind: Service
metadata:
  name: random-exporter
  labels:
    app: random-exporter
spec:
  type: ClusterIP
  selector:
    app: random-exporter
  ports:
    - port: 9800
      targetPort: http
      name: random-port

whose targetPort is http, it's human friendly!

And what I'm interested is that is there more named port such as http in kubernetes? Maybe https

-- Liqang Lau
kubernetes
kubernetes-service

1 Answer

10/30/2019

Usually you refer to target port by its number. But you can give a specific name to each pod`s port and refer this name in your service specification.

This will make your service clearer. Here you have example where you named your ports in pod.

apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: test
    ports:
    - name: http
      containerPort: 8080
    - name: https
      containerPort: 8443 

And here you refer to those ports by name in the service yaml.

apiVersion: v1
kind: Service
metadata:
  name: test-svc
spec:
  ports:
  - name: http
    port: 80
    targetPort: http
  - name: https
    port: 443
    targetPort: https 

Also from the kubernetes documention you may find this information:

targetPort - Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If this is a string, it will be looked up as a named port in the target Pod's container ports.

-- acid_fuji
Source: StackOverflow