Ansible之角色基础服务keepalived高可用部署

Ansible部署keepalived高可用

keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点状态,后来又加入了可以实现高可用的VRRP功能.此,keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx,Haproxy,MySQL等)的高可用解决方案软件.

keepalived软件主要是通过VRRP协议实现高可用功能的.VRRP是Virtual Router Redundancy Protocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,他能够保证当个别节点宕机时,整个网络可以不间断地运行.Keepalived一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。

思路:

  1. 安装keepalived
  2. 配置keepalived配置文件
  3. 使用到yum、file、service等模块。
  4. 启动服务测试

环境准备:

1
[root@ansible01 ~/roles]$mkdir keepalived/{vars,tasks,templates,handlers,files} -p 

安装keepalived软件

1
[root@ceph01 ~/roles]$cat keepalived/tasks/install.yml

准备keepalived配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@ceph01 ~/roles]$cat keepalived/templates/keepalived.conf.j2    
! Configuration File for keepalived

global_defs {
router_id {{ ansible_fqdn }}

}

vrrp_instance VI_1 {
{% if ansible_hostname == "lb01" %}
priority 200
state MASTER
{% elif ansible_hostname == "lb02" %}
priority 100
state BACKUP
{% endif %}
interface eth0
virtual_router_id 250
advert_int 1
nopreempt

authentication {
auth_type PASS
auth_pass 11111111
}
virtual_ipaddress {
10.1.1.50
}
}

分发keepalived配置文件

1
2
3
4
[root@ceph01 ~/roles]$cat keepalived/tasks/conf.yml 
- name: Configrue keepalived
template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
notify: restart keepalived

启动keepalived

1
2
3
4
5
6
[root@ceph01 ~/roles]$cat keepalived/tasks/start.yml 
- name: Server keepalived start
service:
name: keepalived
state: started
enabled: yes

触发器配置

1
2
3
4
5
[root@ceph01 ~/roles]$cat keepalived/handlers/main.yml 
- name: restart keepalived
service:
name: keepalived
state: restarted

tasks内容

1
2
3
4
[root@ceph01 ~/roles]$cat keepalived/tasks/main.yml 
- include_tasks: install.yml
- include_tasks: conf.yml
- include_tasks: start.yml

查看IP

1
2
3
[root@lb01 ~]$ip addr|grep 50
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 10.1.1.50/32 scope global eth0