Kubernetes 多容器 Pod 中的容器间通信
Inter container communication in Kubernetes multi container Pod
我有一个包含 3 个容器 A、B 和 C 的 pod。我想从 C 访问容器 A 和 B 中的服务。localhost:<port>
和 127.0.0.1
都不工作。
我的 yaml
apiVersion: "v1"
kind: Pod
metadata:
name: web3
labels:
name: web
app: demo
spec:
containers:
- name: client
image: ubuntu
command: ['cat']
tty: true
- name: apache1
image: nimmis/apache-php5
ports:
- containerPort: 8080
name: apacheport1
protocol: TCP
- name: apache2
image: nimmis/apache-php5
command: ['cat']
tty: true
ports:
- containerPort: 8088
name: apacheport2
protocol: TCP
我在做什么
kubectl apply -f example.yaml
kubectl exec -it web3 -c client bash
然后尝试访问其他 2 个服务
root@web3:/# curl http://localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
root@web3:/# curl http://localhost:8088
curl: (7) Failed to connect to localhost port 8088: Connection refused
root@web3:/# curl http://localhost:80
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
问题
如何使前 2 个 curl 工作。 (我不想使用该服务,因为我的用例仅用于测试目的)
为什么我没有暴露端口却有开放的80端口
重点是 nimmis/apache-php5
Apache 正在侦听端口 80。
因此,暴露的是端口 80。
通过 containerPort: <P>
你并不是说将容器的端口 80 暴露给 <P>
,而是暴露端口 <P>
本身。另外,正如文档中所写,Not specifying a port here DOES NOT prevent that port from being exposed.
.
我没有找到将内部容器端口映射到 pod 中不同端口的方法。但是,您可以通过 hostPort
.
字段将内部容器端口映射到主机端口
apiVersion: "v1"
kind: Pod
metadata:
name: web3
labels:
name: web
app: demo
spec:
containers:
- name: client
image: ubuntu
command: ['cat']
tty: true
- name: apache1
image: nimmis/apache-php5
ports:
- containerPort: 80
name: apacheport1
hostPort: 8002
protocol: TCP
- name: apache2
image: nimmis/apache-php5
command: ['cat']
tty: true
ports:
- containerPort: 80
hostPort: 8001
name: apacheport2
protocol: TCP
然后你得到节点的 IP,例如,在 Minikube 上
$ minikube ip # e.g., 192.168.97.100
并检查您是否可以从 client
访问 Apache 服务:
$ kubectl exec -it web3 -c client bash
# apt-get update && apt-get install curl
# curl 192.168.99.100:8002
我有一个包含 3 个容器 A、B 和 C 的 pod。我想从 C 访问容器 A 和 B 中的服务。localhost:<port>
和 127.0.0.1
都不工作。
我的 yaml
apiVersion: "v1"
kind: Pod
metadata:
name: web3
labels:
name: web
app: demo
spec:
containers:
- name: client
image: ubuntu
command: ['cat']
tty: true
- name: apache1
image: nimmis/apache-php5
ports:
- containerPort: 8080
name: apacheport1
protocol: TCP
- name: apache2
image: nimmis/apache-php5
command: ['cat']
tty: true
ports:
- containerPort: 8088
name: apacheport2
protocol: TCP
我在做什么
kubectl apply -f example.yaml
kubectl exec -it web3 -c client bash
然后尝试访问其他 2 个服务
root@web3:/# curl http://localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
root@web3:/# curl http://localhost:8088
curl: (7) Failed to connect to localhost port 8088: Connection refused
root@web3:/# curl http://localhost:80
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
问题 如何使前 2 个 curl 工作。 (我不想使用该服务,因为我的用例仅用于测试目的) 为什么我没有暴露端口却有开放的80端口
重点是 nimmis/apache-php5
Apache 正在侦听端口 80。
因此,暴露的是端口 80。
通过 containerPort: <P>
你并不是说将容器的端口 80 暴露给 <P>
,而是暴露端口 <P>
本身。另外,正如文档中所写,Not specifying a port here DOES NOT prevent that port from being exposed.
.
我没有找到将内部容器端口映射到 pod 中不同端口的方法。但是,您可以通过 hostPort
.
apiVersion: "v1"
kind: Pod
metadata:
name: web3
labels:
name: web
app: demo
spec:
containers:
- name: client
image: ubuntu
command: ['cat']
tty: true
- name: apache1
image: nimmis/apache-php5
ports:
- containerPort: 80
name: apacheport1
hostPort: 8002
protocol: TCP
- name: apache2
image: nimmis/apache-php5
command: ['cat']
tty: true
ports:
- containerPort: 80
hostPort: 8001
name: apacheport2
protocol: TCP
然后你得到节点的 IP,例如,在 Minikube 上
$ minikube ip # e.g., 192.168.97.100
并检查您是否可以从 client
访问 Apache 服务:
$ kubectl exec -it web3 -c client bash
# apt-get update && apt-get install curl
# curl 192.168.99.100:8002