準備兩臺機器130和132,130做爲master,132做爲backupphp
兩臺機器都執行yum install -y keepalivedhtml
兩臺機器都安裝nginx,其中130上已經編譯安裝過nginx,132上須要yum安裝nginx: yum install -y nginxmysql
設定vip爲100linux
編輯130上keepalived配置文件,內容獲取地址nginx
130編輯監控腳本,內容獲取地址https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/master_keepalived.confgit
給腳本755權限web
systemctl start keepalived 130啓動服務算法
132上編輯配置文件,內容獲取地址sql
132上編輯監控腳本,內容獲取地址apache
給腳本755權限
132上也啓動服務 systemctl start keepalived
1.首先準備兩臺機器,都安裝keepalived
A機器,安裝keepalived [root@xuexi-001 ~]# yum install -y keepalived B機器,安裝keepalived [root@localhost ~]# yum install -y keepalived
2.這裏使用 nginx ,把它做爲一個高可用的對象——>使用nginx做爲演示對象的緣由,由於nginx在工做中,在生產環境中,不少企業把nginx作一個負載均衡器
3.如果A、B機器沒有裝nginx服務,能夠直接 yum安裝
B機器yum安裝nginx [root@localhost ~]# yum install -y epel-release [root@localhost ~]# yum install -y nginx [root@localhost ~]# systemctl start nginx [root@localhost ~]# ps aux | grep nginx root 1410 0.0 0.2 120752 2096 ? Ss 22:26 0:00 nginx: master process /usr/sbin/nginx nginx 1411 0.0 0.3 121136 3124 ? S 22:26 0:00 nginx: worker process root 1413 0.0 0.0 112676 984 pts/0 R+ 22:27 0:00 grep --color=auto nginx
4.更改keepalived配置文件了,內容地址
A機器更改配置文件 [root@xuexi-001 ~]# ls /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf [root@xuexi-001 ~]# > !$ //直接清空文件內容了 > /etc/keepalived/keepalived.conf [root@xuexi-001 ~]# cat /etc/keepalived/keepalived.conf [root@xuexi-001 ~]# vim /etc/keepalived/keepalived.conf //去文件地址去下載內容 將拷貝的內容複製進去 只須要改網卡名字和飄逸IP爲192.168.133.100 -----------------全局配置 ------------------ global_defs { //global_defs 全局配置標識 notification_email { //notification_email用於設置報警郵件地址 xuexi1@163.com //能夠設置多個,每行一個 } notification_email_from xuexi1@163.com //設置郵件發送地址 smtp_server 127.0.0.1 //設置郵件的smtp server地址 smtp_connect_timeout 30 //設置鏈接smtp sever超時時間 router_id LVS_DEVEL } ---------------------------- VRRP配置 --------------------------------------- vrrp_script chk_nginx { script "/usr/local/sbin/check_ng.sh" //檢查服務是否正常,經過寫腳本實現,腳本檢查服務健康狀態 interval 3 //檢查的時間間斷是3秒 } vrrp_instance VI_1 { //VRRP配置標識 VI_1是實例名稱 state MASTER //定義master相關 interface ens33 //經過vrrp協議去通訊、去發廣播。配置時,需注意本身的網卡名稱 virtual_router_id 51 //定義路由器ID ,配置的時候和從機器一致 priority 100 //權重,主角色和從角色的權重是不一樣的 advert_int 1 //設定MASTER與BACKUP主機質檢同步檢查的時間間隔,單位爲秒 authentication { //認證相關信息 auth_type PASS //這裏認證的類型是PASS auth_pass aminglinux>com //密碼的形式是一個字符串 } virtual_ipaddress { //設置虛擬IP地址 (VIP),又叫作漂移IP地址 192.168.5.100 //更改成192.168.5.100 } track_script { //加載腳本 chk_nginx } } 保存退出
5.定義監控腳本,腳本內容獲取地址https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/master_check_ng.sh
A機器定義監控腳本 [root@xuexi-001 ~]# vi /usr/local/sbin/check_ng.sh #!/bin/bash #時間變量,用於記錄日誌 d=`date --date today +%Y%m%d_%H:%M:%S` #計算nginx進程數量 n=`ps -C nginx --no-heading|wc -l` #若是進程爲0,則啓動nginx,而且再次檢測nginx進程數量, #若是還爲0,說明nginx沒法啓動,此時須要關閉keepalived if [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi 保存退出
6.腳本建立完以後,還須要改變腳本的權限(不更改權限,就沒法自動加載腳本,那就沒法啓動keepalived服務)
[root@xuexi-001 ~]# chmod 755 /usr/local/sbin/check_ng.sh
7.啓動keepalived服務,並查看是否啓動成功(PS:啓動不成功,有多是防火牆未關閉或者規則限制致使的)
[root@xuexi-001 ~]# systemctl start keepalived [root@xuexi-001 ~]# ps aux | grep keepalived root 2276 0.0 0.0 118652 1396 ? Ss 23:11 0:00 /usr/sbin/keepalived -D root 2277 0.0 0.1 127516 3304 ? S 23:11 0:00 /usr/sbin/keepalived -D root 2278 0.0 0.1 127456 2840 ? S 23:11 0:00 /usr/sbin/keepalived -D root 2303 0.0 0.0 112720 976 pts/0 R+ 23:11 0:00 grep --color=auto keepalived
8.查看nginx服務進程
[root@xuexi-001 ~]# ps aux |grep nginx root 1035 0.0 0.0 46040 1292 ? Ss 20:14 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 1041 0.0 0.2 48576 4188 ? S 20:14 0:00 nginx: worker process nobody 1042 0.0 0.2 48576 3928 ? S 20:14 0:00 nginx: worker process root 2389 0.0 0.0 112720 968 pts/0 R+ 23:12 0:00 grep --color=auto nginx
9.這時中止nginx服務
[root@xuexi-001 ~]# /etc/init.d/nginx stop Stopping nginx (via systemctl): [ 肯定 ]
10.再來查看nginx服務進程,會看到自動加載了
[root@xuexi-001 ~]# ps aux |grep nginx root 2478 0.0 0.0 46040 1292 ? Ss 23:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 2480 0.0 0.2 48576 4192 ? S 23:12 0:00 nginx: worker process nobody 2481 0.0 0.2 48576 3928 ? S 23:12 0:00 nginx: worker process root 2515 0.0 0.0 112720 972 pts/0 R+ 23:12 0:00 grep --color=auto nginx
11.keepalived日誌文件路徑
12.查看ip地址,使用 ip add 命令,而不能使用ifconfig命令,由於 ifconfig命令 是沒法查看到vip192.168.5.100這個IP的
[root@xuexi-001 ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 qlen 1000 link/ether 00:0c:29:b3:a2:bf brd ff:ff:ff:ff:ff:ff inet 192.168.5.130/24 brd 192.168.5.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.5.100/32 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.5.150/24 brd 192.168.5.255 scope global secondary ens33:0 valid_lft forever preferred_lft forever inet6 fe80::9625:3e1d:12c7:4fe6/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b3:a2:c9 brd ff:ff:ff:ff:ff:ff
13.檢查A、B 機器下防火牆和selinux是否關閉了,若沒有關閉,可能會致使實驗失敗
以上就是主機器A的配置
1.在B機器yum安裝nginx和keepalived
[root@hf-01 ~]# yum install -y epel-release [root@hf-01 ~]# yum install -y nginx
2.關閉B機器的防火牆和selinux
3.自定義B機器keepalived配置文件,內容獲取地址,更改虛擬IP和主同樣的 https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/backup_keepalived.conf
首先清空B機器keepalived裏面自帶的配置文件 [root@localhost ~]# > /etc/keepalived/keepalived.conf 而後複製配置文件並粘貼進去,更改虛擬IP和主同樣的 [root@localhost ~]# vi !$ vi /etc/keepalived/keepalived.conf global_defs { notification_email { aming@aminglinux.com } notification_email_from root@aminglinux.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/usr/local/sbin/check_ng.sh" interval 3 } vrrp_instance VI_1 { state BACKUP //這裏 和master不同的名字 interface ens33 //網卡和當前機器一致,不然沒法啓動keepalived服務 virtual_router_id 51 //和主機器 保持一致 priority 90 //權重,要比主機器小的數值 advert_int 1 authentication { auth_type PASS auth_pass aminglinux>com } virtual_ipaddress { 192.168.5.100 //這裏更改成192.168.5.100 } track_script { chk_nginx } } 保存退出
4.定義監控腳本,路徑再keepalived裏面已定義過,腳本內容地址 https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/backup_check_ng.sh
[root@localhost ~]# vi /usr/local/sbin/check_ng.sh #時間變量,用於記錄日誌 d=`date --date today +%Y%m%d_%H:%M:%S` #計算nginx進程數量 n=`ps -C nginx --no-heading|wc -l` #若是進程爲0,則啓動nginx,而且再次檢測nginx進程數量, #若是還爲0,說明nginx沒法啓動,此時須要關閉keepalived if [ $n -eq "0" ]; then systemctl start nginx n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi 保存退出
5.改動腳本的權限,設置爲755權限
[root@localhost ~]# chmod 755 /usr/local/sbin/check_ng.sh
6.B機器啓動keepalived服務
[root@localhost ~]# systemctl start keepalived [root@localhost ~]# ps aux |grep keepalived root 1499 0.0 0.1 118608 1388 ? Ss 23:27 0:00 /usr/sbin/keepalived -D root 1500 0.0 0.3 120732 3112 ? S 23:27 0:00 /usr/sbin/keepalived -D root 1501 0.0 0.2 120732 2532 ? S 23:27 0:00 /usr/sbin/keepalived -D root 1538 0.0 0.0 112676 984 pts/0 R+ 23:27 0:00 grep --color=auto keepalived
A機器,是源碼包安裝的nginx(PS:這是lnmp配置好的環境虛擬主機內容)
[root@xuexi-001 ~]# cat /usr/local/nginx/conf/vhost/ aaa.com.conf proxy.conf test.com.conf load.conf ssl.conf [root@xuexi-001 ~]# cat /usr/local/nginx/conf/vhost/aaa.com.conf server { listen 80 default_server; server_name aaa.com index index.html index.htm index.php; root /data/wwwroot/default; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/aming.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name; } } [root@xuexi-001 ~]# cat /data/wwwroot/default/index.html master This is the default site.
[root@localhost ~]# vi /usr/share/nginx/html/index.html backup.backup
1.模擬線上生產環境,主機器宕機環境,最簡單直接的方法,就是直接關閉keepalived服務
2.關閉master機器(即A機器)上的keepalived服務關閉
[root@xuexi-001 ~]# systemctl stop keepalived
3.查看A機器上的VIP被已經釋放掉了
[root@xuexi-001 ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 qlen 1000 link/ether 00:0c:29:b3:a2:bf brd ff:ff:ff:ff:ff:ff inet 192.168.5.130/24 brd 192.168.5.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.5.150/24 brd 192.168.5.255 scope global secondary ens33:0 valid_lft forever preferred_lft forever inet6 fe80::9625:3e1d:12c7:4fe6/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b3:a2:c9 brd ff:ff:ff:ff:ff:ff
4.查看backup機器(即B機器)在監聽VIP
[root@localhost ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 qlen 1000 link/ether 00:0c:29:36:a3:32 brd ff:ff:ff:ff:ff:ff inet 192.168.5.133/24 brd 192.168.5.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.5.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::cf14:5db2:1b49:f0c2/64 scope link valid_lft forever preferred_lft forever
5.查看B機器日誌
[root@localhost ~]# tail /var/log/messages Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100
6.瀏覽器訪問vip,會看到已經變成backup機器上的了
7.在master機器(即A機器)啓動keepalived服務,會看到vip這個IP馬上回來了
[root@xuexi-001 ~]# systemctl start keepalived [root@xuexi-001 ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 qlen 1000 link/ether 00:0c:29:b3:a2:bf brd ff:ff:ff:ff:ff:ff inet 192.168.5.130/24 brd 192.168.5.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.5.100/32 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.5.150/24 brd 192.168.5.255 scope global secondary ens33:0 valid_lft forever preferred_lft forever inet6 fe80::9625:3e1d:12c7:4fe6/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b3:a2:c9 brd ff:ff:ff:ff:ff:ff inet6 fe80::44c4:9bed:dd1f:6c01/64 scope link valid_lft forever preferred_lft forever
8.查看B機器日誌變化
[root@localhost ~]# tail /var/log/messages Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 [root@localhost ~]# tail /var/log/messages Sep 3 23:46:52 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:46:57 localhost Keepalived_vrrp[1501]: Sending gratuitous ARP on ens33 for 192.168.5.100 Sep 3 23:50:01 localhost Keepalived_vrrp[1501]: VRRP_Instance(VI_1) Received advert with higher priority 100, ours 90 Sep 3 23:50:01 localhost Keepalived_vrrp[1501]: VRRP_Instance(VI_1) Entering BACKUP STATE Sep 3 23:50:01 localhost Keepalived_vrrp[1501]: VRRP_Instance(VI_1) removing protocol VIPs.
一、 LVS NAT模式
二、 LVS IP Tunnel模式
三、LVS DR模式
[root@xuexi-003 ~]# yum install -y iptables-services [root@xuexi-003 ~]# systemctl start iptables [root@xuexi-003 ~]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. [root@xuexi-003 ~]# iptables -F [root@xuexi-003 ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables: [ 肯定 ]
而後還須要關閉selinux 全部的機器都須要更改
[root@xuexi-003 ~]# getenforce Disabled [root@xuexi-003 ~]# setenforce 0 setenforce: SELinux is disabled [root@xuexi-003 ~]# vi /etc/selinux/config 更改配置文件關閉selinux
一、安裝ipvsadm
[root@xuexi-001 ~]# yum install -y ipvsadm
二、在dir上編寫腳本,vim /usr/local/sbin/lvs_nat.sh //內容以下
配置文件內容 #! /bin/bash # director 服務器上開啓路由轉發功能,不開啓的話沒有辦法把數據傳輸到後面的rs上面 echo 1 > /proc/sys/net/ipv4/ip_forward # 關閉icmp的重定向 echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects # 注意區分網卡名字,兩個網卡分別爲ens33和ens37 echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects # director 設置nat防火牆 iptables -t nat -F #清空iptables 規則 iptables -t nat -X #清空鏈 iptables -t nat -A POSTROUTING -s 192.168.5.0/24 -j MASQUERADE #能夠實現同網段的內網上網 # director設置ipvsadm IPVSADM='/usr/sbin/ipvsadm' $IPVSADM -C #清空規則 $IPVSADM -A -t 192.168.81.144:80 -s wlc -p 3 #設置一個規則 指定使用的什麼規則(大規則) $IPVSADM -a -t 192.168.81.144:80 -r 192.168.5.133:80 -m -w 1 $IPVSADM -a -t 192.168.81.144:80 -r 192.168.5.134:80 -m -w 1 #具體的規則(小規則)
三、沒有輸出內容,說明配置沒有問題
[root@xuexi-001 ~]# sh /usr/local/sbin/lvs_nat.sh [root@xuexi-001 ~]#
四、
#rs1 [root@xuexi-002 ~]# systemctl start nginx [root@xuexi-002 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 547/rpcbind tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1204/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 802/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1047/master tcp6 0 0 :::111 :::* LISTEN 547/rpcbind tcp6 0 0 :::80 :::* LISTEN 1204/nginx: master tcp6 0 0 :::22 :::* LISTEN 802/sshd tcp6 0 0 ::1:25 :::* LISTEN 1047/master tcp6 0 0 :::3306 :::* LISTEN 1045/mysqld
#rs2 [root@xuexi-003 ~]# systemctl start nginx [root@xuexi-003 ~]# ps aux |grep nginx root 1200 0.0 0.2 120752 2088 ? Ss 23:39 0:00 nginx: master process /usr/sbin/nginx nginx 1201 0.0 0.3 121136 3116 ? S 23:39 0:00 nginx: worker process root 1204 0.0 0.0 112676 980 pts/0 R+ 23:39 0:00 grep --color=auto nginx [root@xuexi-003 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 519/rpcbind tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1200/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 809/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1057/master tcp6 0 0 :::111 :::* LISTEN 519/rpcbind tcp6 0 0 :::80 :::* LISTEN 1200/nginx: master tcp6 0 0 :::22 :::* LISTEN 809/sshd tcp6 0 0 ::1:25 :::* LISTEN 1057/master tcp6 0 0 :::3306 :::* LISTEN 1015/mysqld
#rs1 [root@xuexi-002 ~]# curl localhost backup.backup [root@xuexi-002 ~]# vi /usr/share/nginx/html/index.html xuexi-002 192.168.5.133
#rs2 [root@xuexi-003 ~]# curl localhost backup.backup [root@xuexi-003 ~]# vi /usr/share/nginx/html/index.html xuexi-003 192.168.5.134
能夠在dir上更改一下規則,編輯配置文件vim /usr/local/sbin/lvs_nat.sh 將$IPVSADM -A -t 192.168.81.144:80 -s wlc -p 3 後面的-p 3 去掉
查看ipvsadm 設置的規則
[root@xuexi-001 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.81.144:80 wlc -> 192.168.5.133:80 Masq 1 2 0 -> 192.168.5.134:80 Masq 1 0 4
[root@xuexi-001 ~]# curl 192.168.81.144 xuexi-003 192.168.5.134 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-003 192.168.5.134 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-002 192.168.5.133 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-003 192.168.5.134 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-002 192.168.5.133 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-003 192.168.5.134 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-002 192.168.5.133 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-003 192.168.5.134 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-002 192.168.5.133 [root@xuexi-001 ~]# curl 192.168.81.144 xuexi-003 192.168.5.134
測試結果
(dir)分發器上的配置
[root@xuexi-001 ~]# vi /usr/local/sbin/lvs_dr.sh #! /bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward ipv=/usr/sbin/ipvsadm vip=192.168.5.200 rs1=192.168.5.133 rs2=192.168.5.134 ifdown ens33 ifup ens33 #注意這裏的網卡名字 ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev ens33:2 $ipv -C $ipv -A -t $vip:80 -s wrr $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1 [root@xuexi-001 ~]# sh /usr/local/sbin/lvs_dr.sh 成功斷開設備 'ens33'。 鏈接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/3)
rs1
[root@xuexi-002 ~]# vi /usr/local/sbin/lvs_rs.sh #/bin/bash vip=192.168.5.200 #把vip綁定在lo上,是爲了實現rs直接把結果返回給客戶端 ifdown lo ifup lo ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip lo:0 #如下操做爲更改arp內核參數,目的是爲了讓rs順利發送mac地址給客戶端 #參考文檔www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
rs2
[root@xuexi-003 ~]# vi /usr/local/sbin/lvs_rs.sh #/bin/bash vip=192.168.5.200 #把vip綁定在lo上,是爲了實現rs直接把結果返回給客戶端 ifdown lo ifup lo ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip lo:0 #如下操做爲更改arp內核參數,目的是爲了讓rs順利發送mac地址給客戶端 #參考文檔www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
1.準備三臺機器,分別爲
2.在dir分發器(A機器)上,清空ipvsadm規則,並查看ipvsadm規則,會發現已經清空
[root@xuexi-001 ~]# ipvsadm -C [root@xuexi-001 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
3.在分發器(即A機器)上編輯配置文件,在/etc/keepalived/keepalived.conf 配置,配置文件內容
[root@xuexi-001 ~]# vim /etc/keepalived/keepalived.conf vrrp_instance VI_1 { #備用服務器上爲 BACKUP state MASTER #綁定vip的網卡爲ens33,你的網卡和阿銘的可能不同,這裏須要你改一下 interface ens33 virtual_router_id 51 #備用服務器上爲90 priority 100 advert_int 1 authentication { auth_type PASS auth_pass aminglinux } virtual_ipaddress { 192.168.5.200 } } virtual_server 192.168.5.200 80 { #(每隔10秒查詢realserver狀態) delay_loop 10 #(lvs 算法) lb_algo wlc #(DR模式) lb_kind DR #(同一IP的鏈接60秒內被分配到同一臺realserver) persistence_timeout 60 #(用TCP協議檢查realserver狀態) protocol TCP real_server 192.168.5.133 80 { #(權重) weight 100 TCP_CHECK { #(10秒無響應超時) connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.5.134 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } :wq
4.啓動keepalived服務,查看keepalived服務
[root@xuexi-001 ~]# systemctl start keepalived [root@xuexi-001 ~]# ps aux |grep keepalived root 1678 0.0 0.0 118652 1396 ? Ss 07:44 0:00 /usr/sbin/keepalived -D root 1679 0.0 0.1 127520 3332 ? S 07:44 0:00 /usr/sbin/keepalived -D root 1680 0.0 0.1 127388 2616 ? S 07:44 0:00 /usr/sbin/keepalived -D root 1685 0.0 0.0 112720 976 pts/0 S+ 07:44 0:00 grep --color=auto keepalived
5.查看IP,會看到虛擬IP依然存在
[root@xuexi-001 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 qlen 1000 link/ether 00:0c:29:b3:a2:bf brd ff:ff:ff:ff:ff:ff inet 192.168.5.130/24 brd 192.168.5.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.5.200/32 brd 192.168.5.200 scope global ens33:2 # 虛擬IP 依舊存在 valid_lft forever preferred_lft forever inet 192.168.5.150/24 brd 192.168.5.255 scope global secondary ens33:0 valid_lft forever preferred_lft forever inet6 fe80::9625:3e1d:12c7:4fe6/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b3:a2:c9 brd ff:ff:ff:ff:ff:ff inet 192.168.81.144/24 brd 192.168.81.255 scope global ens37 valid_lft forever preferred_lft forever inet6 fe80::44c4:9bed:dd1f:6c01/64 scope link valid_lft forever preferred_lft forever
6.查看ipvsadm規則
[root@xuexi-001 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.5.200:80 wlc
7.這時關閉keepalived服務,再來查看ip,會看到虛擬IP停掉了
[root@xuexi-001 ~]# systemctl stop keepalived [root@xuexi-001 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 qlen 1000 link/ether 00:0c:29:b3:a2:bf brd ff:ff:ff:ff:ff:ff inet 192.168.5.130/24 brd 192.168.5.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.5.150/24 brd 192.168.5.255 scope global secondary ens33:0 valid_lft forever preferred_lft forever inet6 fe80::9625:3e1d:12c7:4fe6/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b3:a2:c9 brd ff:ff:ff:ff:ff:ff inet 192.168.81.144/24 brd 192.168.81.255 scope global ens37 valid_lft forever preferred_lft forever inet6 fe80::44c4:9bed:dd1f:6c01/64 scope link valid_lft forever preferred_lft forever
8.再來查看規則,會發現沒有啓動規則
[root@xuexi-001 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
9.這時啓動keepalived,再來查看規則
[root@xuexi-001 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.5.200:80 wlc -> 192.168.5.133:80 Route 100 2 0 -> 192.168.5.134:80 Route 100 1 1
10.注意事項:兩點
echo 1 > /proc/sys/net/ipv4/ip_forward //打開端口轉發
#把vip綁定在lo上,是爲了實現rs直接把結果返回給客戶端 ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip lo:0 #如下操做爲更改arp內核參數,目的是爲了讓rs順利發送mac地址給客戶端 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce