How to configure multiple services/containers in Kubernetes?

4/18/2019

I am new to Docker and Kubernetes. Technologies used:

  • Dotnet Core 2.2
  • Asp.NET Core WebAPI 2.2
  • Docker for windows(Edge) with Kubernetes support enabled
  • Code

I am having two services hosted into two docker containers container1 and container2.

Below is my deploy.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webapi-dockerkube
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: webapi-dockerkube
    spec:
      containers:
      - name: webapi-dockerkube
        image: "webapidocker:latest"
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        livenessProbe:
          httpGet:
            path: /api/values
            port: 80
        readinessProbe:
          httpGet:
            path: /api/values
            port: 80
      - name: webapi-dockerkube2
        image: "webapidocker2:latest"
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        livenessProbe:
          httpGet:
            path: /api/other/values
            port: 80
        readinessProbe:
          httpGet:
            path: /api/other/values
            port: 80

When I am running command:

kubectl create -f .\deploy.yaml

I am getting status as CrashLoopBackOff.

But same is running fine when i have only one container configured. When checking logs I am getting following error: Error from server (BadRequest): a container name must be specified for pod webapi-dockerkube-8658586998-9f8mk, choose one of: [webapi-dockerkube webapi-dockerkube2]

-- Vikas Gupta
asp.net-core
docker
docker-for-windows
kubernetes
microservices

2 Answers

4/18/2019
apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:

  restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the debian container > /pod-data/index.html"] 

here sharing example for multi container you can use this template

Also you can check for logs of using

Kubectl logs

Check reason for crashloop back

-- Harsh Manvar
Source: StackOverflow

4/18/2019

You are running two containers in the same pod which bind both to port 80. This is not possible within the same pod. Think of a pod like a 'server' and you can't have two processes bind to the same port.

Solution in your situation: Use different ports inside the pod or use separate pods. From your deployment there seems to be no shared resources like filesystem, so it would be easy to split the containers to separate pods.

Note that it will not suffice to change the pod definition if you want to have both containers running in the same pod with different ports. The application in the container must bind to a different port as well.

-- Thomas
Source: StackOverflow