Kubernetes脚本——检查K8s基础信息
#!/bin/sh # version # node, master, slave # arch # kernel version # docker version # image # cpu, mem and usage # pod, pod limit # service, nodeport, lb # deploy, statefulset, deamonset # cm, secret # namespaces #set -x echo_left(){ if [ "$2" == "" ];then printf "%s\n" $1 | tee -a $fn else printf "%-20s: %s\n" $1 $2 | tee -a $fn fi } check_kubectl_exist() { cmd=$(which kubectl) if [ "$cmd" == "" ];then echo "can't find kubectl in" $PATH "exit." exit 1 fi } check_kubeconfig_exist() { config=$(find ~/.kube -name config) if [ "$config" == "" ];then kubectl version if [ "$?" == "0" ];then echo "apiserver listen on 8080, it's ok too." else echo "can't access apiserver by kubeconfig or 8080, exit" exit 1 fi fi } prepare_output_file() { fn="k8s-info.txt" > $fn echo_left "date" $(date +%Y%m%d%H%M%Z) } get_k8s_version() { rt=$(echo `kubectl version |grep Server | cut -d ',' -f 3 | cut -d':' -f2` | xargs) echo_left "version " $rt } get_k8s_nodes(){ all_nodes_cnt=$(kubectl get node |grep -v NAME| wc -l) echo_left "nodes_count " $all_nodes_cnt m_nodes_cnt=$(kubectl get node |grep master | wc -l) echo_left "=========================================" echo_left "masters_count " $m_nodes_cnt m_nodes=$(kubectl get node |grep master| cut -d ' ' -f1) for m in $m_nodes;do echo_left "-----------------------------------------" echo_left "master " $m get_node_info $m done w_nodes_cnt=$(kubectl get node |grep -v NAME |grep -v master | wc -l) echo_left "=========================================" echo_left "workers_count " $w_nodes_cnt w_nodes=$(kubectl get node | grep -v NAME | grep -v master | cut -d ' ' -f1) for w in $w_nodes;do echo_left "-----------------------------------------" echo_left "worker " $w get_node_info $w done echo_left "=========================================" } get_node_info() { local nname=$1 if [ "$nname" == "" ];then exit fi jp_res=$(kubectl get node $nname -o jsonpath="{.status.nodeInfo.architecture}") echo_left "arch " $jp_res jp_res=$(kubectl get node $nname -o jsonpath="{.status.nodeInfo.kernelVersion}") echo_left "kernel " $jp_res jp_res=$(kubectl get node $nname -o jsonpath="{.status.nodeInfo.containerRuntimeVersion}") echo_left "cri " $jp_res jp_res=$(kubectl get node $nname -o jsonpath="{.status.nodeInfo.osImage}") echo_left "os " $jp_res jp_cpu_res=$(kubectl get node $nname -o jsonpath="{.status.capacity.cpu}") echo_left "cpu " $jp_cpu_res jp_cpu_allocate_res=$(kubectl get node $nname -o jsonpath="{.status.allocatable.cpu}") echo_left "cpu_allocatable " $jp_cpu_allocate_res #set -x #echo_left "cpu_used " $(((jp_cpu_res-15)/jp_cpu_res)) #set -x jp_mem_res=$(kubectl get node $nname -o jsonpath="{.status.capacity.memory}") echo_left "mem " $jp_mem_res jp_mem_allocate_res=$(kubectl get node $nname -o jsonpath="{.status.allocatable.memory}") echo_left "mem_allocatable " $jp_mem_allocate_res get_node_pods $nname } get_pods_cnt(){ all_pods_cnt=$(kubectl get pod --all-namespaces |grep -v NAME| wc -l) echo_left "pods_count " $all_pods_cnt } get_node_pods(){ local nname=$1 if [ "$nname" == "" ];then exit fi jp_res=$(kubectl get pods --all-namespaces -owide |grep $nname | wc -l) echo_left "pods " $jp_res jp_res=$(kubectl get node $nname -o jsonpath="{.status.capacity.pods}") echo_left "pods_limit " $jp_res } get_services_info(){ all_svc_cnt=$(kubectl get svc --all-namespaces |grep -v NAME| wc -l) echo_left "svc_count " $all_svc_cnt all_svc_cnt=$(kubectl get svc --all-namespaces | grep -v NAME | awk '{print $6}' | grep ':' | wc -l) echo_left "svc_nodeport" $all_svc_cnt } get_deploys_info(){ all_deploy_cnt=$(kubectl get deploy --all-namespaces |grep -v NAME| wc -l) echo_left "deploy_count " $all_deploy_cnt } get_stss_info(){ all_sts_cnt=$(kubectl get sts --all-namespaces |grep -v NAME| wc -l) echo_left "sts_count " $all_sts_cnt } get_dss_info(){ all_ds_cnt=$(kubectl get ds --all-namespaces |grep -v NAME| wc -l) echo_left "ds_count " $all_ds_cnt } get_config_info(){ all_cm_cnt=$(kubectl get cm --all-namespaces |grep -v NAME| wc -l) echo_left "cm_count " $all_cm_cnt all_secret_cnt=$(kubectl get secret --all-namespaces |grep -v NAME| grep -v default-token |wc -l) echo_left "secret_nodefault" $all_secret_cnt } get_ns_info(){ all_ns_cnt=$(kubectl get ns --all-namespaces |grep -v NAME| wc -l) echo_left "ns_count " $all_ns_cnt } main(){ check_kubectl_exist check_kubeconfig_exist prepare_output_file get_k8s_version get_services_info get_deploys_info get_stss_info get_dss_info get_config_info get_ns_info get_pods_cnt get_k8s_nodes } main
稳步前行,只争朝夕。