自动化运维——Saltstack部署Keepalived高可用集群

1. 简单了解一下Keepalived

  • Keepalived是Linux下一个轻量级别的高可用解决方案。

  • Keepalived是基于vrrp协议的一款高可用软件。

  • Keepailived有一台主服务器和多台备份服务器,在主服务器和备份服务器上面部署相同的服务配置,使用一个虚拟IP地址对外提供服务,当主服务器出现故障时,虚拟IP地址会自动漂移到备份服务器。

2. 实验环境

主机名(IP)           		服务
server1(172.25.11.1)	salt-master
server2(172.25.11.2)	salt-minion,keeplived
server3(172.25.11.3)	salt-minion,keeplived

3. 部署过程

注:部署过程在《saltstack自动化部署apache服务和nginx服务》该篇博文基础上完成。

在server1上的saltstack的配置目录/srv/salt下建立相应的keepalived的目录并且建立其配置文件存放目录。

[root@server1 nginx]# cd /srv/salt/
[root@server1 salt]# mkdir keepalived
[root@server1 salt]# cd keepalived/
[root@server1 keepalived]# cp /srv/salt/apache/install.sls .
[root@server1 keepalived]# ls
files  install.sls

编辑keepalived的安装文件install.sls

[root@server1 keepalived]# vim install.sls    #编辑keepalived的安装文件
install-keepalived:
  pkg.installed:
    - pkgs:
      - keepalived

  file.managed:
    - name: /etc/keepalived/keepalived.conf
    - source: salt://keepalived/files/keepalived.conf
    - template: jinja    #使用jinja模版
    - context:
      STATE: {{ pillar['state'] }}     #使用pillar模版获取参数
      VRID: {{ pillar['vrid'] }}
      PRIORITY: {{ pillar['priority'] }}
  service.running:
    - name: keepalived
    - watch:
      - file: install-keepalived

自动化运维——Saltstack部署Keepalived高可用集群_第1张图片

编辑keepalived的配置文件,设置在jinja模版中导入的相应参数

[root@server1 files]# cd /srv/salt/keepalived/
[root@server1 keepalived]# mkdir files
[root@server1 keepalived]# cd files/
[root@server1 files]# cp /etc/keepalived/keepalived.conf .

[root@server1 files]# vim keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state {{ STATE }}
    interface eth0
    virtual_router_id {{ VRID }}
    priority {{ PRIORITY }}
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.11.100
    }
}

自动化运维——Saltstack部署Keepalived高可用集群_第2张图片

编辑相应的pillar文件

{% if grains['fqdn'] == 'server2' %}
state: MASTER
vrid: 11
priority: 100
ip: 172.25.11.2
port: 80
{% elif grains['fqdn'] == 'server3' %}
state: BACKUP
vrid:11
priority: 50
ip: 172.25.11.3
port: 80
{% endif %}

编辑顶层top文件,分别在server2和server3上安装keepalived及其测试要用到的nginx和apache

[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# vim top.sls

base:
  'server2':
    - keepalived.install
    - apache.service
  'server3':
    - keepalived.install
    - nginx.service

进行推送

[root@server1 salt]# salt '*' state.highstate

查看vip,此时vip在server2上,关闭server2的keepalived服务后查看vip的漂移。

你可能感兴趣的:(saltstack)