ansible自动部署Keepalived实现Nginx服务双机热备

脚本实现通过ansible-playbook自动化安装Keepalived和配置,主要解决问题如下:

  • Keepalived自动化安装;
  • keepalived_vrid配置,自动根据vip获取最后一段作为vrid,确保同一网段不会出现vrid冲突导致HA切换失败的问题;
  • 自动配置Keepalived;
  • HA检测脚本自定义,根据脚本内容,来做redis或nginx或其他软件的双机热备;
  • 自动配置vip给Keepalived
  • 设置Keepalived开机启动,加入系统服务;

Keepalived安装脚本如下:

 1 - name: keepalived install and configuration
 2   hosts: "{{ host }}"
 3   user: root
 4 
 5   tasks:
 6     - name: Create the dir
 7       file: path={{ item }} state=directory
 8       with_items:
 9         - /usr/local/keepalived
10         - /etc/keepalived
11         - /keepalived_install
12 
13     - name: install rpm pkgs for Keepalived
14       yum: name={{ item }} state=present
15       with_items:
16         - make
17         - wget
18         - gcc
19         - gcc-c++
20         - openssl
21         - openssl-devel
22         - popt-devel
23         - automake
24         - autoconf
25         - libtool
26         - ipvsadm
27         - popt-devel
28         - popt-static
29         - libnl-devel
30         - libnfnetlink-devel
31         - nmap
32 
33     - name: download keepalived
34       get_url: url=https://www.keepalived.org/software/keepalived-1.2.19.tar.gz dest=/keepalived_install
35 
36     - name: unarchive keepalived
37       unarchive: src=/keepalived_install/keepalived-1.2.19.tar.gz dest=/keepalived_install copy=no
38 
39     - name: compile and install keepalived
40       shell: cd /keepalived_install/keepalived-1.2.19 && ./configure --prefix=/usr/local/keepalived && make && make install
41 
42     - name: compile and install keepalived
43       command: "{{ item }}"
44       with_items:
45         - /bin/cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
46         - /bin/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
47         - /bin/cp /usr/local/keepalived/sbin/keepalived /bin/keepalived
48         - /bin/chmod +x /etc/init.d/keepalived
49         - /sbin/chkconfig --add keepalived
50         - /sbin/chkconfig --level 345 keepalived on
51 
52     - name: configure keepalived
53       template: src=/ansible/roles/test/template/keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
54       notify: restart keepalived
55 
56     - name: copy nginx service check scripts to remote host
57       template: src=/ansible/roles/test/template/check_nginx.sh.j2 dest=/usr/local/keepalived/check_nginx.sh mode=0755
58 
59     - name: copy vrid config_scripts to remote host
60       template: src=/ansible/roles/test/template/replace_vrid.sh.j2 dest=/tmp/keepalived.sh mode=0755
61 
62     - name: modify keepalived_vrid 
63       shell: sh /tmp/keepalived.sh
64 
65     - name: delete the tmp files.
66       file: path={{ item }} state=absent
67       with_items:
68         - /keepalived_install/keepalived-1.2.19.tar.gz
69         - /keepalived_install/keepalived-1.2.19
70         - /keepalived_install
71         - /tmp/keepalived.sh
72 
73   handlers:
74     - name: config vrid
75       shell: bash /tmp/keepalived.sh
76 
77   handlers:
78     - name: restart keepalived
79       service: name=keepalived enabled=yes state=restarted
keepalived_install.yml

使用方法:

Usage: ansible-playbook -i /tmp/testhost /ansible/roles/keepalived/tasks/keepalived_install.yml -e "{'host':'10.99.99.99','nginx_havip':'10.99.99.100'}"

 

Keepalived配置模板

 1 ! Configuration File for keepalived
 2 
 3 global_defs {
 4    router_id Nginx
 5 }
 6 
 7 vrrp_script chk_nginx {
 8     script "/usr/local/keepalived/check_nginx.sh"
 9     interval 2
10     fall 3
11     weight -5
12     rise 1
13 }
14 
15 vrrp_instance VI_1 {
16     state BACKUP
17     interface {{ ansible_default_ipv4['alias'] }}
18     virtual_router_id keepalived_vrid 
19     priority 90
20     nopreempt
21     advert_int 1
22     authentication {
23         auth_type PASS
24         auth_pass 1111
25     }
26     virtual_ipaddress {
27         {{ nginx_havip }}
28     }
29     track_script {
30         chk_nginx
31    }
32 }
keepalived.conf.j2

NGINX服务检测脚本模板

 1 #!/bin/sh
 2 # check nginx server status
 3 
 4 # Source Function Library
 5 . /etc/init.d/functions
 6 
 7 NGINX="/usr/local/nginx/sbin/nginx"
 8 NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
 9 NGINX_PID="/usr/local/nginx/logs/nginx.pid"
10 PORT=80
11 
12 start_nginx() {
13     daemon $NGINX -c $NGINX_CONF
14 }
15 
16 stop_nginx() {
17     killproc -p $NGINX_PID $NGINX -TERM
18 }
19 
20 nmap localhost -p $PORT | grep "$PORT/tcp open"
21 
22 if [ $? -ne 0 ];then
23     stop_nginx
24     start_nginx 
25     sleep 3
26     nmap localhost -p $PORT | grep "$PORT/tcp open"
27     [ $? -ne 0 ] && /etc/init.d/keepalived stop
28 fi
check_nginx.sh.j2

keepalived配置中虚拟路由id替换脚本

1 #!/bin/sh
2 havip={{ nginx_havip }}
3 vrid=`echo ${havip##*.}`
4 sed -i "s/keepalived_vrid/$vrid/" /etc/keepalived/keepalived.conf
replace_vrid.sh.j2

说明:

执行此脚本之前,需要安装nginx。

posted @ 2018-09-06 11:11  冷水泡茶  阅读(888)  评论(0编辑  收藏  举报