节点信息 本文章将以下列节点数与规格来进行部署Kubernetes集群,系统采用CentOS 7.X,另外使用keepalived+haproxy来做master高可用 。其中VIP地址为192.168.0.240;
所有操作全部用root权限 
高可用一般建议大于等于3台的奇数台,我使用3台master来做高可用 
 
IP 
Hostname 
CPU 
Memory 
 
 
192.168.0.200 
K8S-M1 
4 
8G 
 
192.168.0.201 
K8S-M1 
4 
8G 
 
192.168.0.202 
K8S-M1 
4 
8G 
 
192.168.0.203 
K8S-N1 
4 
8G 
 
192.168.0.204 
K8S-N2 
4 
8G 
 
事前准备 
所有防火墙与SELinux 已关闭。如CentOS:否则后续 K8S 挂载目录时可能报错 Permission denied
 
1 2 3 systemctl disable --now firewalld NetworkManager0 '/^[^#]*SELINUX=/s#=.+$#=disabled#'  /etc/ selinux/config
关闭 dnsmasq (可选) linux 系统开启了 dnsmasq 后(如 GUI 环境),将系统 DNS Server 设置为 127.0.0.1,这会导致 docker 容器无法解析域名,需要关闭它
 
Kubernetes v1.8+要求关闭系统Swap,若不关闭则需要修改kubelet设定参数( –fail-swap-on 设置为 false 来忽略 swap on),在所有机器使用以下指令关闭swap并注释掉/etc/fstab中swap的行:
 
1 2 swapoff -a && sysctl -w vm.swappiness=0 '/^[^#]*swap/s@^@#@'  /etc/ fstab
如果是centos的话不想升级到最新内核可以忽略更新kernel
 
1 2 3 yum install  epel-release -yinstall  wget git  jq  psmisc socat -y
如果上面yum update加入–exclude=kernel*就重启下加载保守内核
 
因为目前市面上包管理下内核版本会很低,安装docker后无论centos还是ubuntu会有如下bug,4.15的内核依然存在
 
所以建议先升级内核,perl是内核的依赖包,如果没有就安装下
 
1 [ ! -f /usr/ bin/perl ] && yum install perl -y
升级内核需要使用 elrepo 的yum 源,首先我们导入 elrepo 的 key并安装 elrepo 源
 
1 2 rpm --import https:// www.elrepo.org/RPM-GPG-KEY-elrepo.org// www.elrepo.org/elrepo-release-7.0 -3 .el7.elrepo.noarch.rpm
查看可用的内核
 
1 yum --disablerepo= "*"  --enablerepo= "elrepo-kernel"  list available  --showduplicates 
在yum的ELRepo源中,mainline 为最新版本的内核,安装kernel
 
下面是ml的内核和上面归档内核版本任选其一的安装方法
 
1 2 3 export Kernel_Version=4.18 .9 -1 //mi rror.rc.usf.edu/compute_lock/ elrepo/kernel/ el7/x86_64/ RPMS/kernel-ml{,-devel}-${Kernel_Version} .el7.elrepo.x86_64.rpm
最新内核安装(也是我使用的)
 
1 2 yum --disablerepo ="*"  --enablerepo ="elrepo-kernel"  list available  --showduplicates | grep -Po '^kernel-ml.x86_64\s+\K\S+(?=.el7)' --disablerepo ="*"  --enablerepo =elrepo-kernel install -y kernel-ml{,-devel}
修改内核启动顺序,默认启动的顺序应该为1,升级以后内核是往前面插入,为0(如果每次启动时需要手动选择哪个内核,该步骤可以省略)
 
使用下面命令看看确认下是否启动默认内核指向上面安装的内核
 
docker官方的内核检查脚本建议(RHEL7/CentOS7: User namespaces disabled; add ‘user_namespace.enable=1’ to boot command line),使用下面命令开启
 
1 grubby --args ="user_namespace.enable=1"  --update-kernel ="$(grubby --default-kernel) " 
重启加载新内核
 
所有机器安装ipvs(ipvs性能甩iptables几条街并且排错更直观)在每台机器上安装依赖包:
 
1 yum install  ipvsadm ipset sysstat conntrack libseccomp -y
所有机器选择需要开机加载的内核模块,以下是 ipvs 模式需要加载的模块并设置开机自动加载
 
1 2 3 4 5 6 7 8 9 10 11 12 13 :> /etc/m odules-load.d/ipvs.conffor  kernel_module in  ${module[@]} ;do /sbin/m odinfo -F filename $kernel_module  |& grep -qv ERROR && echo $kernel_module  >> /etc/m odules-load.d/ipvs.conf || :
上面如果systemctl enable命令报错可以systemctl status -l systemd-modules-load.service看看哪个内核模块加载不了,在/etc/modules-load.d/ipvs.conf里注释掉它再enable试试
 
所有机器需要设定/etc/sysctl.d/k8s.conf的系统参数。
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 cat  <<EOF > /etc/sysctl.d/k8s.confnet .ipv4.tcp_keepalive_time = 600 net .ipv4.tcp_keepalive_intvl = 30 net .ipv4.tcp_keepalive_probes = 10 net .ipv6.conf.all .disable_ipv6 = 1 net .ipv6.conf.default.disable_ipv6 = 1 net .ipv6.conf.lo.disable_ipv6 = 1 net .ipv4.neigh.default.gc_stale_time = 120 net .ipv4.conf.all .rp_filter = 0 net .ipv4.conf.default.rp_filter = 0 net .ipv4.conf.default.arp_announce = 2 net .ipv4.conf.lo.arp_announce = 2 net .ipv4.conf.all .arp_announce = 2 net .ipv4.ip_forward = 1 net .ipv4.tcp_max_tw_buckets = 5000 net .ipv4.tcp_syncookies = 1 net .ipv4.tcp_max_syn_backlog = 1024 net .ipv4.tcp_synack_retries = 2 net .bridge.bridge-nf-call-ip6tables = 1 net .bridge.bridge-nf-call-iptables = 1 net .bridge.bridge-nf-call-arptables = 1 net .netfilter.nf_conntrack_max = 2310720 fs .inotify.max_user_watches=89100 fs .may_detach_mounts = 1 fs .file-max = 52706963 fs .nr_open = 52706963 vm .swappiness = 0 vm .overcommit_memory=1 vm .panic_on_oom=0 EOF sysctl  --system
所有机器需要安装Docker CE 版本的容器引擎,推荐使用年份命名版本的docker ce:https://github.com/kubernetes/kubernetes  里进对应版本的changelog里搜The list of validated docker versions remain
 
建议使用yum安装docker版本,这里我使用的是18.06.03,并为所有机器配置加速源并配置docker的启动参数使用systemd,使用systemd是官方的建议,详见 https://kubernetes.io/docs/setup/cri/ 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 mkdir -p /etc/ docker//etc/ docker/daemon.json<<EOF"exec-opts" : ["native.cgroupdriver=systemd" ],"registry-mirrors" : ["https://fz5yth0r.mirror.aliyuncs.com" ],"storage-driver" : "overlay2" ,"storage-opts" : ["overlay2.override_kernel_check=true" "log-driver" : "json-file" ,"log-opts" : {"max-size" : "100m" ,"max-file" : "3" 
设置docker开机启动,CentOS安装完成后docker需要手动设置docker命令补全:
 
1 2 yum install -y epel-release bash-completion && cp /usr/ share/bash-completion/ completions/docker / etc/bash_completion.d/ 
切记所有机器需要自行设定ntp,否则不只HA下apiserver通信有问题,各种千奇百怪的问题。