通过连接数据库的pod ip获取pod的信息

#!/bin/bash

ips=("10.25.70.3*" "10.25.184.11*" "10.25.18.16*")
ns_array=()
clusters=(wuwqin gauan)
port=30000

function exec {
	namespace=$1
	pod=$2
	cluster=$3
	echo === ${cluster} ${namespace} ${pod}
	cc=`kubectl exec --kubeconfig=./${cluster} -n ${namespace} ${pod}  -- netstat -antup|grep -v grep|grep ":30000"|wc -l`
	kubectl describe pod  --kubeconfig=./${cluster} -n ${namespace} ${pod} | grep username
}

function check {
	ip=$1
    result="Failure"
	for cluster in ${clusters[@]};do
			for ns in "${ns_array[@]}"; do
				pod=`kubectl get pod -n ${ns}  --kubeconfig=./${cluster} -o wide|grep -E "\s${ip}\s"|awk '{print $1}'`
				echo "ns" $ns $pod
				if [[ -n $pod ]];then
					result="Success"
					exec ${ns} ${pod} ${cluster}
					break 2
				fi
			done
			# 如果namespace比较少可以用这种方式
			#for namespace in `kubectl get ns --kubeconfig=./${cluster} --no-headers|awk '{print $1}'`;do 
				#pod=`kubectl get pod -A --kubeconfig=./${cluster} --no-headers -o wide|grep -E "\s${ip}\s"|awk '{print $1}'`
				#if [[ -n $pod ]];then
					#result="Success"
					#ns_array+=(${namespace})
					#exec ${namespace} ${pod} ${cluster}
					#break 2
				#fi
			#done
			while read -r namespace pod; do
				result="Success"
				ns_array+=(${namespace})
				exec ${namespace} ${pod} ${cluster}
				break 2
			done < <(kubectl get pod -A --kubeconfig=./${cluster} -o wide|grep -E "\s${ip}\s"|awk '{print $1,$2}')
	done
    if [[ "$result" == "Failure" ]];then
    	echo === ${ip} no match===
    fi
}

for ip in "${ips[@]}"; do
    echo $ip "${ns_array[@]}"
    check $ip
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值