Keepalived+Nginx 高可用集群(主从模式)

一、架构图

二、环境

hostnameIP地址说明
keepalived-master10.168.1.20keepalived主服务器(Nginx主负载)
keepalived-backup10.168.1.21keepalived备服务器(Nginx备负载)
nginx110.168.1.160web服务器1
nginx210.168.1.161web服务器2

三、配置

1、在所有节点关闭防火墙和selinux以及设置时间同步

# systemctl stop firewalld         //关闭防火墙
# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux        //关闭selinux,重启生效
# setenforce 0                //关闭selinux,临时生效
# ntpdate 0.centos.pool.ntp.org    //时间同步
# yum install nginx -y           //安装nginx

2、配置后端nginx的web服务器(nginx1和nginx2配置)nginx安装不在多说

# echo "`hostname` `ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'`" > /usr/share/nginx/html/index.html        //准备测试文件,此处是将主机名和ip写到index.html页面中

WEB服务器配置

3、nginx配置文件如下(nginx1和nginx2设置一致即可)

# vim /etc/nginx/nginx.conf        //编辑配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  www.cnbugs.com;
        location / {
            root         /usr/share/nginx/html;
        }
    access_log    /var/log/nginx/access.log main;
    }
}
# systemctl start nginx    //启动nginx 
# systemctl enable nginx    //加入开机启动 

nginx负载均衡配置

4、配置keepalived服务器(配置nginx负载均衡)

# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    upstream backend {
    server 10.168.1.160:80 weight=1 max_fails=3 fail_timeout=20s;
    server 10.168.1.161:80 weight=1 max_fails=3 fail_timeout=20s;
    }
    server {
        listen       80;
        server_name  www.cnbugs.com;
        location / {
        proxy_pass http://backend;
        proxy_set_header Host $host:$proxy_port;
        proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}
# systemctl start nginx     //启动nginx      
# systemctl enable nginx    //加入开机自启动

在其他服务器测试,添加host解析

[root@node01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.168.1.160    www.cnbugs.com
10.168.1.161   www.cnbugs.com

尝试关闭节点进行测试

// 测试时候轮流关闭lb1 和 lb2 节点,关闭后还是能够访问并看到轮循效果即表示 nginx lb集群搭建成功。
[root@node01 ~]# curl www.cnbugs.com
web01 10.168.1.160
[root@node01 ~]# curl www.cnbugs.com
web02 10.168.1.161  

搭建keepalived

安装

# yum install keepalived -y

配置keepalived1节点

[root@LB-01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
    root@cnbugs.com
   }
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    10.168.1.222 dev ens33 label ens33:1
    }
}
[root@LB-01 ~]# systemctl start keepalived     //启动keepalived
[root@LB-01 ~]# systemctl enable keepalived    //加入开机自启动

查看IP

[root@LB-01 ~]# ip a    //查看IP,会发现多出了VIP 10.168.1.222
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:94:17:44 brd ff:ff:ff:ff:ff:ff
    inet 10.168.1.20/24 brd 10.168.1.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 10.168.1.222/24 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe94:1744/64 scope link 
       valid_lft forever preferred_lft forever
......

配置keepalived2节点

[root@LB-02 ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
    381347268@qq.com
   }
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    10.168.1.222/24 dev ens33 label ens33:1
    }
}
[root@LB-02 ~]# systemctl start keepalived        //启动keepalived
[root@LB-02 ~]# systemctl enable keepalived    //加入开机自启动

查看IP

[root@LB-01 ~]# ip a    //查看IP,会发现多出了VIP 192.168.1.110
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:94:17:44 brd ff:ff:ff:ff:ff:ff
    inet 10.168.1.21/24 brd 192.168.1.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 10.168.1.222/24 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe94:1744/64 scope link 
       valid_lft forever preferred_lft forever
......

访问VIP(10.168.1.222)

[root@node01 ~]# curl 10.168.1.222
web01 10.168.1.160
[root@node01 ~]# curl 10.168.1.222
web02 10.168.1.161  

关闭主节点再次访问

//关闭LB-01 节点上面keepalived主节点。再次访问
[root@LB-01 ~]# systemctl stop keepalived
[root@node01 ~]# 
[root@node01 ~]# curl 10.168.1.222
web01 10.168.1.160

再次查看keepalived1节点发现虚拟IP没有了,查看keepalived2节点的虚拟IP发现已经漂移过来了

keepalived高可用集群主从搭建完成

文档参考: https://www.cnblogs.com/yanjieli/p/10682064.html 感谢别来无恙大佬

发表评论

后才能评论