diff --git a/plugins/keepalived/index.html b/plugins/keepalived/index.html index 2fc7e1b33..f67413b19 100755 --- a/plugins/keepalived/index.html +++ b/plugins/keepalived/index.html @@ -4,7 +4,8 @@

服务

自启动

-

配置修改

+ +

配置修改

diff --git a/plugins/keepalived/index.py b/plugins/keepalived/index.py index 4d326c899..0dfb9cd1d 100755 --- a/plugins/keepalived/index.py +++ b/plugins/keepalived/index.py @@ -71,6 +71,31 @@ def getArgs(): tmp[t[0]] = t[1] return tmp +def checkArgs(data, ck=[]): + for i in range(len(ck)): + if not ck[i] in data: + return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!')) + return (True, mw.returnJson(True, 'ok')) + +def configTpl(): + path = getPluginDir() + '/tpl' + pathFile = os.listdir(path) + tmp = [] + for one in pathFile: + file = path + '/' + one + tmp.append(file) + return mw.getJson(tmp) + + +def readConfigTpl(): + args = getArgs() + data = checkArgs(args, ['file']) + if not data[0]: + return data[1] + + content = mw.readFile(args['file']) + content = contentReplace(content) + return mw.returnJson(True, 'ok', content) def status(): data = mw.execShell( @@ -85,6 +110,17 @@ def contentReplace(content): service_path = os.path.dirname(os.getcwd()) content = content.replace('{$SERVER_PATH}', service_path) content = content.replace('{$PLUGIN_PATH}', getPluginDir()) + + # 网络接口 + ethx = mw.execShell("route -n | grep ^0.0.0.0 | awk '{print $8}'") + if data[1]!='': + # 未找到 + content = content.replace('{$ETH_XX}', 'eth1') + else: + # + content = content.replace('{$ETH_XX}', ethx[0]) + + return content @@ -270,5 +306,9 @@ if __name__ == "__main__": print(getConf()) elif func == 'run_log': print(runLog()) + elif func == 'config_tpl': + print(configTpl()) + elif func == 'read_config_tpl': + print(readConfigTpl()) else: print('error') diff --git a/plugins/keepalived/info.json b/plugins/keepalived/info.json index d342961ed..c024e497c 100755 --- a/plugins/keepalived/info.json +++ b/plugins/keepalived/info.json @@ -1,6 +1,6 @@ { "sort": 7, - "ps": "[潜龙勿用]优秀的高可用软件", + "ps": "[内网]优秀的高可用软件", "name": "keepalived", "title": "keepalived", "shell": "install.sh", diff --git a/plugins/keepalived/scripts/chk.sh b/plugins/keepalived/scripts/chk.sh new file mode 100644 index 000000000..6f02ec93c --- /dev/null +++ b/plugins/keepalived/scripts/chk.sh @@ -0,0 +1,15 @@ +#!/bin/bash + + + +# check script bash +curPath=`pwd` +rootPath=$(dirname "$curPath") + +SOFT=$1 + +if [ "$SOFT" == "mysql" ];then + bash ${rootPath}/chk_mysql.sh +else + echo "you should use [chk.sh mysql] exp ." +fi \ No newline at end of file diff --git a/plugins/keepalived/scripts/chk_mysql.sh b/plugins/keepalived/scripts/chk_mysql.sh index 33d9e7429..e5634f2e0 100644 --- a/plugins/keepalived/scripts/chk_mysql.sh +++ b/plugins/keepalived/scripts/chk_mysql.sh @@ -4,7 +4,7 @@ date +'%Y-%m-%d %H:%M:%S' echo "start check mysql status ..." counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l) if [ "${counter}" -eq 0 ]; then - echo "check mysql is down, stop keepalive" + echo "check mysql is down, stop keepalive" systemctl stop keepalived fi diff --git a/plugins/keepalived/tpl/keepalived.mysql.conf b/plugins/keepalived/tpl/keepalived.mysql.conf new file mode 100644 index 000000000..d9f7181e6 --- /dev/null +++ b/plugins/keepalived/tpl/keepalived.mysql.conf @@ -0,0 +1,32 @@ +! Configuration File for keepalived + +global_defs { + router_id MYSQL_MHA +} + +vrrp_script chk_mysql_port { + script "{$PLUGIN_PATH}/scripts/chk_mysql.sh" + interval 2 + weight –5 + fall 2 + rise 1 +} + +vrrp_instance VI_1 { + state MASTER + interface {$ETH_XX} + virtual_router_id 51 + priority 100 + advert_int 1 + authentication { + auth_type PASS + auth_pass 1111 + } + virtual_ipaddress { + 192.168.10.10 + } + + track_script { + chk_mysql_port + } +} \ No newline at end of file diff --git a/plugins/keepalived/tpl/keepalived.mysql.note.conf b/plugins/keepalived/tpl/keepalived.mysql.note.conf new file mode 100644 index 000000000..e20e1d687 --- /dev/null +++ b/plugins/keepalived/tpl/keepalived.mysql.note.conf @@ -0,0 +1,49 @@ +! Configuration File for keepalived + +global_defs { + # 路由标识,主从保持一致 + router_id MYSQL_MHA +} + +#检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等 +vrrp_script chk_mysql_port { + # 这里通过脚本监测 + script "{$PLUGIN_PATH}/scripts/chk.sh mysql" + # 脚本执行间隔,每2s检测一次 + interval 2 + # 脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5 + weight –5 + # 检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间) + fall 2 + # 检测1次成功就算成功。但不修改优先级 + rise 1 +} + +vrrp_instance VI_1 { + # 这里所有节点定义为BACKUP/MASTER + state BACKUP + # 指定虚拟ip的网卡(可能需要手动查看) + # route -n | grep ^0.0.0.0 | awk '{print $8}' + interface {$ETH_XX} + # 路由器标识,MASTER和BACKUP必须是一致的 + virtual_router_id 51 + # 定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级 + priority 99 + # 通知间隔秒数(心跳频率) + advert_int 1 + # 不抢占模式,在优先级高的机器上设置即可,优先级低的机器可不设置 + nopreempt + # 认证信息配置,主从服务器保持一致 + authentication { + auth_type PASS + auth_pass 1111 + } + virtual_ipaddress { + #虚拟IP(VIP) + 192.168.10.10 + } + + track_script { + chk_mysql_port + } +} \ No newline at end of file diff --git a/plugins/mysql/conf/my5.5.cnf b/plugins/mysql/conf/my5.5.cnf index 9df7698e4..cd09e186b 100644 --- a/plugins/mysql/conf/my5.5.cnf +++ b/plugins/mysql/conf/my5.5.cnf @@ -58,7 +58,7 @@ expire_logs_days=7 relay-log=mdserver relay-log-index=mdserver -#主主互备设置 +#多主设置 #auto_increment_offset=2 #auto_increment_increment=2 diff --git a/plugins/mysql/conf/my5.6.cnf b/plugins/mysql/conf/my5.6.cnf index 4950bbce7..e29fd5a35 100644 --- a/plugins/mysql/conf/my5.6.cnf +++ b/plugins/mysql/conf/my5.6.cnf @@ -61,7 +61,7 @@ expire_logs_days=7 relay-log=mdserver relay-log-index=mdserver -#主主互备设置 +#多主设置 #auto_increment_offset=2 #auto_increment_increment=2 diff --git a/plugins/mysql/conf/my5.7.cnf b/plugins/mysql/conf/my5.7.cnf index 40ad650d7..2fc66e783 100644 --- a/plugins/mysql/conf/my5.7.cnf +++ b/plugins/mysql/conf/my5.7.cnf @@ -62,7 +62,7 @@ expire_logs_days=7 relay-log=mdserver relay-log-index=mdserver -#主主互备设置 +#多主设置 #auto_increment_offset=2 #auto_increment_increment=2 diff --git a/plugins/mysql/conf/my8.0.cnf b/plugins/mysql/conf/my8.0.cnf index 7b75db215..ba4f80364 100644 --- a/plugins/mysql/conf/my8.0.cnf +++ b/plugins/mysql/conf/my8.0.cnf @@ -60,7 +60,7 @@ binlog_expire_logs_seconds=2592000 relay-log=mdserver relay-log-index=mdserver -#主主互备设置 +#多主设置 #auto_increment_offset=2 #auto_increment_increment=2