Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could targetPort be set with string value in kubernetes?

Tags:

kubernetes

I understand how to set targetPort as integer value when defining service in k8s.

However, I'm a little confused about how to set targetPort with string value.

Is there any example about this?

Thanks,

like image 360
Jared Avatar asked Aug 02 '17 16:08

Jared


2 Answers

This Service is for Prometheus. In the following manifest, you first have to define web in the Deployment before you can refer to it as a string in targetPort.

apiVersion: v1
kind: Service
metadata:
  labels:
    prometheus: k8s
  name: prometheus-k8s
  namespace: monitoring
spec:
  ports:
  - name: web
    nodePort: 30900
    port: 9090
    protocol: TCP
    targetPort: web
  selector:
    prometheus: k8s
  type: NodePort
like image 183
Eugene Chow Avatar answered Sep 28 '22 06:09

Eugene Chow


To address the comment by @sfgroups :

Port number should be integer, Is there reason you want to set the string value?

I actually don't ever use numbers in my targetPort because from the PoV of a Service, that is the contract you have with the Pods, to say (as in Eugene's snippet) the service will provide "web" content on port 9090 to the outside, and will do so using the exposed (key word there) port named "web" from the Pod, and it is then up to the Pod to map the Pod's "web" to the integer port in its containers. So if they want to use nginx on :80 or tomcat on :8080 or node on :3000 or or or, that is up to the Pod and its containers, and should not be a concern of the Service.

like image 33
mdaniel Avatar answered Sep 28 '22 07:09

mdaniel