1 部署前的环境准备
我用的三台服务器,环境准备部分,三台服务器都需操作
1.1 关闭防火墙,selinux
systemctl disable firewalld --now setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
1.2 关闭交换分区,注释掉/etc/fstab的swap挂载
swapoff -a
[root@k8s-master ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Sun Jun 26 14:22:46 2022 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=717e495b-061b-44d9-b5a6-e4e2f92ee30a /boot xfs defaults 0 0 #/dev/mapper/centos-swap swap swap defaults 0 0
1.3 同步集群时间
yum -y install ntpdate
接下来可以选择阿里云服务器同步时间,或者同步上海电信时间,这个选择很多,我只列举这两例子
ntpdate -u ntp.api.bz && hwclock -w
注: -w表示写入硬件时间
或者另一种同步方法,二选一即可
ntpdate ntp.aliyun.com
1.4 设置主机名,做域名解析
vim /etc/hosts
192.168.123.150 k8s-master 192.168.123.151 k8s-node1 192.168.123.139 k8s-node2
设置主机名
hostname set-hostname k8s-master hostname set-hostname k8s-node1 hostname set-hostname k8s-node2
1.5 如果ip是dhcp模式,请改为static模式(我网卡是ens33,请检查自己网卡)
vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" BOOTPROTO="static" DEFROUTE="yes" NAME="ens33" IPADDR=192.168.123.150 PREFIX=24 GATEWAY=192.168.123.2 DEVICE="ens33" ONBOOT="yes" DNS1="114.114.114.114" DNS2="8.8.8.8"
操作完成需要重启network
systemctl restart network
注: 我将ipv4,ipv6为首的内容删掉了,uuid也删掉了,它自己会生成的。三台机器重复修改,IPADDR注意填写自己的ip,我不是公网ip
2 安装docker(已经有docker环境直接略过2.1步骤,三台服务器均需执行)
2.1 安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2 git yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install docker-ce -y
然后启动docker,并且设置开机自启
2.2 阿里仓库拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.2 docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.2 docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.20.2 docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.2 docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0 docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
2.3 拉取 flannel 镜像
docker pull quay.io/coreos/flannel:v0.14.0
2. 4 阿里云拉取的镜像tag全部改名,后续步骤才能成功
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.2 k8s.gcr.io/kube-controller-manager:v1.20.2 docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.2 k8s.gcr.io/kube-proxy:v1.20.2 docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.20.2 k8s.gcr.io/kube-apiserver:v1.20.2 docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.2 k8s.gcr.io/kube-scheduler:v1.20.2 docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0 k8s.gcr.io/coredns:1.7.0 docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0 docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2
3 在三台服务器中安装kubeadm和kubelet
3.1 配置yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 EOF
3.2 安装对应版本
yum install -y kubelet-1.20.2-0.x86_64 kubeadm-1.20.2-0.x86_64 kubectl-1.20.2-0.x86_64 ipvsadm
4 开机自启相关内核模板和配置转发相关参数
4.1 vim /etc/rc.local
[root@k8s-master ~]# vim /etc/rc.local modprobe ip_vs modprobe ip_vs_rr modprobe ip_vs_wrr modprobe ip_vs_sh modprobe nf_conntrack_ipv4
给文件授权
chmod +x /etc/rc.local
4.2 配置转发参数
cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 vm.swappiness=0 EOF
所有节点都操作完毕之后,重启 reboot
4.3 重启后检查模块是否加载成功
lsmod | grep ip_vs