[Kubernetes series learning] How to execute commands that are not in the pod?

background

I want to execute a command (eg. ip a) in the pod, but there is no ip command in the pod, what should I do?

Implementation process

First check the pod information
insert image description here

Enter the pod, execute ip a, and find that there is no ip command in the container,
insert image description here
but what should we do if we want to execute this command?

Advance into the pod, execute lsns -t net, and get the ns where the container is located
insert image description here

Then go to the host where the container is located, execute lsns ns id, you can get the pid of the container

insert image description here

Enter the ns directly on the host, so that you can execute the command you want to execute in the ns of the container on the host

nsenter -n -t 47108 ip addr

insert image description here

It can also be executed like this

insert image description here

You're done~

lsns [参数]

常用参数:
-J	使用 JSON 输出格式
-l	使用列表格式的输出
-n	不打印标题
-r	使用原生输出格式
-u	不截断列中的文本
-t	名字空间类型(mnt, net, ipc, user, pid, uts, cgroup)

Guess you like

Origin blog.csdn.net/weixin_42072280/article/details/127911632