kubernetes -高可用部署
目录
系统初始化
设置系统主机名以及 Host 文件的相互解析
hostnamectl set-hostname k8s-master01
安装依赖包
yum install -y conntrack ntpdate ntp ipvsadm ipset iptables curl sysstat libseccomp wget vim net-tools git
设置防火墙为 Iptables 并设置空规则
systemctl stop firewalld && systemctl disable firewalld
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
关闭 SELINUX
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
调整内核参数,对于 K8S
cat > 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 空间,只有当系统 OOM 时才允许使用它
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
cp kubernetes.conf /etc/sysctl.d/kubernetes.conf
sysctl -p /etc/sysctl.d/kubernetes.conf
调整系统时区
# 设置系统时区为 中国/上海
timedatectl set-timezone Asia/Shanghai
# 将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务
systemctl restart rsyslog
systemctl restart crond
关闭系统不需要服务
systemctl stop postfix && systemctl disable postfix
设置 rsyslogd 和 systemd journald
mkdir /var/log/journal # 持久化保存日志的目录
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[Journal]
# 持久化保存到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
# 最大占用空间 10G
SystemMaxUse=10G
# 单日志文件最大 200M
SystemMaxFileSize=200M
# 日志保存时间 2 周
MaxRetentionSec=2week
# 不将日志转发到 syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald
升级系统内核为 4.44
CentOS 7.x 系统自带的 3.10.x 内核存在一些 Bugs,导致运行的 Docker、Kubernetes 不稳定,例如: rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装一次!
yum --enablerepo=elrepo-kernel install -y kernel-lt
# 设置开机从新内核启动
grub2-set-default 'CentOS Linux (4.4.189-1.el7.elrepo.x86_64) 7 (Core)'
Sealos 安装
Sealos 相关命令
# 下载并安装 sealos, sealos 是个 golang 的二进制工具,直接下载拷贝到 bin 目录即可, release 页面也可下载
$ wget -c https://sealyun.oss-cn-beijing.aliyuncs.com/latest/sealos && \
chmod +x sealos && mv sealos /usr/bin
# 下载离线资源包
$ wget -c https://sealyun.oss-cn-beijing.aliyuncs.com/2fb10b1396f8c6674355fcc14a8cda7c-v1.20.0/kube1.20.0.tar.gz
# 安装一个三 master 的 kubernetes 集群
$ sealos init --passwd '123456' \
--master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 \
--node 192.168.0.5 \
--pkg-url /root/kube1.20.0.tar.gz \
--version v1.16.0
增加 Master 节点
$ sealos join --master 192.168.0.6 --master 192.168.0.7
# 或者多个连续 IP
$ sealos join --master 192.168.0.6-192.168.0.9
增加 node
$ sealos join --node 192.168.0.6 --node 192.168.0.7
# 或者多个连续 IP
$ sealos join --node 192.168.0.6-192.168.0.9
删除指定 Master 节点
$ sealos clean --master 192.168.0.6 --master 192.168.0.7
# 或者多个连续 IP
$ sealos clean --master 192.168.0.6-192.168.0.9
删除指定 node 节点
$ sealos clean --node 192.168.0.6 --node 192.168.0.7
# 或者多个连续 IP
$ sealos clean --node 192.168.0.6-192.168.0.9
清理集群
$ sealos clean --all
备份集群
$ sealos etcd save
节点状态
kube-scheduler 状态查看
$ kubectl get endpoints kube-scheduler -n kube-system -o yaml
NAME ENDPOINTS AGE
kube-scheduler <none> 12m
[root@k8s-master01 ~]# kubectl get endpoints kube-scheduler -n kube-system -o yaml
apiVersion: v1
kind: Endpoints
metadata:
annotations:
control-plane.alpha.kubernetes.io/leader: '{"holderIdentity":"k8s-master02_d9af3300-cdad-49ce-838a-2ccdd38fbf18","leaseDurationSeconds":15,"acquireTime":"2021-03-01T07:09:24Z","renewTime":"2021-03-01T07:21:27Z","leaderTransitions":1}'
creationTimestamp: "2021-03-01T07:08:32Z"
name: kube-scheduler
namespace: kube-system
resourceVersion: "2013"
selfLink: /api/v1/namespaces/kube-system/endpoints/kube-scheduler
uid: b34592b6-0fe0-4cdd-9887-4b5fc9e8de97
kube-controller-manager 状态查看
$ kubectl get endpoints kube-controller-manager -n kube-system -o yaml
apiVersion: v1
kind: Endpoints
metadata:
annotations:
control-plane.alpha.kubernetes.io/leader: '{"holderIdentity":"k8s-master02_690df468-d680-4231-a8b9-4c8fa452eecf","leaseDurationSeconds":15,"acquireTime":"2021-03-01T07:09:24Z","renewTime":"2021-03-01T07:25:47Z","leaderTransitions":1}'
creationTimestamp: "2021-03-01T07:08:32Z"
name: kube-controller-manager
namespace: kube-system
resourceVersion: "2440"
selfLink: /api/v1/namespaces/kube-system/endpoints/kube-controller-manager
uid: 69f747dd-57d7-4aec-8443-9b3906ee9963