【Mysql】keepalive的安裝與配置

小亮520cl發表於2016-07-01
參考文件:
http://blog.jobbole.com/100390/
http://lanlian.blog.51cto.com/6790106/1303195


環境:主庫:192.168.6.119
         備庫:192.168.6.121
         vip:  192.168.6.120


master與slave端都安裝keepalive
  1. [root@MYSQL src]# wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
  2. [root@MYSQL src]# tar -xf keepalived-1.2.12.tar.gz 
  3. [root@MYSQL src]# cd keepalived-1.2.12
  4. [root@MYSQL src]# yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bison libaio-devel cmake libnl* libpopt* popt-static openssl-devel
  5. [root@MYSQL keepalived-1.2.12]# ./configure
  6. [root@MYSQL keepalived-1.2.12]# make && make install
  7. [root@MYSQL src]#mkdir /etc/keepalived/
  8.  
  9. [root@MYSQL src]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
  10. [root@MYSQL src]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
  11. [root@MYSQL src]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
  12. [root@MYSQL src]#cp /usr/local/sbin/keepalived /usr/sbin/

2. 配置 keepalived
  1. [root@MYSQL keepalived]# vi keepalived.conf 
  2. ! Configuration File for keepalived
  3.  
  4. global_defs {
  5.    notification_email {
  6.      acassen@firewall.loc
  7.      failover@firewall.loc
  8.      sysadmin@firewall.loc
  9.    }
  10.    notification_email_from Alexandre.Cassen@firewall.loc
  11.    smtp_server 192.168.200.1
  12.    smtp_connect_timeout 30
  13.    router_id LVS_DEVEL ##配置是為了標識當前節點,兩個節點的此項設定可相同,也可不相同
  14. }

  1. vrrp_script chk_http_port {            ---配合指令碼使用,比如說檢查nginx是否存活,檢查mysql是否存活等 
       script "/usr/local/keepalived/nginx.sh" #在這裡新增指令碼連結 
       interval 3       #指令碼執行間隔 
       weight -2         #指令碼結果導致的優先順序變更  ###如果檢測返回值不為真weight -2 表示減2,權重值降低,backup server權重值(99)>現Master(100-2)的,切換
    }

  1. vrrp_instance VI_1 {
  2.     state MASTER                   #指定A節點為主節點 備用節點上設定為BACKUP即可
  3.     interface eth0
  4.     virtual_router_id 51              #VRRP組名,兩個節點的設定必須一樣,以指明各個節點屬於同一VRRP
  5.     priority 100              #主節點的優先順序(1-254之間),備用節點必須比主節點優先順序低 
  6.    ###nopreempt                     ###這個引數也可以防止切換到從庫後,主keepalived恢復後自動切換回主庫
  7.          advert_int 1
  8.     authentication {             #設定驗證資訊,兩個節點必須一致
  9.         auth_type PASS 
  10.         auth_pass 1111
  11.     }

 track_script {
        chk_http_port     #新增指令碼執行
    }
  1. virtual_ipaddress {
  2. 192.168.6.120                   =====vip,指定一個或多個vip
  3.     }
  4. }


上面的指令碼簡單的檢視http程式是否存在,不存在就退出返回假。

點選(此處)摺疊或開啟

  1. #!/bin/bash
    A=`netstat -nputl|grep -i 80 |wc -l`
    if [ $A -eq 0 ];then
       exit 1;
       #/etc/init.d/keepalived stop
    fi


  1. 實驗一:
  2. master與backup ip分別如下
  3. [root@node1 keepalived]# ip a
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
        link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
        inet 192.168.6.120/32 scope global eth0
        inet6 fe80::a00:27ff:fe7a:c69c/64 scope link 
           valid_lft forever preferred_lft forever
    [root@node2 keepalived]# ip a
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
        link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
        inet6 fe80::a00:27ff:fe03:1ba1/64 scope link 
           valid_lft forever preferred_lft forever


  4. 關閉主庫http
  5. [root@node1 keepalived]# /etc/init.d/httpd stop
    Stopping httpd:                                            [  OK  ]

  6. 主庫與從庫ip:
  7. [root@node1 keepalived]# ip a
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
        link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
        inet6 fe80::a00:27ff:fe7a:c69c/64 scope link 
           valid_lft forever preferred_lft forever
  8. [root@node2 keepalived]# ip a
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
        link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
        inet 192.168.6.120/32 scope global eth0                          ---vip飄到從庫了
        inet6 fe80::a00:27ff:fe03:1ba1/64 scope link 
           valid_lft forever preferred_lft forever


  9. 恢復主庫http
  10. [root@node1 keepalived]# /etc/init.d/httpd start
    Start
    ing httpd:                                            [  OK  ]
  11. 主庫與從庫ip:
  12. [root@node1 keepalived]# ip a
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
        link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
        inet 192.168.6.120/32 scope global eth0                            ---vip又飄回主庫了
        inet6 fe80::a00:27ff:fe7a:c69c/64 scope link 
           valid_lft forever preferred_lft forever
    [root@node2 keepalived]# ip a
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
        link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
        inet6 fe80::a00:27ff:fe03:1ba1/64 scope link 
           valid_lft forever preferred_lft forever


  13.   nopreempt 引數的作用:靜止vip再飄回來(少用,根據情況配置)
    1. node1 node2 vip漂移過程(關閉主庫http,恢復主庫http) 原因
      nopreempt  nopreempt
      node1》node2》node1
      nopreempt 
      nopreempt
      node1》node2》node2(vip不飄了) node1靜止vip飄回去
      nopreempt
      nopreempt 一直在node1上,飄不動 node2靜止vip飄過去
      nopreempt 
      nopreempt 
      一直在nide1上,漂不動 node2靜止vip飄過去



實驗二:
  1. 配置檔案為
    1. [root@node1 keepalived]# more keepalived.conf
      ! Configuration File for keepalived
       
      global_defs {
         notification_email {
           acassen@firewall.loc
           failover@firewall.loc
           sysadmin@firewall.loc
         }
         notification_email_from Alexandre.Cassen@firewall.loc
         smtp_server 192.168.200.1
         smtp_connect_timeout 30
         router_id LVS_DEVEL
      }


      vrrp_script chk_http_port {      
         script "/usr/local/keepalived/nginx.sh"     
      }


      vrrp_instance VI_1 {
          state MASTER
          interface eth0
          virtual_router_id 51    
          priority 99           
               advert_int 1
          authentication {      
              auth_type PASS 
              auth_pass 1111
          }


       track_script {
              chk_http_port    
          }
      virtual_ipaddress {
      192.168.6.120           
          }
      }

    指令碼改為:若http服務失效,則kill keepalive程式

    1. #!/bin/bash
      A=`netstat -nputl|grep -i 80 |wc -l`
      if [ $A -eq 0 ];then
         #exit 1;
         /etc/init.d/keepalived stop
      fi

    和實驗一的效果一樣:vip先飄到從庫,恢復http後,啟動keepalive後又飄回主庫,要想不飄回主庫,改小權重

實驗一二的區別:一個是透過權重來控制vip的飄逸。一個直接是控制keepalive程式的存活來控制vip的飄逸



3.如何在狀態轉換時進行通知?
  1. 1.要在狀態轉換是進行通知,需要定義通知指令碼可以在

    vrrp_sync_group{

    }中定義,也可以在

    vrrp_instance{

    }中定義

    透過man keepalived命令可以檢視通知指令碼定義的兩種方法

    第一種

    # to MASTER transition

    notify_master /path/to_master.sh

    # to BACKUP transition

    notify_backup /path/to_backup.sh

    # FAULT transition

    notify_fault "/path/fault.sh VG_1"

    第二種

    #arguments

    # $1 ="GROUP"|"INSTANCE"

    # $2 = name of group or instance

    # $3 = target state of transition

    # ("MASTER"|"BACKUP"|"FAULT")

    notify /path/notify.sh

    下面用一個指令碼notify.sh實現狀態轉換通知的簡單示例:

    1. #!/bin/bash
    2. #Author: MageEdu <linuxedu@foxmail.com>
    3. #description: An example of notify script
    4. #
    5. vip=192.168.6.120
    6. contact='root@localhost'
    7. notify(){
    8.     mailsubject="`hostname` to be $1: $vip floating"
    9.     mailbody="`date '+%F %H:%M:%S'`: vrrptransition, `hostname` changed to be $1"
    10.     echo $mailbody | mail -s"$mailsubject" $contact
    11. }
    12. case "$1" in
    13.     master)
    14.         notify master
    15.         exit 0
    16.     ;;
    17.     backup)
    18.         notify backup
    19.         exit 0
    20.     ;;
    21.     fault)
    22.         notify fault
    23.         exit 0
    24.     ;;
    25.     *)
    26.         echo 'Usage: `basename $0`{master|backup|fault}'
    27.         exit 1
    28.     ;;
    29. esac

    chmod +x notify.sh
  2. 主從配置檔案修改
    1. [root@node1 keepalived]# more keepalived.conf
    2. ! Configuration File for keepalived
    3.  
    4. global_defs {
    5.    notification_email {
    6.      acassen@firewall.loc
    7.      failover@firewall.loc
    8.      sysadmin@firewall.loc
    9.    }
    10.    notification_email_from Alexandre.Cassen@firewall.loc
    11.    smtp_server 192.168.200.1
    12.    smtp_connect_timeout 30
    13.    router_id LVS_DEVEL
    14. }

    15. vrrp_script chk_http_port {
    16.    script "/usr/local/keepalived/nginx.sh"
    17.    interval 3
    18.    weight -2
    19. }

    20. vrrp_instance VI_1 {
    21.     state MASTER
    22.     interface eth0
    23.     virtual_router_id 51
    24.     priority 99
    25. # nopreempt
    26.          advert_int 1
    27.     authentication {
    28.         auth_type PASS
    29.         auth_pass 1111
    30.     }

    31.  track_script {
    32.         chk_http_port
    33.     }
    34.    

    35.    notify_master "/usr/local/keepalived/notify.sh master"
    36.    notify_backup"/usr/local/keepalived/notify.sh backup"
    37.    notify_fault"/usr/local/keepalived/notify.sh fault"

    38. virtual_ipaddress {
    39. 192.168.6.120
    40.     }
    41. }

  3. 測試:
    1. 關閉主庫http服務
    2. [root@node1 ~]# /etc/init.d/httpd stop
    3. Stopping httpd: [ OK ]
    4. [root@node1 ~]# mail
    5. Mail version 8.1 6/6/93. Type ? for help.
    6. "/var/spool/mail/root": 1 message 1 new
    7. >N 1 root@localhost.local Thu Jun 30 21:10 16/716 "node1 to be backup:"

    8. [root@node2 keepalived]# mail
      Mail version 8.1 6/6/93.  Type ? for help.
      "/var/spool/mail/root": 1 message 1 new
      >N  1 root@localhost.local  Thu Jun 30 21:10  16/716   "node2 to be master:"

    9. 恢復主庫http服務
    10. [root@node1 ~]# mail
      Mail version 8.1 6/6/93.  Type ? for help.
      "/var/spool/mail/root": 2 messages 1 new 2 unread
       U  1 root@localhost.local  Thu Jun 30 21:10  17/726   "node1 to be backup:"
      >N  2 root@localhost.local  Thu Jun 30 21:12  16/716   "node1 to be master:"

    11.    [root@node2 keepalived]# mail
      Mail version 8.1 6/6/93.  Type ? for help.
      "/var/spool/mail/root": 2 messages 1 new 2 unread
       U  1 root@localhost.local  Thu Jun 30 21:10  17/726   "node2 to be master:"
      >N  2 root@localhost.local  Thu Jun 30 21:12  16/716   "node2 to be backup:"




參考文件
http://kaifly.blog.51cto.com/3209616/1665729   --MySQL主庫高可用 -- 雙主單活故障自動切換方案
     ---
結合keepalived實現redis群集高可用故障自動切換

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2121347/,如需轉載,請註明出處,否則將追究法律責任。

相關文章