Pod 为了亲密性应用而存在:
- 两个应用需要通过 127.0.0.1 或者 Socket 通信;
- 两个应用之间发生文件交互;
- 两个应用发生频繁的调用
共享网络
Pod 中的所有容器拥有同一个 IP 地址:
# 生成 Pod 的 YAML 文件
[root@k8s-master01 ~]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
app: my-podz
name: my-podz
namespace: default
spec:
containers:
- name: nginx-demoz
image: nginx:1.8
- name: java-demoz
image: www.myharbor.com/wangqihan/java-demo:v1
# 创建 Pod
[root@k8s-master01 ~]# kubectl apply -f pod.yaml
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-podz 2/2 Running 0 11s
# 进入 java-demoz 查看 IP 地址
[root@k8s-master01 ~]# kubectl exec -it my-podz -c java-demoz bash
root@my-podz:/usr/local/tomcat# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
3: eth0@if22: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default
link/ether ce:67:02:aa:10:c2 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.244.1.15/24 brd 10.244.1.255 scope global eth0
valid_lft forever preferred_lft forever
root@my-podz:/usr/local/tomcat# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 14 01:31 ? 00:00:24 /usr/local/openjdk-8/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging
root 40 0 0 01:33 pts/0 00:00:00 bash
root 46 40 0 01:34 pts/0 00:00:00 ps -ef
# 进入 nginx-demoz 查看 IP 地址
[root@k8s-master01 ~]# kubectl exec -it my-podz -c nginx-demoz bash
root@my-podz:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
3: eth0@if22: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default
link/ether ce:67:02:aa:10:c2 brd ff:ff:ff:ff:ff:ff
inet 10.244.1.15/24 brd 10.244.1.255 scope global eth0
valid_lft forever preferred_lft forever
root@my-podz:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 01:31 ? 00:00:00 nginx: master process nginx -g daemon off;
nginx 6 1 0 01:31 ? 00:00:00 nginx: worker process
root 7 0 0 01:34 pts/0 00:00:00 bash
root 13 7 0 01:34 pts/0 00:00:00 ps -ef
共享存储
为了持久化保存容器的数据,可以使用 Kubernetes Volume
Volume 存储卷 是 Pod 中能够被多个容器访问的共享目录,Kubernetes 的 Volume 概念,用途和目的与 Docker 的 Volume 类似,但两者不能等价:
- 首先,Kubernetes 中的 Volume 定义在 Pod 上,被一个 Pod 里的多个容器挂在到具体的文件目录下;
- 其次,Kubernetes 中的 Volume 与 Pod 生命周期相同,但与 Docker 容器生命周期不相关,容器终止或者重启时,Volume 中的数据也不会丢失;
- 最后,Kubernetes 中的 Volume 支持多种 backend 类型,包括 emptyDir、hostPath、GCE Persistent Disk、AWS Elastic Block Store、NFS、Ceph 等。
[root@k8s-master01 ~]# vim mypodzz.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-podzz
namespace: default
spec:
containers:
- name: producer
image: centos:7
command: ["bash","-c","for i in {1..100};do echo $i >> /producer_dir/hello;sleep 1;done"]
volumeMounts:
- name: shared-volume
mountPath: /producer_dir
- name: consumer
image: centos:7
command: ["bash","-c","tail -f /consumer_dir/hello"]
volumeMounts:
- name: shared-volume
mountPath: /consumer_dir
volumes:
- name: shared-volume
emptyDir: {}
# 创建 Pod 资源
[root@k8s-master01 ~]# kubectl apply -f podzz.yaml
# 检查 Pod 资源状态
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-podzz 2/2 Running 0 20s
# 查看 comsumer 容器内挂载目录
[root@k8s-master01 ~]# kubectl exec -it my-podzz -c consumer bash
[root@my-podzz /]# cat /consumer_dir/hello
1
2
3
...
# 查看 producer 容器内挂载目录
[root@k8s-master01 ~]# kubectl exec -it my-podzz -c producer bash
[root@my-podzz /]# cat /producer_dir/hello
1
2
3
...
# 查看 Pod 资源详细信息,可以看到被分配到 k8s-node2 节点
[root@k8s-master01 ~]# kubectl describe pod my-podzz | tail -4
Name: my-podzz
Namespace: default
Priority: 0
Node: k8s-node2/172.16.1.54
# 到 Node 节点上观察 Docker 容器
[root@k8s-node02 data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
633ac7b61c12 7e6257c9f8d8 "bash -c 'for i in {…" About a minute ago Up About a minute k8s_producer_my-podzz_default_e1a6acc0-4903-4599-a922-4c1beb6a7b56_4
36f4c5957b51 7e6257c9f8d8 "bash -c 'tail -f /c…" 9 minutes ago Up 9 minutes k8s_consumer_my-podzz_default_e1a6acc0-4903-4599-a922-4c1beb6a7b56_0
# 观察挂载目录
[root@k8s-node02 data]# docker inspect 633ac7b61c12 | grep -i source
[root@k8s-node02 data]# docker inspect 36f4c5957b51 | grep -i source