Keepalive Nginx 热备

Keepaliaved:2.2.7

CentOS: 7

Nginx: 1.23.2

服务器: 10.10.2.18(主服务器)

服务器: 10.10.2.17(备服务器)

部署图

一、在18服务器安装Nginx

1、创建相关文件夹

[root@apple-test02 /]# mkdir -p /opt/nginx/data/conf.d /opt/nginx/data/log /opt/nginx/data/html && ls /opt/nginx/data

2、在目录/opt/nginx/data/目录下编写docker-compose.yml文件

[root@apple-test02 /]# cd /opt/nginx && vim docker-compose.yml

version: "3" services: nginx: container_name: nginx image: 10.10.2.111/cssca/nginx:1.23.2 ##公网直接写nginx:1.23.2 restart: always ports: - "80:80" volumes: - ./data/nginx.conf:/etc/nginx/nginx.conf - ./data/conf.d:/etc/nginx/conf.d - ./data/log:/var/log/nginx - ./data/html:/usr/share/nginx/html

3、创建nginx.conf 在目录/opt/nginx/data/下

[root@apple-test02 nginx]# cd /opt/nginx/data/ && vim nignx.conf

user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include ./conf.d/*.conf; }

4、创建default.conf 在目录/opt/nginx/data/conf.d下

[root@apple-test02 data]# cd /opt/nginx/data/conf.d/ && vim default.conf

server { listen 80; listen [::]:80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }

5、配置index.hmtl 在目录/opt/nginx/data/html下

[root@apple-test02 conf.d]# cd /opt/nginx/data/html/ && vim index.html

6、运行docker-compose 起动Nginx

[root@apple-test02 html]# cd /opt/nginx && docker-compose up -d

7、查看运行状态

[root@apple-test02 html]# docker ps

8、浏览器访问

http://10.10.2.18/

二、在17服务器安装Nginx

1、把18服务的/opt/nginx目录拷贝到17服务上

[root@apple-test01 html]# scp -r 10.10.2.18:/opt/nginx /opt/

2、配置index.hmtl 在目录/opt/nginx/data/html下

[root@apple-test01 conf.d]# cd /opt/nginx/data/html/ && vim index.html

3、运行docker-compose 起动Nginx

[root@apple-test01 ~]# cd /opt/nginx && docker-compose up -d

4、查看运行状态

[root@apple-test01 nginx]# docker ps

5、浏览器访问


三、在18服务服器上安装keepalive 软件

1、安装依赖软件: lrzsz ntpdate wget gcc curl openssl-devel libnl3-devel net-snmpdevel -y

[root@apple-test02 home]# yum install lrzsz ntpdate wget gcc curl openssl-devel libnl3-devel net-snmpdevel -y

2、下载软件到目录/opt/software

官方下载:Keepalived for Linux

wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz --no-check-certificate

软件放到到/opt/software/目录下

[root@apple-test02 software]# pwd && ls

3、解压软件

[root@apple-test02 software]# tar -zxvf keepalived-2.2.7.tar.gz

4、进入keepalived目录

[root@apple-test02 software]# cd keepalived-2.2.7/ && ls

5、配置编译路径

[root@apple-test02 keepalived-2.2.7]# ./configure --prefix=/usr/local/keepalived --sysconf=/etc

6、编译

[root@apple-test02 keepalived-2.2.7]# make && make install

7、查询版本

[root@apple-test02 keepalived-2.2.7]# /usr/local/keepalived/sbin/keepalived -v

8、配置文件

[root@apple-test02 ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived global_defs {#全局变量 notification_email { # 邮件通知信息 fhh6666@126.com # 定义收件人 } notification_email_from Alexandre.Cassen@firewall.loc # 定义发件人 smtp_server 192.168.200.1 # SMTP服务器地址(邮箱服务器地址) smtp_connect_timeout 30 #超时时间 router_id LVS_MASTER # 路由器标识,一般不用改,也可以写成每个主机自己的主机名 } vrrp_script ch_nginx { #脚本模块 script "/etc/keepalived/check.sh" #脚本路径 interval 5 # 设置执行间隔大于advert_int } vrrp_instance VI_1 { #一个vrrp_instance就是定义一个虚拟路由器的,实例名称 state MASTER #定义初始状态,可以是MASTER或者BACKUP interface ens33 #当前使用的网卡名 virtual_router_id 110 #集群ID,MASTER和BACKUP一样 priority 100 #当前服务器优先级,数字越大越优先 advert_int 1 #检测心跳时间为1秒 authentication { #身份验证方式通信认证机制,这里是明文认证还有一种是加密认证 auth_type PASS #密码验证 auth_pass 1111 #密码为1111 } track_script { #添加监控条件 ch_nginx #脚本模块后边定义的名称 } virtual_ipaddress { 10.10.2.20/16 #VIP地址 } }

9、创建check.sh脚本防止重复执行

[root@apple-test02 keepalived]# vim /etc/keepalived/check.sh

#! /bin/bash exec flock -xn /etc/keepalived/demo.lock -c /etc/keepalived/check_nginx.sh

10、创建check_nginx.sh脚本

[root@apple-test02 keepalived]# vim /etc/keepalived/check_nginx.sh

#!/bin/bash echo "检查nginx 运行状态!" run=`docker ps | grep nginx| grep Up|wc -l` if [ $run -eq 0 ]; then sleep 4 if [ `docker ps | grep nginx| grep Up|wc -l` -eq 0 ]; then echo "结束服务!" exit -1 fi else echo "运行正常!" fi

11、同步时间

[root@localhost ~]# ntpdate ntp1.aliyun.com

12、确保Keepalive使用的网卡开启了多播

[root@localhost ~]# ip a s

如果没有开启,可以使用该命令打开ip link set multicast on dev ens33,ens33是网卡名称

13、起动服务

[root@apple-test02 ~]# systemctl start keepalived

14、 查看状态

[root@apple-test02 keepalived-2.2.7]# systemctl status keepalived

15、查看虚拟ip

[root@apple-test02 ~]# ip add | grep ens33

16、网页访问虚拟IP

四、在17服务服器上安装keepalive 软件

1、安装软件参照18服务器

2、配置文件

[root@apple-test01 keepalived-2.2.7]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived global_defs { notification_email { fhh6666@126.comP } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_BACKUP } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 110 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.10.2.20/16 } }

3、起动服务

[root@apple-test01 keepalived-2.2.7]# systemctl start keepalived

4、查看状态

[root@apple-test01 keepalived-2.2.7]# systemctl status keepalived

五、验证

1、停止18服务器Nginx

[root@apple-test02 keepalived]# docker stop nginx

2、访问虚拟IP

3、重新起动18的Nginx

[root@apple-test02 keepalived]# docker start nginx

4、重新访问虚拟IP

注意:

1、BACKUP 节点不用配置脚本

2、两台服务在同一个网段

3、如果服务不存在通过exit -1 来切换虚拟IP

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值