栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

k8s基础-kubectl node-shell 登录节点

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

k8s基础-kubectl node-shell 登录节点

通过kubectl node-shell可以直接登录任意节点主机:

(weops) [root@node201 helm]# curl -LO https://github.com/kvaps/kubectl-node-shell/raw/master/kubectl-node_shell
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   150  100   150    0     0    123      0  0:00:01  0:00:01 --:--:--   123
100  3438  100  3438    0     0   1405      0  0:00:02  0:00:02 --:--:--  8013
(weops) [root@node201 helm]# chmod +x ./kubectl-node_shell
(weops) [root@node201 helm]# sudo mv ./kubectl-node_shell /usr/local/bin/kubectl-node_shell

(weops) [root@node201 helm]# kubectl get node
NAME      STATUS   ROLES                  AGE   VERSION
node2     Ready    control-plane,master   91d   v1.20.2
node201   Ready                     91d   v1.20.2

(weops) [root@node201 helm]# kubectl node-shell node2
spawning "nsenter-xhadg3" on "node2"
If you don't see a command prompt, try pressing enter.
[root@node2 /]# hostname
node2
[root@node2 /]# exit
logout
pod "nsenter-xhadg3" deleted
(weops) [root@node201 helm]#

源码解析:

#!/usr/bin/env sh
set -e

kubectl=kubectl
version=1.5.5
generator=""
node=""
nodefaultctx=0
nodefaultns=0
cmd='[ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--"'
if [ -t 0 ]; then
  tty=true
else
  tty=false
fi
while [ $# -gt 0 ]; do
  key="$1"

  case $key in
  -v | --version)
    echo "kubectl-node-shell $version"
    exit 0
    ;;
  --context)
    nodefaultctx=1
    kubectl="$kubectl --context $2"
    shift
    shift
    ;;
  --kubecontext=*)
    nodefaultctx=1
    kubectl="$kubectl --context=${key##*=}"
    shift
    ;;
  --kubeconfig)
    kubectl="$kubectl --kubeconfig $2"
    shift
    shift
    ;;
  --kubeconfig=*)
    kubectl="$kubectl --kubeconfig=${key##*=}"
    shift
    ;;
  -n | --namespace)
    nodefaultns=1
    kubectl="$kubectl --namespace $2"
    shift
    shift
    ;;
  --namespace=*)
    nodefaultns=1
    kubectl="$kubectl --namespace=${key##*=}"
    shift
    ;;
  --)
    shift
    break
    ;;
  *)
    if [ -z "$node" ]; then
      node="${1#node/}"
      shift
    else
      echo "exactly one node required"
      exit 1
    fi
    ;;
  esac
done

# Set the default context and namespace to avoid situations where the user switch them during the build process
[ "$nodefaultctx" = 1 ] || kubectl="$kubectl --context=$(${kubectl} config current-context)"
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(${kubectl} config view --minify --output 'jsonpath={.contexts..namespace}')"

if [ $# -gt 0 ]; then
  while [ $# -gt 0 ]; do
    cmd="$cmd, "$(echo "$1" | 
      awk '{gsub(/["\]/,"\\&");gsub(/x1b/,"\u001b");printf "%s",last;last=$0"\n"} END{print $0}' 
    )""
    shift
  done
  cmd="$cmd ]"
else
  cmd="$cmd, "bash", "-l" ]"
fi

if [ -z "$node" ]; then
  echo "Please specify node name"
  exit 1
fi

image="${KUBECTL_NODE_SHELL_IMAGE:-docker.io/library/alpine}"
pod="nsenter-$(env LC_ALL=C tr -dc a-z0-9 /dev/null || exit 1

container_cpu="${KUBECTL_NODE_SHELL_POD_CPU:-100m}"
container_memory="${KUBECTL_NODE_SHELL_POD_MEMORY:-256Mi}"
labels="${KUBECTL_NODE_SHELL_LABELS}"

overrides="$(
  cat <&2 || true; exit $EC" EXIT INT TERM

echo "spawning "$pod" on "$node"" >&2
$kubectl run --image "$image" --restart=Never --overrides="$overrides" --labels="$labels" $([ "$tty" = true ] && echo -t) -i "$pod" $generator

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/829981.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号