SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡

文章目录

      • 1、 keepalived - Haproxy基本思路
      • 2、keepalived
        • 2.1 keepalived安装脚本:install.sls
        • 2.2 keepalived启动脚本:service.sls
        • 2.3 keepalived相关文件及软件包
      • 3、haproxy
        • 3.1 haproxy的安装脚本:install.sls
        • 3.2 haproxy的服务脚本:service.sls
        • 3.3 haproxy的配置文件
      • 4、apache
        • 4.1 apache 的安装脚本:install.sls
      • 5、nginx
        • 5.1 nginx的安装脚本:install.sls
        • 5.2 nginx的服务脚本:service.sls
      • 5.3 nginx的相关配置文件
      • 6、 keepalived中需要用到的pillar
      • 7、全局推送脚本
      • 8、运行结果

实验环境

主机名 ip 服务
server1 172.25.2.1 salt-master
server2 172.25.2.2 salt-minion , httpd
server3 172.25.2.3 salt-minion , nginx
server4 172.25.2.4 salt-minion , haproxy、keepalived
server5 172.25.2.5 salt-minion , haproxy、keepalived

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第1张图片

1、 keepalived - Haproxy基本思路

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第2张图片

2、keepalived

[root@server1 salt]# cd /srv/salt
[root@server1 salt]# mkdir keepalived
[root@server1 keepalived]# mkdir file   #配置文件和软件包存放目录

2.1 keepalived安装脚本:install.sls

/mnt/libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm:
  file.managed:    #依赖性,镜像中没有,因此需要手动安装
    - source: salt://keepalived/file/libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm

keepalived-install: 
  pkg.installed: #依赖性
    - pkgs:
      - gcc
      - openssl-devel
      - libnl-devel

  file.managed:   #keepalived的安装包
    - name: /mnt/keepalived-2.0.6.tar.gz
    - source: salt://keepalived/file/keepalived-2.0.6.tar.gz

  cmd.run:
    - name: cd /mnt && tar zxf keepalived-2.0.6.tar.gz && yum install -y libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm && cd keepalived-2.0.6 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
    - create: /usr/local/keepalived  #如果此目录存在则不进行安装

2.2 keepalived启动脚本:service.sls

include:
  - keepalived.install   #包含了keepalived的安装脚本,实现一键部署

/etc/keepalived:
  file.directory:
    - user: root
    - group: root
    - mode: 644
    - makedirs: True

/etc/keepalived/keepalived.conf:
  file.managed:     #文件管理
    - source: salt://keepalived/file/keepalived.conf
    - mode: 644
    - template: jinja  #使用了jinja模块
    - context:
      STATE: {
     {
      pillar['state'] }}   #使用了pillar定义变量
      PRIORITY: {
     {
      pillar['priority'] }}

keepalived-service:
  service.running:
    - enable: keepalived
    - name: keepalived
    - reload: True
    - watch:
      - file: /etc/keepalived/keepalived.conf

2.3 keepalived相关文件及软件包

在这里插入图片描述

keepalived配置文件:keepalived.conf

! Configuration File for keepalived

global_defs {
     
   notification_email {
     
	root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_haproxy {
        
        script "/opt/check_haproxy.sh"
        interval 2 
        weight 2
}

vrrp_instance VI_1 {
     
    state {
     {
      STATE }}    #pillar值
    interface eth0
    virtual_router_id 51 
    priority {
     {
      PRIORITY }}   #pillar值
    advert_int 1
    authentication {
     
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     
	172.25.2.100
    }

   track_script {
          
        check_haproxy
    }
}

3、haproxy

3.1 haproxy的安装脚本:install.sls

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第3张图片

3.2 haproxy的服务脚本:service.sls

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第4张图片

3.3 haproxy的配置文件

在这里插入图片描述
更改配置文件haproxy.cfg:
SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第5张图片

4、apache

4.1 apache 的安装脚本:install.sls

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第6张图片

5、nginx

5.1 nginx的安装脚本:install.sls

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第7张图片

5.2 nginx的服务脚本:service.sls

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第8张图片

5.3 nginx的相关配置文件

在这里插入图片描述

6、 keepalived中需要用到的pillar

vim /srv/pillar/web/install.sls

{
     % if grains['fqdn'] == 'server4' %}
state: MASTER
priority: 100
{
     % elif grains['fqdn'] == 'server5' %}
state: BACKUP
priority: 50
{
     % endif %}

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第9张图片
vim pillar/top.sls

base:
  '*':
    - web.install

7、全局推送脚本

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

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第10张图片

8、运行结果

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第11张图片
SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第12张图片
SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第13张图片

SaltStack自动化运维——SaltStack部署keepalived - Haproxy高可用负载均衡_第14张图片

你可能感兴趣的:(企业,自动化运维,Linux)