系统相关
首页 > 系统相关> > 一个快速进入pod中容器的shell脚本

一个快速进入pod中容器的shell脚本

作者:互联网

#!/bin/bash

# define log & color
readonly FG_GREY="30"             #灰色
readonly FG_RED="31"
readonly FG_GREEN="32"
readonly FG_YELLOW="33"
readonly FG_BLUE="34"          #前景色蓝色
readonly FG_VIOLET="35"        #紫色
readonly FG_SKY_BLUE="36"
readonly FG_WHITE="37"

readonly BG_RED="41"
readonly BG_GREEN="42"
readonly BG_YELLOW="43"
readonly BG_BLUE="44"
readonly BG_VIOLET="45"
readonly BG_SKYBLUE="46"
readonly BG_WHITE="47"

readonly LOG_DEBUG="[调试]"
readonly LOG_INFO="[信息]"
readonly LOG_WARN="[警告]"
readonly LOG_ERROR="[错误]"

function debug()
{
    echo -e "\033[${FG_WHITE}m${LOG_DEBUG}\033[0m $1"
}
function info(){
    echo -e "\033[${FG_GREEN}m${LOG_INFO}\033[0m $1"
}
function warn()
{
    echo -e "\033[${FG_YELLOW}m${LOG_WARN}\033[0m $1"
}
function error()
{
    echo -e "\033[${FG_RED}m${LOG_ERROR}\033[0m $1"
}
function coloredLog()
{
    echo -e "\033[$1;$2m $3\033[0m"
}


function chooseContext() {
    kubectl config get-contexts
#    for context in ${contexts}
#    do
#        echo "${context}"
#    done
    echo -n "choose one: "
    read inputContext
    if [ "tx" = ${inputContext} ];then
        inputContext="context-tx"
    fi

    if [ "pre" = ${inputContext} ];then
        inputContext="context-pre"
    fi

    if [ "dc1" = ${inputContext} ];then
        inputContext="context-dc1"
    fi
    if [ "prd" = ${inputContext} ];then
        inputContext="context-prd"
    fi
    chooseContextResult=$(kubectl config use-context ${inputContext})
    info "${chooseContextResult}"
}

function chooseResource() {
    echo -n "input resource name: "
    read inputResource
    echo -n "input namespace: "
    read inputNamespace
    kubectl get ${inputResource} -n ${inputNamespace}
}

function intoDocker() {
    echo -n "choose namespace: "
    read inputNamespace
    info "切换到命名空间: ${inputNamespace}..."
    echo -n "input container name: "
    read containerName
    pods=($(kubectl get pod -n ${inputNamespace} |grep ${containerName}|awk '{print $1}'))
    info "正在查找pod列表..."
    num=0
    for pod in ${pods[@]}
    do
        echo "${num}: ${pod}"
        let num++
    done
    echo -n "choose a pod: "
    read podNum
    info "正在执行命令:kubectl exec -it -n ${inputNamespace} ${pods[podNum]} --container ${containerName} /bin/bash"
    kubectl exec -it -n ${inputNamespace} ${pods[podNum]} --container ${containerName} /bin/bash
}


# config env
info "正在设置环境变量..."
source ~/.bash_profile

info "正在查找集群..."
chooseContext

#info "正在收集资源..."
#chooseResource

intoDocker








#kubectl config use-context context-pre
#
#podName=$(kubectl get pod -n edu |grep edu-dating |awk '{print $1}')
#
#echo "edu-dating podName:${podName}"
#
#kubectl exec -it -n edu ${podName} --container edu-dating /bin/bash
#
#globalContext=default



# print context

log="this is a log string"

BOLD="01"             #加粗
UNDERLINE="04"        #下划线
BLINK="05"            #闪烁

标签:容器,shell,echo,inputContext,readonly,context,FG,033,pod
来源: https://blog.csdn.net/qq_35890572/article/details/119350036