企业实战_21_MyCat_keepalived 安装配置验证
生活随笔
收集整理的這篇文章主要介紹了
企业实战_21_MyCat_keepalived 安装配置验证
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
接上一篇:企業實戰_20_MyCat使用HAPpoxy對Mycat負載均衡
https://gblfy.blog.csdn.net/article/details/100087884
| mycat | 192.168.43.32 | MYCAT MYSQL,ZK,Haproxy,Keepalived |
| node1 | 192.168.43.104 | MYSQL,ZK |
| node2 | 192.168.43.217 | MYSQL,ZK |
| node3 | 192.168.43.172 | MYSQL,MYCAT ,Haproxy,Keepalived |
文章目錄
- 一、mycat01 節點
- 1. 在線安裝keepalived
- 2. 獲取網卡名
- 3. 配置 keepalived.conf
- 4. 編寫監控腳本
- 5. 腳本賦予權限
- 6. 運行腳本
- 7. 查看腳本狀態
- 8. 查看虛擬vip
- 二、mycat02 節點
- 2.1. 在線安裝keepalived
- 2. 獲取網卡名
- 2.3. 配置 keepalived.conf
- 2.4. 編寫監控腳本
- 2.5. 腳本賦予權限
- 2.6. 運行腳本
- 2.7. 查看腳本狀態
- 2.8. 查看虛擬vip
- 三、vip遷移模擬
- 3.1. 驗證思路
- 3.2. keepalived 主節點服務停止掉
- 3.3. keepalived 從節點驗證
- 3.4. 重啟啟動主節點keepalived
- 3.5. 登錄從節點
- 3.6. 結論
一、mycat01 節點
1. 在線安裝keepalived
#安裝依賴包yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel#yum安裝軟件yum install -y keepalived2. 獲取網卡名
# 獲取網卡名,等會用于綁定 ip addr3. 配置 keepalived.conf
#進入/etc/keepalived/cd /etc/keepalived/#編輯keepalived.confvim keepalived.conf ! Configuration Fileforkeepalived vrrp_script chk_http_port {script"/etc/keepalived/check_haproxy.sh"interval 2weight 2 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 150 advert_int 1 authentication {auth_type PASSauth_pass 1111 } track_script {chk_http_port } virtual_ipaddress { 192.168.92.127 dev ens33 scope global} }說明:虛擬vip怎樣獲取的?
自己虛擬機玩耍:自己隨機找一個與本機ip段一致的ip即可,
在公司的話:需要發郵件申請
4. 編寫監控腳本
#創建check_haproxy.sh vim /etc/keepalived/check_haproxy.sh#添加內容 #!/bin/bash STARTHAPROXY="/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg" #STOPKEEPALIVED="/etc/init.d/keepalived stop" STOPKEEPALIVED="/usr/bin/systemctl stop keepalived" LOGFILE="/var/log/keepalived-haproxy-state.log" echo "[check_haproxy status]" >> $LOGFILE A=`ps -C haproxy --no-header |wc -l` echo "[check_haproxy status]" >> $LOGFILE date >> $LOGFILE if [ $A -eq 0 ];thenecho $STARTHAPROXY >> $LOGFILE$STARTHAPROXY >> $LOGFILE 2>&1sleep 5 fi if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];thenexit 0 elseexit 1 fi5. 腳本賦予權限
#給這個腳本賦予可執行權限 chmod a+x /etc/keepalived/check_haproxy.sh6. 運行腳本
# 啟動 keepalived systemctl start keepalived7. 查看腳本狀態
# 查看 keepalived 狀態 systemctl status keepalived8. 查看虛擬vip
ip addr二、mycat02 節點
2.1. 在線安裝keepalived
#安裝依賴包yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel#yum安裝軟件yum install -y keepalived2. 獲取網卡名
# 獲取網卡名,等會用于綁定 ip addr2.3. 配置 keepalived.conf
#進入/etc/keepalived/cd /etc/keepalived/#編輯keepalived.conf,把下面的配置覆蓋掉默認的vim keepalived.conf ! Configuration Fileforkeepalived vrrp_script chk_http_port {script"/etc/keepalived/check_haproxy.sh"interval 2weight 2 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 120 advert_int 1 authentication {auth_type PASSauth_pass 1111 } track_script {chk_http_port } virtual_ipaddress { 192.168.92.127 dev ens33 scope global} }2.4. 編寫監控腳本
#創建check_haproxy.sh vim /etc/keepalived/check_haproxy.sh#添加內容 #!/bin/bash STARTHAPROXY="/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg" #STOPKEEPALIVED="/etc/init.d/keepalived stop" STOPKEEPALIVED="/usr/bin/systemctl stop keepalived" LOGFILE="/var/log/keepalived-haproxy-state.log" echo "[check_haproxy status]" >> $LOGFILE A=`ps -C haproxy --no-header |wc -l` echo "[check_haproxy status]" >> $LOGFILE date >> $LOGFILE if [ $A -eq 0 ];thenecho $STARTHAPROXY >> $LOGFILE$STARTHAPROXY >> $LOGFILE 2>&1sleep 5 fi if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];thenexit 0 elseexit 1 fi2.5. 腳本賦予權限
#給這個腳本賦予可執行權限 chmod a+x /etc/keepalived/check_haproxy.sh2.6. 運行腳本
# 啟動 keepalived systemctl start keepalived2.7. 查看腳本狀態
# 查看 keepalived 狀態 systemctl status keepalived2.8. 查看虛擬vip
ip addr發現是沒有虛擬vip,對吧!
說明:
由于我們在keepalived 主節點MASTER配置的權重是150,在keepalived 從節點BACKUP配置的權重是120,因此虛擬vip默認會在權重高的機器上。
什么場景下虛擬vip會遷移到keepalived 從節點BACKUP呢?
當keepalived 主節點宕機之后,虛擬vip就會自動遷移到keepalived的slave節點。
三、vip遷移模擬
3.1. 驗證思路
把帶有虛擬ip的haproxy停止掉,虛擬ip,keepalived就會把虛擬ip轉移到另一臺haproxy機器上,當停止掉的haproxy重啟啟動后,虛擬ip就會轉移回來,因為設置了權重。3.2. keepalived 主節點服務停止掉
[root@node1 ~]# systemctl stop keepalived [root@node1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::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 1000link/ether 00:0c:29:72:fe:1f brd ff:ff:ff:ff:ff:ffinet 192.168.92.101/24 brd 192.168.92.255 scope global noprefixroute ens33valid_lft forever preferred_lft foreverinet6 fe80::e596:6d50:ebed:be08/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft foreverinet6 fe80::301b:b1b8:4c05:8828/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft foreverinet6 fe80::dec4:912f:fd7f:bbd5/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever [root@node1 ~]#
發現虛擬vip不見了。
3.3. keepalived 從節點驗證
ip addr
發現虛擬vip啟動遷移到了從節點機器上
3.4. 重啟啟動主節點keepalived
[root@node1 ~]# systemctl start keepalived You have new mail in /var/spool/mail/root [root@node1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::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 1000link/ether 00:0c:29:72:fe:1f brd ff:ff:ff:ff:ff:ffinet 192.168.92.101/24 brd 192.168.92.255 scope global noprefixroute ens33valid_lft forever preferred_lft foreverinet 192.168.92.127/32 scope global ens33valid_lft forever preferred_lft foreverinet6 fe80::e596:6d50:ebed:be08/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft foreverinet6 fe80::301b:b1b8:4c05:8828/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft foreverinet6 fe80::dec4:912f:fd7f:bbd5/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever [root@node1 ~]#3.5. 登錄從節點
ip addr
發現虛擬vip自動又遷移了回去
3.6. 結論
由于主節點的keepalived 權重比從節點的keepalived高,因此,當主從服務都正常運行的場景,虛擬vip會在主節點上;當主節點宕機后,虛擬vip會自動遷移到從節點上。
下一篇:企業實戰_22_MyCatSQL攔截
https://gblfy.blog.csdn.net/article/details/100073474
總結
以上是生活随笔為你收集整理的企业实战_21_MyCat_keepalived 安装配置验证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库系统 - 范式
- 下一篇: Spring Tool Suite 4