如何在 kubernetes 中使用字符串值设置 targetPort?

Jar*_*red 6 kubernetes

我了解在 k8s 中定义服务时如何将 targetPort 设置为整数值。

但是,我对如何使用字符串值设置 targetPort 有点困惑。

有没有关于这个的例子?

谢谢,

Eug*_*how 6

此服务适用于 Prometheus。在以下清单中,您首先必须web在 Deployment 中定义,然后才能将其作为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
Run Code Online (Sandbox Code Playgroud)


mda*_*iel 5

针对 @sfgroups 的评论:

端口号应该是整数,您是否有理由要设置字符串值?

实际上,我从来没有在 my 中使用数字,targetPort因为从服务的 PoV 来看,这是您与 Pod 签订的合同,也就是说(如尤金的代码片段中)该服务将在端口 9090 上向外部提供“Web”内容,并且将使用 Pod 中名为“web”的公开(关键字)端口来执行此操作,然后由 Pod 将 Pod 的“web”映射到其容器中的整数端口。因此,如果他们想在 :80 上使用 nginx 或在 :8080 上使用 tomcat 或在 :3000 上使用节点或或或,这取决于 Pod 及其容器,而不应该是服务关心的问题。