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

2021/8/4 7:10:14

本文主要是介绍一个快速进入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"            #闪烁



这篇关于一个快速进入pod中容器的shell脚本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程