Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **VIP: 10.110.0.213**
- server1: 10.110.0.81
- server2: 10.110.0.94
- ### Install keepalived on both servers
- ```
- yum install keepalived
- ```
- ### /etc/keepalived/keepalived.conf
- ```
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- denis.myasnychenko@intexsys.lv
- }
- notification_email_from denis.myasnychenko@intexsys.lv
- smtp_server intexsys.lv
- smtp_connect_timeout 30
- router_id ecenty-1
- }
- vrrp_script check_mysql {
- script "/etc/keepalived/check_mysql.sh" ##script for check mysql
- interval 1 ## every 1 seconds
- fall 3 ## counts errors before chanch VIP to another server
- }
- vrrp_instance Mysql-VIP {
- state BACKUP
- interface enp0s3 ##interface to bind VIP
- virtual_router_id 10
- priority 101 ##VRRP prior for server, for the second server is 100
- advert_int 1
- unicast_peer {
- 10.110.0.94 ## for second server is 10.110.0.81
- }
- authentication {
- auth_type PASS
- auth_pass myvrrp
- }
- track_script {
- check_mysql
- }
- nopreempt ##do not change VIP if Master come back
- smtp_alert ##enable email notification
- virtual_ipaddress {
- 10.110.0.213
- }
- notify_master "/etc/keepalived/check_mysql.sh master"
- notify_backup "/etc/keepalived/check_mysql.sh backup"
- notify_fault "/etc/keepalived/check_mysql.sh fault"
- }
- ```
- ### /etc/keepalived/my.cnf
- ```
- [client]
- user = super_user
- password = super_pass
- ```
- ### /etc/keepalived/mysql_check.sh
- ```
- #!/bin/bash
- #This script is used by keepalived for checking mysqld work
- #Additional info about keepalived work you can find in /var/log/messages
- state=$1
- host=`hostname`
- current_date=`/bin/date +"%b %d %H:%M:%S"`
- logfile='/var/log/messages'
- case $state in
- master)
- echo "$current_date Keepalived: $host now Master server with VIP on it" >> $logfile
- ;;
- backup)
- echo "$current_date Keepalived: $host now Backup server" >> $logfile
- ;;
- fault)
- echo "$current_date Keepalived: $host server fault. Mysql check failed. Trying switchover" >> $logfile
- ;;
- *)
- mysql --defaults-extra-file=/etc/keepalived/my.conf --connect_timeout=2 -e "select version();" 2>/dev/null
- ;;
- esac
- ```
- ### another version to check mysql with analyze Seconds_Behind_Master
- ```
- #!/bin/bash
- # monitor mysql status
- # 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.
- export MYSQL_HOME=/mysql
- export PATH=$MYSQL_HOME/bin:$PATH
- mysql="/usr/bin/mysql"
- delay_file="$MYSQL_HOME/slave_delay_second.log"
- slave_host=$1
- /usr/bin/mysqladmin ping| grep 'mysqld is alive' > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- delayseconds=`cat $delay_file`
- if [ $delayseconds -le 120 ]; then
- systemctl stop keepalived
- fi
- exit 1 #bad
- fi
- # Get slave delay time and save it
- $mysql --defaults-extra-file=/path/my.conf --connect_timeout=3 -e"select version();" > /dev/null 2>&1
- if [ $? -eq 0 ]; then
- 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
- echo "MySQL delay_seconds=$delayseconds" >> /var/log/messages
- if [[ "$delayseconds" =~ ^[0-9]+$ ]] ; then
- echo "$delayseconds" > $delay_file
- else
- echo "9999" > $delay_file
- fi
- fi
- exit 0 #good
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement