装前必读
本文档适用于k8s 1.17+
注:请不要使用带中文的服务器或者克隆的虚拟机,容易出问题。
安装说明
OS: CentOS stream 8
k8s: 1.20.x
操作工具:Xshell (可同时向多个机器传输命令)
注:生产环境中,建议使用小版本大于5的Kubernetes版本,比如1.19.5以后的才可用于生产环境。
基础环境安装
注意事项
- 服务器IP地址配置成静态IP;
- VIP(虚拟IP)不要和公司内网的IP重复,配置前可以ping一下, 不通则可以使用,需要和主机在同一个局域网内;
- 请全局替换IP地址;
- 本文档显示的都是从master01节点操作命令,根据不同的要求,请将命令同时发送到不同的节点(xshell支持)。
配置信息
- K8s Service网段:10.96.0.0/12;
- K8s Pod网段:172.16.0.0/12;
- 宿主机IP分配,实际环境中请自行更改;
192.168.74.134 k8s-master01 192.168.74.135 k8s-master02 192.168.74.136 k8s-master03 192.168.74.251 k8s-master-lb 192.168.74.137 k8s-node01
- 系统环境
[root@k8s-master01 ~]# cat /etc/redhat-release CentOS Stream release 8
基础配置
- 所有节点配置hosts文件
[root@k8s-master01 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.74.134 k8s-master01 192.168.74.135 k8s-master02 192.168.74.136 k8s-master03 192.168.74.251 k8s-master-lb 192.168.74.137 k8s-node01
- 所有节点安装yum源
[root@k8s-master01 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo [root@k8s-master01 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 [root@k8s-master01 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo [root@k8s-master01 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
- 所有节点安装必备工具
[root@k8s-master01 ~]# yum install wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 git -y
- 所有节点关闭firewalld 、dnsmasq、selinux(CentOS7需要关闭NetworkManager, CentOS8不需要)
[root@k8s-master01 ~]# systemctl disable --now firewalld [root@k8s-master01 ~]# systemctl disable --now dnsmasq # centos7 需要执行 # [root@k8s-master01 ~]# systemctl disable --now NetworkManager [root@k8s-master01 ~]# setenforce 0 [root@k8s-master01 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux [root@k8s-master01 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
- 所有节点关闭swap分区,fstab注释swap
[root@k8s-master01 ~]# swapoff -a && sysctl -w vm.swappiness=0 [root@k8s-master01 ~]# sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
- 所有节点同步时间
# 安装chrony [root@k8s-master01 ~]# rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm [root@k8s-master01 ~]# dnf install wntp -y # centos7 -》yum install ntpdate -y # 同步时间 [root@k8s-master01 ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [root@k8s-master01 ~]# echo 'Asia/Shanghai' >/etc/timezone [root@k8s-master01 ~]# ntpdate time2.aliyun.com # 加入crontab [root@k8s-master01 ~]# crontab -e */5 * * * * /usr/local/bin/ntpdate time2.aliyun.com # centos7 使用下面语句 # */5 * * * * /usr/sbin/ntpdate time2.aliyun.com
- 所有节点配置limit
[root@k8s-master01 ~]# ulimit -SHn 65535 [root@k8s-master01 ~]# vim /etc/security/limits.conf # 末尾添加 * soft nofile 655360 * hard nofile 131072 * soft nproc 655350 * hard nproc 655350 * soft memlock unlimited * hard memlock unlimited
- master01节点配置免密钥登陆到其它的节点。安装过程中生成配置文件及证书均在master01节点完成,集群管理也在master01节点。
# 生成密钥 [root@k8s-master01 ~]# ssh-keygen -t rsa # 配置免密钥登陆到其它节点 [root@k8s-master01 ~]# for i in k8s-master01 k8s-master02 k8s-master03 k8s-node01;do ssh-copy-id -i .ssh/id_rsa.pub $i;done
- 所有节点安装基本工具
[root@k8s-master01 ~]# yum install wget jq psmisc vim net-tools yum-utils device-mapper-persistent-data lvm2 git -y
- master01节点节点下载安装文件
[root@k8s-master01 ~]# cd /root/ [root@k8s-master01 ~]# git clone https://github.com/dotbalo/k8s-ha-install.git
- 所有节点升级系统并重启
注:CentOS7需要升级,CentOS8可以按需升级系统[root@k8s-master01 ~]# reboot
[root@k8s-master01 ~]# yum update -y --exclude=kernel*
内核升级【CentOS7需要做该步骤】
说明:CentOS7 需要升级内核至4.18+,本地升级的版本为4.19
- master01节点内核下载
[root@k8s-master01 ~]# cd /root [root@k8s-master01 ~]# wget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm [root@k8s-master01 ~]# wget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm
- master01节点传输内核到其它节点
[root@k8s-master01 ~]# for i in k8s-master02 k8s-master03 k8s-node01;do scp kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm $i:/root/ ; done
- 所有节点安装内核
[root@k8s-master01 ~]# cd /root && yum localinstall -y kernel-ml*
- 所有节点更改内核启动顺序
[root@k8s-master01 ~]# grub2-set-default 0 && grub2-mkconfig -o /etc/grub2.cfg [root@k8s-master01 ~]# grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
- 所有节点检查默认内核是不是4.19
[root@k8s-master01 ~]# grubby --default-kernel /boot/vmlinuz-4.19.12-1.el7.elrepo.x86_64
- 所有节点重启,然后检查内核是不是4.19
[root@k8s-master01 ~]# reboot [root@k8s-master01 ~]# uname -a
内核配置
-
所有节点安装ipvsadm
[root@k8s-node01 ~]# yum install ipvsadm ipset sysstat conntrack libseccomp -y
-
所有节点配置ipvs模块
注:在内核4.19+版本nf_conntrack_ipv4已经改为nf_conntrack, 4.18以下使用nf_conntrack_ipv4即可
[root@k8s-node01 ~]# vim /etc/modules-load.d/ipvs.conf # 加入以下内容 ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_fo ip_vs_nq ip_vs_sed ip_vs_ftp ip_vs_sh nf_conntrack_ipv4 ip_tables ip_set xt_set ipt_set ipt_rpfilter ipt_REJECT ipip [root@k8s-master01 ~]# systemctl enable --now systemd-modules-load.service
-
所有节点配置k8s内核参数
[root@k8s-master01 ~]# vim /etc/sysctl.d/k8s.conf # 加入 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 fs.may_detach_mounts = 1 vm.overcommit_memory=1 vm.panic_on_oom=0 fs.inotify.max_user_watches=89100 fs.file-max=52706963 fs.nr_open=52706963 net.netfilter.nf_conntrack_max=2310720 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_keepalive_intvl =15 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_max_orphans = 327680 net.ipv4.tcp_orphan_retries = 3 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.ip_conntrack_max = 65536 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_timestamps = 0 net.core.somaxconn = 16384 [root@k8s-master01 ~]# sysctl --system
-
所有节点配置完内核后,重启服务器
[root@k8s-master01 ~]# reboot [root@k8s-master01 ~]# lsmod | grep --color=auto -e ip_vs -e nf_conntrack
输出:
基本组件安装
Docker安装
-
所有节点查看是否安装Podman
[root@k8s-master01 ~]# rpm -q podman podman-3.1.0-0.13.module_el8.5.0+733+9bb5dffa.x86_64
-
所有节点删除Podman
[root@k8s-master01 ~]# dnf remove podman [root@k8s-master01 ~]# yum erase podman buildah
-
所有节点安装Docker-ce 19.03
[root@k8s-master01 ~]# yum install docker-ce-19.03.* -y
-
所有节点修改docker配置
温馨提示:由于新版kubelet建议使用systemd,所以可以把docker的CgroupDriver改成systemd
[root@k8s-master01 ~]# mkdir /etc/docker [root@k8s-master01 ~]# vim /etc/docker/daemon.json # 添加: { "exec-opts": ["native.cgroupdriver=systemd"] }
-
所有节点设置开机自启动Docker
[root@k8s-master01 ~]# systemctl daemon-reload && systemctl enable --now docker
K8s及etcd安装
- Master01下载kubernetes安装包
注:安装是可下载最新的1.20.x版本, https://github.com/kubernetes/kubernetes/tree/master/CHANGELOG[root@k8s-master01 ~]# cd [root@k8s-master01 ~]# wget https://dl.k8s.io/v1.21.1/kubernetes-server-linux-amd64.tar.gz
- Master01下载etcd安装包
[root@k8s-master01 ~]# wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz
- Master01解压kubernetes安装文件
[root@k8s-master01 ~]# tar -xf kubernetes-server-linux-amd64.tar.gz --strip-components=3 -C /usr/local/bin kubernetes/server/bin/kube{let,ctl,-apiserver,-controller-manager,-scheduler,-proxy}
- Master01解压etcd安装文件
[root@k8s-master01 ~]# tar -zxvf etcd-v3.4.13-linux-amd64.tar.gz --strip-components=1 -C /usr/local/bin etcd-v3.4.13-linux-amd64/etcd{,ctl}
- Master01版本查看
[root@k8s-master01 ~]# kubelet --version Kubernetes v1.21.1 [root@k8s-master01 ~]# etcdctl version etcdctl version: 3.4.13 API version: 3.4
- Master01将组件发送到其他节点
[root@k8s-master01 ~]# MasterNodes='k8s-master02 k8s-master03' [root@k8s-master01 ~]# WorkNodes='k8s-node01' [root@k8s-master01 ~]# for NODE in $MasterNodes; do \ echo $NODE; \ scp /usr/local/bin/kube{let,ctl,-apiserver,-controller-manager,-scheduler,-proxy} $NODE:/usr/local/bin/; \ scp /usr/local/bin/etcd* $NODE:/usr/local/bin/; \ done [root@k8s-master01 ~]# for NODE in $WorkNodes; do \ scp /usr/local/bin/kube{let,-proxy} $NODE:/usr/local/bin/; \ done
- 所有节点创建/opt/cni/bin目录
[root@k8s-master01 ~]# mkdir -p /opt/cni/bin
- Master01切换到1.20.x分支(其他版本可以切换到其他分支)
[root@k8s-master01 ~]# cd k8s-ha-install && git checkout manual-installation-v1.20.x
生成证书(★★★★★)
工具准备
- Master01下载生成证书工具
[root@k8s-master01 ~]# wget "https://pkg.cfssl.org/R1.2/cfssl_linux-amd64" -O /usr/local/bin/cfssl [root@k8s-master01 ~]# wget "https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64" -O /usr/local/bin/cfssljson [root@k8s-master01 ~]# chmod +x /usr/local/bin/cfssl /usr/local/bin/cfssljson