Kubernetes - Create two containers in one pod

10/8/2018

I'm developing an application that consists of two containers. I want to deploy them in Kubernetes into one Pod, since I want the two services to be behind one IP address. However, I'm having a hard time trying to connect the kubernetes Services with the containers. How could I write a deployment.yml file, so that when the user calls a x.x.x.x:port1, the request is forwarded to the first container, and when the x.x.x.x:port2 is called, the request is forwarded to the second container. How could I specify the Services?

Here's what I have until now:

apiVersion:v1
kind: Pod
metadata:
  name: my_app
spec:
  containers:
  - name: first_container
    image: first_image

  - name: second_container
    image: second_image
---
apiVersion: v1
kind: Service
...

Thank you in advance!

-- UrmLmn
deployment
docker
kubernetes

1 Answer

10/8/2018

In your containers section you need to define a containerPort for each:

  containers:
  - name: first_container
    image: first_image
    ports:
    - containerPort: port1
  - name: second_container
    image: second_image
    ports:
    - containerPort: port2

And then in the ports section of the service definition you need to point the targetPorts of the service at those ports like in https://stackoverflow.com/a/42272547/9705485

-- Ryan Dawson
Source: StackOverflow