一、基础环境
| ip | hostname | 配置 | 操作系统 | 功能 |
|---|---|---|---|---|
| 192.168.51.190 | Haproxy-1 | 1c2g | Ubuntu 20.04.3 LTS | VIP192.168.51.192 |
| 192.168.51.191 | Haproxy-1 | 1c2g | Ubuntu 20.04.3 LTS | |
| 192.168.51.201 | master-1 | 2c4g | Ubuntu 20.04.3 LTS | kube-apiserver kube-controller-manager kube-scheduler kubectl etcd docker |
| 192.168.51.202 | master-2 | 2c4g | Ubuntu 20.04.3 LTS | |
| 192.168.51.203 | master-3 | 2c4g | Ubuntu 20.04.3 LTS | |
| 192.168.51.204 | node-1 | 2c4g | Ubuntu 20.04.3 LTS | kubelet kube-proxy docker |
| 192.168.51.205 | node-2 | 2c4g | Ubuntu 20.04.3 LTS |
二、系统设置
2.1主机名
主机名必须合法,并且每个节点都不一样(建议命名规范:数字+字母+中划线组合,不要包含其他特殊字符)。
# 查看主机名hostname# 修改主机名hostnamectl set-hostname <your_hostname># 配置host,使主节点之间可以通过hostname互相访问vim /etc/hosts192.168.51.201 master1.czerospace.org master-1192.168.51.202 master2.czerospace.org master-2192.168.51.203 master3.czerospace.org master-3192.168.51.204 node1.czerospace.org node-1192.168.51.205 node2.czerospace.org node-2
2.2配置免密登录
# 在一台master节点上生成密钥ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa# 使用脚本批量做免密登录
#!/bin/bashfor i in {201..205}do/usr/bin/expect -c "spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.51.$iexpect {\"*yes/no*\" {send \"yes\r\"; exp_continue}\"*password*\" {send \"ipanel\r\"; exp_continue}\"*password*\" {send \"ipanel\r\";}} "done
2.3关闭系统swap
# 临时关闭swapoff -a && free –h# 重启生效关闭vim /etc/fstab注释掉swap那行# 内核禁用swap参数vm.swappiness = 0# 修改grub文件vim /etc/default/grub在GRUB_CMDLINE_LINUX追加cgroup_enable=memory swapaccount=1# 更新grubupdate-grub# 重启服务器reboot
2.4配置k8s内核参数
# 编辑独立配置文件cat > /etc/sysctl.d/kubernetes.conf <<EOFnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1net.ipv4.ip_nonlocal_bind = 1net.ipv4.ip_forward = 1vm.swappiness = 0vm.overcommit_memory = 1EOF# 加载内核模块modprobe br_netfiltermodprobe overlay# 使配置文件生效sysctl -p /etc/sysctl.d/kubernetes.conf
三、安装docker
#!/bin/bash#安装依赖环境apt-get updateapt-get install apt-transport-https ca-certificates curl gnupg lsb-release software-properties-common -y#配置阿里云源# step 1: 安装GPG证书curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -# Step 2: 写入软件源信息sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"# Step 3: 更新并安装Docker-CEsudo apt-get -y updatesudo apt-get -y install docker-ce docker-ce-cli containerd.io#定制docker启动方式 配置加速器 添加私有harbor仓库echo '{"exec-opts":["native.cgroupdriver=systemd"],"registry-mirrors":["https://hub-mirror.c.163.com/"],"insecure-registries":["http://harbor.homed.tv"]}' > /etc/docker/daemon.json#定制docker启动方式#重启服务systemctl daemon-reloadsystemctl restart dockersystemctl enable docker
四、安装keepalived和haproxy高可用配置
4.1软件安装
apt install keepalived haproxy -y
4.2配置keepalived
# cp /usr/share/doc/keepalived/samples/keepalived.conf.vrrp /etc/keepalived/keepalived.conf# 编辑配置文件/etc/keepalived/keepalived.conf## 主:global_defs {router_id ha-1}vrrp_script chk_haproxy {script "/etc/keepalived/check_haproxy.sh"interval 2weight 2}vrrp_instance VI_1 {state MASTERinterface ens32virtual_router_id 191priority 100authentication {auth_type PASSauth_pass 1111}track_script {chk_haproxy}virtual_ipaddress {192.168.51.193 dev ens32 label ens32:1}}## 从global_defs {router_id ha-2}vrrp_script chk_haproxy {script "/etc/keepalived/check_haproxy.sh"interval 2weight 2}vrrp_instance VI_1 {state BACKUPinterface ens32virtual_router_id 191priority 90authentication {auth_type PASSauth_pass 1111}track_script {chk_haproxy}virtual_ipaddress {192.168.51.193 dev ens32 label ens32:1}}
4.3编辑检查haproxy状态脚本
keepalived master节点检测脚本
#!/bin/bashhaproxy_status=$(ps -C haproxy --no-header | wc -l)if [ $haproxy_status -eq 0 ];thensystemctl start haproxyfi
keepalived backup节点检测脚本
#!/bin/bashhaproxy_status=$(ps -C haproxy --no-header | wc -l)vip_status=$(ip a|grep 192.168.51.193)if [ $haproxy_status -eq 0 ] && [ -n vip_status ];thensystemctl start haproxyfi
4.4配置haproxy
# 修改配置文件vim /etc/haproxy/haproxy.cfg中间配置省略,对vip监听如下listen statusbind 192.168.51.193:9999mode httplog globalstats enablestats uri /haproxy-statsstats auth haadmin:123456listen k8s-api-6443bind 192.168.51.193:6443mode tcpserver master1 192.168.51.201:6443 check inter 3s fall 3 rise 5server master2 192.168.51.202:6443 check inter 3s fall 3 rise 5server master3 192.168.51.203:6443 check inter 3s fall 3 rise 5
#注意事项上面的haproxy.cfg是最终部署完三个节点后的状态,负载到master1 master2 master3在部署过程中,不要一次写三个server,部署完一个master节点,添加一个server
