前言:

部署k8s的方式有很多,之前使用二进制方式,步骤很多,多次部署不是很方便。看到官网推荐kubespray工具,尝试之后就依赖上了。kubespray使用ansible编写,阅读起来比较方便,根据自身需求修改安装参数。同时支持卸载,扩容,易于后期管理。缺点就是所有的镜像地址在外头,不想修改地址的话,需要先准备好镜像。

准备环境:

此次在测试环境安装,准备了6台机器

  • control 这台机器只做安装机器,和k8s集群没有关系
  • k8s-master-1
  • k8s-master-2
  • k8s-node-1

另两台备用,测试扩容

登录到control机器进行安装操作

安装ansible

$ yum install -y epel-release 
$ yum install -y ansible vim wget unzip

配置/etc/ansible/hosts

[k8s-master]
k8s-master-1 ansible_ssh_host=192.168.16.28
k8s-master-2 ansible_ssh_host=192.168.16.29
[k8s-node]
k8s-node-2 ansible_ssh_host=192.168.16.33
[k8s:children]
k8s-master
k8s-node

生成ssh 密钥

$ssh-keygen -t rsa 
#一直回车

对所有机器免密

$ ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub')}}' path='/root/.ssh/authorized_keys' manage_dir=no" --ask-pass -c paramiko
# 可能要多次输入yes

# 测试一下是否全部成功
$ ansible all -m ping

对其他机器进行一些基础安装和配置,依然在control操作,可以把下面写到脚本中(basic.sh)执行

#!/bin/sh
ansible all -m shell -a "cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtime"
ansible all -m yum -a "name=ntpdate state=present"
ansible all -m yum -a "name=unzip state=present"
ansible all -m yum -a "name=wget state=present"
ansible all -m shell -a "ntpdate ntp1.aliyun.com"
ansible all -m systemd -a "name=firewalld state=stopped enabled=False"
ansible all -m shell -a "setenforce 0"
ansible all -m lineinfile -a "dest=/etc/selinux/config regex=enforcing$ line=SELINUX=disabled"
ansible k8s -m shell -a "swapoff -a "
ansible k8s -m shell -a "sed -i /swap/s/^/#/ /etc/fstab"

下载kubespray

地址:wget https://github.com/kubernetes-sigs/kubespray/archive/v2.15.1.tar.gz

$ yum install -y python36 python36-devel python36-pip python-netaddr;

$ unzip kubespray-2.15.1.zip;
$ cd kubespray-2.15.1 && pip3 install -r requirements.txt;

$ declare -a IPS=(192.168.16.28 192.168.16.29 192.168.16.33 );

$ CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}
#mycluster目录不存在,需要复制一份inventory/sample
$ cat inventory/mycluster/hosts.yaml
all:
  hosts:
    node1:
      ansible_host: 192.168.16.28
      ip: 192.168.16.28
      access_ip: 192.168.16.28
    node2:
      ansible_host: 192.168.16.33
      ip: 192.168.16.33
      access_ip: 192.168.16.33
    node3:
      ansible_host: 192.168.16.34
      ip: 192.168.16.34
      access_ip: 192.168.16.34
  children:
    kube-master:
      hosts:
        node1:
        node2:
    kube-node:
      hosts:
        node1:
        node2:
        node3:
    etcd:
      hosts:
        node1:
        node2:
        node3:
    k8s-cluster:
      children:
        kube-master:
        kube-node:
    calico-rr:
      hosts: {}

要查看上面生成的hosts,和预期的是否一致,不一致手动更改。
初始安装的参数都在inventory/mycluster/group_vars/下的几个文件。

$ vim inventory/mycluster/group_vars/k8s-cluster/k8s-cluster.yml
例如修改 kubernetes版本、网络插件cni类型、集群IP、proxy方式等
```kube_version: v1.19.9```
```kube_network_plugin: flannel```

上传提前下载的镜像和安装包

$ ls 
basic.sh distribute.sh docker-rpm.zip  kubespray-2.15.1.zip  kubespray-images.zip  load_images.sh  releases.zip

这些是我提前准备好的,basic.sh就是上面初始化机器脚本,docker-rpm.zip,kubespray-images.zip,releases.zip打包的镜像和安装包,distribute.sh 是分发镜像的脚本,load_images.sh是导入镜像的脚本。

$ cat distribute.sh
#!/bin/sh
for i in docker-rpm.zip kubespray-images.zip releases.zip
do
ansible k8s -m unarchive -a "copy=yes src=$i dest=/tmp/"
done
$ cat load_images.sh
#!/bin/sh
for i in `ls /tmp/docker-rpm`;do yum install -y /tmp/docker-rpm/$i;done

echo "docker 安装完成"

systemctl start docker && systemctl enable docker
sleep 5;
echo "导入镜像"

for i in `ls /tmp/kubespray-images`;do docker load -i /tmp/kubespray-images/$i;done

这个脚本有时候执行失败,再此执行又好了

$ ansible k8s -m script -a "./load_images.sh"

执行安装

所有工作做完就可以安装了

$ cd kubespray-2.15.1;
$ ansible-playbook -i inventory/mycluster/hosts.yaml  --become --become-user=root cluster.yml

安装过程要持续十几分钟,中间出错,就针对错误进行排错,可以多次执行。

重置安装

$ ansible-playbook -i inventory/mycluster/hosts.yaml  --become --become-user=root reset.yml

新增node节点

今天需要新增一台node专门用于部署prometheus服务。步骤如下

  • 准备好机器,关闭防火墙
  • yum install wget vim unzip -y
  • wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  • wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  • yum install epel-release -y
  • control机器设置免密登录
  • 安装docker,导入所需镜像
  • kubelet,kube-proxy,kubeadm,cni插件拷贝到/tmp/releases下(最好所有k8s节点都保留/tmp/releases)
    当一切就绪,就在control机器kubespray目录下,编辑inventory/mycluster/hosts.yaml,把新增机器信息加入当前,千万不要动之前的配置。
$ ansible-playbook -i inventory/mycluster/hosts.yaml -b -v scale.yml