skip to Main Content

Hope life is great for everyone. I am trying to figure to use named ports for Deployment and Service. For pods and service using named ports works fine. But doesn’t for Deployment and Service. I dig and no answers are working, example https://github.com/GoogleCloudPlatform/k8s-multicluster-ingress/issues/196 and there is one in Stack Overflow which doesn’t helps much, How to make use of Kubernetes port names?

This is the YAML files.

deployment-named-port.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test-named-ports
  name: test-named-ports
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test-named-ports
  strategy: {}
  template:
    metadata:
      labels:
        app: test-named-ports
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
          name: nginx-port

named-port-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: test-named-ports
  ports:
    - port: nginx-port
      targetPort: nginx-port
      nodePort: 30007

vagrant@kubemaster:~$ kubectl apply -f named-port.yaml
deployment.apps/test-nameed-ports created

vagrant@kubemaster:~$ kubectl apply -f named-port-svc.yaml
error: error validating "named-port-svc.yaml": 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

Can someone please help me. Appreciate all your support.

Thanks,

Joe Antony

2

Answers


  1. The port and targetPort should 80 not string nginx-port.

    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      type: NodePort
      selector:
        app: test-named-ports
      ports:
        - name: nginx-port
          port: 80
          targetPort: 80
          nodePort: 30007
    
    
    Login or Signup to reply.
  2. Try:

    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      type: NodePort
      selector:
        app: test-named-ports
      ports:
      - name: nginx-port       # <-- port name that this service expose
        port: 80               # <-- port # that this service expose
        targetPort: nginx-port # <-- port name that the backing pod expose
        nodePort: 30007
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search