官方文档地址:https://kubernetes.io/zh/docs/reference/setup-tools/kubeadm/
软件版本:CentOS Linux release 7.7.1908,kubernetes1.15
安装的节点:所有master节点+node节点
建议:使用自动化运维工具ansible,编写playbook+shell脚本完成这项重复性工作
必填项:所有master节点
选填项:node节点
虚拟机下安装:备份机 ip+hostname
[root@bogon ~]# yum clean all
[root@bogon ~]# yum makecache
[root@bogon ~]# yum install -y conntrack ntpdate ntp ipvsadm ipset jp iptables curl sysstat libseccomp wget vim net-tools git unzip
[root@bogon ~]# systemctl stop firewalld && systemctl disable firewalld
[root@bogon ~]# yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
[root@bogon ~]# swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
[root@bogon ~]# setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
[root@bogon ~]# cat > /etc/sysctl.d/kubernetes.conf << EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0 #禁用swap分区
vm.overcommit_memory=1 #不检查物理内存是否够用
vm.panic_on_oom=0 #开启OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720 #修改最大连接数
EOF
使生效
[root@bogon ~]# sysctl -p /etc/sysctl.d/kubernetes.conf
设置系统时区为:中国/上海
[root@bogon ~]# timedatectl set-timezone Asia/Shanghai
将当前的UTC时间写入硬件时钟
[root@bogon ~]# timedatectl set-local-rtc 0
重启依赖于系统时间的服务
[root@bogon ~]# systemctl restart rsyslog
[root@bogon ~]# systemctl restart crond
[root@bogon ~]# systemctl stop postfix && systemctl disable postfix
持久化保存日志的目录
[root@bogon ~]# mkdir /var/log/journal
[root@bogon ~]# mkdir /etc/systemd/journald.conf.d
[root@bogon ~]# cat > /etc/systemd/journald.conf.d/99-prophet.conf <
# 持久化保存到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
#最大占用空间 10G
SystemMaxUse=10G
#单日志文件最大 200M
SystemMaxFileSize=200M
#日志保存时间 2 周
MaxRetentionSec=2week
# 不将日志转发到 syslog
ForwardToSyslog=no
EOF
重启systemd-journald
[root@bogon ~]# systemctl restart systemd-journald
[root@bogon ~]# yum install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
[root@bogon ~]# yum makecache
[root@bogon ~]# yum --enablerepo=elrepo-kernel list kernel-lt
[root@bogon ~]# yum --enablerepo=elrepo-kernel install -y kernel-lt
[root@bogon ~]# grub2-editenv list
[root@bogon ~]# ls /boot
[root@bogon ~]# grub2-set-default "CentOS Linux (4.4.214-1.el7.elrepo.x86_64) 7 (Core)"
[root@bogon ~]# grub2-editenv list
安装内核源代码
[root@bogon ~]# yum --enablerepo=elrepo-kernel install kernel-lt-devel-$(uname -r) kernel-lt-headers-$(uname -r)
[root@bogon ~]# cp /etc/default/grub{,.bak}
[root@bogon ~]# vim /etc/default/grub //添加参数 numa=off
GRUB_CMDLINE_LINUX="crashkernel=auto spectre_v2=retpoline rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet numa=off"
[root@bogon ~]# diff /etc/default/grub.bak /etc/default/grub
[root@bogon ~]# cp /boot/grub2/grub.cfg{,.bak}
[root@bogon ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
[root@bogon ~]# reboot
[root@bogon ~]# modprobe br_netfilter
[root@bogon ~]# cat > /etc/sysconfig/modules/ipvs.modules <
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
[root@bogon ~]# chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.7. Latest validated version: 18.09
[root@bogon ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@bogon ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@bogon ~]# yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
[root@bogon ~]# yum update -y && yum install -y docker-ce-18.09.0-3.el7 docker-ce-cli-18.09.0-3.el7 containerd.io
[root@bogon ~]# systemctl start docker && systemctl enable docker
修改docker的 Cgroup Driver为systemd,与kubelet保持一致
[root@bogon ~]# cat > /etc/docker/daemon.json <
"exec-opts" : ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts":{
"max-size":"100m"
}
}
EOF
创建目录,存放docker配置文件,并重启docker服务
[root@bogon ~]# mkdir -p /etc/systemd/system/docker.service.d
[root@bogon ~]# systemctl daemon-reload && systemctl restart docker && systemctl enable docker
[root@bogon ~]# docker info
[root@bogon ~]# reboot
[root@bogon ~]# uname -r
[root@bogon ~]# cat > /etc/yum.repos.d/kubernetes.repo <
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
[root@bogon ~]# yum -y install kubeadm-1.15.1 kubectl-1.15.1 kubelet-1.15.1
[root@bogon ~]# systemctl enable kubelet.service
由于国内网络下载镜像很慢,所以需要事先下载,此处已打包至kubeadm-basic.images.tar.gz,上传至该目录 /root/k8s_install
[root@bogon ~]# mkdir /root/k8s_install
[root@bogon ~]# tar -zxvf /root/k8s_install/kubeadm-basic.images.tar.gz
[root@bogon ~]# vim /root/k8s_install/load-images.sh
#!/bin/bash
cd /root/k8s-install/kubeadm-basic.images/
ls /root/k8s-install/kubeadm-basic.images/ | grep -v load-images.sh > /tmp/k8s-images.txt
for i in $( cat /tmp/k8s-images.txt )
do
docker load -i $i
done
rm -rf /tmp/k8s-images.txt
[root@bogon ~]# sh /root/k8s_install/load-images.sh
[root@bogon ~]# docker images
[root@bogon ~]# ip a
[root@bogon ~]# ifdown ens33
[root@bogon ~未测]# cp /etc/sysconfig/network-scripts/ifcfg-ens33{,.bakup} && sed -i 's/^ONBOOT=.*/ONBOOT="no"/' /etc/sysconfig/network-scripts/ifcfg-ens33
使用kubeadm方式安装集群,api server证书默认有效期只有一年。
可通过修改源码的方式来修改证书有效期。
参考《kubernetes_01_kubeadm方式安装_02_apiserver证书默认有效期修改_kubeadm重新编译_20191023》
https://blog.csdn.net/wuyuezhengbian/article/details/105103185
++++++++++++++++++++至此公共部分已经完成+++++++++++++++++++
[root@bogon ~]# shutdown -h now
原主机作为备份机,克隆3个master节点+2~n个node节点,修改网卡mac。一台一台配置
[root@bogon ~]# ifconfig
[root@bogon ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34
[root@bogon ~]# hostnamectl set-hostname k8s-ha-master01
[root@bogon ~]# reboot
[root@******* ~]# hostname