设为首页 收藏本站
查看: 1865|回复: 1

[资源发布] 容器内抓包小技巧

[复制链接]
累计签到:224 天
连续签到:4 天
发表于 2019-11-26 11:14:22 | 显示全部楼层 |阅读模式
当需要在容器中抓网络包时,如果容器镜像中没有tcpdump命令,抓包就会变得很困难。本文提供一种方法可以方便的抓网络包。想怎么抓就怎么抓。让容器内抓包变得so easy!

原理就是进入容器/Pod的网络命名空间(netns),然后利用宿主机上的tcpdump进行抓包。

1. 首先查到pod所在的节点。
Kubectl get pod -owide

2. 登录到pod所在的节点,将如下的函数贴到当前终端:
function e() {

   set -eu

   ns=${2-"default"}

   pod=`kubectl -n $ns describe pod $1 | grep -Eo 'docker://.*$' | head -n 1 | sed 's/docker:\/\/\(.*\)$/\1/'`

   pid=`docker inspect -f {{.State.Pid}} $pod`

   echo "enter pod netns successfully for $ns/$1"

  nsenter -n -u --target $pid

}

3. 输入 e POD_NAME NAMESPACE 即可进入此pod的netns,此时即可便使用宿主机上的各种命令了。
首先执行ifconfig 或 ip a 命令查看当前pod的ip。
[root@SZD-L0119857 ~]# e web-79d98584c4-7st4t test
enter pod netns successfully for test/web-79d98584c4-7st4t
**************************************************************************
*                                                                        *
*Attention: Auditing process will report your every action!              *
*Warning: Don't delete any files in directory /root/slogs!!              *
*                                                                        *
*                          -PING AN INSURANCE (GRP) COMPANY OF CHINA,LTD.*
**************************************************************************
Script started, file is /root/slogs/2019-08-16_15:45:51pts-1root.log
[root@web-79d98584c4-7st4t ~]#
[root@web-79d98584c4-7st4t ~]#
[root@web-79d98584c4-7st4t ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 172.1.39.3  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 02:42:ac:01:27:03  txqueuelen 0  (Ethernet)
        RX packets 144937891  bytes 11905780495 (11.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 121090106  bytes 28579561835 (26.6 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@web-79d98584c4-7st4t ~]#

4. 使用tcpdump抓包
[root@web-79d98584c4-7st4t ~]# tcpdump -i eth0 -nn -vvv
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
15:39:38.175494 IP (tos 0x0, ttl 63, id 25519, offset 0, flags [DF], proto TCP (6), length 60)
    172.1.12.0.37908 > 172.1.39.3.80: Flags [S], cksum 0x1abd (correct), seq 3998146004, win 28200, options [mss 1410,sackOK,TS val 356826747 ecr 0,nop,wscale 6], length 0
15:39:38.175567 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 60)
    172.1.39.3.80 > 172.1.12.0.37908: Flags [S.], cksum 0x8b34 (incorrect -> 0x79df), seq 3454557644, ack 3998146005, win 27960, options [mss 1410,sackOK,TS val 3897068990 ecr 356826747,nop,wscale 6], length 0
15:39:38.175832 IP (tos 0x0, ttl 63, id 25520, offset 0, flags [DF], proto TCP (6), length 52)
    172.1.12.0.37908 > 172.1.39.3.80: Flags [.], cksum 0x13f7 (correct), seq 1, ack 1, win 441, options [nop,nop,TS val 356826748 ecr 3897068990], length 0
15:39:38.175915 IP (tos 0x0, ttl 63, id 25521, offset 0, flags [DF], proto TCP (6), length 141)
    172.1.12.0.37908 > 172.1.39.3.80: Flags [P.], cksum 0x2277 (correct), seq 1:90, ack 1, win 441, options [nop,nop,TS val 356826748 ecr 3897068990], length 89: HTTP, length: 89
        GET / HTTP/1.1
        Host: web.test.svc.cluster.local
        User-Agent: Wget
        Connection: close

15:39:38.175925 IP (tos 0x0, ttl 64, id 53363, offset 0, flags [DF], proto TCP (6), length 52)

同样可以使用telnet,traceroute等命令,只要宿主机上安装了即可使用。

上面的命令需要宿主机上kubectl命令有相应的命令空间的权限。若没有。可使用下面的方法进入pod的netns中
1. 查看pod的CONTAINER ID 或 NAME
docker ps |grep POD_NAME
2. 获取容器中进程的Pid
docker inspect -f {{.State.Pid}} CONTAINER ID
3. 进入此容器的netns
nsenter -n -u -t Pid

[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]# kubectl get pod -owide -n test |grep 30.23.9.105
web-79d98584c4-7st4t             1/1       Running       0          9d        172.1.39.3    30.23.9.105   <none>
web-79d98584c4-ctlc9             0/1       Terminating   0          16d       172.1.39.12   30.23.9.105   <none>
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]# docker ps |grep web-79d98584c4-7st4t
e95decf69c10        b8efb18f159b                    "nginx -g 'daemon ..."   9 days ago          Up 9 days                               k8s_web_web-79d98584c4-7st4t_test_9e0dde76-b826-11e9-b221-0680e60011a7_0
59fe8c6d5e19        pause-amd64:3.0                 "/pause"                 9 days ago          Up 9 days                               k8s_POD_web-79d98584c4-7st4t_test_9e0dde76-b826-11e9-b221-0680e60011a7_0
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]# docker inspect -f {{.State.Pid}} e95decf69c10
23557
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]#
[root@SZD-L0119857 ~]# nsenter -n -u -t 23557
**************************************************************************
*                                                                        *
*Attention: Auditing process will report your every action!              *
*Warning: Don't delete any files in directory /root/slogs!!              *
*                                                                        *
*                          -PING AN INSURANCE (GRP) COMPANY OF CHINA,LTD.*
**************************************************************************
Script started, file is /root/slogs/2019-08-16_15:50:09pts-1root.log
[root@web-79d98584c4-7st4t ~]#
[root@web-79d98584c4-7st4t ~]#
[root@web-79d98584c4-7st4t ~]#
[root@web-79d98584c4-7st4t ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 172.1.39.3  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 02:42:ac:01:27:03  txqueuelen 0  (Ethernet)
        RX packets 144980595  bytes 11909288233 (11.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 121125791  bytes 28587982883 (26.6 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@web-79d98584c4-7st4t ~]#


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-988940-1-1.html 上篇帖子: 一文带你读懂CNCF Landscape 下篇帖子: k8s 滚动升级及回滚
累计签到:224 天
连续签到:4 天
 楼主| 发表于 2019-11-26 11:15:28 | 显示全部楼层
6666666666

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表