How to configure multiple services/containers in Kubernetes?

跟風遠走 提交于 2021-02-08 06:09:39

问题


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]


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/55751220/how-to-configure-multiple-services-containers-in-kubernetes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!