How do I set these docker-compose ports in a kubernetes yaml file?

6/2/2019

Given the following ports defined in a docker-compose.yml file, how do I do the equivalent in a kubernetes yml file?

docker-compose.yml

  seq.logging:
    image: datalust/seq
    networks: 
      - backend
    container_name: seq.logging
    environment:
      - ACCEPT_EULA=Y
    ports:
      - "5300:80" # UI
      - "5301:5341" # Data ingest

kubernetes.yml

---
apiVersion: v1
kind: Pod
metadata:
  name: backend-infrastructure
  labels:
    system: backend
    app: infrastructure
spec:

  containers:

  - name: seq-logging
    image: datalust/seq
#    ports: ?????????????????????????????????????
#    - containerPort: "5300:80" # UI
#    - containerPort: "5301:5341" # Data ingest
    env:
    - name: ACCEPT_EULA
      value: "Y"
-- Pure.Krome
docker
docker-compose
kubernetes

1 Answer

6/4/2019

You do not expose a port using Pod/deployment yaml. Services are the way to do it. Here you can either use multiple services on top of your pod/deployment but this will result in multiple IP addresses. Other way is to name each port and then create a multi port service definition.

In your case it should look somewhat like this (note this is just a quickly written example). Also

When using multiple ports you must give all of your ports names, so that endpoints can be disambiguated.

apiVersion: v1
kind: Pod
metadata:
  name: backend-infrastructure
  labels:
    system: backend
    app: infrastructure
spec:
  containers:
  - name: seq-logging
    image: datalust/seq
    ports:
    - containerPort: 80 # UI
      name: ui
    - containerPort: 5341 # Data ingest
      name: data-ingest
    env:
    - name: ACCEPT_EULA
      value: "Y"
---
apiVersion: v1
kind: Service
metadata:
  name: seq-logging-service
spec:
  type: #service type
  ports:
  - name: ui
    port: 5300
    targetPort: 80
  - name: data-ingest
    port: 5301
    targetPort: 5341

Some more resources: - Docs about connecting applications with services. - example yaml from the above featuring deployment with multiple port container and corresponding service.

Update: containerPort

List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.

-- aurelius
Source: StackOverflow