脚本实现通过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。

ansible自动部署Keepalived实现Nginx服务双机热备的更多相关文章

  1. Nginx+keepalived 脚本安装主从双机热备自动切换解决方案

    Nginx+keepalived 脚本安装主从双机热备自动切换解决方案 2013-07-02 19:24:13 标签:filesnginx keepalived 原创作品,允许转载,转载时请务必以超链 ...

  2. Nginx+Keeplived双机热备(主从模式)

    Nginx+Keeplived双机热备(主从模式) 参考资料: http://www.cnblogs.com/kevingrace/p/6138185.html 双机高可用一般是通过虚拟IP(漂移IP ...

  3. nginx简单双机热备:backup参数的使用

    nginx简单双机热备:backup参数的使用 nginx简单双机热备:backup参数的使用

  4. keepalived+nginx实现双机热备

    keepalived是一个类似于layer3, 4, 5 交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机, ...

  5. 使用keepalived监控tomcat 达到双机热备

    通常说的双机热备是指两台机器都在运行,但并不是两台机器都同时在提供服务. 当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,而且切换的时间非常短.下面来以keepalived结合to ...

  6. HA(High available)-Keepalived高可用性集群(双机热备)单点实验-菜鸟入门级

    HA(High available)-Keepalived高可用性集群   Keepalived 是一个基于VRRP虚拟路由冗余协议来实现的WEB 服务高可用方案,虚拟路由冗余协议 (Virtual ...

  7. keepalived + nginx 实现双机热备

    # docker run -itd --name centos_m1 centos # 进入容器 # docker exec -it centos_m1 /bin/bash # 安装nginx # r ...

  8. KeepAlived+HaProxy+MyCat+Percona双机热备PXC集群

    一.搭建PXC集群 1.环境:centos7+PXC5.7.21+mycat1.6.5 2.卸载mariadb rpm -qa | grep mariadb* yum -y remove mariad ...

  9. keepalived+LVS 实现双机热备、负载均衡、失效转移 高性能 高可用 高伸缩性 服务器集群

    本章笔者亲自动手,使用LVS技术实现实现一个可以支持庞大访问量.高可用性.高伸缩性的服务器集群 在读本章之前,可能有不少读者尚未使用该技术,或者部分读者使用Nginx实现应用层的负载均衡.这里大家都可 ...

随机推荐

  1. 使用extentreports美化testng报告2,增加监听

    有兴趣研究了extentreports报告美化插件,但是因为发现插件有很多内容不能自定义,所以放弃了这个插件,我扩充了官方代码的demo,在testng中增加了监听,并打印了一些测试用例,现在我把两个 ...

  2. when 让你跳出异步回调噩梦 node.js下promise/A规范的使用

    其实关于promise 的博客,前端时间专门写了一篇关于 promise 规范的文章,promise规范 让 javascript 中的异步调用更加人性化. 简单回忆下: promise/A规范定义的 ...

  3. python剑指offer数组中出现次数超过一半的数字

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  4. MySQL中的if和case语句使用总结

    create table test( id int primary key auto_increment, name ), sex int ) ),(),(),() ,'男','女') from te ...

  5. 6.安装使用vue-quill-editor

    前言: 在vue项目中,因为涉及到使用文本编辑器, 恰恰vue-quill-editor就是一个简单实用的富文本编辑器. 参考文档:vue中使用vue-quill-editor富文本编辑器,自定义to ...

  6. Nodejs 调试方法

    nodejs内部提供一个debug机制,可以让程序进入debug模式,供开发者一步一步分析代码发现问题. 共有3中启动参数可以让程序进入debug模式,假设我们要对app.js进行调试. node d ...

  7. 文本处理工具-AWK

    awk简介 awk功能与sed相似,都是用来进行文本处理的.awk可以自动地搜索输入文件,并把每一个输入行切分成字段.许多工作都是自动完成的,例如读取每个输入行.字段分割. awk工作原理 awk一次 ...

  8. Linux实战教学笔记05:远程SSH连接服务与基本排错

    第1章 远程连接LInux系统管理 1.1 为什么要远程连接Linux系统 在实际的工作场景中,虚拟机界面或物理服务器本地的窗口都是很少能够接触到的,因为服务器装完系统后,都要拉到IDC机房托管,如果 ...

  9. select值改变

    改变select的值,然后执行一个方法.可以用chang: $("#select").change(function(){ //要执行的内容 });

  10. Urllib库:python内置的http请求库

    1.四个模块: request error parse robotparser 2.urlopen(url, data, timeout) 发送请求 get请求无data: post请求有data 3 ...