Ansible-完成keepalived高可用部署

1.roles/keepalivd/tasks/main.yml

# nopreempt不抢占,只有在主备都是backup模式时才生效;
# 安装keepalived
yum -y install keepalived

# 编辑nginx监控脚本(Master and Backup)
mkdir /scripts
echo '
#!/bin/bash
if ! ss -lntup |grep nginx &> /dev/null;then
        systemctl stop keepalived
fi     
' > /scripts/montoring_nginx.sh
chmod o+x /scripts/montoring_nginx.sh

# 配置keepalived (Master端)
echo '
global_defs {
     router_id 10.0.0.5
}

vrrp_script check_web {
    script "/scripts/montoring_nginx.sh" 
    interval 5  
    weight 2 
}

vrrp_instance VIP_1 {
    state BACKUP 
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 152
    priority 100
    nopreempt 
    advert_int 1

     authentication {
           auth_type PASS
           auth_pass 1111
     }
     virtual_ipaddress {
        10.0.0.3/24 dev eth0 lavel eth0:1
     }
     track_script { 
           check_web    
     }
}
' > /etc/keepalived/keepalived.conf

# 配置keepalived (backup端)
echo '
global_defs {
     router_id 10.0.0.6
}

vrrp_script check_web {
    script "/scripts/montoring_nginx.sh" 
    interval 5  
    weight 2 
}

vrrp_instance VIP_1 {
    state BACKUP 
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 152
    priority 90
    nopreempt 
    advert_int 1

     authentication {
           auth_type PASS
           auth_pass 1111
     }
     virtual_ipaddress {
        10.0.0.3/24 dev eth0 lavel eth0:1
     }
     track_script { 
           check_web    
     }
}
' > /etc/keepalived/keepalived.conf

# 启动服务并加入自启动 (Master and Backup)
systemctl start keepalived
systemctl enable keepalived
- name: Install keepalived
  yum:
    name: keepalived
    state: installed

- name: Create scripts directory
  file:
    path: /scripts
    state: directory

- name: Remote pull script file
  copy:
    src: montoring_nginx.sh
    dest: /scripts/montoring_nginx.sh
    mode: 777

- name: Edit keepalived configure
  template:
    src: keepalived.conf.j2
    dest: /etc/keepalived/keepalived.conf
  notify: Restart keepalived

- name: Start keepalived
  systemd:
    name: keepalived
    state: started
    enabled: yes

2.roles/keepalived/handlers/main.yml

# 重启服务,使配置生效
systemctl restart keepalived
- name: Restart keepalived
  systemd:
    name: keepalived
    state: restarted

3.roles/keepalived/files/montoring_nginx.sh

#!/bin/bash
if ! ss -lntup |grep nginx &> /dev/null;then
        systemctl stop keepalived
fi    

4.roles/keepalived/templates/keepalived.conf.j2

global_defs {
     router_id {{ ansible_hostname }}
}

vrrp_script check_web {
    script "/scripts/montoring_nginx.sh" 
    interval 5  
    weight 2 
}

vrrp_instance VIP_1 {
{% if ansible_hostname == "nginx_proxy1" %}
        priority 100
{% elif ansible_hostname == "nginx_proxy2" %}
        priority 90
{% endif %}
    state BACKUP 
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 152
    nopreempt 
    advert_int 1

     authentication {
           auth_type PASS
           auth_pass 1111
     }
     virtual_ipaddress {
        10.0.0.3/24 dev eth0 lavel eth0:1
     }
     track_script { 
           check_web    
     }
}

你可能感兴趣的:(Ansible-完成keepalived高可用部署)