使用 Ansible 部署 K8s 集群



sudo -i

password="root2020"
echo -e "$password\n$password" |sudo passwd root

echo 'PermitRootLogin  yes' >> /etc/ssh/sshd_config
sudo service  ssh restart


apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y

 
apt-get install  -y python2.7 python-pip 

pip install pip --upgrade -i https://mirrors.aliyun.com/pypi/simple/
pip install ansible==2.6.18 netaddr==0.7.19 -i https://mirrors.aliyun.com/pypi/simple/


ssh-keygen 

ssh-copy-id $ip 


# 下载工具脚本easzup,举例使用kubeasz版本2.0.2
export release=2.2.1
curl -C- -fLO --retry 3 https://github.com/easzlab/kubeasz/releases/download/${release}/easzup
chmod +x ./easzup
# 使用工具脚本下载
./easzup -D





# 分步安装
ansible-playbook 01.prepare.yml
ansible-playbook 02.etcd.yml
ansible-playbook 03.docker.yml
ansible-playbook 04.kube-master.yml
ansible-playbook 05.kube-node.yml
ansible-playbook 06.network.yml
ansible-playbook 07.cluster-addon.yml
# 一步安装
#ansible-playbook 90.setup.yml

如果提示kubectl: command not found,退出重新ssh登录一下,环境变量生效即可

kubectl version         # 验证集群版本     
kubectl get node        # 验证节点就绪 (Ready) 状态
kubectl get pod -A      # 验证集群pod状态,默认已安装网络插件、coredns、metrics-server等
kubectl get svc -A      # 验证集群服务状态

重启所有组件

systemctl restart etcd && systemctl status etcd

systemctl restart flanneld && systemctl status flanneld

systemctl restart docker && systemctl status docker

systemctl stop nginx && systemctl start nginx && systemctl status nginx

systemctl restart keepalived && systemctl status keepalived

systemctl restart kube-apiserver && systemctl status kube-apiserver

systemctl restart kube-controller-manager && systemctl status kube-controller-manager

systemctl restart kube-scheduler && systemctl status kube-scheduler

systemctl restart kubelet && systemctl status kubelet

systemctl restart kube-proxy && systemctl status kube-proxy

清理

以上步骤创建的K8S开发测试环境请尽情折腾,碰到错误尽量通过查看日志、上网搜索、提交issues等方式解决;当然你也可以清理集群后重新创建。

在宿主机上,按照如下步骤清理

清理集群 docker exec -it kubeasz easzctl destroy
清理运行的容器 ./easzup -C
清理容器镜像 docker system prune -a
停止docker服务 systemctl stop docker
删除docker文件
 umount /var/run/docker/netns/default
 umount /var/lib/docker/overlay
 rm -rf /var/lib/docker /var/run/docker
上述清理脚本执行成功后,建议重启节点,以确保清理残留的虚拟网卡、路由等信息。

其他


master:

sudo ln -s /etc/kubernetes/kubelet.kubeconfig /etc/kubernetes/kubelet.conf

ln -sf /etc/kubernetes/kube-controller-manager.kubeconfig /etc/kubernetes/bootstrap-kubelet.conf

ln -s /etc/kubernetes/kube-controller-manager.kubeconfig /etc/kubernetes/admin.conf


###  stat /etc/kubernetes/bootstrap-kubelet.conf: no such file or directory 


nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf 

发现找不到 /etc/kubernetes/kubelet.conf 文件, 添加软连接

sudo ln -s /etc/kubernetes/kubelet.kubeconfig /etc/kubernetes/kubelet.conf


systemctl status kubelet.service
journalctl -xefu kubelet 


sudo ln -s /etc/kubernetes/kubelet.kubeconfig /etc/kubernetes/bootstrap-kubelet.conf

systemctl status kubelet.service|grep Active

rm /usr/bin/kubelet

cp bin/kubelet /usr/bin/


/opt/kube/bin/kubectl get node 192.168.70.151

cp bin/kubectl /opt/kube/bin/kubectl

scp bin/kubectl 192.168.70.152:/opt/kube/bin/kubectl

nano  roles/kube-master/defaults/main.yml 


cat /etc/kubernetes/ssl/basic-auth.csv