keepalived实现haporxy负载均衡机高可用
目录
环境说明
系统信息 | 主机名 | IP | 服务 |
---|---|---|---|
centos8 | master | 192.168.111.141 | haproxy keepalived |
centos8 | backup | 192.168.111.142 | haproxy keepalived |
centos8 | RS1 | 192.168.111.143 | httpd |
centos8 | RS2 | 192.168.111.144 | nginx |
1.首先部署好web界面
RS1配置
//修改名字
[root@localhost ~]# hostnamectl set-hostname RS1
[root@localhost ~]# bash
[root@RS1 ~]#
//关闭防火墙和selinux
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS1 ~]# systemctl disable --now firewalld
[root@RS1 ~]# reboot
//配置yum源
[root@RS1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@RS1 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//安装httpd服务,主页内容为web1
[root@RS1 ~]# dnf -y install httpd
[root@RS1 ~]# echo "web1" > /var/www/html/index.html
[root@RS1 ~]# systemctl enable --now httpd.service
[root@RS1 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@RS1 ~]# curl 192.168.111.143
web1
RS2配置
//修改名字
[root@RS1 ~]# hostnamectl set-hostname RS2
[root@RS1 ~]# bash
[root@RS2 ~]#
//关闭防火墙和selinux
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS2 ~]# systemctl disable --now firewalld
[root@RS2 ~]# reboot
//配置yum源
[root@RS2 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@RS2 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//安装nginx服务,主页内容为web2
[root@RS2 ~]# dnf -y install nginx
[root@RS2 ~]# echo "web2" > /usr/share/nginx/html/index.html
[root@RS2 ~]# systemctl enable --now nginx.service
[root@RS2 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@RS2 ~]# curl 192.168.111.144
web2
2.在部署haproxy负载均衡
master端
//修改名字
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]#
//关闭防火墙和selinux
[root@master ~]# setenforce 0
[root@master ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@master ~]# systemctl disable --now firewalld
[root@master ~]# reboot
//配置yum源
[root@master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@master ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//创建用户
[root@master ~]# useradd -rMs /sbin/nologin haproxy
//安装依赖包
[root@master ~]# dnf -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel wget vim
//下载haproxy压缩包
[root@master ~]# wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-2.6.0.tar.gz/sha512/7bb70bfb5606bbdac61d712bc510c5e8d5a5126ed8827d699b14a2f4562b3bd57f8f21344d955041cee0812c661350cca8082078afe2