From f7e02bae70e43731fe8244db2b9c8ef87639b1dc Mon Sep 17 00:00:00 2001 From: lijia Date: Fri, 23 Oct 2020 15:54:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=88=92=E5=88=86=E9=9B=86?= =?UTF-8?q?=E7=BE=A4keepalive=20virtual=5Froute=5Fid,=20=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=AE=A1=E7=90=86HA=E9=9B=86=E7=BE=A4=E4=BD=BF=E7=94=A854.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install_config/group_vars/all.yml | 2 + roles/ha_backup/tasks/main.yml | 43 ++++++++++++++++ .../templates/check_oam_service.sh.j2 | 22 -------- .../templates/keepalived_backup.conf.j2 | 40 ++++++--------- .../templates/keepalived_master.conf.j2 | 46 ----------------- .../templates/keepalived_backup.conf.j2 | 50 ------------------- .../templates/keepalived_master.conf.j2 | 2 +- 7 files changed, 60 insertions(+), 145 deletions(-) create mode 100644 roles/ha_backup/tasks/main.yml delete mode 100644 roles/ha_backup/templates/check_oam_service.sh.j2 delete mode 100644 roles/ha_backup/templates/keepalived_master.conf.j2 delete mode 100644 roles/ha_master/templates/keepalived_backup.conf.j2 diff --git a/install_config/group_vars/all.yml b/install_config/group_vars/all.yml index 80a14f6..26ba45d 100644 --- a/install_config/group_vars/all.yml +++ b/install_config/group_vars/all.yml @@ -38,11 +38,13 @@ ha_master: instance_state: MASTER ethname: eth0 virtual_ip: "192.168.44.58" + oam_virtual_router_id: 54 ha_backup: instance_state: BACKUP ethname: eth0 virtual_ip: "192.168.44.58" + oam_virtual_router_id: 54 java_version: jdk1.8.0_73 bifang_api_ip: "192.168.43.5" diff --git a/roles/ha_backup/tasks/main.yml b/roles/ha_backup/tasks/main.yml new file mode 100644 index 0000000..57a2fff --- /dev/null +++ b/roles/ha_backup/tasks/main.yml @@ -0,0 +1,43 @@ +- name: "copy keepalived rpm to destination server" + copy: + src: "{{ role_path }}/files/" + dest: /tmp + mode: 0755 + +- name: "install keepalived" + yum: + name: + - /tmp/keepalived-1.3.5-16.el7.x86_64.rpm + state: present + +- name: "install ipvsadm" + yum: + name: + - /tmp/ipvsadm-1.27-8.el7.x86_64.rpm + state: present + +- name: "Template check_service_health.sh.j2" + template: + src: "{{ role_path }}/templates/check_service_health.sh.j2" + dest: /etc/keepalived/check_service_health.sh + tags: template + +- name: "Template keepalived_backup.conf.j2" + template: + src: "{{ role_path }}/templates/keepalived_backup.conf.j2" + dest: /etc/keepalived/keepalived.conf + tags: template + +- name: Template the keepalived.service.j2 + template: + src: "{{ role_path }}/files/keepalived.service.j2" + dest: "/usr/lib/systemd/system/keepalived.service" + tags: template + +- name: "Start keepalived" + systemd: + name: keepalived.service + enabled: yes + state: restarted + enabled: yes + diff --git a/roles/ha_backup/templates/check_oam_service.sh.j2 b/roles/ha_backup/templates/check_oam_service.sh.j2 deleted file mode 100644 index 94207cc..0000000 --- a/roles/ha_backup/templates/check_oam_service.sh.j2 +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -#running:0, stop:3 -STATE=$(systemctl status oam_cluster.service) - -if [ $STATE -ne 0 ] -then - systemctl restart oam_cluster - sleep 10 - STATE=$(systemctl status oam_cluster.service) - if [ $STATE -ne 0 ] - then - killall keepalived - exit 1 - else - exit 0 - fi -else - exit 0 -fi - -#todo ,check consul_cluster, check influxdb diff --git a/roles/ha_backup/templates/keepalived_backup.conf.j2 b/roles/ha_backup/templates/keepalived_backup.conf.j2 index 1f01054..58f9dc1 100644 --- a/roles/ha_backup/templates/keepalived_backup.conf.j2 +++ b/roles/ha_backup/templates/keepalived_backup.conf.j2 @@ -1,50 +1,38 @@ ! Configuration File for keepalived global_defs { - router_id LVSTEST2 + router_id OAMHA } #监控服务httpd, mysql等 -vrrp_script chk_http_service { - script "/etc/keepalived/chk_http_service.sh" +vrrp_script check_service_health { + script "/etc/keepalived/check_service_health.sh" #每2s检查一次 interval 3 - #每次检查-20 + #每次检查-10 weight -10 - fail 3 - #失败次数,如果请求失败2次就认为此节点资源发生故障将进行切换 + fail 3 + #失败次数,如果请求失败2次就认为此节点资源发生故障将进行切换 - rise 1 - #监测成功就立即成功,如果请求一次成功就默认此节点资源恢复正常 + rise 1 + #监测成功就立即成功,如果请求一次成功就默认此节点资源恢复正常 } vrrp_instance VI_1 { - state BACKUP - interface eth0 - virtual_router_id 51 + state {{ ha_backup.instance_state }} + interface {{ ha_backup.ethname }} + virtual_router_id {{ ha_backup.oam_virtual_router_id }} priority 10 advert_int 1 authentication { auth_type PASS - auth_pass 1111 + auth_pass oamha } virtual_ipaddress { - 192.168.44.244 + {{ ha_backup.virtual_ip }} } #触发的脚本 track_script { - chk_http_service #检测脚本,上面配置的 + check_service_health #检测脚本,上面配置的 } } - -virtual_server 192.168.44.244 80 { - delay_loop 6 - lb_algo rr - lb_kind NAT - persistence_timeout 50 - protocol TCP - - #real_server 127.0.0.1 80 { - # weight 1 - #} -} diff --git a/roles/ha_backup/templates/keepalived_master.conf.j2 b/roles/ha_backup/templates/keepalived_master.conf.j2 deleted file mode 100644 index 1dffc70..0000000 --- a/roles/ha_backup/templates/keepalived_master.conf.j2 +++ /dev/null @@ -1,46 +0,0 @@ -! Configuration File for keepalived - -global_defs { - router_id LVSTEST1 -} - -#监控服务httpd, mysql等 -vrrp_script chk_http_service { - script "/etc/keepalived/chk_http_service.sh" - #每2s检查一次 - interval 3 - #每次检查-20 - weight -10 - fail 3 - #失败次数,如果请求失败2次就认为此节点资源发生故障将进行切换 - - rise 1 - #监测成功就立即成功,如果请求一次成功就默认此节点资源恢复正常 -} - -vrrp_instance VI_1 { - state MASTER - interface eth0 - virtual_router_id 51 - priority 100 - advert_int 1 - authentication { - auth_type PASS - auth_pass 1111 - } - virtual_ipaddress { - 192.168.44.244 - } - #触发的脚本 - track_script { - chk_http_service #检测脚本,上面配置的 - } -} - -virtual_server 192.168.44.244 80 { - delay_loop 6 - lb_algo rr - lb_kind NAT - persistence_timeout 50 - protocol TCP -} diff --git a/roles/ha_master/templates/keepalived_backup.conf.j2 b/roles/ha_master/templates/keepalived_backup.conf.j2 deleted file mode 100644 index 1f01054..0000000 --- a/roles/ha_master/templates/keepalived_backup.conf.j2 +++ /dev/null @@ -1,50 +0,0 @@ -! Configuration File for keepalived - -global_defs { - router_id LVSTEST2 -} - -#监控服务httpd, mysql等 -vrrp_script chk_http_service { - script "/etc/keepalived/chk_http_service.sh" - #每2s检查一次 - interval 3 - #每次检查-20 - weight -10 - fail 3 - #失败次数,如果请求失败2次就认为此节点资源发生故障将进行切换 - - rise 1 - #监测成功就立即成功,如果请求一次成功就默认此节点资源恢复正常 -} - -vrrp_instance VI_1 { - state BACKUP - interface eth0 - virtual_router_id 51 - priority 10 - advert_int 1 - authentication { - auth_type PASS - auth_pass 1111 - } - virtual_ipaddress { - 192.168.44.244 - } - #触发的脚本 - track_script { - chk_http_service #检测脚本,上面配置的 - } -} - -virtual_server 192.168.44.244 80 { - delay_loop 6 - lb_algo rr - lb_kind NAT - persistence_timeout 50 - protocol TCP - - #real_server 127.0.0.1 80 { - # weight 1 - #} -} diff --git a/roles/ha_master/templates/keepalived_master.conf.j2 b/roles/ha_master/templates/keepalived_master.conf.j2 index 7935be0..acba5a2 100644 --- a/roles/ha_master/templates/keepalived_master.conf.j2 +++ b/roles/ha_master/templates/keepalived_master.conf.j2 @@ -21,7 +21,7 @@ vrrp_script check_service_health { vrrp_instance VI_1 { state {{ ha_master.instance_state }} interface {{ ha_master.ethname }} - virtual_router_id 51 + virtual_router_id {{ ha_master.oam_virtual_router_id }} priority 100 advert_int 1 authentication {