K8S常用工具<k8s_pod.sh> 一键脚本登录kubectl exec <pod>
Posted 江南飞羽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K8S常用工具<k8s_pod.sh> 一键脚本登录kubectl exec <pod>相关的知识,希望对你有一定的参考价值。
疼点:
繁琐:每次登录pod都要执行n条命令,kubectl get pod 获取pod名字,然后kubectl exec -it 登录pod
联系作者:如有疑问可评论区留言, 或加本人微信咨询q613230000, 请说明CSDN文章
躺着运维:
备注:如通过一键脚本工具,解决了哪些问题
1)快速登录:bash pod.sh -p <pod_name> -e 即可快速登录pod,用于替代kubectl exec -it
2)常见问题排查:是否有僵尸进程、pod端口是否监听、到底有多少cpu_limit、node cpu多少
3) 一键输出史上最相信的pod配额, 你无需再各种命令,一次过打印明细给你。躺着运维
目标:
开发一个一键脚本, 输出pod信息如:
1)配额:cpu_limit, ram_limit,
2)路径: 对应node上的存储路径, pod占用存储空间信息,
3)Pod状态: used cpu, used ram , 以及其他信息
功能说明:
1) 参数说明
-e 一键登录等效kubectl exec -it <pod_name> -- /bin/bash
-p 输入pod_name , 无需输入nameSpace(自动匹配), 且只需输入pod_name关键字如dep2即可(完整pod_name dep2-647d655dfb-6bfvh)
2)命令示例 @一键登录 dep2 (pod_name)
bash pod.sh -p dep2 -e
3)命令示例 @查看pod明细信息 (cpu/ram) (path/container_id/其他)
bash pod.sh -p dep2
脚本代码
前言:ok上干货,效果图请看上文, 脚本源码如下
使用:将以下源码copy到本地,明明为pod.sh/k8s_pod.sh都可以,bash pod.sh -p <你的pod名字>即可运行
备注: 原创不易,兄弟路过可点个赞, 如有新需求可留言,下个版本迭代
#!/bin/bash
# how to use: this shell script is to lookup container merge dir via pod_name
# usage: sh k8s_pod.sh -p <pod_name>
#. /tmp/import_function.sh
script_name=$0
while getopts "p:i:e" INPUT_OPT; do
case $INPUT_OPT in
p)INPUT_POD_NAME=$OPTARG;;
i)IS_LOOKUP_DOCKER_INSPECT=enable;;
e)IS_LOGIN_CONTAINER_VIA_KUBECTL_EXEC=enable;;
esac
done
format_1="%-25s%-55s\\n"
format_2="%-25s%-55.1f\\n"
format_3="%-25s%-10s%-25s\\n"
print_color_blue() {
input_1="$*"; red=31; green=32; yellow=33; blue=34; white=37
color=$blue
printf "\\033[4;${color}m${input_1}\\033[0m\\n"
}
sum_help=1
print_function_name() {
input_1=$1
input_2=$2
split_line="==================================="
print_color_blue "\\n""["${sum_help}"]"$input_1""
sum_help=$((${sum_help}+1))
}
print_release_note() {
print_function_name $FUNCNAME
echo release_note: ::'6/12 v1.2 release for pod cpu/ram percentage lookup' | awk -F:: '{printf("'"$format_1"'",$1,$2)}'
}
print_usage() {
print_function_name $FUNCNAME
echo "
e.g: bash $script_name -p pod01 # pod short name, via cmd 'docker ps -a | grep <pod_short_name>'
e.g: bash $script_name -p pod01-xxx-yyy # pod full namae
" | sed "1d"
}
container_id_dir_cpuquota_ramquota_lookup() {
# lookup id/path/cpu/ram via podname and docker inspect
print_function_name $FUNCNAME
pod_name=`kubectl get pod -A |grep $INPUT_POD_NAME |awk '{print $2}'`
[ -z $pod_name ] && echo "error: pod name not exist , pls check: kubectl get pod -o wide |grep $INPUT_POD_NAME" && exit 88
pod_namespace=`kubectl get pod -A |grep $INPUT_POD_NAME |awk '{print $1}'`
container_id=`docker ps -a |grep $INPUT_POD_NAME |grep -v '/pause' |grep -v 'Exited' |awk '{print $1}'`
container_dir_merge=`docker inspect $container_id |grep merge |sed "s/ //g"`
container_dir_merge_only_value=`docker inspect $container_id |grep merge |sed "s/ //g"|awk -F: '{print $2}' |sed "s/\\"//g; s/,//g"`
container_inspect_info=`docker inspect $container_id`
container_cpu_limit=`echo "$container_inspect_info" | cat -n |grep CpuQuota | awk -F: '{print $2}' |sed "s/,//g; s/ //g"`
container_ram_limit=`echo "$container_inspect_info" | cat -n |grep '"Memory"' | awk -F: '{print $2}' |sed "s/,//g; s/ //g"`
node_cpu_quota=`echo "$container_inspect_info" | cat -n |grep CpuPeriod | awk -F: '{print $2}' |sed "s/,//g; s/ //g"`
# output like: cpu/ram/node_cpu 50000=500m 1073741824=1Gi cpu=100000=1core
echo pod_name: $pod_name | awk '{printf("'"$format_1"'",$1,$2)}'
echo container_id: $container_id | awk '{printf("'"$format_1"'",$1,$2)}'
echo container_disk_path: $container_dir_merge_only_value | awk '{printf("'"$format_1"'",$1,$2)}'
# use awk -F:: as sperator
echo container_cpu_limit: ::${container_cpu_limit} ::'(e.g: 500 = 0.5 cpu_core)' | awk -F'::' '{printf("'"$format_3"'",$1,$2/100,$3)}' # cpu 50000=500m=0.5c
echo container_ram_limit'(MB)': $container_ram_limit | awk '{printf("'"$format_1"'",$1,$2/1024/1024)}' # ram 1073741824=1024MB
echo node_cpu_quota: ::${node_cpu_quota} ::'(e.g: 4000 = 4 cpu_core)' | awk -F'::' '{printf("'"$format_3"'",$1,$2/100,$3)}' # cpu 50000=500m=0.5c
}
container_cpu_mem_resource_lookup_via_id() {
print_function_name $FUNCNAME
container_pid=`ps aux |grep $container_id |grep -v grep |awk '{printf $2}'`
container_cpu_pct=`ps aux |grep $container_id |grep -v grep |grep -v Exited |awk '{printf $3}'`
container_mem_pct=`ps aux |grep $container_id |grep -v grep |grep -v Exited |awk '{printf $4}'`
container_mem_vsz=`ps aux |grep $container_id |grep -v grep |awk '{printf $5}'`
container_mem_rsz=`ps aux |grep $container_id |grep -v grep |awk '{printf $6}'`
echo container_pid: $container_pid | awk '{printf("'"$format_1"'",$1,$2)}'
echo container_cpu%: $container_cpu_pct | awk '{printf("'"$format_1"'",$1,$2)}'
echo container_mem%: $container_mem_pct | awk '{printf("'"$format_1"'",$1,$2)}'
echo container_mem_vsz'(MB)': $container_mem_vsz | awk '{printf("'"$format_2"'",$1,$2/1024)}'
echo container_mem_rsz'(MB)': $container_mem_rsz | awk '{printf("'"$format_2"'",$1,$2/1024)}'
}
node_cpu_mem_resource_lookup_via_shell() {
print_function_name $FUNCNAME
cpu_core=`lscpu |egrep '^CPU.s.:' |awk '{print $2}'`
cpu_load=`uptime |awk '{print $(NF-2),$(NF-1),$NF}'|sed "s/ /\\//g"|sed "s/,//g"`
process_status=`ps aux --no-header |awk '{a[$8]++} END{for(i in a) print i,a[i]}'|sort -k2n|sed "s/ /_/g"|tr "\\n" "/"`
tcp_socket_count=`ss -tn |grep -v State |cat -n|wc -l`
ram_total=`free -m|grep Mem:|awk '{print $2}'`
ram_free=`free -m|grep Mem:|awk '{print $4}'`
ram_free_available=`free -m|grep Mem:|awk '{print $NF}'`
ram_free_available_total=`echo $ram_free $ram_free_available $ram_total | sed "s/ /\\//g"` # sed combine free/available/total
echo cpu_core: $cpu_core | awk '{printf("'"$format_1"'",$1,$2)}'
echo cpu_load: $cpu_load | awk '{printf("'"$format_1"'",$1,$2)}'
echo process_status: $process_status | awk '{printf("'"$format_1"'",$1,$2)}'
echo tcp_socket_count: $tcp_socket_count | awk '{printf("'"$format_1"'",$1,$2)}'
echo ram_total'(MB)': $ram_total | awk '{printf("'"$format_1"'",$1,$2)}'
echo ram_free'(MB)': $ram_free | awk '{printf("'"$format_1"'",$1,$2)}'
echo ram_free_available'(MB)': $ram_free_available | awk '{printf("'"$format_1"'",$1,$2)}'
echo ram_free_avail_total: $ram_free_available_total | awk '{printf("'"$format_1"'",$1,$2)}'
}
container_inside_process_and_socket_info() {
print_function_name $FUNCNAME
echo -e "--------------------ps aux--------------------"
kubectl -n $pod_namespace exec -it $pod_name -- ps aux
echo -e "--------------------netstat -tunlp--------------------"
kubectl -n $pod_namespace exec -it $pod_name -- netstat -tunlp
}
login_container_via_kubectl_exec() {
# need pod_name arg from function container_id_dir_lookup_via_podname
print_function_name $FUNCNAME
echo -e "login container successfully: $pod_name \\n"
echo -e "please type exit if you want to exit container \\n\\n"
kubectl -n $pod_namespace exec -it $pod_name -- /bin/bash || kubectl -n $pod_namespace exec -it $pod_name -- /bin/sh
}
lookup_docker_inspect_info() {
print_function_name $FUNCNAME
docker inspect $container_id
}
#print_release_note
print_usage
container_id_dir_cpuquota_ramquota_lookup
container_cpu_mem_resource_lookup_via_id
node_cpu_mem_resource_lookup_via_shell
container_inside_process_and_socket_info
[ "$IS_LOOKUP_DOCKER_INSPECT" == "enable" ] && lookup_docker_inspect_info
[ "$IS_LOGIN_CONTAINER_VIA_KUBECTL_EXEC" == "enable" ] && login_container_via_kubectl_exec
echo -e "\\n\\n"
以上是关于K8S常用工具<k8s_pod.sh> 一键脚本登录kubectl exec <pod>的主要内容,如果未能解决你的问题,请参考以下文章
K8S常用工具<k8s_pod.sh> 一键脚本登录kubectl exec <pod>