Kubernetes :使用名称从服务中引用 containerPort

Kubernetes : refering to containerPorts from services with their name

嗨,documentation 谈到了 containers.ports 中的名称字段:

name If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.

我尝试按如下方式在我的服务中使用它,但解析器出现错误:

kind: Pod
apiVersion: v1
metadata:
  name: banana-app
  labels:
    app: banana
spec:
  containers:
    - name: banana-app
      image: hashicorp/http-echo
      args:
        - "-text=banana"
      ports:
      - containerPort: 5678
        name: bananaport
  terminationGracePeriodSeconds: 0
---

kind: Service
apiVersion: v1
metadata:
  name: banana-service
spec:
  selector:
    app: banana
  ports:
    - port: bananaport

解析器错误是:

error: error validating "temp.yml": error validating data: ValidationError(Service.spec.ports[0].port): invalid type for io.k8s.api.core.v1.ServicePort.port: got "string", expected "integer"; if you choose to ignore these errors, turn validation off with --validate=false

所以我想我没有正确使用端口名称。在我的服务中使用端口名称的正确方法是什么?

提前致谢, 阿卜杜勒加尼

port字段表示Service对象监听的端口,targetPort表示容器监听的端口

试试这个:

kind: Service
apiVersion: v1
metadata:
  name: banana-service
spec:
  selector:
    app: banana
  ports:
  - port: 8080
    targetPort: bananaport