ansible-playbook自动化安装Keepalived实现Nginx服务双机热备自动化配置

Posted 冷水泡茶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible-playbook自动化安装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=http://10.86.87.142/evunsoft/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

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

说明:

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

 

以上是关于ansible-playbook自动化安装Keepalived实现Nginx服务双机热备自动化配置的主要内容,如果未能解决你的问题,请参考以下文章

ansible-playbook实现MySQL的二进制部署

ansible-playbook基于role的配置一键安装zabbix客户端以及拉取自定义监控脚本

ansible自动运维工具之ansible-playbook详解

ansible-playbook roles 安装zabbix-agent 监控 php

ansible-playbook实例

运维自动化工具 Ansible-playbook