Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make use of Kubernetes port names?

Tags:

In a kubernetes deployment I specify a port like so:

 containers:
 - name: nginx
   image: nginx:latest
   ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP

Now in a service I can reference that port like so (allows me to only specify the external port in the service):

spec:
  type: ClusterIP
  ports:
  - name: nginx-port
    port: 80
    targetPort: nginx-port
    protocol: TCP

Now the question, can I reference service and port elsewhere using the following syntax nginx-service.default.svc.cluster.local:nginx-port? You know I can make reference to services using this special names, but I find myself hardcoding the port number like so nginx-service.default.svc.cluster.local:80.

like image 629
soosap Avatar asked Feb 20 '18 13:02

soosap


People also ask

How do ports work in Kubernetes?

Port exposes the Kubernetes service on the specified port within the cluster. Other pods within the cluster can communicate with this server on the specified port. TargetPort is the port on which the service will send requests to, that your pod will be listening on.

Which ports are used by Kubernetes?

By default, the Kubernetes API server listens on port 6443 on the first non-localhost network interface, protected by TLS. In a typical production Kubernetes cluster, the API serves on port 443. The port can be changed with the --secure-port , and the listening IP address with the --bind-address flag.

How do you expose a port in Kubernetes?

From the Service type drop-down list, select Node port. Click Expose. When your Service is ready, the Service details page opens, and you can see details about your Service. Under Ports, make a note of the Node Port that Kubernetes assigned to your Service.


2 Answers

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

This will make your service clearer. Here a small example:

apiVersion: v1 kind: Pod metadata:   name: named-port-pod   labels:     app: named-port-pod spec:   containers:     - name: echoserver       image: gcr.io/google_containers/echoserver:1.4       ports:       - name: pod-custom-port         containerPort: 8080 --- apiVersion: v1 kind: Service metadata:   name: named-port-svc spec:   ports:     - port: 80       targetPort: pod-custom-port   selector:     app: named-port-pod 
like image 104
Maxim Suslov Avatar answered Oct 14 '22 04:10

Maxim Suslov


Kubernetes DNS service provides SRV records for all named ports of services using below format

_my-port-name._my-port-protocol.my-svc.my-namespace.svc.cluster-domain.example

In your case, you should be able to access it by querying below domain

_nginx-port._tcp.nginx-service.default.svc.cluster.local

Example

nslookup -type=SRV _nginx-port._tcp.nginx-service.default.svc.cluster.local

And the result:

_nginx-port._tcp.nginx-service.default.svc.cluster.local   service = 0 100 80 nginx-service.default.svc.cluster.local.

In the above output, nginx-service.default.svc.cluster.local is the target domain and 80 is the target port that you should connect to, i.e. nginx-service.default.svc.cluster.local:80

like image 44
Mr.Dinh Avatar answered Oct 14 '22 06:10

Mr.Dinh