keepalived实现nginx负载均衡机高可用
环境说明:
系统 | 主机名 | IP | 服务 |
---|---|---|---|
centos8 | master | 192.168.111.141 | nginx keepalived |
centos8 | backup | 192.168.111.142 | nginx keepalived |
centos8 | apache | 192.168.111.143 | httpd |
centos8 | nginx | 192.168.111.144 | nginx |
配置web界面
apache
//修改名字
[root@localhost ~]# hostnamectl set-hostname apache
[root@localhost ~]# bash
[root@apache ~]#
//关闭防火墙和selinux
[root@apache ~]# setenforce 0
[root@apache ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@apache ~]# systemctl disable --now firewalld
[root@apache ~]# reboot
//配置yum源
[root@apache ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@apache ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//安装apache,主页内容为apache
[root@apache ~]# yum -y install httpd
[root@apache ~]# echo "apache" > /var/www/html/index.html
[root@apache ~]# systemctl enable --now httpd
[root@apache ~]# 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@apache ~]# curl 192.168.111.143
apache
nginx
//修改名字
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# bash
[root@nginx ~]#
//关闭防火墙和selinux
[root@nginx ~]# setenforce 0
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@nginx ~]# systemctl disable --now firewalld
[root@nginx ~]# reboot
//配置yum源
[root@nginx ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@nginx ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//安装nginx,主页内容为nginx
[root@nginx ~]# yum -y install nginx
[root@nginx ~]# echo "nginx" > /usr/share/nginx/html/index.html
[root@nginx ~]# systemctl enable --now nginx
[root@nginx ~]# 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@nginx ~]# curl 192.168.111.144
nginx
配置nginx负载均衡
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 nginx
//安装依赖包
[root@master ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
//编译安装nginx
[root@backup ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@master ~]# tar xf nginx-1.22.0.tar.gz
[root@master ~]# cd nginx-1.22.0
[root@master nginx-1.22.0]# ./configure \
-