内容转载自:https://github.com/opsnull/follow-me-install-kubernetes-cluster/blob/master/01.%E7%B3%BB%E7%BB%9F%E5%88%9D%E5%A7%8B%E5%8C%96%E5%92%8C%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F.md
上一篇:K8s-组件版本和配置策略.00
172.27.129.101 k8s-master-0001
172.27.129.102 k8s-master-0002
172.27.129.103 k8s-master-0003
172.27.129.104 k8s-etcd-0001
172.27.129.105 k8s-etcd-0002
172.27.129.106 k8s-etcd-0003
172.27.129.107 k8s-node-0001
172.27.129.108 k8s-node-0002
172.27.129.109 k8s-node-0003
本文档中的 etcd 集群、master 节点、worker 节点使用共9台机器。
设置永久主机名称,然后重新登录,按照集群机器分别改名:
$ sudo hostnamectl set-hostname k8s-master-0001 # 将 k8s-master-0001 替换为当前主机
修改每台机器的 /etc/hosts
文件,添加主机名和 IP 的对应关系:
$ grep k8s /etc/hosts
k8s-master-0001 172.27.129.101
k8s-master-0002 172.27.129.102
k8s-master-0003 172.27.129.103
k8s-etcd-0001 172.27.129.104
k8s-etcd-0002 172.27.129.105
k8s-etcd-0003 172.27.129.106
k8s-node-0001 172.27.129.107
k8s-node-0002 172.27.129.108
k8s-node-0003 172.27.129.109
在每台机器上添加 k8s 账户,可以无密码 sudo:
$ sudo useradd -m k8s $ sudo sh -c 'echo 123456 | passwd k8s --stdin' # 为 k8s 账户设置密码 $ sudo visudo $ sudo grep '%wheel.*NOPASSWD: ALL' /etc/sudoers %wheel ALL=(ALL) NOPASSWD: ALL $ sudo gpasswd -a k8s wheel
在每台机器上添加 docker 账户,将 k8s 账户添加到 docker 组中,同时配置 dockerd 参数:
$ sudo useradd -m docker $ sudo gpasswd -a k8s docker $ sudo mkdir -p /etc/docker/ $ cat /etc/docker/daemon.json { "registry-mirrors": ["https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"], "max-concurrent-downloads": 20 }
如果没有特殊指明,本文档的所有操作均在 k8s-master-0001 节点上执行,然后远程分发文件和执行命令。
设置 kube-node1 可以无密码登录所有节点的 k8s 和 root 账户:
[k8s@k8s-master-0001 k8s]$ ssh-keygen -t rsa [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-master-0001 [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-master-0002 [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-master-0003 [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-etcd-0001 [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-etcd-0002 [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-etcd-0003 [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-node-0001 [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-node-0002 [k8s@k8s-master-0001 k8s]$ ssh-copy-id root@k8s-node-0003 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-master-0001 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-master-0002 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-master-0003 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-etcd-0001 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-etcd-0002 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-etcd-0003 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-node-0001 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-node-0002 [k8s@k8s-master-0001 k8s]$ ssh-copy-id k8s@k8s-node-0003
在每台机器上添加环境变量:
sudo sh -c "echo 'PATH=/opt/k8s/bin:$PATH:$HOME/bin:$JAVA_HOME/bin' >>/root/.bashrc" echo 'PATH=/opt/k8s/bin:$PATH:$HOME/bin:$JAVA_HOME/bin' >>~/.bashrc
$ PATH=/opt/k8s/bin:$PATH:$HOME/bin:$JAVA_HOME/bin
在每台机器上安装依赖包:
CentOS:
$ sudo yum install -y epel-release conntrack ipvsadm ipset jq sysstat curl iptables libseccomp
Ubuntu:
$ sudo apt-get install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp
- ipvs 依赖 ipset;
在每台机器上关闭防火墙:
$ sudo systemctl stop firewalld $ sudo systemctl disable firewalld $ sudo iptables -F && sudo iptables -X && sudo iptables -F -t nat && sudo iptables -X -t nat $ sudo iptables -P FORWARD ACCEPT
如果开启了 swap 分区,kubelet 会启动失败(可以通过将参数 --fail-swap-on 设置为 false 来忽略 swap on),故需要在每台机器上关闭 swap 分区:
$ sudo swapoff -a
为了防止开机自动挂载 swap 分区,可以注释 /etc/fstab
中相应的条目:
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab #如无,可忽略
关闭 SELinux,否则后续 K8S 挂载目录时可能报错 Permission denied
:
$ sudo setenforce 0 $ grep SELINUX /etc/selinux/config SELINUX=disabled
linux 系统开启了 dnsmasq 后(如 GUI 环境),将系统 DNS Server 设置为 127.0.0.1,这会导致 docker 容器无法解析域名,需要关闭它:
$ sudo service dnsmasq stop $ sudo systemctl disable dnsmasq
$ sudo modprobe br_netfilter $ sudo modprobe ip_vs
$ cat > kubernetes.conf <
$ # 调整系统 TimeZone
$ sudo timedatectl set-timezone Asia/Shanghai
$ # 将当前的 UTC 时间写入硬件时钟
$ sudo timedatectl set-local-rtc 0
$ # 重启依赖于系统时间的服务
$ sudo systemctl restart rsyslog
$ sudo systemctl restart crond
$ sudo ntpdate cn.pool.ntp.org
在每台机器上创建目录:
$ sudo mkdir -p /opt/k8s/bin && chown -R k8s /opt/k8s $ sudo mkdir -p /etc/kubernetes/cert && chown -R k8s /etc/kubernetes $ sudo mkdir -p /etc/etcd/cert && chown -R k8s /etc/etcd/cert $ sudo mkdir -p /var/lib/etcd && chown -R k8s /etc/etcd/cert
后续的部署步骤将使用 environment.sh 中定义的全局环境变量,请根据自己的机器、网络情况修改。
然后,把全局变量定义脚本拷贝到所有节点的 /opt/k8s/bin
目录:
source environment.sh for colony_ip in 172.27.129.10{1..9} do echo ">>> ${colony_ip}" scp environment.sh k8s@${colony_ip}:/opt/k8s/bin/ ssh k8s@${colony_ip} "chmod +x /opt/k8s/bin/*" done
下一篇:K8s-创建CA证书和秘钥.02