ansible-playbook安裝keepalived-指定tags安裝MASTER或BACKUP
阿新 • • 發佈:2018-04-13
ansible-playbook keepalived 在實際工作中,新項目上線往往都需要搭建HA,此時我們可以通過ansible的playbook來秒級搭建好一個HA環境,從而解放雙手;
此playbook適用於centos6、centos7的keepalived搭建,但是請註意var/main.yml中變量的修改,修改為你自己想的設定的配置;
playbook的目錄結構:
[root@shvpuppet01 roles]# tree keepalived/ keepalived/ ├── files │ ├── check_nginx.sh │ └── notify.sh ├── handles ├── meta ├── tasks │ ├── keepalived_backup.yml │ ├── keepalived_master.yml │ └── main.yml ├── templates │ ├── keepalived_backup.conf.j2 │ └── keepalived_master.conf.j2 └── vars └── main.yml 6 directories, 8 files 文件說明: check_nginx.sh:用於檢查nginx是否存活,如果nginx不存活,剛keepalived權重降級20,VIP將發生切換 notify.sh:當VIP切換發生後,郵件通知相關用戶 keepalived_master.yml:keepalived MASTER節點的安裝任務 keepalived_backup.yml:keepalived BACKUP節點的安裝任務 tasks/main.yml:將要執行的任務include在此文件中,並同時指定tags對各安裝任務進行區別 keepalived_master.conf.j2:MASTER節點的模板配置文件 keepalived_backup.conf.j2:BACKUP節點的模板配置文件 vars/main.yml:所以變量都定義在此文件中
check_nginx.sh的內容:
#!/bin/bash port=80 nmap localhost -p $port | grep "$port/tcp open" if [ $? -ne 0 ];then exit 10 fi
notify.sh的內容:
#!/bin/bash VIP=$2 sendmail (){ maillist=( [email protected] ) if [ "$1" == "master" ];then subject="${VIP}'s server keepalived state is translate" content="`date +'%F %T'`: `hostname`'s state change to master" for mail in ${maillist[*]};do echo $content | mail -s "$subject" $mail done elif [ "$1" == "backup" ];then subject="${VIP}'s server keepalived state is translate" content="`date +'%F %T'`: `hostname`'s state change to backup" for mail in ${maillist[*]};do echo $content | mail -s "$subject" $mail done fi } case "$1" in master) sendmail master ;; backup) sendmail backup ;; *) echo "Usage:$0 master|backup VIP" ;; esac
keepalived_master.conf.j2模板的配置內容:
! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id {{ ROUTE_ID }} } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 1 weight -20 } vrrp_instance VI_1 { state {{ MASTER }} interface {{ INTERFACE }} virtual_router_id {{ VIRTUAL_ROUTE_ID }} priority {{ PRIOTIRY_MASTER }} advert_int 1 authentication { auth_type {{ AUTH_TYPE }} auth_pass {{ AUTH_PASS }} } virtual_ipaddress { {{ VIRTUAL_IP }}/25 } track_script { chk_nginx } notify_master "/etc/keepalived/notify.sh master {{ VIRTUAL_IP }}" notify_backup "/etc/keepalived/notify.sh backup {{ VIRTUAL_IP }}" }
keepalived_backup.conf.j2模板的配置內容:
! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id {{ ROUTE_ID }} } vrrp_instance VI_1 { state {{ BACKUP }} interface {{ INTERFACE }} virtual_router_id {{ VIRTUAL_ROUTE_ID }} priority {{ PRIOTIRY_BACKUP }} advert_int 1 authentication { auth_type {{ AUTH_TYPE }} auth_pass {{ AUTH_PASS }} } virtual_ipaddress { {{ VIRTUAL_IP }}/25 } notify_master "/etc/keepalived/notify.sh master {{ VIRTUAL_IP }}" notify_backup "/etc/keepalived/notify.sh backup {{ VIRTUAL_IP }}" }
vars/main.yml變量的內容:
--- MASTER: "MASTER" BACKUP: "BACKUP" PRIOTIRY_MASTER: "100" PRIOTIRY_BACKUP: "90" VIRTUAL_ROUTE_ID: "80" VIRTUAL_IP: "10.10.5.99" AUTH_TYPE: "pass" AUTH_PASS: "111111" ROUTE_ID: "TEST" INTERFACE: "ens160"
keepalived_master.yml任務的內容:
--- - name: "安裝keepalived" yum: name: keepalived state: present - name: "復制檢測、通知腳本" copy: src={{ item }} dest=/etc/keepalived/ mode=755 with_items: - check_nginx.sh - notify.sh - name: "復制配置文件" template: src=keepalived_master.conf.j2 dest=/etc/keepalived/keepalived.conf - name: "配置keepalived日誌格式" lineinfile: path: /etc/sysconfig/keepalived regexp: 'KEEPALIVED_OPTIONS="-D"' line: 'KEEPALIVED_OPTIONS="-D -S 3"' backrefs: no - name: "創建日誌目錄" file: path: /var/log/keepalived state: directory - name: "配置rsyslog中關於keepalived的" lineinfile: path: /etc/rsyslog.conf insertafter: 'local7.* /var/log/boot.log' line: 'local3.* /var/log/keepalived/keepalived.log' - name: "配置日誌的udp" lineinfile: path: /etc/rsyslog.conf insertafter: 'UDPServerRun 514' line: '$ModLoad imudp' - name: "配置日誌的udp" lineinfile: path: /etc/rsyslog.conf insertafter: 'UDPServerRun 514' line: '$UDPServerRun 514' - name: "重啟日誌" service: name=rsyslog state=restarted - name: "啟動keepalived,設置開機自動啟動" service: name=keepalived state=started enabled=yes
keepalived_backup.yml任務的內容:
--- - name: "安裝keepalived" yum: name: keepalived state: present - name: "復制檢測、通知腳本" copy: src={{ item }} dest=/etc/keepalived/ mode=755 with_items: - notify.sh - name: "復制配置文件" template: src=keepalived_backup.conf.j2 dest=/etc/keepalived/keepalived.conf - name: "配置keepalived日誌格式" lineinfile: path: /etc/sysconfig/keepalived regexp: 'KEEPALIVED_OPTIONS="-D"' line: 'KEEPALIVED_OPTIONS="-D -S 3"' backrefs: no - name: "創建日誌目錄" file: path: /var/log/keepalived state: directory - name: "配置rsyslog中關於keepalived的" lineinfile: path: /etc/rsyslog.conf insertafter: 'local7.* /var/log/boot.log' line: 'local3.* /var/log/keepalived/keepalived.log' - name: "配置日誌的udp" lineinfile: path: /etc/rsyslog.conf insertafter: 'UDPServerRun 514' line: '$ModLoad imudp' - name: "配置日誌的udp" lineinfile: path: /etc/rsyslog.conf insertafter: 'UDPServerRun 514' line: '$UDPServerRun 514' - name: "重啟日誌" service: name=rsyslog state=restarted - name: "啟動keepalived,設置開機自動啟動" service: name=keepalived state=started enabled=yes
tasks/main.yml內容:
--- - include: keepalived_master.yml tags: master - include: keepalived_backup.yml tags: backup
如何執行安裝:
MASTER安裝命令:ansible-playbook keepalived.yml --tags master
BACKUP安裝命令:ansible-playbook keepalived.yml --tags backup
ansible-playbook安裝keepalived-指定tags安裝MASTER或BACKUP