#脚本-k8s加入集群节点

#!/usr/bin/env bash

##############################################################
# 脚本名称: get_token.sh
# 脚本功能: 获取加入节点命令信息
###############################################################



# 生成token
#kubeadm token create|awk '{print $1}'|tail -n 1
# 获取token
#Token=$(kubeadm token list|awk '{print $1}'|tail -n 1)
Token=$(kubeadm token create|awk '{print $1}'|tail -n 1)
# 获取masterUrl
masterUrl=$(kubectl cluster-info|grep master |awk -F"https://" '{print $2}')
# 显示token
echo "Token=${Token}"

if [ ! -f /etc/kubernetes/pki/ca.crt ];then
  echo "not find /etc/kubernetes/pki/ca.crt , Please Check!"
  exit
fi
# 生成密钥
Pubkey=$(openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //')

certificate_key=$(kubeadm init phase upload-certs --upload-certs|awk '{print $1}'|tail -n 1)

# 返回信息
# 返回信息
echo "添加control-plane命令"
echo "kubeadm join ${masterUrl} --token ${Token} \\
--discovery-token-ca-cert-hash sha256:${Pubkey} \\
--control-plane --certificate-key ${certificate_key}"

echo "添加node命令"  
echo "kubeadm join ${masterUrl} --token ${Token} \\
--discovery-token-ca-cert-hash sha256:${Pubkey}"
exit

留下评论