自学内容网 自学内容网

配置nginx+keepalived高可用代理数据库ip端口

需求:配置nginx+keepalived高可用反向代理数据库ip端口(数据库服务器无法增加新SCAN IP或者需要隐藏数据库IP的情况下适用)

环境准备:
两台centos7.9
本机ip为:192.168.20.10和192.168.20.11
浮动IP为:192.168.20.20
主要思路:

    1. 安装好操作系统,禁用selinux,firewalld
    1. 安装nginx和keepalive软件
    1. 配置conf文件
    1. 测试ip漂移
[root@node1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@node1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:12:61:b7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.20.10/24 brd 192.168.20.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::5900:8f03:c42:7d7a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

以下内容两个节点都操作:
关闭防火墙等:

[root@node1 ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled

systemctl stop firewalld
systemctl disable firewalld

安装依赖包和nginx和keepalived

[root@node1 ~]#
yum update -y
yum -y install epel-release
yum install gcc pcre-devel zlib-devel openssl-devel net-tools sysstat
yum install nginx nginx-mod-stream keepalived -y

配置keepalived.conf

[root@node1 ~]# cat /etc/keepalived/keepalived.conf 
global_defs {
   router_id LVS_DEVEL
   script_user root
   enable_script_security
}

vrrp_script chk_db_port ##该脚本必须放在vrrp的前面,有顺序问题
{
    script "/usr/local/src/nginx_check.sh"
#    script "</dev/tcp/127.0.0.1/3306" ###mysql
#    script "</dev/tcp/127.0.0.1/1521" ###oracle

    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.20.20/24
    }

    track_script {
   chk_db_port
    }

}

#vrrp_script chk_db_port ##该脚本必须放在vrrp的前面,有顺序问题:观察/var/log/messages是否有报错Keepalived_vrrp[2245]:     chk_db_port no match, ignoring...

#{
#   script "/usr/local/src/nginx_check.sh"
#    script "</dev/tcp/127.0.0.1/3306"
#    interval 2
#    weight -20
#}

[root@node1 ~]# cat /usr/local/src/nginx_check.sh
#!/bin/bash
CC=`ps -C nginx --no-header | wc -l`
if [ $CC -eq 0 ]; then
    exit 1
else
   exit 0
fi

[root@node1 ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;

}
stream {
    upstream oracle_backend {
        server 192.168.207.143:3306;
#如果是oracle,则server 192.168.207.143:1521;
    }

    server {
        listen 3306;
        proxy_pass oracle_backend;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}

启停服务:

systemctl restart keepalived
systemctl restart nginx
systemctl stop nginx
systemctl start nginx

测试漂移IP:
1.任意节点停nginx:浮动ip会自动漂移到另外节点。

systemctl stop nginx
ip a

[root@node2 ~]# netstat -tunlp|grep LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1078/master         
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      7037/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      906/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1078/master         
tcp6       0      0 :::22                   :::*                    LISTEN      906/sshd            
[root@node2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:be:8b:19 brd ff:ff:ff:ff:ff:ff
    inet 192.168.20.11/24 brd 192.168.20.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.20.20/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::5900:8f03:c42:7d7a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::3a0a:e37:990:304a/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever

复制

2.任意节点关机或重启系统,浮动ip也会自动漂移到另外节点。

观察日志:

tail -f /var/log/messages


相关参考:
Keepalived +VIP(虚拟IP) 漂移 部署系统高可用 离线部署|故障转移_内网vip+keepalived-CSDN博客


原文地址:https://blog.csdn.net/jycjyc/article/details/135837239

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!