1-5 部署keepalived高可用与haproxy负载均衡

目录

  • 前言
  • 环境准备
  • keepalived的安装配置
  • haproxy的安装配置

前言

高可用与负载均衡是运维领域的常见架构。在K8s中也广泛应用,例如master节点,etcd数据库,harbor等。本文将介绍keepalived高可用与haproxy负载均衡。
1-5 部署keepalived高可用与haproxy负载均衡_第1张图片

高可用与负载均衡拓扑图


环境准备

使用的系统为Ubuntu20.04,首先修改主机名:

sudo hostnamectl set-hostname ha1 ; bash
sudo hostnamectl set-hostname ha2 ; bash

默认系统安装了bash_completion,但没启用。将if井号注释删除,开启自动补全。

vim ~/.bashrc

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

source ~/.bashrc

修改程序安装镜像源为清华源:

vim /etc/apt/sources.list

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

apt update

可选:安装apt-file,方便查看软件的安装路径,配置文件,帮助文件等。

apt install -y apt-file
sudo apt-file update

keepalived的安装配置

两台主机都安装上keepalived:

apt install -y keepalived

打开配置文件,只保留一个vrrp实例,按dG删除光标后全部内容。

cp /usr/share/doc/keepalived/samples/keepalived.conf.vrrp /etc/keepalived/keepalived.conf

root@ha1:~# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
	...
    interface ens33
    ...
    priority 100
    ...
    virtual_ipaddress {
        192.168.100.150 dev ens33 label eth33:1
    }
}

root@ha2:~# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
	...
    interface ens33
    ...
    priority 80
    ...
    virtual_ipaddress {
        192.168.100.150 dev ens33 label eth33:1
    }
}
配置项 参数
interface 填写本机IP的网卡接口
priority 优先级,数字越大越优先,要有差异。
virtual_ipaddress 虚拟地址VIP,要配成一样。

重启服务后,可查看接口配置:

sudo systemctl restart keepalived ; sudo systemctl status keepalived

ifconfig
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.150  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:7d:f2:10  txqueuelen 1000  (Ethernet)

找到第三台机子对VIP不间断ping,再依次关闭ha主机,一直能ping通则为高可用。

C:\WINDOWS\system32> ping 192.168.100.150 -t

root@ha1:~# poweroff
root@ha2:~# poweroff

haproxy的安装配置

参考kubeadm和containerd部署K8s master节点,先部署三台master节点。再用了haproxy对这个三节点做反向代理,实现负载均衡。

apt install -y haproxy

打开配置文件,按Go到文本最末尾,添加以下内容:

root@ha1:~# vim /etc/haproxy/haproxy.cfg 
listen web
  bind 192.168.100.146:80
  mode tcp
  server server1 192.168.100.141:80 check inter 3s fall 3 rise 5
  server server2 192.168.100.143:80 check inter 3s fall 3 rise 5
  server server3 192.168.100.145:80 check inter 3s fall 3 rise 5

root@ha2:~#vim /etc/haproxy/haproxy.cfg 
listen web
  bind 192.168.100.147:80
  mode tcp
  server server1 192.168.100.141:80 check inter 3s fall 3 rise 5
  server server2 192.168.100.143:80 check inter 3s fall 3 rise 5
  server server3 192.168.100.145:80 check inter 3s fall 3 rise 5

sudo systemctl restart haproxy.service 
配置项 参数
listen 新建一个监听,自定义名称
bind 绑定用于监听的本机IP与端口
mode 模式设置为tcp
server 添加一个服务器,后面名字自定义
IP:Port 这个服务器的IP与端口
check 启用健康检查
inter 检查间隔,3s每三秒检查一次
fall 连续检查3次,服务器都断线。则fall掉,以后负载不再转发。
rise 连续检查5次,服务器都在线。则rise起,以后负载可以转发。


你可能感兴趣的:(云原生,负载均衡,运维,云原生)