8.1 Keepalived+Nginx 高可用集群(主從模式)
image.png
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.17.129
smtp_connect_timeout 30
router_id LVS_DEVEL }
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(檢測(cè)腳本執(zhí)行的間隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 備份服務(wù)器上將 MASTER 改為 BACKUP
interface ens33 //網(wǎng)卡
virtual_router_id 51 # 主湃望、備機(jī)的 virtual_router_id 必須相同
priority 100 # 主宇驾、備機(jī)取不同的優(yōu)先級(jí)汗贫,主機(jī)值較大,備份機(jī)值較小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.17.50 // VRRP H 虛擬地址
} }
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
(1)在所有節(jié)點(diǎn)上面進(jìn)行配置
# systemctl stop firewalld //關(guān)閉防火墻
# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux //關(guān)閉 selinux讯壶,重啟生效
# setenforce 0 //關(guān)閉 selinux,臨時(shí)生效
# ntpdate 0.centos.pool.ntp.org //時(shí)間同步
# yum install nginx -y //安裝 nginx
(2)配置后端 web 服務(wù)器(兩臺(tái)一樣)
# echo "`hostname` `ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'`" >
/usr/share/nginx/html/index.html
//準(zhǔn)備測(cè)試文件末盔,此處是將主機(jī)名和 ip 寫(xiě)到 index.html 頁(yè)面中
# 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.mtian.org;
location / {
root /usr/share/nginx/html;
}
access_log /var/log/nginx/access.log main;
}
}
# systemctl start nginx //啟動(dòng) nginx
# systemctl enable nginx //加入開(kāi)機(jī)啟動(dòng)
(3)配置 LB 服務(wù)器(兩臺(tái)都一樣)
# 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 192.168.1.33:80 weight=1 max_fails=3 fail_timeout=20s;
server 192.168.1.34:80 weight=1 max_fails=3 fail_timeout=20s;
}
server {
listen 80;
server_name www.mtian.org;
location / {
proxy_pass http://backend;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
# systemctl start nginx //啟動(dòng) nginx
# systemctl enable nginx //加入開(kāi)機(jī)自啟動(dòng)
(4)在測(cè)試機(jī)(192.168.1.35)上面添加 host 解析薛训,并測(cè)試 lb 集群是否正常。(測(cè)試機(jī)任意都可以辈毯,只要能訪問(wèn) lb 節(jié)點(diǎn)坝疼。)
[root@node01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.32 www.mtian.org
192.168.1.31 www.mtian.org
// 測(cè)試時(shí)候輪流關(guān)閉 lb1 和 lb2 節(jié)點(diǎn),關(guān)閉后還是能夠訪問(wèn)并看到輪循效果即表示 nginx lb 集群搭建
成功谆沃。
[root@node01 ~]# curl www.mtian.org
web01 192.168.1.33
[root@node01 ~]# curl www.mtian.org
web02 192.168.1.34
[root@node01 ~]# curl www.mtian.org
web01 192.168.1.33
[root@node01 ~]# curl www.mtian.org
web02 192.168.1.34
[root@node01 ~]# curl www.mtian.org
web01 192.168.1.33
[root@node01 ~]# curl www.mtian.org
web02 192.168.1.34
(5)上面步驟成功后钝凶,開(kāi)始搭建 keepalived,在兩臺(tái) lb 節(jié)點(diǎn)上面安裝 keepalived(也可以源碼編譯安
裝唁影、此處直接使用 yum 安裝)
# yum install keepalived -y
(6)配置 LB-01 節(jié)點(diǎn)
[root@LB-01 ~]# 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 MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.110/24 dev ens33 label ens33:1
}
}
[root@LB-01 ~]# systemctl start keepalived //啟動(dòng) keepalived
[root@LB-01 ~]# systemctl enable keepalived //加入開(kāi)機(jī)自啟動(dòng)
[root@LB-01 ~]# ip a //查看 IP耕陷,會(huì)發(fā)現(xiàn)多出了 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 192.168.1.31/24 brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.1.110/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
......
(7)配置 LB-02 節(jié)點(diǎn)
[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 {
192.168.1.110/24 dev ens33 label ens33:1
}
}
[root@LB-02 ~]# systemctl start keepalived //啟動(dòng) keepalived
[root@LB-02 ~]# systemctl enable keepalived //加入開(kāi)機(jī)自啟動(dòng)
[root@LB-02 ~]# ifconfig //查看 IP,此時(shí)備節(jié)點(diǎn)不會(huì)有 VIP(只有當(dāng)主掛了的時(shí)候据沈,VIP 才會(huì)飄到備節(jié)點(diǎn))
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.32 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:feab:6532 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ab:65:32 txqueuelen 1000 (Ethernet)
RX packets 43752 bytes 17739987 (16.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4177 bytes 415805 (406.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
......
(8)在測(cè)試機(jī)器上面訪問(wèn) Keepalived 上面配置的 VIP 192.168.1.110
[root@node01 ~]# curl 192.168.1.110
web01 192.168.1.33
[root@node01 ~]# curl 192.168.1.110
web02 192.168.1.34
[root@node01 ~]# curl 192.168.1.110
web01 192.168.1.33
[root@node01 ~]# curl 192.168.1.110
web02 192.168.1.34
//關(guān)閉 LB-01 節(jié)點(diǎn)上面 keepalived 主節(jié)點(diǎn)哟沫。再次訪問(wèn)
[root@LB-01 ~]# systemctl stop keepalived
[root@node01 ~]#
[root@node01 ~]# curl 192.168.1.110
web01 192.168.1.33
[root@node01 ~]# curl 192.168.1.110
web02 192.168.1.34
[root@node01 ~]# curl 192.168.1.110
web01 192.168.1.33
[root@node01 ~]# curl 192.168.1.110
web02 192.168.1.34
//此時(shí)查看 LB-01 主節(jié)點(diǎn)上面的 IP ,發(fā)現(xiàn)已經(jīng)沒(méi)有了 VIP
[root@LB-01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.31 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe94:1744 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:94:17:44 txqueuelen 1000 (Ethernet)
RX packets 46813 bytes 18033403 (17.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9350 bytes 1040882 (1016.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
...
//查看 LB-02 備節(jié)點(diǎn)上面的 IP锌介,發(fā)現(xiàn) VIP 已經(jīng)成功飄過(guò)來(lái)了
[root@LB-02 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.32 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:feab:6532 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ab:65:32 txqueuelen 1000 (Ethernet)
RX packets 44023 bytes 17760070 (16.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4333 bytes 430037 (419.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.110 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:ab:65:32 txqueuelen 1000 (Ethernet)
...
到此嗜诀,Keepalived+Nginx 高可用集群就搭建完成了。
8.2 Keepalived+Nginx 高可用集群(雙主模式)
image.png
說(shuō)明:還是按照上面的環(huán)境繼續(xù)做實(shí)驗(yàn)孔祸,只是修改 LB 節(jié)點(diǎn)上面的 keepalived 服務(wù)的配置文件即可隆敢。此時(shí)
LB-01 節(jié)點(diǎn)即為 Keepalived 的主節(jié)點(diǎn)也為備節(jié)點(diǎn),LB-02 節(jié)點(diǎn)同樣即為 Keepalived 的主節(jié)點(diǎn)也為備節(jié)點(diǎn)融击。
LB-01 節(jié)點(diǎn)默認(rèn)的主節(jié)點(diǎn) VIP(192.168.1.110)筑公,LB-02 節(jié)點(diǎn)默認(rèn)的主節(jié)點(diǎn) VIP(192.168.1.210) (
1)配置 LB-01 節(jié)點(diǎn)
[root@LB-01 ~]# vim /etc/keepalived/keepalived.conf //編輯配置文件,增加一段新的
vrrp_instance 規(guī)則
! 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 MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.110/24 dev ens33 label ens33:1
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.1.210/24 dev ens33 label ens33:2
}
}
[root@LB-01 ~]# systemctl restart keepalived //重新啟動(dòng) keepalived
// 查看 LB-01 節(jié)點(diǎn)的 IP 地址尊浪,發(fā)現(xiàn) VIP(192.168.1.110)同樣還是默認(rèn)在該節(jié)點(diǎn)
[root@LB-01 ~]# ip a
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 192.168.1.31/24 brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.1.110/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
(2)配置 LB-02 節(jié)點(diǎn)
[root@LB-02 ~]# vim /etc/keepalived/keepalived.conf //編輯配置文件匣屡,增加一段新的
vrrp_instance 規(guī)則
! 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 {
192.168.1.110/24 dev ens33 label ens33:1
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.1.210/24 dev ens33 label ens33:2
}
}
[root@LB-02 ~]# systemctl restart keepalived //重新啟動(dòng) keepalived
// 查看 LB-02 節(jié)點(diǎn) IP,會(huì)發(fā)現(xiàn)也多了一個(gè) VIP(192.168.1.210)拇涤,此時(shí)該節(jié)點(diǎn)也就是一個(gè)主了捣作。
[root@LB-02 ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ab:65:32 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.32/24 brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.1.210/24 scope global secondary ens33:2
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feab:6532/64 scope link
valid_lft forever preferred_lft forever
(3)測(cè)試
[root@node01 ~]# curl 192.168.1.110
web01 192.168.1.33
[root@node01 ~]# curl 192.168.1.110
web02 192.168.1.34
[root@node01 ~]# curl 192.168.1.210
web01 192.168.1.33
[root@node01 ~]# curl 192.168.1.210
web02 192.168.1.34
// 停止 LB-01 節(jié)點(diǎn)的 keepalived 再次測(cè)試
[root@LB-01 ~]# systemctl stop keepalived
[root@node01 ~]# curl 192.168.1.110
web01 192.168.1.33
[root@node01 ~]# curl 192.168.1.110
web02 192.168.1.34
[root@node01 ~]# curl 192.168.1.210
web01 192.168.1.33
[root@node01 ~]# curl 192.168.1.210
web02 192.168.1.34
測(cè)試可以發(fā)現(xiàn)我們?cè)L問(wèn) keepalived 中配置的兩個(gè) VIP 都可以正常調(diào)度等,當(dāng)我們停止任意一臺(tái) keepalived節(jié)點(diǎn)鹅士,同樣還是正常訪問(wèn)券躁;到此,keepalived+nginx 高可用集群(雙主模式)就搭建完成了。