Kubernetes | 工作负载之Pods
Kubernetes 集群中的最小部署和管理的基本单元,Pod由一个或多个容器组成,同一个Pod内的所有容器共享网络和存储。
1.Kubernetes为何要引入Pod?
- 容器之间需要紧密工作,例如应用日志采集,业务容器提供服务,另一个容器协助采集日志,彼此紧密联系且对业务无侵入。
- Pod内的所有容器使用同一个网络,即所有容器拥有相同的IP地址。容器之间可以直接用
localhost通信。且所有容器可以共享存储,Pod挂载Volume其实是挂载到了每一个容器中,所有容器均可以使用同一个Volume。 - Kubernetes不直接管理容器,而是通过Pod,Pod管理支持CRI接口的容器,这意味着可以支持多种容器运行时,不局限于Docker这一种,其他只要支持CRI接口的均可以,体现了Kubernetes的可扩展性。
这里描述的内容可能不太准确,这是靠自己结合网上内容总结到的,但是也足够了。
2.多个容器如何实现网络共享?
多个容器竟然能够实现网络共享,看起来很屌的样子,出于好奇经过探索和翻阅广大网友的文章,了解到多个容器之间是通过 pause 容器来实现网络共享,通过实践来看下是否如此。
分别启动 pause nginx ghost 3个容器,看下它们的IP地址、进程、端口、主机名。
pause 容器要优先启动,后面2个容器关联 pause 容器的 net ipc pid 命名空间。
[root@k8s-master-1 ~]# docker run -d --name pause --ipc=shareable registry.aliyuncs.com/google_containers/pause
cbbfefd957572c41485758cf757188c18f0a846f813bbd0ac932c37c8c0a4b24
[root@k8s-master-1 ~]# docker run -d --name nginx --net=container:pause --ipc=container:pause --pid=container:pause nginx
ba542226e161e53756440accd07c1a15a983424e53db2e6b8d7728f6c119babc
[root@k8s-master-1 ~]# docker run -d --name ghost --net=container:pause --ipc=container:pause --pid=container:pause ghost
cd72af68565ef346d386ca9d6e147bccd8c7406cbc107c2675b92f583ab51b34
[root@k8s-master-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd72af68565e ghost "docker-entrypoint.s…" 4 seconds ago Up 4 seconds ghost
ba542226e161 nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds nginx
cbbfefd95757 registry.aliyuncs.com/google_containers/pause "/pause" 16 seconds ago Up 15 seconds pause
[root@k8s-master-1 ~]#
docker exec -it nginx /bin/bash 进入到 nginx 容器内查看进程、IP地址、端口、主机名。
nginx容器默认未安装psipss等命令,通过apt-get来安装。
[root@k8s-master-1 ~]# docker exec -it nginx /bin/bash
root@cbbfefd95757:/# apt-get update
Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:2 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB]
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [39.4 kB]
Get:4 http://security.debian.org/debian-security bullseye-security/main amd64 Packages [153 kB]
Get:5 http://deb.debian.org/debian bullseye/main amd64 Packages [8182 kB]
Get:6 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [2592 B]
Fetched 8537 kB in 2s (3469 kB/s)
Reading package lists... Done
root@cbbfefd95757:/# apt-get install -y procps iproute2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libatm1 libbpf0 libcap2 libcap2-bin libelf1 libgpm2 libmnl0 libncurses6 libncursesw6 libpam-cap libprocps8 libxtables12 psmisc
Suggested packages:
iproute2-doc gpm
The following NEW packages will be installed:
iproute2 libatm1 libbpf0 libcap2 libcap2-bin libelf1 libgpm2 libmnl0 libncurses6 libncursesw6 libpam-cap libprocps8 libxtables12 procps psmisc
0 upgraded, 15 newly installed, 0 to remove and 0 not upgraded.
Need to get 2429 kB of archives.
After this operation, 8160 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bullseye/main amd64 libelf1 amd64 0.183-1 [165 kB]
Get:2 http://deb.debian.org/debian bullseye/main amd64 libbpf0 amd64 1:0.3-2 [98.3 kB]
Get:3 http://deb.debian.org/debian bullseye/main amd64 libcap2 amd64 1:2.44-1 [23.6 kB]
Get:4 http://deb.debian.org/debian bullseye/main amd64 libmnl0 amd64 1.0.4-3 [12.5 kB]
Get:5 http://deb.debian.org/debian bullseye/main amd64 libxtables12 amd64 1.8.7-1 [45.1 kB]
Get:6 http://deb.debian.org/debian bullseye/main amd64 libcap2-bin amd64 1:2.44-1 [32.6 kB]
Get:7 http://deb.debian.org/debian bullseye/main amd64 iproute2 amd64 5.10.0-4 [930 kB]
Get:8 http://deb.debian.org/debian bullseye/main amd64 libncurses6 amd64 6.2+20201114-2 [102 kB]
Get:9 http://deb.debian.org/debian bullseye/main amd64 libncursesw6 amd64 6.2+20201114-2 [132 kB]
Get:10 http://deb.debian.org/debian bullseye/main amd64 libprocps8 amd64 2:3.3.17-5 [63.9 kB]
Get:11 http://deb.debian.org/debian bullseye/main amd64 procps amd64 2:3.3.17-5 [502 kB]
Get:12 http://deb.debian.org/debian bullseye/main amd64 libatm1 amd64 1:2.5.1-4 [71.3 kB]
Get:13 http://deb.debian.org/debian bullseye/main amd64 libgpm2 amd64 1.20.7-8 [35.6 kB]
Get:14 http://deb.debian.org/debian bullseye/main amd64 libpam-cap amd64 1:2.44-1 [15.4 kB]
Get:15 http://deb.debian.org/debian bullseye/main amd64 psmisc amd64 23.4-2 [198 kB]
Fetched 2429 kB in 2s (1374 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libelf1:amd64.
(Reading database ... 7824 files and directories currently installed.)
Preparing to unpack .../00-libelf1_0.183-1_amd64.deb ...
Unpacking libelf1:amd64 (0.183-1) ...
Selecting previously unselected package libbpf0:amd64.
Preparing to unpack .../01-libbpf0_1%3a0.3-2_amd64.deb ...
Unpacking libbpf0:amd64 (1:0.3-2) ...
Selecting previously unselected package libcap2:amd64.
Preparing to unpack .../02-libcap2_1%3a2.44-1_amd64.deb ...
Unpacking libcap2:amd64 (1:2.44-1) ...
Selecting previously unselected package libmnl0:amd64.
Preparing to unpack .../03-libmnl0_1.0.4-3_amd64.deb ...
Unpacking libmnl0:amd64 (1.0.4-3) ...
Selecting previously unselected package libxtables12:amd64.
Preparing to unpack .../04-libxtables12_1.8.7-1_amd64.deb ...
Unpacking libxtables12:amd64 (1.8.7-1) ...
Selecting previously unselected package libcap2-bin.
Preparing to unpack .../05-libcap2-bin_1%3a2.44-1_amd64.deb ...
Unpacking libcap2-bin (1:2.44-1) ...
Selecting previously unselected package iproute2.
Preparing to unpack .../06-iproute2_5.10.0-4_amd64.deb ...
Unpacking iproute2 (5.10.0-4) ...
Selecting previously unselected package libncurses6:amd64.
Preparing to unpack .../07-libncurses6_6.2+20201114-2_amd64.deb ...
Unpacking libncurses6:amd64 (6.2+20201114-2) ...
Selecting previously unselected package libncursesw6:amd64.
Preparing to unpack .../08-libncursesw6_6.2+20201114-2_amd64.deb ...
Unpacking libncursesw6:amd64 (6.2+20201114-2) ...
Selecting previously unselected package libprocps8:amd64.
Preparing to unpack .../09-libprocps8_2%3a3.3.17-5_amd64.deb ...
Unpacking libprocps8:amd64 (2:3.3.17-5) ...
Selecting previously unselected package procps.
Preparing to unpack .../10-procps_2%3a3.3.17-5_amd64.deb ...
Unpacking procps (2:3.3.17-5) ...
Selecting previously unselected package libatm1:amd64.
Preparing to unpack .../11-libatm1_1%3a2.5.1-4_amd64.deb ...
Unpacking libatm1:amd64 (1:2.5.1-4) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../12-libgpm2_1.20.7-8_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.7-8) ...
Selecting previously unselected package libpam-cap:amd64.
Preparing to unpack .../13-libpam-cap_1%3a2.44-1_amd64.deb ...
Unpacking libpam-cap:amd64 (1:2.44-1) ...
Selecting previously unselected package psmisc.
Preparing to unpack .../14-psmisc_23.4-2_amd64.deb ...
Unpacking psmisc (23.4-2) ...
Setting up libgpm2:amd64 (1.20.7-8) ...
Setting up psmisc (23.4-2) ...
Setting up libatm1:amd64 (1:2.5.1-4) ...
Setting up libcap2:amd64 (1:2.44-1) ...
Setting up libcap2-bin (1:2.44-1) ...
Setting up libncurses6:amd64 (6.2+20201114-2) ...
Setting up libmnl0:amd64 (1.0.4-3) ...
Setting up libncursesw6:amd64 (6.2+20201114-2) ...
Setting up libxtables12:amd64 (1.8.7-1) ...
Setting up libelf1:amd64 (0.183-1) ...
Setting up libpam-cap:amd64 (1:2.44-1) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.32.1 /usr/local/share/perl/5.32.1 /usr/lib/x86_64-linux-gnu/perl5/5.32 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.32 /usr/share/perl/5.32 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Setting up libprocps8:amd64 (2:3.3.17-5) ...
Setting up procps (2:3.3.17-5) ...
Setting up libbpf0:amd64 (1:0.3-2) ...
Setting up iproute2 (5.10.0-4) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.32.1 /usr/local/share/perl/5.32.1 /usr/lib/x86_64-linux-gnu/perl5/5.32 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.32 /usr/share/perl/5.32 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Processing triggers for libc-bin (2.31-13+deb11u3) ...
root@cbbfefd95757:/#
ps -ef 查看 nginx 容器的进程状况。发现除了 nginx 还有其他的进程(node current/index.js),正常情况下 nginx 容器是只有 nginx 进程的。
这说明 nginx 容器环境内有 ghost 的进程,后面进入 ghost 容器再看下是不是也会有 nginx 的进程。
root@cbbfefd95757:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:25 ? 00:00:00 /pause
root 10 0 0 10:25 ? 00:00:00 nginx: master process nginx -g daemon off;
nginx 40 10 0 10:25 ? 00:00:00 nginx: worker process
1000 41 0 2 10:25 ? 00:00:03 node current/index.js
root 128 0 0 10:26 pts/0 00:00:00 /bin/bash
root 582 128 0 10:28 pts/0 00:00:00 ps -ef
root@cbbfefd95757:/#
ip addr show 查看 nginx 容器的IP地址,有2个接口:lo 和 eth0,IP地址是 172.17.0.2。
root@cbbfefd95757:/# ip addr show
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
22: eth0@if23: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
root@cbbfefd95757:/#
ss -lnpt 查看 nginx 容器的端口监听情况。
端口有 80 和 2368,nginx 容器默认只监听 80 端口,2368 是 ghost 容器监听的。
root@cbbfefd95757:/# ss -lnpt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=10,fd=7))
LISTEN 0 128 [::]:80 [::]:* users:(("nginx",pid=10,fd=8))
LISTEN 0 128 *:2368 *:*
root@cbbfefd95757:/#
hostname 查看 nginx 容器的主机名是 cbbfefd95757。
root@cbbfefd95757:/# hostname
cbbfefd95757
root@cbbfefd95757:/#
以上均是在 nginx 容器查看到的,下面进入到 ghost 容器看下进程、IP地址、端口、主机名是否相同。
docker exec -it ghost /bin/bash 进入到 ghost 容器内,ghost 容器默认也是未安装 ps ip ss 等命令,通过 apt-get 来安装。
[root@k8s-master-1 ~]# docker exec -it ghost /bin/bash
root@cbbfefd95757:/var/lib/ghost# ps
bash: ps: command not found
root@cbbfefd95757:/var/lib/ghost# ip
bash: ip: command not found
root@cbbfefd95757:/var/lib/ghost# ss
bash: ss: command not found
root@cbbfefd95757:/var/lib/ghost# apt-get update
Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:2 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB]
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [39.4 kB]
Get:4 http://security.debian.org/debian-security bullseye-security/main amd64 Packages [153 kB]
Get:5 http://deb.debian.org/debian bullseye/main amd64 Packages [8182 kB]
Get:6 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [2592 B]
Fetched 8537 kB in 2s (3741 kB/s)
Reading package lists... Done
root@cbbfefd95757:/var/lib/ghost# apt-get install -y procps iproute2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libatm1 libbpf0 libbsd0 libcap2 libcap2-bin libelf1 libgpm2 libmd0 libmnl0 libncurses6 libncursesw6 libpam-cap libprocps8 libxtables12 psmisc
Suggested packages:
iproute2-doc gpm
The following NEW packages will be installed:
iproute2 libatm1 libbpf0 libbsd0 libcap2 libcap2-bin libelf1 libgpm2 libmd0 libmnl0 libncurses6 libncursesw6 libpam-cap libprocps8 libxtables12
procps psmisc
0 upgraded, 17 newly installed, 0 to remove and 0 not upgraded.
Need to get 2564 kB of archives.
After this operation, 8435 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bullseye/main amd64 libelf1 amd64 0.183-1 [165 kB]
Get:2 http://deb.debian.org/debian bullseye/main amd64 libbpf0 amd64 1:0.3-2 [98.3 kB]
Get:3 http://deb.debian.org/debian bullseye/main amd64 libmd0 amd64 1.0.3-3 [28.0 kB]
Get:4 http://deb.debian.org/debian bullseye/main amd64 libbsd0 amd64 0.11.3-1 [108 kB]
Get:5 http://deb.debian.org/debian bullseye/main amd64 libcap2 amd64 1:2.44-1 [23.6 kB]
Get:6 http://deb.debian.org/debian bullseye/main amd64 libmnl0 amd64 1.0.4-3 [12.5 kB]
Get:7 http://deb.debian.org/debian bullseye/main amd64 libxtables12 amd64 1.8.7-1 [45.1 kB]
Get:8 http://deb.debian.org/debian bullseye/main amd64 libcap2-bin amd64 1:2.44-1 [32.6 kB]
Get:9 http://deb.debian.org/debian bullseye/main amd64 iproute2 amd64 5.10.0-4 [930 kB]
Get:10 http://deb.debian.org/debian bullseye/main amd64 libncurses6 amd64 6.2+20201114-2 [102 kB]
Get:11 http://deb.debian.org/debian bullseye/main amd64 libncursesw6 amd64 6.2+20201114-2 [132 kB]
Get:12 http://deb.debian.org/debian bullseye/main amd64 libprocps8 amd64 2:3.3.17-5 [63.9 kB]
Get:13 http://deb.debian.org/debian bullseye/main amd64 procps amd64 2:3.3.17-5 [502 kB]
Get:14 http://deb.debian.org/debian bullseye/main amd64 libatm1 amd64 1:2.5.1-4 [71.3 kB]
Get:15 http://deb.debian.org/debian bullseye/main amd64 libgpm2 amd64 1.20.7-8 [35.6 kB]
Get:16 http://deb.debian.org/debian bullseye/main amd64 libpam-cap amd64 1:2.44-1 [15.4 kB]
Get:17 http://deb.debian.org/debian bullseye/main amd64 psmisc amd64 23.4-2 [198 kB]
Fetched 2564 kB in 1s (2777 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libelf1:amd64.
(Reading database ... 6662 files and directories currently installed.)
Preparing to unpack .../00-libelf1_0.183-1_amd64.deb ...
Unpacking libelf1:amd64 (0.183-1) ...
Selecting previously unselected package libbpf0:amd64.
Preparing to unpack .../01-libbpf0_1%3a0.3-2_amd64.deb ...
Unpacking libbpf0:amd64 (1:0.3-2) ...
Selecting previously unselected package libmd0:amd64.
Preparing to unpack .../02-libmd0_1.0.3-3_amd64.deb ...
Unpacking libmd0:amd64 (1.0.3-3) ...
Selecting previously unselected package libbsd0:amd64.
Preparing to unpack .../03-libbsd0_0.11.3-1_amd64.deb ...
Unpacking libbsd0:amd64 (0.11.3-1) ...
Selecting previously unselected package libcap2:amd64.
Preparing to unpack .../04-libcap2_1%3a2.44-1_amd64.deb ...
Unpacking libcap2:amd64 (1:2.44-1) ...
Selecting previously unselected package libmnl0:amd64.
Preparing to unpack .../05-libmnl0_1.0.4-3_amd64.deb ...
Unpacking libmnl0:amd64 (1.0.4-3) ...
Selecting previously unselected package libxtables12:amd64.
Preparing to unpack .../06-libxtables12_1.8.7-1_amd64.deb ...
Unpacking libxtables12:amd64 (1.8.7-1) ...
Selecting previously unselected package libcap2-bin.
Preparing to unpack .../07-libcap2-bin_1%3a2.44-1_amd64.deb ...
Unpacking libcap2-bin (1:2.44-1) ...
Selecting previously unselected package iproute2.
Preparing to unpack .../08-iproute2_5.10.0-4_amd64.deb ...
Unpacking iproute2 (5.10.0-4) ...
Selecting previously unselected package libncurses6:amd64.
Preparing to unpack .../09-libncurses6_6.2+20201114-2_amd64.deb ...
Unpacking libncurses6:amd64 (6.2+20201114-2) ...
Selecting previously unselected package libncursesw6:amd64.
Preparing to unpack .../10-libncursesw6_6.2+20201114-2_amd64.deb ...
Unpacking libncursesw6:amd64 (6.2+20201114-2) ...
Selecting previously unselected package libprocps8:amd64.
Preparing to unpack .../11-libprocps8_2%3a3.3.17-5_amd64.deb ...
Unpacking libprocps8:amd64 (2:3.3.17-5) ...
Selecting previously unselected package procps.
Preparing to unpack .../12-procps_2%3a3.3.17-5_amd64.deb ...
Unpacking procps (2:3.3.17-5) ...
Selecting previously unselected package libatm1:amd64.
Preparing to unpack .../13-libatm1_1%3a2.5.1-4_amd64.deb ...
Unpacking libatm1:amd64 (1:2.5.1-4) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../14-libgpm2_1.20.7-8_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.7-8) ...
Selecting previously unselected package libpam-cap:amd64.
Preparing to unpack .../15-libpam-cap_1%3a2.44-1_amd64.deb ...
Unpacking libpam-cap:amd64 (1:2.44-1) ...
Selecting previously unselected package psmisc.
Preparing to unpack .../16-psmisc_23.4-2_amd64.deb ...
Unpacking psmisc (23.4-2) ...
Setting up libgpm2:amd64 (1.20.7-8) ...
Setting up psmisc (23.4-2) ...
Setting up libatm1:amd64 (1:2.5.1-4) ...
Setting up libcap2:amd64 (1:2.44-1) ...
Setting up libcap2-bin (1:2.44-1) ...
Setting up libncurses6:amd64 (6.2+20201114-2) ...
Setting up libmnl0:amd64 (1.0.4-3) ...
Setting up libncursesw6:amd64 (6.2+20201114-2) ...
Setting up libxtables12:amd64 (1.8.7-1) ...
Setting up libmd0:amd64 (1.0.3-3) ...
Setting up libbsd0:amd64 (0.11.3-1) ...
Setting up libelf1:amd64 (0.183-1) ...
Setting up libpam-cap:amd64 (1:2.44-1) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.32.1 /usr/local/share/perl/5.32.1 /usr/lib/x86_64-linux-gnu/perl5/5.32 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.32 /usr/share/perl/5.32 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Setting up libprocps8:amd64 (2:3.3.17-5) ...
Setting up procps (2:3.3.17-5) ...
Setting up libbpf0:amd64 (1:0.3-2) ...
Setting up iproute2 (5.10.0-4) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.32.1 /usr/local/share/perl/5.32.1 /usr/lib/x86_64-linux-gnu/perl5/5.32 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.32 /usr/share/perl/5.32 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Processing triggers for libc-bin (2.31-13+deb11u3) ...
root@cbbfefd95757:/var/lib/ghost#
ps -ef 查看 ghost 容器的进程状况。跟 nginx 容器的进程是一样,也有 nginx 的进程。
说明 nginx 和 ghost 2个容器使用了同一个进程空间。
小实验:在
nginx容器内执行while true; do sleep 3600;done,然后在ghost容器试下能不能看到对应的进程。
root@cbbfefd95757:/var/lib/ghost# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:25 ? 00:00:00 /pause
root 10 0 0 10:25 ? 00:00:00 nginx: master process nginx -g daemon off;
101 40 10 0 10:25 ? 00:00:00 nginx: worker process
node 41 0 0 10:25 ? 00:00:04 node current/index.js
root 586 0 0 10:41 pts/0 00:00:00 /bin/bash
root 1049 586 0 10:43 pts/0 00:00:00 ps -ef
root@cbbfefd95757:/var/lib/ghost#
ip addr show 查看 ghost 容器的IP地址,跟 nginx 容器一样,IP地址也是 172.17.0.2。
这也说明2个容器使用了同一个IP地址。
root@cbbfefd95757:/var/lib/ghost# ip addr show
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
22: eth0@if23: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
root@cbbfefd95757:/var/lib/ghost#
ss -lnpt 查看 ghost 容器的端口监听情况。端口也同样有 80 和 2368。
root@cbbfefd95757:/var/lib/ghost# ss -lnpt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=10,fd=7))
LISTEN 0 128 [::]:80 [::]:* users:(("nginx",pid=10,fd=8))
LISTEN 0 128 *:2368 *:*
root@cbbfefd95757:/var/lib/ghost#
hostname 查看 ghost 容器的主机名是 cbbfefd95757,跟 nginx 容器一样。
root@cbbfefd95757:/var/lib/ghost# hostname
cbbfefd95757
root@cbbfefd95757:/var/lib/ghost#
既然2个容器的端口是共享的,那是不是可以通过 localhost 来访问呢?
在 nginx 容器使用 curl 命令访问一下 80 和 2368,看看是否能够正常访问。
root@cbbfefd95757:/# curl -vo /dev/null localhost:80
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 127.0.0.1:80...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.21.6
< Date: Wed, 08 Jun 2022 13:51:11 GMT
< Content-Type: text/html
< Content-Length: 615
< Last-Modified: Tue, 25 Jan 2022 15:03:52 GMT
< Connection: keep-alive
< ETag: "61f01158-267"
< Accept-Ranges: bytes
<
{ [615 bytes data]
100 615 100 615 0 0 600k 0 --:--:-- --:--:-- --:--:-- 600k
* Connection #0 to host localhost left intact
root@cbbfefd95757:/# curl -vo /dev/null localhost:2368
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 127.0.0.1:2368...
* Connected to localhost (127.0.0.1) port 2368 (#0)
> GET / HTTP/1.1
> Host: localhost:2368
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Cache-Control: public, max-age=0
< Content-Type: text/html; charset=utf-8
< Content-Length: 10353
< ETag: W/"2871-AADdbDxeW6501NBD2t+bBUA15Ko"
< Vary: Accept-Encoding
< Date: Wed, 08 Jun 2022 13:51:13 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
<
{ [10353 bytes data]
100 10353 100 10353 0 0 459k 0 --:--:-- --:--:-- --:--:-- 459k
* Connection #0 to host localhost left intact
root@cbbfefd95757:/#
通过上面的实践可以看出来,nginx 和 ghost 容器的进程、IP地址、端口、主机名都是一样的,当然了是依赖 pause 容器实现的。
3.哪些场景可以将多个容器放到一个Pod内?
- 应用日志采集场景,业务容器提供服务,另一个容器协助采集日志,彼此紧密联系且对业务无侵入。
- 应用限流场景,业务容器提供服务,其中一个容器提供反向代理,外部流量先经过反向代理做处理后再转发到业务容器。
- 其他。