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    <none>                 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/urandom | head -c 6)"

# Check the node
$kubectl get node "$node" >/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 <<EOT
{
  "spec": {
    "nodeName": "$node",
    "hostPID": true,
    "hostNetwork": true,
    "containers": [
      {
        "securityContext": {
          "privileged": true
        },
        "image": "$image",
        "name": "nsenter",
        "stdin": true,
        "stdinOnce": true,
        "tty": $tty,
        "command": $cmd,
        "resources": {
          "limits": {
            "cpu": "${container_cpu}",
            "memory": "${container_memory}"
          },
          "requests": {
            "cpu": "${container_cpu}",
            "memory": "${container_memory}"
          }
        }
      }
    ],
    "tolerations": [
      {
        "key": "CriticalAddonsOnly",
        "operator": "Exists"
      },
      {
        "effect": "NoExecute",
        "operator": "Exists"
      }
    ]
  }
}
EOT
)"

# Support Kubectl <1.18
m=$(kubectl version --client -o yaml | awk -F'[ :"]+' '$2 == "minor" {print $3+0}')
if [ "$m" -lt 18 ]; then
  generator="--generator=run-pod/v1"
fi

trap "EC=\$?; $kubectl delete pod --wait=false $pod >&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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Ansible 的 time 模块来计算集群节点之间的时间差。 首先,在 Ansible 控制节点上创建一个 playbook 文件,比如叫做 `time_diff.yml`,内容如下: ```yaml - hosts: k8s_nodes gather_facts: false tasks: - name: Get current time set_fact: current_time: "{{ ansible_date_time.epoch }}" - name: Get remote time shell: date +%s register: remote_time - name: Calculate time difference set_fact: time_diff: "{{ current_time - remote_time.stdout|int }}" - name: Print time difference debug: msg: "Time difference with {{ inventory_hostname }} is {{ time_diff }} seconds." ``` 在这个 playbook 中,我们首先使用 `set_fact` 模块获取当前时间,并将其保存在 `current_time` 变量中。然后,使用 `shell` 模块在远程节点上执行 `date +%s` 命令,获取远程节点的当前时间,并将其保存在 `remote_time` 变量中。接着,使用 `set_fact` 模块计算时间差,并将其保存在 `time_diff` 变量中。最后,使用 `debug` 模块打印时间差。 注意,这个 playbook 需要在 k8s_nodes 组内的所有节点上执行,因此需要在 inventory 文件中定义这个组,比如: ``` [k8s_nodes] node1 ansible_host=192.168.1.101 node2 ansible_host=192.168.1.102 node3 ansible_host=192.168.1.103 ``` 然后执行 playbook: ```bash ansible-playbook -i inventory.txt time_diff.yml ``` 执行完后,你应该可以看到类似于这样的输出: ``` ok: [node1] => { "msg": "Time difference with node1 is 0 seconds." } ok: [node2] => { "msg": "Time difference with node2 is 2 seconds." } ok: [node3] => { "msg": "Time difference with node3 is -1 seconds." } ``` 这表示在 node1 上执行 playbook 时,node1 的时间与当前时间相同,因此时间差为 0。在 node2 上执行 playbook 时,node2 的时间比当前时间慢了 2 秒,因此时间差为 2。在 node3 上执行 playbook 时,node3 的时间比当前时间快了 1 秒,因此时间差为 -1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值