k8s,kubecm,kubectx,kubectl

第一针对configmap中有配置相关数据库信息的服务

脚本中有用到jq工具

输出当前环境下有配置configmap的deployment的名称

strconfig="config"
for line in `kubectl get deployment | awk '{print $1}'`
do
    name=`kubectl get deployment $line -ojson|jq .spec.template.spec.volumes | grep name`
    #echo $line $name
    result=$(echo $name | grep "${strconfig}")
        if [ -n "$result" ]; then
        echo $line
        fi
done

输出当前集群命名空间下有配置configmap的deployment的configmap的名称

strconfig="config"
for line in `kubectl get deployment | awk '{print $1}'`
do
    name=`kubectl get deployment $line -ojson|jq .spec.template.spec.volumes | grep name`
    result=$(echo $name | grep "${strconfig}")
        if [ -n "$result" ]; then
        for configname in `echo $name | awk '{print $2}'| sed 's/\"//g'`
        do
               echo ${configname%?}
        done
        fi
done

输出带有pg或者mysql的configMapName

strconfig="config"
str="\-config"
for line in `kubectl get deployment | awk '{print $1}'`
do
    name=`kubectl get deployment $line -ojson|jq .spec.template.spec.volumes | grep name`
    result=$(echo $name | grep "${strconfig}")
        if [ -n "$result" ]; then
        for finalresult in `echo $name | awk '{print $0}'| sed 's/\"//g'`
        do
            configname=$(echo $finalresult | grep "${str}")
                    if [ -n "$configname" ]; then
               finalConfigName=`echo $configname | sed -e 's/,//g'`
                   has_mysql=`kubectl get configmap $finalConfigName -o yaml | grep mysql`
                   if [ -n "$has_mysql" ]; then
                         echo "has mysql" $finalConfigName
                   fi
                   has_pg=`kubectl get configmap $finalConfigName -o yaml | grep pg`
                   if [ -n "$has_pg" ]; then
                             echo "has pg" $finalConfigName
                       fi
            fi
        done
        fi
done

输出带有pg或者mysql的deployment

strconfig="config"
str="\-config"
for line in `kubectl get deployment | awk '{print $1}'`
do
    name=`kubectl get deployment $line -ojson|jq .spec.template.spec.volumes | grep name`
    result=$(echo $name | grep "${strconfig}")
        if [ -n "$result" ]; then
        for finalresult in `echo $name | awk '{print $0}'| sed 's/\"//g'`
        do
            configname=$(echo $finalresult | grep "${str}")
                    if [ -n "$configname" ]; then
               finalConfigName=`echo $configname | sed -e 's/,//g'`
                   has_mysql=`kubectl get configmap $finalConfigName -o yaml | grep mysql`
                   if [ -n "$has_mysql" ]; then
                         echo "has mysql" $line
                   fi
                   has_pg=`kubectl get configmap $finalConfigName -o yaml | grep pg`
                   if [ -n "$has_pg" ]; then
                             echo "has pg" $line
                       fi
            fi
        done
        fi
done

第二针对环境变量中有配置相关数据库信息的服务

这个比较简单,直接查看deployment的yaml文件即可

kubectl get deploy deployment_name -o yaml | grep mysql

综合后shell脚本如下

strconfig="config"
str="\-config"

function get_deployment_from_configmap(){
for line in `kubectl get deployment | awk '{if (NR>1) print $1}'`
do
    name=`kubectl get deployment $line -ojson|jq .spec.template.spec.volumes | grep name`
    result=$(echo $name | grep "${strconfig}")
        if [ -n "$result" ]; then
        for finalresult in `echo $name | awk '{print $0}'| sed 's/\"//g'`
        do
            configname=$(echo $finalresult | grep "${str}")
                    if [ -n "$configname" ]; then
               finalConfigName=`echo $configname | sed -e 's/,//g'`
                   has_mysql=`kubectl get configmap $finalConfigName -o yaml | grep mysql`
                   if [ -n "$has_mysql" ]; then
                         echo "该服务在configMap中引用了mysql" $line
                   fi
                   has_pg=`kubectl get configmap $finalConfigName -o yaml | grep pg`
                   if [ -n "$has_pg" ]; then
                             echo "该服务在configMap中引用了pg" $line
                       fi
            fi
        done
        fi
done
}

function get_deployment_from_env(){
    str_mysql="mysql"
    str_pg="pg"
    for line in `kubectl get deployment | awk '{if (NR>1) print $1}'`
    do
        name=`kubectl get deploy $line -o yaml | grep mysql`
        mysql_result=$(echo $name | grep "${str_mysql}")
            if [ -n "$mysql_result" ]; then
           echo "该服务在环境变量中引用了mysql:" $line
        fi
        pg_result=$(echo $name | grep "${str_pg}")
            if [ -n "$pg_result" ]; then
               echo "该服务在环境变量中引用了pg:" $line
            fi
    done
}

function main(){
     get_deployment_from_configmap
     get_deployment_from_env
}

main

以上配合集群管理工具使用更佳哦!


世界碎掉了,但潮汐在牡蛎心里。

星空博客。

© 2024 星空的博客