#!/bin/bash
which kubectl > /dev/null 2>&1
if [ $? != 0 ];then 
    echo "kubelet工具未安装,请先安装!!"
    exit
fi
if [ ! -f ~/.kube/config ];then
    echo "当前用户家目录下没有k8s集群配置文件.kube/config,请联系运维获取配置!"
    exit
fi
read -p $'请选择要操作的命名空间:\n1: test \n2: stg:\n' NAMESPACE
case $NAMESPACE in
    1)
        NAMESPACE=zzmed-test
        read -p $'请输入要操作POD的关键字:\n' POD
        kubectl get pods -n $NAMESPACE -owide|grep $POD
        read -p $'请复制粘贴上方POD的全名,并回车:\n' PODNAME
        read -p $'请选择要操作的终端:\n1: bash \n2: sh:\n' BASH
        case $BASH in
            1)
                BASH=bash
                kubectl exec -it -n $NAMESPACE $PODNAME $BASH
                ;;
            2)
                BASH=sh
                kubectl exec -it -n $NAMESPACE $PODNAME $BASH
                ;;
            *)
                echo "请输入正确的终端1: bash 2: sh"
        esac
        ;;
    2)
        NAMESPACE=zzmed-stg
        read -p $'请输入要操作的POD的关键字:\n' POD
        kubectl get pods -n $NAMESPACE -owide|grep $POD
        read -p $'请复制粘贴上方POD的全名,并回车:\n' PODNAME
        read -p $'请选择要操作的终端:\n1: bash \n2: sh': BASH
        case $BASH in
            1)
                BASH=bash
                kubectl get pods -n $NAMESPACE |grep $PODNAME
                kubectl exec -it -n $NAMESPACE $PODNAME $BASH
                ;;
            2)
                BASH=sh
                kubectl get pods -n $NAMESPACE |grep $PODNAME
                kubectl exec -it -n $NAMESPACE $PODNAME $BASH
                ;;
            *)
                echo "请输入正确的终端1: bash 2: sh"
        esac
        ;;
    *)
    echo "请选择正确的命名空间1:test 2:stg"
esac

给脚本加可执行权限

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐