@Pod资源共享机制

Kubernetes直接管理Pod,而不是容器
Pod不是进程,而是容器运行的环境
Pod是Kubernetes创建和管理的最小单元,不同Pod可以在不同Node上
Pod中可以有一个或多个容器[1],且始终部署在一个Node上,这些容器共享存储、网络

说明[1]:
Pod中运行单个容器 Pod可以看作是单个容器的抽象封装
Pod中运行多个容器 需要共享资源或紧密耦合的应用程序[2],Pod 将这些容器和存储资源打包为一个可管理的实体。

说明[2]
应用程序之间发生文件、数据交互
应用程序之间需要通过本地通信(127.0.0.1、socket)
应用程序之间频繁调用

官方文档传送门 
Pods -> https://kubernetes.io/zh/docs/concepts/workloads/pods/
Init容器 -> https://kubernetes.io/zh/docs/concepts/workloads/pods/init-containers
 

在每个Node上执行docker ps 会发现有不少pause

pause容器/infra (infrastucture container)容器和业务容器一一对应,业务容器共享pause容器的网络栈和Volume挂载卷。

[root@k8s-master ~]# docker ps | grep dash
64cea4b64eb1   86262685d9ab                                        "/metrics-sidecar"       3 hours ago   Up 3 hours             k8s_dashboard-metrics-scraper_dashboard-metrics-scraper-7b59f7d4df-xqb96_kubernetes-dashboard_7435c1e5-359b-4350-8fa7-44facdc33e63_8
fc7a1d65ad43   registry.aliyuncs.com/google_containers/pause:3.2   "/pause"                 3 hours ago   Up 3 hours             k8s_POD_dashboard-metrics-scraper-7b59f7d4df-xqb96_kubernetes-dashboard_7435c1e5-359b-4350-8fa7-44facdc33e63_8
[root@k8s-master ~]#
[root@k8s-master ~]# docker ps | grep etcd
4f0eadebcb08   0369cf4303ff                                        "etcd --advertise-cl…"   3 hours ago   Up 3 hours             k8s_etcd_etcd-k8s-master_kube-system_729d533a85352265aebac662f2565332_12
e5d19e95ec85   registry.aliyuncs.com/google_containers/pause:3.2   "/pause"                 3 hours ago   Up 3 hours             k8s_POD_etcd-k8s-master_kube-system_729d533a85352265aebac662f2565332_12
[root@k8s-master ~]#

[root@k8s-node1 ~]# docker ps | grep web
a049aebee4bb   nginx                                               "/docker-entrypoint.…"   25 minutes ago   Up 25 minutes             k8s_nginx_web-6f7dfb48c4-gmr8d_default_8ed106ee-b02e-4f26-a7c4-c4f014e4c192_0
0330feccf8b4   registry.aliyuncs.com/google_containers/pause:3.2   "/pause"                 26 minutes ago   Up 26 minutes             k8s_POD_web-6f7dfb48c4-gmr8d_default_8ed106ee-b02e-4f26-a7c4-c4f014e4c192_0
[root@k8s-node1 ~]#

@Pod资源共享举例(网络)

创建一个包含两个容器的Pod

注意:单独创建Pod可以使用命令例如 kubectl run nginx --image=nginx 或 yaml文件,这里举例使用yaml文件。一般不单独创建Pod,而是通过控制器创建。

#清理deployment
[root@k8s-master ~]# kubectl delete deploy --all
deployment.apps "web" deleted
[root@k8s-master ~]#

#编辑Pod的yaml文件
[root@k8s-master ~]# cat test-netshare.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: test
  name: test-netshare
  namespace: default
spec:
  containers:
    - image: busybox
      name: hello
      command: ["/bin/sh","-c",'echo "Hello, Kubernetes!" && sleep 3600']
    - image: nginx
      name: web
[root@k8s-master ~]#

#创建Pod
[root@k8s-master ~]# kubectl apply -f test-netshare.yaml
pod/test-netshare created
[root@k8s-master ~]#

kubectl get pod -o wide可以看到Pod的IP

kubectl describe pod <pod name>,可以看到test-netshare这个Pod里有两个容器,hello和web

[root@k8s-master ~]# kubectl get pod -o wide
NAME            READY   STATUS    RESTARTS   AGE     IP              NODE        NOMINATED NODE   READINESS GATES
test-netshare   2/2     Running   0          2m49s   10.244.36.107   k8s-node1   <none>           <none>
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl describe pod test-netshare
Name:         test-netshare
……
IPs:
  IP:  10.244.36.107
Containers:
  hello:
    Container ID:  docker://73b6704f226233c1776b1fc4ab66a1e2b6326b108f77e5dc1d57b338b68193d8
    Image:         busybox
……
  web:
    Container ID:   docker://459f0e2bf7bc33f66dfc523ec3210229783c86ddb27fde309224b481a3219502
    Image:          nginx
……

首先进入容器hello看看,注意ipaddr输出的结果,和上述分配给Pod的IP一致

[root@k8s-master ~]# kubectl exec -it test-netshare -c hello -- sh
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop qlen 1000
    link/ipip 0.0.0.0 brd 0.0.0.0
4: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1440 qdisc noqueue
    link/ether 96:02:7f:2d:9c:f0 brd ff:ff:ff:ff:ff:ff
    inet 10.244.36.107/32 scope global eth0
       valid_lft forever preferred_lft forever
/ #

由于另一个容器web用的nginx镜像,这里wget一把,能够下载到nginx的主页

[root@k8s-master ~]# kubectl exec -it test-netshare -c hello -- sh
/ # curl 127.0.0.1:80
sh: curl: not found
/ # wget 127.0.0.1:80
Connecting to 127.0.0.1:80 (127.0.0.1:80)
saving to 'index.html'
index.html           100% |*********************************************************************************************************|   612  0:00:00 ETA
'index.html' saved
/ # cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
……
</html>
/ #

说明:web容器不支持查看网络的命令

[root@k8s-master ~]# kubectl exec -it test-netshare -c web -- sh
#
# ip addr
sh: 2: ip: not found
# ifconfig
sh: 3: ifconfig: not found
# cd /etc/sysconfig
sh: 4: cd: can't cd to /etc/sysconfig

@Pod资源共享举例(存储)

创建一个包含两个容器的Pod

[root@k8s-master ~]# cat test-volshare.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: test
  name: test-volshare
  namespace: default
spec:
  containers:
    - image: busybox
      name: hello
      command: ["/bin/sh","-c",'echo "Hello, Kubernetes!" && sleep 3600']
      volumeMounts:
        - name: log
          mountPath: /var/data
    - image: nginx
      name: web
      volumeMounts:
      - name: log
        mountPath: /usr/share/data
  volumes:
    - name: log
      emptyDir: {}
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f test-volshare.yaml
pod/test-volshare created
[root@k8s-master ~]#

先进入容器hello,在其数据卷挂载的路径下创建一个文件

[root@k8s-master ~]# kubectl get pod
NAME            READY   STATUS    RESTARTS   AGE
test-volshare   2/2     Running   0          67s
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl exec -it test-volshare -c hello -- sh
/ # cd /var/data
/var/data # ls
/var/data # touch createbyhello
/var/data # echo 'hello' >> createbyhello
/var/data # exit
[root@k8s-master ~]#

然后进入容器web,在其数据卷挂载的路径下可以看到刚才在hello容器中创建的文件

[root@k8s-master ~]# kubectl exec -it test-volshare -c web -- bash
root@test-volshare:/# cd /usr/share/data/
root@test-volshare:/usr/share/data# ls
createbyhello
root@test-volshare:/usr/share/data# cat createbyhello
hello
root@test-volshare:/usr/share/data#

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐