使用nsenter连接容器

下载安装包:
wget https://www.kernel.org/pub/linux/utils/util-linux/v2.29/util-linux-2.29.1.tar.gz

解压、编译

[root@wuhan31-ceph01 util-linux-2.29.1]#  ./configure -without-ncurses
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/xiaoxuan/util-linux-2.29.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

安装gcc
yum install gcc -y

继续编译然后make #注意不要make install
make结束后将编译出的程序nsenter放到/usr/local/bin/
cp nsenter /usr/local/bin/

现在就可以使用nsenter命令了
例:先找出一个容器ip

[root@wuhan31-ceph01 ~]# docker ps -a 
CONTAINER ID        IMAGE                                                 COMMAND                  CREATED             STATUS              PORTS               NAMES
d141ac504ec6        kolla/centos-source-grafana:rocky                     "dumb-init --single-…"   2 months ago        Up 2 days                               grafana
41e12c24ba7f        kolla/centos-source-horizon:rocky                     "dumb-init --single-…"   2 months ago        Up 2 days                               horizon
6989a4aeb33a        kolla/centos-source-heat-engine:rocky                 "dumb-init --single-…"   2 months ago        Up 2 days                               heat_engine
35665589b4a4        kolla/centos-source-heat-api-cfn:rocky                "dumb-init --single-…"   2 months ago        Up 2 days                               heat_api_cfn
f18c98468796        kolla/centos-source-heat-api:rocky                    "dumb-init --single-…"   2 months ago        Up 2 days                               heat_api
bc77f4d3c957        kolla/centos-source-neutron-metadata-agent:rocky      "dumb-init --single-…"   2 months ago        Up 2 days                               neutron_metadata_agent
7334b93c6564        kolla/centos-source-neutron-lbaas-agent:rocky         "dumb-init --single-…"   2 months ago        Up 2 days                               neutron_lbaas_agent
0dd7a55245c4        kolla/centos-source-neutron-l3-agent:rocky            "dumb-init --single-…"   2 months ago        Up 2 days                               neutron_l3_agent
beec0f19ec7f        kolla/centos-source-neutron-dhcp-agent:rocky          "dumb-init --single-…"   2 months ago        Up 2 days                               neutron_dhcp_agent
af6841ebc21e        kolla/centos-source-neutron-linuxbridge-agent:rocky   "dumb-init --single-…"   2 months ago        Up 2 days                               neutron_linuxbridge_agent
49dc0457445d        kolla/centos-source-neutron-server:rocky              "dumb-init --single-…"   2 months ago        Up 2 days                               neutron_server
677c0be4ab6b        kolla/centos-source-nova-compute:rocky                "dumb-init --single-…"   2 months ago        Up 2 days                               nova_compute
402b1e673777        kolla/centos-source-nova-novncproxy:rocky             "dumb-init --single-…"   2 months ago        Up 2 days                               nova_novncproxy
e35729b76996        kolla/centos-source-nova-consoleauth:rocky            "dumb-init --single-…"   2 months ago        Up 2 days                               nova_consoleauth
8b193f562e47        kolla/centos-source-nova-conductor:rocky              "dumb-init --single-…"   2 months ago        Up 2 days                               nova_conductor
885581445be0        kolla/centos-source-nova-scheduler:rocky              "dumb-init --single-…"   2 months ago        Up 2 days                               nova_scheduler
171128b7bcb7        kolla/centos-source-nova-api:rocky                    "dumb-init --single-…"   2 months ago        Up 2 days                               nova_api
8d7f3de2ad63        kolla/centos-source-nova-placement-api:rocky          "dumb-init --single-…"   2 months ago        Up 2 days                               placement_api
ab763320f268        kolla/centos-source-nova-libvirt:rocky                "dumb-init --single-…"   2 months ago        Up 2 days                               nova_libvirt
bbd4c3e2c961        kolla/centos-source-nova-ssh:rocky                    "dumb-init --single-…"   2 months ago        Up 2 days                               nova_ssh
80e7098f0bfb        kolla/centos-source-cinder-backup:rocky               "dumb-init --single-…"   2 months ago        Up 2 days                               cinder_backup
20e2ff43d0e1        kolla/centos-source-cinder-volume:rocky               "dumb-init --single-…"   2 months ago        Up 2 days                               cinder_volume
6caba29f7ce2        kolla/centos-source-cinder-scheduler:rocky            "dumb-init --single-…"   2 months ago        Up 2 days                               cinder_scheduler
3111622e4e83        kolla/centos-source-cinder-api:rocky                  "dumb-init --single-…"   2 months ago        Up 2 days                               cinder_api
2c011cfae829        kolla/centos-source-glance-api:rocky                  "dumb-init --single-…"   2 months ago        Up 2 days                               glance_api
be84e405afdd        kolla/centos-source-kafka:rocky                       "dumb-init --single-…"   2 months ago        Up 2 days                               kafka
09aef04ad59e        kolla/centos-source-keystone-fernet:rocky             "dumb-init --single-…"   2 months ago        Up 2 days                               keystone_fernet
2ba9e19844fd        kolla/centos-source-keystone-ssh:rocky                "dumb-init --single-…"   2 months ago        Up 2 days                               keystone_ssh
8eebe226b065        kolla/centos-source-keystone:rocky                    "dumb-init --single-…"   2 months ago        Up 2 days                               keystone
662d85c00a64        kolla/centos-source-rabbitmq:rocky                    "dumb-init --single-…"   2 months ago        Up 2 days                               rabbitmq
7d373ef0fdee        kolla/centos-source-mariadb:rocky                     "dumb-init kolla_sta…"   2 months ago        Up 2 days                               mariadb
ab9f5d612925        kolla/centos-source-memcached:rocky                   "dumb-init --single-…"   2 months ago        Up 2 days                               memcached
a728298938f7        kolla/centos-source-kibana:rocky                      "dumb-init --single-…"   2 months ago        Up 2 days                               kibana
7d22d71cc31b        kolla/centos-source-keepalived:rocky                  "dumb-init --single-…"   2 months ago        Up 2 days                               keepalived
dae774ca7e33        kolla/centos-source-haproxy:rocky                     "dumb-init --single-…"   2 months ago        Up 2 days                               haproxy
14b340bb8139        kolla/centos-source-redis-sentinel:rocky              "dumb-init --single-…"   2 months ago        Up 2 days                               redis_sentinel
3023e95f465f        kolla/centos-source-redis:rocky                       "dumb-init --single-…"   2 months ago        Up 2 days                               redis
a3ed7e8fe8ff        kolla/centos-source-elasticsearch:rocky               "dumb-init --single-…"   2 months ago        Up 2 days                               elasticsearch
06b28cd0f7c7        kolla/centos-source-zookeeper:rocky                   "dumb-init --single-…"   2 months ago        Up 2 days                               zookeeper
630219f5fb29        kolla/centos-source-chrony:rocky                      "dumb-init --single-…"   2 months ago        Up 2 days                               chrony
6f6189a4dfda        kolla/centos-source-cron:rocky                        "dumb-init --single-…"   2 months ago        Up 2 days                               cron
039f08ec1bbf        kolla/centos-source-kolla-toolbox:rocky               "dumb-init --single-…"   2 months ago        Up 2 days                               kolla_toolbox
f839d23859cc        kolla/centos-source-fluentd:rocky                     "dumb-init --single-…"   2 months ago        Up 2 days                               fluentd

如:先找出容器ID

[root@wuhan31-ceph01 ~]# docker ps -a | grep -w mariadb | awk '{print $1}'
7d373ef0fdee

通过容器ID找出容器PID

[root@wuhan31-ceph01 ~]# docker inspect --format "{{ .State.Pid }}" 7d373ef0fdee
8144

通过容器PID连接

[root@wuhan31-ceph01 ~]# nsenter --target 8144 --mount --uts --ipc --net --pid
()[root@wuhan31-ceph01 /]# 

这样就连接到容器了,

为了方便这里做了一个脚本:

#!/bin/bash
#
# Description: This is sysytem optimization scripts about centos !
################################################################
# Author:tommy xiao
# QQ: 610000107
# Date: 2019.07.31
################################################################

# Variable settings
CONTAINER_NAME=$1

if [ ! -n "$CONTAINER_NAME" ];then
    echo -e "\033[40;33mUsage: /bin/bash docker.sh container_name\n\033[40;37m"
else
    CONTAINER_ID=`docker ps -a | grep $CONTAINER_NAME -w | awk '{print $1}'`
    echo -e "\033[40;32mContainer ID:$CONTAINER_ID!!!\n\033[40;37m"
    CONTAINER_PID=`docker inspect --format "{{ .State.Pid }}" $CONTAINER_ID`
    echo -e "\033[40;32mContainer PID:$CONTAINER_PID\n\033[40;37m"
    echo -e "\033[40;32mStart connecting container\n\033[40;37m"
    nsenter --target $CONTAINER_PID --mount --uts --ipc --net --pid
fi

docker容器连接_第1张图片