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

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

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

Keepalived安装脚本如下:

 - name: keepalived install and configuration
hosts: "{{ host }}"
user: root tasks:
- name: Create the dir
file: path={{ item }} state=directory
with_items:
- /usr/local/keepalived
- /etc/keepalived
- /keepalived_install - name: install rpm pkgs for Keepalived
yum: name={{ item }} state=present
with_items:
- make
- wget
- gcc
- gcc-c++
- openssl
- openssl-devel
- popt-devel
- automake
- autoconf
- libtool
- ipvsadm
- popt-devel
- popt-static
- libnl-devel
- libnfnetlink-devel
- nmap - name: download keepalived
get_url: url=https://www.keepalived.org/software/keepalived-1.2.19.tar.gz dest=/keepalived_install - name: unarchive keepalived
unarchive: src=/keepalived_install/keepalived-1.2.19.tar.gz dest=/keepalived_install copy=no - name: compile and install keepalived
shell: cd /keepalived_install/keepalived-1.2.19 && ./configure --prefix=/usr/local/keepalived && make && make install - name: compile and install keepalived
command: "{{ item }}"
with_items:
- /bin/cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
- /bin/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
- /bin/cp /usr/local/keepalived/sbin/keepalived /bin/keepalived
- /bin/chmod +x /etc/init.d/keepalived
- /sbin/chkconfig --add keepalived
- /sbin/chkconfig --level 345 keepalived on - name: configure keepalived
template: src=/ansible/roles/test/template/keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
notify: restart keepalived - name: copy nginx service check scripts to remote host
template: src=/ansible/roles/test/template/check_nginx.sh.j2 dest=/usr/local/keepalived/check_nginx.sh mode=0755 - name: copy vrid config_scripts to remote host
template: src=/ansible/roles/test/template/replace_vrid.sh.j2 dest=/tmp/keepalived.sh mode=0755 - name: modify keepalived_vrid
shell: sh /tmp/keepalived.sh - name: delete the tmp files.
file: path={{ item }} state=absent
with_items:
- /keepalived_install/keepalived-1.2.19.tar.gz
- /keepalived_install/keepalived-1.2.19
- /keepalived_install
- /tmp/keepalived.sh handlers:
- name: config vrid
shell: bash /tmp/keepalived.sh handlers:
- name: restart keepalived
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配置模板

 ! Configuration File for keepalived

 global_defs {
router_id Nginx
} vrrp_script chk_nginx {
script "/usr/local/keepalived/check_nginx.sh"
interval 2
fall 3
weight -5
rise 1
} vrrp_instance VI_1 {
state BACKUP
interface {{ ansible_default_ipv4['alias'] }}
virtual_router_id keepalived_vrid
priority 90
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
{{ nginx_havip }}
}
track_script {
chk_nginx
}
}

keepalived.conf.j2

NGINX服务检测脚本模板

 #!/bin/sh
# check nginx server status # Source Function Library
. /etc/init.d/functions NGINX="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
PORT= start_nginx() {
daemon $NGINX -c $NGINX_CONF
} stop_nginx() {
killproc -p $NGINX_PID $NGINX -TERM
} nmap localhost -p $PORT | grep "$PORT/tcp open" if [ $? -ne ];then
stop_nginx
start_nginx
sleep
nmap localhost -p $PORT | grep "$PORT/tcp open"
[ $? -ne ] && /etc/init.d/keepalived stop
fi

check_nginx.sh.j2

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

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

replace_vrid.sh.j2

说明:

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

上一篇:msnodesql的使用


下一篇:C#简易播放器(基于开源VLC)