ansible-playbook安装keepalived-指定tags安装MASTER或BACKUP

在实际工作中,新项目上线往往都需要搭建HA,此时我们可以通过ansible的playbook来秒级搭建好一个HA环境,从而解放双手;

此playbook适用于centos6、centos7的keepalived搭建,但是请注意var/main.yml中变量的修改,修改为你自己想的设定的配置

playbook的目录结构:

[[email protected] 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

原文地址:http://blog.51cto.com/liuzhengwei521/2103000

时间: 2024-10-10 04:40:18

ansible-playbook安装keepalived-指定tags安装MASTER或BACKUP的相关文章

Keepalived概述和安装(1)

一.Keepalived概述 本文主要了解开源高可用负载均衡集群利器keepalived,掌握keepalived的安装,运用keepalived配置高可用集群,并能够实现keepalived与负载均衡集群LVS的完美组合. 1.什么是keepalived? keepalived是一个类似于三.四.五层交换机的软件,也是我们平时说的第三层.第四层.第五层交换.Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,keepalived将检测到,并将有故障的

Ansible 一键配置安装Keepalived+Nginx作为前端,httpd+php作为后端

一.环境:  Ansible控制机:172.16.0.6                 Ansible nginx:172.16.0.{2|4}         Ansible Keepalived: 172.16.0.{2|4}         Ansible httpd: 172.16.0.{128|129}         Keepalived IP:192.168.220.5/32 除控制机全部采用Linux Cento7,外网统一192.168.220.0/27 一般生产机我们会把Y

运维自动化之ansible playbook安装ruby环境

本来不想打算写安装ruby的,但看几个puppet的群里有人对安装ruby比较茫然,所以这里简单介绍一下如何安装ruby. ps:话说现在也就gitlab.capistrano.puppet等软件使用ruby,最新2010年的软件好的都是python了,比如ansible.salt等. 下面是安装ruby的信息: ruby_version: 1.9.3 ruby_dir: /usr/local gem_version: 1.8.23 bundle_version: 1.6.3 可以看到ruby的

运维自动化之ansible playbook安装node环境

现在介绍如何使用ansible安装node. 下面是安装node的信息: node_dir: /data node_version: 0.10.21 node_port: 3301 可以看到node的版本是0.10.21,测试的node应用服务监听3301端口 备注:此playbook仅能对centos或者redhat的6.x版本进行安装. 下面是安装node的playbook结构 09:33:16 # tree node_* node_delete ├── files ├── handlers

运维自动化之ansible playbook安装lnmp环境

之前介绍了使用ansible playbok安装zabbix客户端.mysql服务端.nginx,现在介绍一下安装lnmp环境. 下面是安装nginx的信息: nginx_user: www                        nginx用户 nginx_port: 80                         nginx监听端口 nginx_web_dir: /data/webroot/nginx     nginx的目录 nginx_version: 1.4.3      

运维自动化之ansible playbook结合docker安装smokeping

本次介绍ansible的paly book结合docker进行虚拟机里安装2.6.8版本smokeping(apache版本是2.4.7). docker版本 09:26:53 # docker version Client version: 0.11.1 Client API version: 1.11 Go version (client): go1.2.1 Git commit (client): fb99f99/0.11.1 Server version: 0.11.1 Server A

运维自动化之ansible playbook安装lamp环境

下面介绍使用ansible playbook安装lamp环境 下面是apache的安装信息: apr_version: 1.5.0 apr_util_version: 1.5.3 libiconv_version: 1.14 apache_version: 2.4.7 apache_web_dir: /data/webroot/apache apache_log: /data/webroot/apache/logs apache_vhost: /data/webroot/apache/vhost

Jenkins+GitLab+Ansible playbook安装与基本使用

GitLab 什么是GitLab? GitLab是一个开源分布式版本控制系统,开发语言为Ruby,功能为管理项目源代码.版本控制.代码复用与查找 GitLab与GitHub的不同 github分布式在线代码托管仓库,个人版本可直接在线免费使用,企业版本收费且需要服务器安装gitlab分布式在线代码仓库托管软件,分社区免费版本与企业收费版本,都需要服务器安装 GitLab的优势和应用场景 ? 开源免费,适合中小型公司将代码放置在该系统中 ? 差异化版本管理,离线同步y以及强大分支管理功能 ? 便捷

运维自动化之ansible playbook安装mysql

上次介绍了如何使用ansible playbook安装zabbix客户端(http://dl528888.blog.51cto.com/2382721/1436745),这次介绍一下如何使用playbook安装mysql. 下面是安装mysql的信息: mysql_basedir: /data/mysql/basedir                    源码目录 mysql_datadir: /data/mysql/datadir                    数据目录 mysql