TLS Bootstraping:在kubernetes集群中,Node上组件kebelet和kube-proxy都需要与kube-apiserver进行通信,为了增加传输安全性,采用https方式,
这就涉及到node组件需要具备kube-apiserver用的证书颁发机构CA签发客户端证书,当规模较大时,这种客户端证书颁发需要大量工作,同时也会增加集群扩展复杂度。
为了简化流程,kubernetes引入了TLS bootstraping机制来自动颁发客户端证书,所以强烈建议在node上使用这种方式
具体实现步骤有5步
前提是已经存在二进制环境的集群
1、kube-apiserver启用bootstrap Token (master操作)
--enable-bootstrap-token-auth=true (必须启用)
查看的文件路径:
1:ps -ef | grep kube-apiserver #查找到安装路径
2:确认system守护文件:cat /usr/lib/systemd/system/kube-apiserver.service
EnvironmentFile就是配置文件的位置,找到配置文件位置后查看是否有
--enable-bootstrap-token-auth=true 这个选项或者启用状态
2、使用Secret存储Bootstrap Token
Bootstrap Token值格式:07401b.f395accd246ae52d (左边是token,右边是Token Secret)
生成token ID方式:
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
cat secret-token.yaml
apiVersion: v1 kind: Secret metadata: name: bootstrap-token-07401b namespace: kube-system type: bootstrap.kubernetes.io/token stringData: description: "The default bootstrap token generated by 'kubeadm init'." token-id: 07401b token-secret: f395accd246ae52d expiration: 2020-10-10T03:22:11Z #token id 过期时间,当前时间往后推 usage-bootstrap-authentication: "true" usage-bootstrap-signing: "true" auth-extra-groups: system:bootstrappers:worker,system:bootstrappers:ingress
生效清单文件
kubectl apply -f secret-token.yaml kubectl get secret -n kube-system
3、创建RBAC角色绑定,运行kubelet bootstrap创建CSR请求
4、kubelet配置Bootstrap kubeconfig文件
5、查看申请和批准
kubectl get csr kubectl certificate approve xxx
3-5一起操作
master操作
创建认证授权清单文件
cat bootstrap.yaml
# enable bootstrapping nodes to create CSR apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: create-csrs-for-bootstrapping subjects: - kind: Group name: system:bootstrappers apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: system:node-bootstrapper apiGroup: rbac.authorization.k8s.io
生效认证清单文件
kubectl apply -f bootstrap.yaml
node操作
相关系统优化
纯净的系统,优化相关参数,关闭防火墙、selinux、docker(加速)、关闭swap等
1、时间同步
echo "#time sync by fage at 2019-7-22" >>/var/spool/cron/root
echo "*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" >>/var/spool/cron/root
systemctl restart crond.service
2、关闭防火墙和selinux
systemctl stop firewalld systemctl disable firewalld setenforce 0 && sed -i s#SELINUX=enforcing#SELINUX=disable#g /etc/selinux/config
3、更改主机名
hostname k8s-node-3 echo " k8s-node-3" >/etc/hostname
4、更改hosts文件
cat >/etc/hosts <<EOF 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.10.160 k8s-master-1 192.168.10.161 k8s-node-1 192.168.10.162 k8s-node-2 192.168.10.163 k8s-node-3 EOF
5、节点node要禁用swap设备 不禁用要配置声明
swapoff -a sed -i "s@/dev/mapper/centos-swap swap@#/dev/mapper/centos-swap swap@g" /etc/fstab
6、将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system #生效配置
安装docker
方法一:yum方式安装
yum install -y docker mkdir -p /etc/docker cat > /etc/docker/daemon.json << EOF { "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"] } EOF systemctl daemon-reload && systemctl start docker.service && systemctl enable docker.service
方法二:二进制方式
在部署好的机器上直接复制新增加的节点上,没有就下载软件包或者选择yum安装
scp -r /usr/lib/systemd/system/docker.service root@192.168.10.163:/usr/lib/systemd/system/ cd /usr/bin/ scp -r containerd containerd-shim docker dockerd docker-init docker-proxy runc root@192.168.10.163:/usr/bin/ scp -r /etc/docker root@192.168.10.163:/etc/ systemctl daemon-reload && systemctl start docker.service && systemctl enable docker.service
其他部署好的node上操作:拷贝节点kubelet、CNI、kube-proxy组件到节点
scp -r /opt/kubernetes/ root@192.168.10.163:/opt/ scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.10.163:/usr/lib/systemd/system/ scp -r /opt/cni/ root@192.168.10.163:/opt
新部署的node操作:
需要删除kubelet的相关配置文件,kubelet的证书会自动颁发,每个节点上的证书都不一样,kubelet.kubeconfig自动生成,bootstrap.kubeconfig需要重新生成
rm -f /opt/kubernetes/ssl/kube* cd /opt/kubernetes/cfg/ && rm -f kubelet.kubeconfig bootstrap.kubeconfig
更改指向master地址、主机名
sed -i "s#k8s-master#k8s-node-3#g" /opt/kubernetes/cfg/kubelet.conf sed -i "s#k8s-master#k8s-node-3#g" /opt/kubernetes/cfg/kube-proxy-config.yml
创建bootstrap
cat >/opt/kubernetes/cfg/bootstrap.kubeconfig <<EOF apiVersion: v1 kind: Config clusters: - cluster: certificate-authority: /opt/kubernetes/ssl/ca.pem server: https://192.168.10.160:6443 name: bootstrap contexts: - context: cluster: bootstrap user: kubelet-bootstrap name: bootstrap current-context: bootstrap preferences: {} users: - name: kubelet-bootstrap user: token: 07401b.f395accd246ae52d EOF
需要重启docker和kubelet
systemctl restart docker.service systemctl start kubelet && systemctl enable kubelet systemctl start kube-proxy && systemctl enable kube-proxy
master操作:验证配置、批准加入
kubectl get csr #查询到最新的颁发请求,复制这个请求 kubectl certificate approve node-csr-xxxxxxxxxxxxx #批准颁发证书 kubectl get nodes # 会自动安装cni网络插件,安装完成后就会就绪了
至此node节点加入完成