defining 2 ports in deployment.yaml in Kubernetes

Adam picture Adam · Sep 9, 2018 · Viewed 9k times · Source

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?

Answer

Nicola Ben picture Nicola Ben · Sep 9, 2018

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