主机配置

主机 系统 配置 IP
Ansible CentOS 7 1核1G 192.168.137.100
Mater、Node CentOS 7 2核4G 192.168.137.101
Node CentOS 7 2核2G 192.168.137.102

关闭防火墙

三台主机都需要操作

1
2
3
4
# 关闭防火墙
systemctl stop firewalld
# 禁止防火墙开机启动
systemctl disable firewalld

安装Python3.6

1
2
3
4
# 安装 epel 源
yum install epel-release
# 安装Python3.6
yum install python36 -y

安装pip3

参考官网

1
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
1
python3 get-pip.py

安装KubeSpray

参考官网

1
2
3
4
5
6
# 从官网下载最新版
wget https://github.com/kubernetes-sigs/kubespray/archive/v2.11.0.zip
# 安装unzip
yum install nuzip
# 解压
unzip v2.11.0.zip

安装KubeSpray所需的包(包括Ansible在内的一系列软件)

1
2
cd kubespray-2.11.0
pip3 install -r requirements.txt

在Ansible主机上设置免密码操作其它主机

  1. 首先生成ssh公钥和私钥
1
ssh-keygen

按三次回车.

  1. 建立ssh通道,将秘钥分发给master主机
1
2
ssh-copy-id root@192.168.137.101 
ssh-copy-id root@192.168.137.102

定义集群

1
2
3
cp -r inventory/sample inventory/mycluster
declare -a IPS=(192.168.137.101 192.168.137.102)
CONFIG_FILE=inventory/mycluster/hosts.yml python3 contrib/inventory_builder/inventory.py ${IPS[@]}

根据需求调整集群配置

1
vi inventory/mycluster/hosts.yml

使用ansible playbook部署Kubernetes集群

1
2
ansible-playbook -i inventory/mycluster/hosts.yml cluster.yml -b -v \
--private-key=~/.ssh/id_rsa

由于国内网络问题,肯定会失败.

  1. 配置linux 代理
  2. 配置docker代理,Mater、Node都需要配置
  3. calicoctl\hyperkube\kubeadm等文件下载失败,可以手动下载,并根据提示放到对应的目录.

验证是否安装成功

1
kubectl get no

添加节点

  1. 调整inventory/mycluster/hosts.yml集群配置文件.

  2. 增加 worker, master or etcd 节点,可以重新执行cluster.yml

1
2
ansible-playbook -i inventory/mycluster/hosts.yml cluster.yml -b -v \
--private-key=~/.ssh/id_rsa
  1. 增加 worker节点 运行ansible-playbook命令,替换cluster.ymlscale.yml
1
2
ansible-playbook -i inventory/mycluster/hosts.yml scale.yml -b -v \
--private-key=~/.ssh/id_rsa

删除节点

1
2
3
ansible-playbook -i inventory/mycluster/hosts.yml remove-node.yml -b -v \
--private-key=~/.ssh/id_rsa \
--extra-vars "node=nodename,nodename2"