1、常用环境配置 2、配置ip免密登录 ssh-keygen -t rsa -f /root/.ssh/id_rsa -P "" ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.91 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.92 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.93 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.94 cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys chmod 600 /root/.ssh/authorized_keys 3、安装、配置ansible 请参照 https://www.cnblogs.com/effortsing/p/10012070.html ansible主机组配置必须如下: 说明:主机组必须配置如下,因为python脚本里面ansible命令就是按照下面执行的 cat >/etc/ansible/hosts <<EOF [all] 192.168.0.91 192.168.0.92 192.168.0.93 192.168.0.94 [k8s] 192.168.0.91 192.168.0.92 192.168.0.93 [test0] 192.168.0.92 192.168.0.93 [test1] 192.168.0.91 [test2] 192.168.0.92 [test3] 192.168.0.93 [test4] 192.168.0.94 EOF 5、修改所有主机名 sed -i '$a est1' /etc/hostname sed -i '$ahostname=test1' /etc/sysconfig/network && hostnamectl set-hostname test1 ssh 192.168.0.92 sed -i '$a est2' /etc/hostname sed -i '$ahostname=test2' /etc/sysconfig/network && hostnamectl set-hostname test2 exit ssh 192.168.0.93 sed -i '$a est3' /etc/hostname sed -i '$ahostname=test3' /etc/sysconfig/network && hostnamectl set-hostname test3 exit ssh 192.168.0.94 sed -i '$a est4' /etc/hostname sed -i '$ahostname=test4' /etc/sysconfig/network && hostnamectl set-hostname test4 exit 说明:不要一次全部复制,一个个复制,否则会出乱 用ansible总是显示格式不正确,所以直接用shell 6、test1上准备hosts文件 cat >/etc/hosts<<EOF 192.168.0.91 test1 192.168.0.92 test2 192.168.0.93 test3 192.168.0.94 test4 EOF 7、下发hosts文件、关掉selinux、防火墙、swap ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts force=yes' ansible all -m shell -a "sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux" ansible all -m shell -a "sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinux" ansible all -m shell -a "swapoff -a" ansible all -m shell -a "sed -i 's//dev/mapper/centos-swap/#/dev/mapper/centos-swap/g' /etc/fstab" ansible all -m shell -a "systemctl stop firewalld && systemctl disable firewalld" 8、hostname -i 验证ip hostname -i [root@test3 ~]# hostname -i 192.168.0.93 所有节点都要验证 说明:务必使用 hostname -i 验证所有节点包括test4节点,是否能看到ip地址,因为脚本中的ip变量就是使用hostname -i获取的, 之前kubelet报bootstrap认证错误,后来查看kubelet启动参数文件json文件里面的address竟然是0.0.0.0,没有被替换成真实的ip,导致出错 出现0.0.0.0的原因是因为这四个节点中其中有一个节点没有配置hosts解析,d掉一个ip也不行,必须写全,必须所有节点都相互配置hosts解析才不会看到0.0.0.0 9、配置主机名免密登录 分开复制,否则出现 ssh-copy-id -i ~/.ssh/id_rsa.pub root@test1 ssh-copy-id -i ~/.ssh/id_rsa.pub root@test2 ssh-copy-id -i ~/.ssh/id_rsa.pub root@test3 ssh-copy-id -i ~/.ssh/id_rsa.pub root@test4 10、测试用节点名称登录 ssh root@test1 ssh root@test2 ssh root@test3 ssh root@test4 说明:测试是否能用主机名登上对方主机,不要用ip,因为脚本里面是用的主机名,之前做实验就登不上test4节点,导致出错 11、、所有退出xshell查看主机名是否改变(必须) 12、python、pip安装包放到test1节点/usr/local/下 cd /usr/local/ rz pip-18.0.tar Python-3.6.5 13、准备pip-python.sh脚本 mkdir -p /script/ cd /script/ rz chmod +x /script/pip-python.sh 13、所有节点安装pip、python ansible all -m copy -a 'src=/usr/local/pip-18.0.tar.gz dest=/usr/local/pip-18.0.tar.gz force=yes' ansible all -m copy -a 'src=/usr/local/Python-3.6.5.tgz dest=/usr/local/Python-3.6.5.tgz force=yes' ansible all -m file -a 'path=/script/ state=directory mode=0777' ansible all -m copy -a 'src=/script/pip-python.sh dest=/script/pip-python.sh force=yes' ansible all -m shell -a "chmod +x /script/pip-python.sh" ansible all -m shell -a "sh /script/pip-python.sh" 说明:安装完后,ansible会出现许多错误,但是是绿色的,这个时候登录任意节点,输入python -v 查看python版本如果是3.6.5说明安装成功,就可以ctrl+c停掉脚本了 14、所有主机重启 ansible all -m shell -a "reboot" 说明:如果没有重启,安装完etcd总是无法启动