• 周五. 7月 1st, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

K8S与Docker 如何通过K8S 查询docker ,通过docker 查询pod

admin

11月 28, 2021

一. 如果在K8S 上查询pod 对应的docker 那? 

pod 是K8S 最小的计量单位,一个pod 可以对应一个容器, 也可以对应多个容器,我们怎么知道我们pod 对应的docker 是那个? 

1. 通过kubectl get pods -A | grep pod-name   将所需要的pod 过滤出来

[[email protected] ~]# kubectl get pods -A | grep nginx
default nginx-6799fc88d8-5m5j5 1/1 Terminating 0 24d
default nginx-6799fc88d8-hj6kw 1/1 Running 0 30m

2. 通过查看pod详细信息确认pod 运行所在的物理机

[[email protected] ~]# kubectl get pods -n default nginx-6799fc88d8-hj6kw -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6799fc88d8-hj6kw 1/1 Running 0 35m 192.168.1.5 node1 <none> <none>

由此可见我们的nginxi-6799 的pod 是运行在我们的node1 物理节点

2. 我们查询pod 的yaml 文件,过滤出他的docker 信息。 可以获取到docker id 

[[email protected] ~]#
[[email protected] ~]# kubectl describe pod -n default nginx-6799fc88d8-hj6kw | grep docker
Container ID: docker://19e51808b28027a8a5236d22975732d954ddad2fff233c60f71e7ca25eb52eb5
Image ID: docker-pullable://[email protected]:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
[[email protected] ~]#

3. 登录到pod 所在的物理节点,检查docker 可以检查到我们2 步骤中的docker id

[[email protected] ~]# ssh node1
The authenticity of host ‘node1 (192.168.0.51)’ can’t be established.
ECDSA key fingerprint is SHA256:xy2TbO8lBhNG95ymZ+h3qG9VYkLEGrsDJftKvt5z0Gw.
ECDSA key fingerprint is MD5:dd:b7:6d:82:76:cb:60:51:b9:6e:f9:85:b3:fe:c4:cb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘node1’ (ECDSA) to the list of known hosts.
Last login: Sun Jul 25 21:42:46 2021 from 192.168.0.104
[[email protected] ~]# docker ps –all | grep 19e51808
19e51808b280 nginx “/docker-entrypoint.…” 25 minutes ago Up 25 minutes k8s_nginx_nginx-6799fc88d8-hj6kw_default_b137fcb6-df6a-47f3-b2c3-f8bae5f07619_0

通过以上方法可以查询到pod 对应的docker 

2. 通过docker  id ,或者是docker 工作目录id 来反向查找pod 信息

通过docker id 来找pod 信息

[[email protected] ~]# docker inspect -f “{{.Id}} {{.State.Pid}} {{.Config.Hostname}}” 19e51808b280
19e51808b28027a8a5236d22975732d954ddad2fff233c60f71e7ca25eb52eb5 51240 nginx-6799fc88d8-hj6kw

在master 节点上 验证通过kubectl  get pod -A | grep  nginx-6799fc88d8-hj6kw

[[email protected] ~]# kubectl get pod -A | grep nginx-6799fc88d8-hj6kw
default nginx-6799fc88d8-hj6kw 1/1 Running 0 121m

[[email protected] overlay2]# docker ps -q | xargs docker inspect –format ‘{{.State.Pid}},{{.Id}},{{.Name}},{{.GraphDriver.Data.WorkDir}}’ | grep 0b5032594a9840968bd2a5ff6ba13232f184553879e233faed8325b3af1d5e1f
51240,19e51808b28027a8a5236d22975732d954ddad2fff233c60f71e7ca25eb52eb5,/k8s_nginx_nginx-6799fc88d8-hj6kw_default_b137fcb6-df6a-47f3-b2c3-f8bae5f07619_0,/var/lib/docker/overlay2/0b5032594a9840968bd2a5ff6ba13232f184553879e233faed8325b3af1d5e1f/work

19e51808b28027a8a5236d22975732d954ddad2fff233c60f71e7ca25eb52eb5       #docker 的id 

nginx-6799fc88d8-hj6kw                           #pod name 

/var/lib/docker/overlay2/0b5032594a9840968bd2a5ff6ba13232f184553879e233faed8325b3af1d5e1f/work         #docker  的工作目录

验证可通过kubectl  get pod -A | grep  nginx-6799fc88d8-hj6kw  验证

发表评论

您的电子邮箱地址不会被公开。