defining 2 ports in deployment.yaml in Kubernetes

11,593

Solution 1

You can add as many ports as you need.

Here your deployment.yml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: websphere
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: websphere
    spec:
      containers:
      - name: websphere
        image: ibmcom/websphere-traditional:install
        ports:
        - containerPort: 9043
        - containerPort: 9443
        resources:
          requests: 
            memory: 500Mi
            cpu: 0.5
          limits:
            memory: 500Mi
            cpu: 0.5
        imagePullPolicy: IfNotPresent

Here your service.yml:

apiVersion: v1
kind: Service
metadata:
  name: websphere
  labels:
    app: websphere
spec:
  type: NodePort #Exposes the service as a node ports
  ports:
  - port: 9043
    name: hello
    protocol: TCP
    targetPort: 9043
    nodePort: 30043
  - port: 9443
    name: privet
    protocol: TCP
    targetPort: 9443
    nodePort: 30443
  selector:
    app: websphere

Check on your kubernetes api-server configuration what is the range for nodePorts (usually 30000-32767, but it's configurable).

EDIT

If I remove from deployment.yml the resources section, it starts correctly (after about 5 mins). Here a snippet of the logs:

[9/10/18 8:08:06:004 UTC] 00000051 webcontainer I com.ibm.ws.webcontainer.VirtualHostImpl addWebApplication SRVE0250I: Web Module Default Web Application has been bound to default_host[:9080,:80,:9443,:506 0,:5061,:443].

Problems come connecting to it (I use ingress with traefik), because of certificates (I suppose):

[9/10/18 10:15:08:413 UTC] 000000a4 SSLHandshakeE E SSLC0008E: Unable to initialize SSL connection. Unauthorized access was denied or security settings have expired. Exception is javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

To solve that (I didn't go further) this may help: SSLHandshakeE E SSLC0008E: Unable to initialize SSL connection. Unauthorized access was denied or security settings have expired

Trying to connect with port-forward:

enter image description here

and using dthe browser to connect, I land on this page:

enter image description here

Solution 2

Well in kubernetes you can define your ports using #port label. This label comes under ports configuration in your deployment. According to the configurations you can simply define any numbers of ports you wish. Following example shows how to define two ports.

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 9376
    - name: https
      protocol: TCP
      port: 443
      targetPort: 9377
Share:
11,593
Adam
Author by

Adam

Updated on July 19, 2022

Comments

  • Adam
    Adam almost 2 years

    I have a docker image from I am doing

    docker run --name test -h test -p 9043:9043 -p 9443:9443 -d ibmcom/websphere-traditional:install
    

    I am trying to put into a kubernetes deploy file and I have this:

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: websphere
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: websphere
        spec:
          containers:
          - name: websphere
            image: ibmcom/websphere-traditional:install
            ports:
            - containerPort: 9443
            resources:
              requests: 
                memory: 500Mi
                cpu: 0.5
              limits:
                memory: 500Mi
                cpu: 0.5
            imagePullPolicy: Always
    

    my service.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: websphere
      labels:
        app: websphere
    spec:
      type: NodePort #Exposes the service as a node ports
      ports:
      - port: 9443
        protocol: TCP
        targetPort: 9443
      selector:
        app: websphere
    

    May I have guidance on how to map 2 ports in my deployment file?

  • Adam
    Adam over 5 years
    Hi Nicola, I tried to do $ kubectl apply -f WB_Sphere_service.yml The Service "websphere" is invalid: * spec.ports[0].name: Required value * spec.ports[1].name: Required value
  • Adam
    Adam over 5 years
    Thank you NIcola. I tried to do a port forward to my local host: kubectl port-forward websphere-7874884868-ntz54 9043:9043 9443:9443 but it told me that 043 -> 9043: error forwarding port 9043 to pod 7e184665bf11f95ee02d07d40224a52368aa6b50240f0c7faf1a335d5b87‌​a86e, uid : exit status 1: 2018/09/09 16:49:06 socat[1668] E connect(5, AF=2 127.0.0.1:9043, 16): Connection refused
  • Nicola Ben
    Nicola Ben over 5 years
    Does your image ibmcom/websphere-traditional open those port numbers?
  • Adam
    Adam over 5 years
    yes based on documentation. docker run --name test -h test -p 9043:9043 -p 9443:9443 -d \ ibmcom/websphere-traditional:install
  • Nicola Ben
    Nicola Ben over 5 years
    I added some info.
  • Adam
    Adam over 5 years
    i got that too @nicola. But i tried to access the app and nothing happened . how did you access it? https://<master ip>:30043/ibm/console/login.do?action=secure
  • Nicola Ben
    Nicola Ben over 5 years