如何在 Kubernetes 中配置多个 services/containers?
How to configure multiple services/containers in Kubernetes?
我是 Docker 和 Kubernetes 的新手。
使用的技术:
- 点网核心 2.2
- Asp.NET 核心 WebAPI 2.2
- Docker for windows(Edge) 启用了 Kubernetes 支持
- Code
我将两个服务托管到两个 docker 容器 container1 和 container2 中。
下面是我的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
当我运行命令时:
kubectl create -f .\deploy.yaml
我的状态为 CrashLoopBackOff
。
但是当我只配置了一个容器时,运行 也很好。
检查日志时出现以下错误:
Error from server (BadRequest): a container name must be specified for pod webapi-dockerkube-8658586998-9f8mk, choose one of: [webapi-dockerkube webapi-dockerkube2]
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
您还可以查看使用日志
Kubectl logs
检查返回崩溃循环的原因
您是 运行 同一个 pod 中的两个容器,它们都绑定到端口 80。这在同一个 pod 中是不可能的。
将 pod 想象成 'server',您不能让两个进程绑定到同一个端口。
针对您情况的解决方案:在 pod 内使用不同的端口或使用单独的 pods。从您的部署来看,似乎没有像文件系统这样的共享资源,因此很容易将容器拆分为单独的 pods.
请注意,如果您希望两个容器 运行 在具有不同端口的同一个 pod 中,仅更改 pod 定义是不够的。容器中的应用程序也必须绑定到不同的端口。
我是 Docker 和 Kubernetes 的新手。 使用的技术:
- 点网核心 2.2
- Asp.NET 核心 WebAPI 2.2
- Docker for windows(Edge) 启用了 Kubernetes 支持
- Code
我将两个服务托管到两个 docker 容器 container1 和 container2 中。
下面是我的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
当我运行命令时:
kubectl create -f .\deploy.yaml
我的状态为 CrashLoopBackOff
。
但是当我只配置了一个容器时,运行 也很好。
检查日志时出现以下错误:
Error from server (BadRequest): a container name must be specified for pod webapi-dockerkube-8658586998-9f8mk, choose one of: [webapi-dockerkube webapi-dockerkube2]
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
您还可以查看使用日志
Kubectl logs
检查返回崩溃循环的原因
您是 运行 同一个 pod 中的两个容器,它们都绑定到端口 80。这在同一个 pod 中是不可能的。 将 pod 想象成 'server',您不能让两个进程绑定到同一个端口。
针对您情况的解决方案:在 pod 内使用不同的端口或使用单独的 pods。从您的部署来看,似乎没有像文件系统这样的共享资源,因此很容易将容器拆分为单独的 pods.
请注意,如果您希望两个容器 运行 在具有不同端口的同一个 pod 中,仅更改 pod 定义是不够的。容器中的应用程序也必须绑定到不同的端口。