程序员的资源宝库

网站首页 > gitee 正文

K8S-Sealos部署高可用集群(k8s如何实现高可用)

sanyeah 2024-04-03 18:38:08 gitee 4 ℃ 0 评论

理论部分

传统高可用部署

使用nginx/LVS/haproxy代理,实现高可用

 

 

 Sealos部署高可用原理

 以下架构,如果在三个master节点前面部署了负载均衡,那么只需要将APIServer地址代理即可,scheduler和controller manager不需要执行负载均衡,因为如果APIServer挂了,调度器和控制器去访问负载均衡访问不到会报错;只需要使用回环接口访问APIServer,当某一个APIServer挂了,那么这两个组件也挂了;pod不受影响。

kubernetes和kuber-proxy 由IPVS生成路由规则,通过访问域名apiserver.cluster.local解析到10.103.97.2,转发到master节点,但是IPVS是没有健康检测的,当某一台master宕机,那么会有33%几率失败,所以k8s高版本使用pod实时监控master节点状态,当某一台宕机,自动移除路由规则。

kubectl 配置/etc/hosts文件指定当前所在的APIServer node节点的IP即可。比如:192.168.0.1   apiserver.cluster.local

 

 

 开始搭建

环境准备

3台master,master必须要是奇数,因为要选举

2台node节点

 

节点初始化

设置系统主机名以及 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

安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装一次!

 

]# cat /boot/grub2/grub.cfg  |grep  initrd16
        initrd16 /boot/initramfs-0-rescue-262c28bb9aba48a0ad5294749a4fddaa.img
        initrd16 /boot/initramfs-4.4.222-1.el7.elrepo.x86_64.img
        initrd16 /boot/initramfs-3.10.0-1160.45.1.el7.x86_64.img
        initrd16 /boot/initramfs-0-rescue-21acf41b46a64ca4a55e93cb350a7749.img

 

设置开机从新内核启动

grub2-set-default 'CentOS Linux (4.4.189-1.el7.elrepo.x86_64) 7 (Core)'

 

kube-proxy开启ipvs的前置条件
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF

 

chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

Sealos

官网文档:https://www.sealyun.com/instructions/1st

下载并安装 sealos, sealos 是个 golang 的二进制工具,直接下载拷贝到 bin 目录即可, release 页面也可下载

wget -c https://sealyun.oss-cn-beijing.aliyuncs.com/latest/sealos && chmod +x sealos && mv sealos /usr/bin

 

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表