{"id":918,"date":"2020-12-17T14:55:29","date_gmt":"2020-12-17T06:55:29","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=918"},"modified":"2020-12-17T15:00:43","modified_gmt":"2020-12-17T07:00:43","slug":"keepalived-mysql-mgr-group-replication-mode-automatic","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2020\/12\/keepalived-mysql-mgr-group-replication-mode-automatic\/","title":{"rendered":"Keepalived + Mysql MGR \u7ec4\u590d\u5236\u6a21\u5f0f\u6545\u969c\u81ea\u52a8\u5207\u6362\uff08\u4e8c\uff09"},"content":{"rendered":"
MGR\u4e3b\u8282\u70b9\u6545\u969c\u81ea\u52a8\u5207\u6362<\/strong><\/p>\n MGR\u5355\u4e3b\u6a21\u5f0f\u4e0b\uff0c\u5f53\u4e3b\u8282\u70b9\u6545\u969c\uff0cMGR\u5185\u90e8\u5c06\u4f1a\u53d1\u8d77\u4e00\u8f6e\u65b0\u7684\u9009\u4e3e\uff0c\u9009\u51fa\u65b0\u7684\u4e3b\uff0c\u8fd9\u662f\u7531MGR\u5185\u90e8\u51b3\u5b9a\u5e76\u6267\u884c\u7684\u3002\u4f46\u662f\u5f53\u5e94\u7528\u7684\u8fde\u63a5\u9047\u5230\u4e3b\u8282\u70b9\u6302\u6389\u7684\u60c5\u51b5\u4e0b\uff0cIP\u662f\u4e0d\u4f1a\u81ea\u52a8\u53d1\u751f\u5207\u6362\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cMGR\u5185\u90e8\u6ca1\u6709\u63d0\u4f9b\u4e00\u79cd\u673a\u5236\uff0c\u6765\u5b9e\u73b0\u4e3b\u8282\u70b9\u6545\u969c\u5207\u6362\u5bf9\u5e94\u7528\u65e0\u611f\u77e5\u3002<\/p>\n \u89e3\u51b3\u529e\u6cd5\uff0c\u7528Keepalived \u5b9e\u73b0MGR\u8282\u70b9\u6545\u969c\u81ea\u52a8\u5207\u6362\u3002\u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u6570\u636e\u5e93\u4f7f\u7528VIP\u5373\u53ef\uff0c\u540e\u9762master\u662f\u54ea\u4e00\u53f0\u6570\u636e\u5e93\u662f\u901a\u8fc7keepalived\u670d\u52a1\u7aef\u5b9e\u73b0\u7684\uff1bkeepalived\u901a\u8fc7\u68c0\u6d4b\u811a\u672c\u786e\u5b9a\u54ea\u4e00\u53f0\u662fmaster\u5c31\u628aVIP\u98d8\u5728\u54ea\u4e00\u53f0\u670d\u52a1\u5668\u4e0a\u3002<\/p>\n <\/p>\n \u539f\u7406\uff1a<\/strong><\/p>\n vrrp_script\u811a\u672c\u4e2dweight\u8fd9\u4e2a\u503c\u5fc5\u987b\u6307\u5b9a\uff0c\u5426\u5219\u6709\u65f6\u5019\u91cd\u542f\u670d\u52a1\u540e\u8be5\u8282\u70b9\u88ab\u663e\u793a\u4e3afault \u72b6\u6001\u3002weight\u503c\u5206\u4e3a\u6b63\u503c\u548c\u8d1f\u503c\uff0c\u5047\u5b9aweight\u503c\u4e3aW\uff0c\u521d\u59cb\u7684\u4f18\u5148\u7ea7\u4e3aP\u3002<\/p>\n \u5f53weight\u503c\u5c0f\u4e8e0\u65f6\uff1a<\/p>\n \u5982\u679c\u68c0\u6d4b\u811a\u672c\u8fd4\u56de\u503c=0\uff0c\u5219\u8282\u70b9\u6700\u7ec8\u4f18\u5148\u7ea7\u4e0d\u6539\u53d8\uff1b<\/p>\n \u5982\u679c\u68c0\u6d4b\u811a\u672c\u8fd4\u56de\u503c\u22600\uff0c\u5219\u8282\u70b9\u6700\u7ec8\u4f18\u5148\u7ea7=P-W\uff0c\u4f18\u5148\u7ea7\u4f1a\u51cf\u5c0f\u3002<\/p>\n \u5f53weight\u503c\u5927\u4e8e0\u65f6\uff1a<\/p>\n \u5982\u679c\u68c0\u6d4b\u811a\u672c\u8fd4\u56de\u503c=0\uff0c\u5219\u8282\u70b9\u6700\u7ec8\u4f18\u5148\u7ea7=P+W\uff0c\u4f18\u5148\u7ea7\u4f1a\u589e\u52a0\uff1b<\/p>\n \u5982\u679c\u68c0\u6d4b\u811a\u672c\u8fd4\u56de\u503c\u22600\uff0c\u5219\u8282\u70b9\u6700\u7ec8\u4f18\u5148\u7ea7\u4e0d\u6539\u53d8\u3002<\/p>\n \u6743\u91cd\u7b56\u7565\u662f\uff1a<\/p>\n \u5f53weight > 0\u65f6\uff1a\u811a\u672c\u6267\u884c\u6210\u529f\u4e86 Priority+Weight \u6267\u884c\u5931\u8d25 Priority<\/p>\n \u5f53weight < 0\u65f6\uff1a\u811a\u672c\u6267\u884c\u6210\u529f\u4e86 Priority \u6267\u884c\u5931\u8d25 Priority+Weight<\/p>\n \u901a\u8fc7chk_mysql.sh\u811a\u672c\u68c0\u6d4bmysql\u670d\u52a1\u662f\u5426\u5b58\u5728\u8fd4\u56de\u8fd4\u56de\u503c\uff0cKeepalived\u5224\u65ad\u811a\u672c\u8fd4\u56de\u503c\u8c03\u6574\u4f18\u5148\u7ea7\uff0c\u8c03\u6574\u540eVIP\u4f1a\u98d8\u5230\u4f18\u5148\u7ea7\u6700\u5927\u7684\u673a\u5668\u4e0a\uff0c<\/p>\n \u6574\u4f53\u6765\u8bf4\u5c31\u662f\u901a\u8fc7\u6539\u53d8\u673a\u5668\u7684\u6743\u91cd\u6765\u628aVIP\u98d8\u5728\u6743\u91cd\u6700\u5927\u7684\u673a\u5668\u4e0a\u7684\uff1b\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u901a\u8fc7\u811a\u672c\u5224\u65ad\u662f\u5426\u6267\u884c\uff0c\u4f7f\u5f97master\u7684\u6743\u91cd\u6700\u9ad8\uff1b\u8fd9\u91cc\u7684\u6743\u9650\u4e00\u5b9a\u5f97\u63a7\u5236\u597d\uff1b<\/p>\n<\/blockquote>\n \u4e09\u53f0\u8282\u70b9\u4e0d\u540c\u5904\uff0c\u6839\u636e\u5b9e\u9645\u4fee\u6539\u3002<\/p>\n interface \uff1a \u7f51\u5361\u6839\u636e\u5b9e\u9645\u7f51\u5361\u540d\u4fee\u6539<\/p>\n priority \uff1a\u4f18\u5148\u7ea7\uff0c\u4e09\u53f0\u673a\u5668\u4e0d\u540c<\/p>\n Centos76(master): 100<\/p>\n k8s-node1(slave): 98<\/p>\n k8s-node2(slave): 96<\/p>\n<\/blockquote>\n<\/blockquote>\n <\/p>\n1\u3001\u5b89\u88c5Keepalived<\/h3>\n
yum install -y keepalived \n<\/code><\/pre>\n
\n
\n
vim \/etc\/keepalived\/keepalived.conf\n\n! Configuration File for keepalived\n\n#\u6307\u5b9amysql\u670d\u52a1\u68c0\u6d4b\u811a\u672c\nvrrp_script chk_mysql_port {\n script "\/root\/chk_mysql_port.sh" #\u811a\u672c\u8def\u5f84\n interval 2 #\u811a\u672c\u68c0\u6d4b\u9891\u7387\n weight -5 #\u811a\u672c\u6267\u884c\u8fd4\u56de\u503c =0 \uff0c\u4f18\u5148\u7ea7\u4e0d\u53d8\u3002 !=0 \u65f6\u4f18\u5148\u7ea7 -5\n fall 2 #\u5982\u679c\u8fde\u7eed\u4e24\u6b21\u68c0\u6d4b\u5931\u8d25\uff0c\u8ba4\u4e3a\u8282\u70b9\u670d\u52a1\u4e0d\u53ef\u7528\n rise 1 #\u5982\u679c\u8fde\u7eed2\u6b21\u68c0\u67e5\u6210\u529f\u5219\u8ba4\u4e3a\u8282\u70b9\u6b63\u5e38\n}\n#\u76d1\u6d4bmysql \u662f\u5426\u662fmaster\nvrrp_script chk_mysql_master {\nscript "\/root\/chk_mysql_master.sh"\ninterval 2 \nweight 10 #\u811a\u672c\u6267\u884c\u8fd4\u56de\u503c =0 \uff0c\u4f18\u5148\u7ea7\u52a0 10\u3002 !=0 \u65f6\u4f18\u5148\u7ea7\u4e0d\u53d8\u3002\n}\n\nvrrp_instance VI_1 {\n state MASTER #\u6307\u5b9a\u5b9e\u4f8b\u521d\u59cb\u72b6\u6001\uff0c\u5b9e\u9645\u7684MASTER\u548cBACKUP\u662f\u9009\u4e3e\u51b3\u5b9a\u7684\u3002\n interface eth0 #\u6307\u5b9a\u5b9e\u4f8b\u7ed1\u5b9a\u7684\u7f51\u5361\n virtual_router_id 88 #\u8bbe\u7f6eVRID\u6807\u8bb0(0..255)\n priority 100 #\u8bbe\u7f6e\u4f18\u5148\u7ea7\uff0c\u4f18\u5148\u7ea7\u9ad8\u7684\u4f1a\u88ab\u7ade\u9009\u4e3aMaster\n advert_int 1 #\u68c0\u67e5\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u9ed8\u8ba41s\n authentication { #\u8282\u70b9\u95f4\u7684\u8ba4\u8bc1\uff0c\u6240\u6709\u7684\u5fc5\u987b\u4e00\u81f4\n auth_type PASS\n auth_pass 1111\n }\n virtual_ipaddress { #\u8bbe\u7f6eVIP\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a\uff0c\u7528\u4e8e\u5207\u6362\u65f6\u7684\u5730\u5740\u7ed1\u5b9a\u3002\n 64.115.5.111\n }\n\n track_script { #\u6307\u5b9a\u524d\u9762\u811a\u672c\u7684\u540d\u5b57\n chk_mysql_port\n chk_mysql_master\n }\n}\n<\/code><\/pre>\n
\n
\n
\n
global_defs {\n notification_email { #\u8bbe\u7f6e keepalived \u5728\u53d1\u751f\u4e8b\u4ef6\uff08\u6bd4\u5982\u5207\u6362\uff09\u7684\u65f6\u5019\uff0c\u9700\u8981\u53d1\u9001\u5230\u7684email\u5730\u5740\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a\uff0c\u6bcf\u884c\u4e00\u4e2a\u3002\n acassen@firewall.loc\n failover@firewall.loc\n sysadmin@firewall.loc\n }\n notification_email_from Alexandre.Cassen@firewall.loc #\u8bbe\u7f6e\u901a\u77e5\u90ae\u4ef6\u53d1\u9001\u6765\u81ea\u4e8e\u54ea\u91cc\uff0c\u5982\u679c\u672c\u5730\u5f00\u542f\u4e86sendmail\u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u7684\u9ed8\u8ba4\u503c\u3002\n smtp_server 192.168.200.1 #\u6307\u5b9a\u53d1\u9001\u90ae\u4ef6\u7684smtp\u670d\u52a1\u5668\u3002\n smtp_connect_timeout 30 #\u8bbe\u7f6esmtp\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\u3002\n router_id LVS_DEVEL #\u662f\u8fd0\u884ckeepalived\u7684\u4e00\u4e2a\u8868\u793a\uff0c\u591a\u4e2a\u96c6\u7fa4\u8bbe\u7f6e\u4e0d\u540c\u3002\n}\n<\/code><\/pre>\n
\n
##chk_mysql.sh \u68c0\u67e5mysql \u670d\u52a1\u811a\u672c\n#!\/bin\/bash\nmysql_port=
netstat -antp |grep :::3306 |wc -l<\/code>\nif [$mysql_port -ne 1 ];then\n exit 1\nfi\n<\/code><\/pre>\n
##chk_mysql_master.sh \u68c0\u67e5mysql master \u811a\u672c\n#!\/bin\/bash\nmaster_hostname=
mysql -h127.0.0.1 -uroot -pWANG_feng123 -e "SELECT * FROM performance_schema.replication_group_members WHERE MEMBER_ID = (SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member')" |awk 'NR==2{print}'|awk -F" " '{print $3}'<\/code>\n\nhostname=
hostname<\/code>\nif [ $master_hostname == $hostname ];then\n exit 0\nelse\n exit 1\nfi\n<\/code><\/pre>\n
\n
mysql> grant all on *.* to root@'%' identified by "WANG_feng123";\nmysql> flush privileges;\n<\/code><\/pre>\n