Advertisement
Guest User

Untitled

a guest
May 24th, 2017
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. **VIP: 10.110.0.213**
  2. server1: 10.110.0.81
  3. server2: 10.110.0.94
  4.  
  5. ### Install keepalived on both servers
  6. ```
  7. yum install keepalived
  8. ```
  9.  
  10. ### /etc/keepalived/keepalived.conf
  11. ```
  12. ! Configuration File for keepalived
  13.  
  14. global_defs {
  15. notification_email {
  16. denis.myasnychenko@intexsys.lv
  17. }
  18. notification_email_from denis.myasnychenko@intexsys.lv
  19. smtp_server intexsys.lv
  20. smtp_connect_timeout 30
  21. router_id ecenty-1
  22. }
  23.  
  24. vrrp_script check_mysql {
  25. script "/etc/keepalived/check_mysql.sh" ##script for check mysql
  26. interval 1 ## every 1 seconds
  27. fall 3 ## counts errors before chanch VIP to another server
  28. }
  29.  
  30. vrrp_instance Mysql-VIP {
  31. state BACKUP
  32. interface enp0s3 ##interface to bind VIP
  33. virtual_router_id 10
  34. priority 101 ##VRRP prior for server, for the second server is 100
  35. advert_int 1
  36. unicast_peer {
  37. 10.110.0.94 ## for second server is 10.110.0.81
  38. }
  39. authentication {
  40. auth_type PASS
  41. auth_pass myvrrp
  42. }
  43. track_script {
  44. check_mysql
  45. }
  46. nopreempt ##do not change VIP if Master come back
  47. smtp_alert ##enable email notification
  48. virtual_ipaddress {
  49. 10.110.0.213
  50. }
  51. notify_master "/etc/keepalived/check_mysql.sh master"
  52. notify_backup "/etc/keepalived/check_mysql.sh backup"
  53. notify_fault "/etc/keepalived/check_mysql.sh fault"
  54. }
  55.  
  56. ```
  57.  
  58. ### /etc/keepalived/my.cnf
  59. ```
  60. [client]
  61. user = super_user
  62. password = super_pass
  63. ```
  64.  
  65. ### /etc/keepalived/mysql_check.sh
  66. ```
  67. #!/bin/bash
  68. #This script is used by keepalived for checking mysqld work
  69. #Additional info about keepalived work you can find in /var/log/messages
  70.  
  71. state=$1
  72. host=`hostname`
  73. current_date=`/bin/date +"%b %d %H:%M:%S"`
  74. logfile='/var/log/messages'
  75.  
  76. case $state in
  77. master)
  78. echo "$current_date Keepalived: $host now Master server with VIP on it" >> $logfile
  79. ;;
  80. backup)
  81. echo "$current_date Keepalived: $host now Backup server" >> $logfile
  82. ;;
  83. fault)
  84. echo "$current_date Keepalived: $host server fault. Mysql check failed. Trying switchover" >> $logfile
  85. ;;
  86. *)
  87. mysql --defaults-extra-file=/etc/keepalived/my.conf --connect_timeout=2 -e "select version();" 2>/dev/null
  88. ;;
  89. esac
  90. ```
  91.  
  92. ### another version to check mysql with analyze Seconds_Behind_Master
  93. ```
  94. #!/bin/bash
  95. # monitor mysql status
  96. # if this node mysql is dead and its slave delay less than 120 seconds, then stop its keepalived. The other node will bind the IP.
  97.  
  98. export MYSQL_HOME=/mysql
  99. export PATH=$MYSQL_HOME/bin:$PATH
  100.  
  101. mysql="/usr/bin/mysql"
  102. delay_file="$MYSQL_HOME/slave_delay_second.log"
  103. slave_host=$1
  104.  
  105. /usr/bin/mysqladmin ping| grep 'mysqld is alive' > /dev/null 2>&1
  106.  
  107. if [ $? -ne 0 ]; then
  108. delayseconds=`cat $delay_file`
  109. if [ $delayseconds -le 120 ]; then
  110. systemctl stop keepalived
  111. fi
  112. exit 1 #bad
  113. fi
  114.  
  115. # Get slave delay time and save it
  116. $mysql --defaults-extra-file=/path/my.conf --connect_timeout=3 -e"select version();" > /dev/null 2>&1
  117. if [ $? -eq 0 ]; then
  118. delayseconds=`$mysql --defaults-extra-file=/path/my.conf --connect_timeout=3 -e"show slave status\G"|grep Seconds_Behind_Master|awk '{print $2}'` > /dev/null 2>&1
  119. echo "MySQL delay_seconds=$delayseconds" >> /var/log/messages
  120. if [[ "$delayseconds" =~ ^[0-9]+$ ]] ; then
  121. echo "$delayseconds" > $delay_file
  122. else
  123. echo "9999" > $delay_file
  124. fi
  125. fi
  126. exit 0 #good
  127. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement