kubeadm 线上集群部署(二) K8S Master集群安装以及工作节点的部署

时间:2021-04-14 18:59:25

PS:所有机器主机名请提前设置好

在上一篇,ETCD集群我们已经搭建成功了,下面我们需要搭建master相关组件,apiverser需要与etcd通信并操作

1.配置证书

将etcd证书上传到master节点,在etcd01上操作

USER=root
export K8SHOST1=192.168.1.27
for HOST in ${K8SHOST1}
do
ssh ${USER}@${HOST} 'mkdir -p /etc/kubernetes/pki/etcd'
scp -r /etc/kubernetes/pki/etcd/ca.crt ${USER}@${HOST}:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/apiserver-etcd-client.crt ${USER}@${HOST}:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/apiserver-etcd-client.key ${USER}@${HOST}:/etc/kubernetes/pki/
done

 在k8s1上操作

kubeadm init phase certs ca --config=kubeadm-config-init.yaml
# 若要新增访问apiserver的IP,更新apiserver证书
kubeadm init phase certs apiserver --config=kubeadm-config-init.yaml
kubeadm init phase certs apiserver-kubelet-client --config=kubeadm-config-init.yaml
kubeadm init phase certs front-proxy-ca --config=kubeadm-config-init.yaml
kubeadm init phase certs front-proxy-client --config=kubeadm-config-init.yaml

执行脚本(包含了系统初始化,软件的安装)并将k8s1证书上传到2,3上执行init操作

curl -s https://gitee.com/hewei8520/File/raw/master/1.13.5/base-env-config-multi-node.sh |bash
curl -s https://gitee.com/hewei8520/File/raw/master/1.13.5/add_masterCluster.sh |bash

 

若上面没有报错,那么整个集群就安装成功了

kubeadm会生成kubelet的配置,默认是加载到/var/lib/kubelet中的, 提前创建会导致集群初始化失败,只能在集群安装成功以后在集体修改

IPS='192.168.1.28 192.168.1.29'

USER=root
systemctl stop kubelet
systemctl stop docker
sed -i 's/var\/lib/data/g' /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf sed -i 's/Environment="KUBELET_KUBECONFIG_ARGS=/Environment="KUBELET_KUBECONFIG_ARGS=--root-dir=\/data\/kubelet /g' /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf mv /var/lib/kubelet /data/ ln -s /data/kubelet /var/lib/kubelet systemctl start kubelet systemctl start docker systemctl enable --now kubelet
systemctl enable --now docker
for HOST in ${IPS}
do
  ssh ${USER}@${HOST} systemctl stop kubelet
  ssh ${USER}@${HOST} systemctl stop docker
  ssh ${USER}@${HOST} sed -i 's/var\/lib/data/g' /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
  ssh ${USER}@${HOST} sed -i  's/Environment="KUBELET_KUBECONFIG_ARGS=/Environment="KUBELET_KUBECONFIG_ARGS=--root-dir=\/data\/kubelet  /g' /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
  ssh ${USER}@${HOST} mv /var/lib/kubelet /data/
  ssh ${USER}@${HOST} ln -s /data/kubelet /var/lib/kubelet
  ssh ${USER}@${HOST} systemctl start kubelet
  ssh ${USER}@${HOST} systemctl start docker
  ssh ${USER}@${HOST} systemctl enable --now kubelet
done