Kubernetes Node 节点上,可能有一些基于 docker
命令的维护需求,可以通过脚本调用 docker inspect
等获取到更易读的信息。
列出所有业务容器
docker ps
列出的容器名称很长,不容易看出是哪个业务。通过 docker inspect
,将获取到的容器输出为以下格式:
1 |
CONTAINER-ID NETNS CPU MEM APP NAMESPACE |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function lsapp() { echo "CONTAINER-ID NETNS CPU MEM APP NAMESPACE" for id in `docker ps |awk 'NR!=1{print $1}'`;do LOG="/tmp/docker_inspect_$id.log" docker inspect $id > $LOG dockerType=`cat $LOG |grep io.kubernetes.docker.type |awk -F'"' '{print $4}'` if [ "$dockerType"x == "container"x ];then ns=`cat $LOG |grep "io.kubernetes.pod.namespace" |awk -F'"' '{print $4}'` sandboxId=`cat $LOG |grep io.kubernetes.sandbox.id |awk -F'"' '{print $4}'` SANDLOG="/tmp/docker_inspect_$sandboxId.log" docker inspect $sandboxId > $SANDLOG app=`cat $SANDLOG |grep -w '"app"' |awk -F'"' '{print $4}'` if [ "$app"x == ""x ];then app=`cat $LOG |grep -w '"io.kubernetes.pod.name"' |awk -F'"' '{print $4}'` fi cpu=`cat $LOG |grep -w '"CpuShares"' |awk '{print $NF}' |tr -d ','` mem=`cat $LOG |grep -w '"Memory"' |awk '{print $NF}' |tr -d ',' |awk '{print int($0/1024/1024)"MB"}'` netns=`cat $SANDLOG |grep -w "SandboxKey" |awk -F'"' '{print $4}' |awk -F'/' '{print $NF}'` printf "%s %-12s %-15s %-15s %-15s %-15s %s\n" "$id" "$netns" "`color $cpu`" "$mem" "$app" "$ns" else continue fi done } |
查看业务容器日志
1 2 3 4 5 6 7 8 9 10 11 |
function taillog() { [ $# -lt 1 ] && helpinfo logpath=`docker inspect $1 2>/dev/null|grep "LogPath" |awk -F'"' '{print $4}'` if [ "$logpath"x == ""x ];then dockerid=`docker ps |grep "$1" |head -n1 |awk '{print $1}'` logpath=`docker inspect $dockerid 2>/dev/null|grep "LogPath" |awk -F'"' '{print $4}'` fi if [ "$logpath"x != ""x ];then tail -f $logpath fi } |
查看容器TCP状态
在容器镜像没有 ss
或者 netstat
工具时,可以在宿主机上用 nsenter
来获取容器的 TCP 状态。首先获取到容器的网络命名空间,然后用 nsenter --net=$netns cmd
来查看,例如:
1 2 3 4 |
# nsenter --net=/var/run/docker/netns/893ddbaab579 ss -ant State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 :::9000 :::* |
代码如下。
1 2 3 4 5 6 |
function ssant() { [ $# -lt 1 ] && helpinfo sandboxid=`docker ps |grep $1 |grep "POD" |head -n1 |awk '{print $1}'` netns=`docker inspect $sandboxid | grep -w "SandboxKey" |awk -F'"' '{print $4}'` nsenter --net=$netns ss -ant } |
进入容器mergedir
1 2 3 4 5 |
function mergedir() { [ $# -lt 1 ] && helpinfo merge=`docker inspect $1 | grep "MergedDir" |awk -F'"' '{print $4}'` echo $merge } |
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#!/bin/bash function color() { echo -e "\033[41m$1\033[0m" } NETNSPRE="/var/run/docker/netns/" function lsapp() { echo "CONTAINER-ID NETNS CPU MEM APP NAMESPACE" for id in `docker ps |awk 'NR!=1{print $1}'`;do LOG="/tmp/docker_inspect_$id.log" docker inspect $id > $LOG dockerType=`cat $LOG |grep io.kubernetes.docker.type |awk -F'"' '{print $4}'` if [ "$dockerType"x == "container"x ];then ns=`cat $LOG |grep "io.kubernetes.pod.namespace" |awk -F'"' '{print $4}'` sandboxId=`cat $LOG |grep io.kubernetes.sandbox.id |awk -F'"' '{print $4}'` SANDLOG="/tmp/docker_inspect_$sandboxId.log" docker inspect $sandboxId > $SANDLOG app=`cat $SANDLOG |grep -w '"app"' |awk -F'"' '{print $4}'` if [ "$app"x == ""x ];then app=`cat $LOG |grep -w '"io.kubernetes.pod.name"' |awk -F'"' '{print $4}'` fi cpu=`cat $LOG |grep -w '"CpuShares"' |awk '{print $NF}' |tr -d ','` mem=`cat $LOG |grep -w '"Memory"' |awk '{print $NF}' |tr -d ',' |awk '{print int($0/1024/1024)"MB"}'` netns=`cat $SANDLOG |grep -w "SandboxKey" |awk -F'"' '{print $4}' |awk -F'/' '{print $NF}'` printf "%s %-12s %-15s %-15s %-15s %-15s %s\n" "$id" "$netns" "`color $cpu`" "$mem" "$app" "$ns" else continue fi done } function taillog() { [ $# -lt 1 ] && helpinfo logpath=`docker inspect $1 2>/dev/null|grep "LogPath" |awk -F'"' '{print $4}'` if [ "$logpath"x == ""x ];then dockerid=`docker ps |grep "$1" |head -n1 |awk '{print $1}'` logpath=`docker inspect $dockerid 2>/dev/null|grep "LogPath" |awk -F'"' '{print $4}'` fi if [ "$logpath"x != ""x ];then tail -f $logpath fi } function helpinfo() { echo "$0 ls list apps" echo "$0 log containerid or app tail log of container" echo "$0 ss app run ss in container net namespace" echo "$0 cd containerid cd container mergedir" exit 1 } function ssant() { [ $# -lt 1 ] && helpinfo sandboxid=`docker ps |grep $1 |grep "POD" |head -n1 |awk '{print $1}'` netns=`docker inspect $sandboxid | grep -w "SandboxKey" |awk -F'"' '{print $4}'` nsenter --net=$netns ss -ant } function mergedir() { [ $# -lt 1 ] && helpinfo merge=`docker inspect $1 | grep "MergedDir" |awk -F'"' '{print $4}'` echo $merge } case $1 in ls) lsapp;; log) taillog $2;; ss) ssant $2;; cd) mergedir $2;; *) helpinfo;; esac |
发表回复