Architecture diagram
The installation steps are omitted.
158.140 keepalived.conf
! Configuration File for keepalivedglobal_defs {#全局标识模块 Notification_email {#定义邮件通知 [email protected] #目标邮箱} notification_email_from [email protected] #发送邮箱 Smtp_ser ver 127.0.0.1 #发送邮箱的smtp服务器 smtp_connect_timeout #smtp服务器连接超时时间 router_id lvs_2}vrrp_instance vi_1 { State BACKUP #本实例启动状态: Master/backup interface eth0 #监控的网络接口 Virtual_rou ter_id #vrrp实例 (same as a group master standby server Settings) Priority #优先级高的为master, cannot exceed 255. (Backup can be set to) Advert_int 1 #均衡器检测间隔1秒 (server settings are the same) # Nopreempt authentication {#验 Certificate type and password (server settings are the same) Auth_type pass #认证方式, pass or AH auth_pass 123456 #认证密码} virtual_ipaddre SS {#虚拟ip地址virtual_ipaddress, you can define multiple 192.168.158.120}}virtual_server 192.168.158.120 3306 {#定义虚拟服务器, Same as the virtual_server above Delay_loop 6 #健康检查时间间隔, 6 sec lb_algo wrr #负载均衡调度算法: RR|WRR|LC|WLC|SH|DH|LBLC lb_kind DR #负载均衡转发规则: nat| dr| TUN nat_mask 255.255.255.0 persistence_timeout #回话保持时间50秒, dynamic Services recommends opening protocol TCP #转 Protocol protocol, generally have TCP and UDP two kinds of #后端真实服务器, there are several sets of several real_server 192.168.158.140 3306 {weight 1 #权重越大负载分越大, 0 indicates a failure #notify_down/etc/keepalived/mysqlcheck/mysql.sh ssl_get {#健康检查 Way: http_get| Ssl_get| tcp_check| smtp_check| Misc_check connect_timeout 3 nb_get_retry 3 delay_before_retry 3}}158.141 keepalived.conf
! Configuration File for keepalivedglobal_defs {#全局标识模块 Notification_email {#定义邮件通知 [email protected] #目标邮箱} notification_email_from [email protected] #发送邮箱 Smtp_ser ver 127.0.0.1 #发送邮箱的smtp服务器 smtp_connect_timeout #smtp服务器连接超时时间 router_id lvs_1}vrrp_instance vi_1 { State BACKUP #本实例启动状态: Master/backup interface eth0 #监控的网络接口 Virtual_rou ter_id #vrrp实例 (same as a group master standby server Settings) Priority #优先级高的为master, cannot exceed 255. (Backup can be set to a) Advert_int 1 #均衡器检测间隔1秒 (server settings are the same authentication {#验证类型及密码 (server settings are the same ) Auth_type pass #认证方式, pass or AH auth_pass 123456 #认证密码} virtual_ipaddress {#虚 The proposed IP address virtual_ipaddress, you can define multiple 192.168.158.120}}virtual_server 192.168.158.120 3306 {#定义虚拟服务器, with the above Virtual_ser Ver Same as Delay_loop 6 #健康检查时Interval, 6 sec lb_algo wrr #负载均衡调度算法: RR|WRR|LC|WLC|SH|DH|LBLC lb_kind DR #负 Load balancer Forwarding rule: nat| dr| TUN nat_mask 255.255.255.0 persistence_timeout #回话保持时间50秒, dynamic Services recommends opening protocol TCP #转 Protocol protocol, generally have TCP and UDP two kinds of #后端真实服务器, there are several sets of several real_server 192.168.158.141 3306 {weight 1 #权重越大负载分越大, 0 indicates a failure #notify_down/etc/keepalived/mysqlcheck/mysql.sh ssl_get {#健康检查 Way: http_get| Ssl_get| tcp_check| smtp_check| Misc_check connect_timeout 3 nb_get_retry 3 delay_before_retry 3}}Vip
Ifconfig eth0:0 192.168.158.120 netmask 255.255.255.0 upifconfig eth0:0ifconfig eth0:0 upip a s dev eth0
check.sh
#!/bin/bashwhile [true]; Do/bin/sleep 1sh Mysql_status.shdone
mysql_status.sh
#!/bin/bash #/usr/bin/nmap localhost | grep 3306 #lsof-i:3306 mysqlport= ' NETSTAT-TLNP | grep "3306" |awk-f[: ""]+ ' {print $4} ' function Checkmysqlstatus () {/usr/bin/mysql-uroot-plizhenghua--connect_ Timeout=5-e "show databases;" &>/dev/null 2>&1 if [$?-ne 0] then Restartmysqlservice If ["$MYSQLPORT" = = "3306"];then echo "MySQL restart successful ..." Else echo "MySQL restart failure ..." echo "Server: $MYSQLIP MySQL is off, please try the restart MySQL by Manu al! ">/var/log/mysqlerr #mail-S" warn! Server: $MYSQLIP MySQL is down "[email protected] </var/log/mysqlerr fi else echo" MySQL Is running ... "fi} function Restartmysqlservice () {# echo" Test restart MySQL service! " echo "Erase MySQL remnants!" Chengkill mysqld echo "Off keepalived!!" Chengkill keepalived echo "Clear success!"} If ["$MYSQLPORT" = = "3306"] then CHEckmysqlstatus Else Restartmysqlservice fi Primary Master
Change MASTER to master_host= ' 192.168.158.140 ', master_user= ' sync ', master_password= ' sync ', master_log_file= ' mysql-bin.000125 ', Master_log_pos=286514181;start slave;show slave status \g
Drift
Daemon mode start Check.shcheck will automatically run the mysql_status.sh script, Mysql_status script function is to detect the current MySQL, when MySQL hangs out, together kill keepalived, so that can drift.
lvs+keepalived+mysql+ primary master database architecture [2 units]