如何在 Kubernetes 中的同一个 POD 中的容器之间进行通信?
How to communicate between containers in same POD in Kubernetes?
对于一个 POD,已创建三个图像。这里的问题是同一 pod 中的容器之间没有通信。我的应用程序应该如何连接这三个容器?
我的 pod 有以下容器。
[dev-application dev-app-nginx dev-app-redis]
这里我只能看到rails是运行,redis和nginx不是运行。因为 Redis 和 nix 运行 作为同一个 pod 中的不同容器。
kubectl exec -ti test-deployment-5f59864c8b-mv4kk sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulting container name to dev-application.
Use 'kubectl describe pod/test-deployment-5f59864c8b-mv4kk -n dev-app' to see all of the containers in this pod.
# rails -v
Rails 4.2.11.3
# redis -v
sh: 2: redis: not found
# nginx -v
sh: 3: nginx: not found
#
下面是我使用的yam文件
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: dev-app
name: test-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: Dev-app
spec:
nodeSelector:
cloud.io/sec-zone-green: "true"
containers:
- name: dev-application
image: hub.docker.net/appautomation/dev.app.1.0:latest
command: ["/bin/sh"]
args: ["-c", "while true; do echo test; sleep 20;done"]
resources:
limits:
memory: 8Gi
cpu: 5
requests:
memory: 8Gi
cpu: 5
ports:
- containerPort: 3000
- name: dev-app-nginx
image: hub.docker.net/appautomation/dev.nginx.1.0:latest
resources:
limits:
memory: 4Gi
cpu: 4
requests:
memory: 4Gi
cpu: 4
ports:
- containerPort: 80
- name: dev-app-redis
image: hub.docker.net/appautomation/dev.redis.1.0:latest
resources:
limits:
memory: 4Gi
cpu: 4
requests:
memory: 4Gi
cpu: 4
ports:
- containerPort: 6379
使用 localhost 与同一 pod 中的其他容器通信。
例如容器的地址是
- 127.0.0.1:3000
- 127.0.0.1:80
- 127.0.0.1:6379
Jonas 是对的,但我想扩展一下这个话题。
让我们讨论一下容器可以用来在 Kubernetes 中相互通信的两种方法:
- 进程间通信 (IPC):
Containers in a Pod share the same IPC namespace, which means they can
also communicate with each other using standard inter-process
communications such as SystemV semaphores or POSIX shared
memory. Containers use the strategy of the localhost hostname for
communication within a Pod.
Pod 中的容器可以通过“localhost”访问;他们使用相同的网络名称space。此外,对于容器,可观察的主机名是 Pod 的名称。由于容器共享相同的 IP 地址和端口 space,因此您应该在容器中使用不同的端口进行传入连接。换句话说,Pod 中的应用程序必须协调它们对端口的使用。因此,每个容器都可以作为本地主机上的不同端口访问 pod 中的其他容器。
- Kubernetes Pod 中的共享卷:
In Kubernetes, you can use a shared Kubernetes Volume as a simple and
efficient way to share data between containers in a Pod. For most
cases, it is sufficient to use a directory on the host that is shared
with all containers within a Pod.
如果你想更多地探索第二种方法,我建议你阅读官方指南:Communicate Between Containers in the Same Pod Using a Shared Volume:
This page shows how to use a Volume to communicate between two
Containers running in the same Pod.
此外,您还可以在下面找到包含更多详细信息和示例的完整源文章:
对于一个 POD,已创建三个图像。这里的问题是同一 pod 中的容器之间没有通信。我的应用程序应该如何连接这三个容器?
我的 pod 有以下容器。
[dev-application dev-app-nginx dev-app-redis]
这里我只能看到rails是运行,redis和nginx不是运行。因为 Redis 和 nix 运行 作为同一个 pod 中的不同容器。
kubectl exec -ti test-deployment-5f59864c8b-mv4kk sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulting container name to dev-application.
Use 'kubectl describe pod/test-deployment-5f59864c8b-mv4kk -n dev-app' to see all of the containers in this pod.
# rails -v
Rails 4.2.11.3
# redis -v
sh: 2: redis: not found
# nginx -v
sh: 3: nginx: not found
#
下面是我使用的yam文件
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: dev-app
name: test-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: Dev-app
spec:
nodeSelector:
cloud.io/sec-zone-green: "true"
containers:
- name: dev-application
image: hub.docker.net/appautomation/dev.app.1.0:latest
command: ["/bin/sh"]
args: ["-c", "while true; do echo test; sleep 20;done"]
resources:
limits:
memory: 8Gi
cpu: 5
requests:
memory: 8Gi
cpu: 5
ports:
- containerPort: 3000
- name: dev-app-nginx
image: hub.docker.net/appautomation/dev.nginx.1.0:latest
resources:
limits:
memory: 4Gi
cpu: 4
requests:
memory: 4Gi
cpu: 4
ports:
- containerPort: 80
- name: dev-app-redis
image: hub.docker.net/appautomation/dev.redis.1.0:latest
resources:
limits:
memory: 4Gi
cpu: 4
requests:
memory: 4Gi
cpu: 4
ports:
- containerPort: 6379
使用 localhost 与同一 pod 中的其他容器通信。
例如容器的地址是
- 127.0.0.1:3000
- 127.0.0.1:80
- 127.0.0.1:6379
Jonas 是对的,但我想扩展一下这个话题。
让我们讨论一下容器可以用来在 Kubernetes 中相互通信的两种方法:
- 进程间通信 (IPC):
Containers in a Pod share the same IPC namespace, which means they can also communicate with each other using standard inter-process communications such as SystemV semaphores or POSIX shared memory. Containers use the strategy of the localhost hostname for communication within a Pod.
Pod 中的容器可以通过“localhost”访问;他们使用相同的网络名称space。此外,对于容器,可观察的主机名是 Pod 的名称。由于容器共享相同的 IP 地址和端口 space,因此您应该在容器中使用不同的端口进行传入连接。换句话说,Pod 中的应用程序必须协调它们对端口的使用。因此,每个容器都可以作为本地主机上的不同端口访问 pod 中的其他容器。
- Kubernetes Pod 中的共享卷:
In Kubernetes, you can use a shared Kubernetes Volume as a simple and efficient way to share data between containers in a Pod. For most cases, it is sufficient to use a directory on the host that is shared with all containers within a Pod.
如果你想更多地探索第二种方法,我建议你阅读官方指南:Communicate Between Containers in the Same Pod Using a Shared Volume:
This page shows how to use a Volume to communicate between two Containers running in the same Pod.
此外,您还可以在下面找到包含更多详细信息和示例的完整源文章: