saltstack实践haproxy+keepalived

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack实践haproxy+keepalived相关的知识,希望对你有一定的参考价值。

172.16.10.199 fonsview     作为minion

172.16.10.128  controller   作为master


[[email protected] cluster]# vim /etc/salt/master

file_roots:

  base:

    - /srv/salt/base

  prod:

    - /srv/salt/prod


pillar_roots:

  base:

    - /srv/pillar/base

  prod:

    - /srv/pillar/prod


[[email protected] cluster]# cd /srv/salt/

[[email protected] salt]# ll

total 8

drwxr-xr-x 3 root root 4096 Mar  5 14:41 base

drwxr-xr-x 4 root root 4096 Mar  5 10:43 prod


[[email protected] salt]# cat base/top.sls 

base:

  ‘*‘:

    - init.init


prod:

  ‘*‘:

#    - cluster.haproxy-outside

    - cluster.haproxy-outside-keepalived


[[email protected] salt]# tree base/   #这里都是定义一些初始化的

base/

├── init

│   ├── audit.sls

│   ├── dns.sls

│   ├── epel.sls

│   ├── files

│   │   ├── resolv.conf

│   │   └── zabbix_agentd.conf

│   ├── history.sls

│   ├── init.sls

│   ├── sysctl.sls

│   └── zabbix-agent.sls

└── top.sls


2 directories, 10 files


[[email protected] salt]# cat base/init/init.sls 

include:

  - init.dns

  - init.history

  - init.audit

  - init.sysctl

#  - init.epel

  - init.zabbix-agent


[[email protected] salt]# cd prod/

[[email protected] prod]# ll

total 8

drwxr-xr-x 3 root root 4096 Mar  5 12:05 cluster

drwxr-xr-x 8 root root 4096 Mar  5 10:43 modules

[[email protected] prod]# tree

.

├── cluster

│   ├── files

│   │   ├── haproxy-outside.cfg

│   │   └── haproxy-outside-keepalived.conf

│   ├── haproxy-outside-keepalived.sls

│   └── haproxy-outside.sls

└── modules

    ├── haproxy

    │   ├── files

    │   │   ├── haproxy-1.6.3.tar.gz

    │   │   └── haproxy.init

    │   └── install.sls

    ├── keepalived

    │   ├── files

    │   │   ├── keepalived-1.2.17.tar.gz

    │   │   ├── keepalived.init

    │   │   └── keepalived.sysconfig

    │   └── install.sls

    ├── memecached

    ├── nginx

    ├── php

    └── pkg

        └── make.sls


11 directories, 12 files

[[email protected] prod]# cat modules/pkg/make.sls 
make-pkg:
  pkg.installed:
    - pkgs:
      - make
      - gcc
      - gcc-c++
      - autoconf
      - openssl
      - openssl-devel
      - pcre
      - pcre-devel
      
      
[[email protected] prod]# cat modules/haproxy/install.sls 
include:
  - modules.pkg.make

haproxy-install:
  file.managed:
    - name: /usr/local/src/haproxy-1.6.3.tar.gz
    - source: salt://modules/haproxy/files/haproxy-1.6.3.tar.gz
    - mode: 755
    - user: root
    - group: root
  cmd.run:
    - name: cd /usr/local/src && tar xf haproxy-1.6.3.tar.gz && cd haproxy-1.6.3 && make TARGET=linux26 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy 
    - unless: test -d /usr/local/haproxy
    - require:
      - pkg: make-pkg
      - file: haproxy-install

haproxy-init:
  file.managed:
    - name: /etc/init.d/haproxy
    - source: salt://modules/haproxy/files/haproxy.init
    - mode: 755
    - user: root
    - group: root
    - require_in:
      - file: haproxy-install
  cmd.run:
    - name: chkconfig --add haproxy
    - unless: chkconfig --list | grep haproxy

net.ipv4.ip_nonlocal_bind:
  sysctl.present:
    - value: 1

/etc/haproxy:
  file.directory:
    - user: root
    - group: root
    - mode: 755


定义keepalived安装

[[email protected] prod]# cat modules/keepalived/install.sls 
{% set keepalived_tar = ‘keepalived-1.2.17.tar.gz‘ %}
keepalived-install:
  file.managed:
    - name: /usr/local/src/{{ keepalived_tar }}
    - source: salt://modules/keepalived/files/{{ keepalived_tar }}
    - mode: 755
    - user: root
    - group: root
  cmd.run:
    - name: cd /usr/local/src && tar zxf keepalived-1.2.17.tar.gz && cd keepalived-1.2.17 && ./configure --prefix=/usr/local/keepalived --disable-fwmark && make && make install
    - unless: test -d /usr/local/keepalived
    - require:
      - file: keepalived-install

/etc/sysconfig/keepalived:
  file.managed:
    - source: salt://modules/keepalived/files/keepalived.sysconfig
    - mode: 644
    - user: root
    - group: root

/etc/init.d/keepalived:
  file.managed:
    - source: salt://modules/keepalived/files/keepalived.init
    - mode: 755
    - user: root
    - group: root

keepalived-init:
  cmd.run:
    - name: chkconfig --add keepalived
    - unless: chkconfig --list | grep keepalived
    - require:
      - file: /etc/init.d/keepalived

/etc/keepalived:
  file.directory:
    - user: root
    - group: root
    
    
引入配置文件
[[email protected] prod]# cat cluster/haproxy-outside-keepalived.sls 
include:
  - modules.keepalived.install
keepalived-server:
  file.managed:
    - name: /etc/keepalived/keepalived.conf
    - source: salt://cluster/files/haproxy-outside-keepalived.conf
    - mode: 644
    - user: root
    - group: root
    - template: jinja
    {% if grains[‘fqdn‘] == ‘controller‘ %}
    - ROUTEID: haproxy_ha
    - STATEID: MASTER
    - PRIORITYID: 150
    {% elif grains[‘fqdn‘] == ‘fonsview‘ %}
    - ROUTEID: haproxy_ha
    - STATEID: BACKUP
    - PRIORITYID: 100
    {% endif %}
  service.running:
    - name: keepalived
    - enable: True
    - watch:
      - file: keepalived-server


执行高级状态

[[email protected] cluster]# salt ‘*‘ state.highstate  

验证结果
[[email protected] prod]# salt ‘*‘ cmd.run ‘ps -ef|grep haproxy‘
fonsview:
    nobody     7097      1  0 00:16 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
    root       8462   8461  0 01:10 ?        00:00:00 /bin/sh -c ps -ef|grep haproxy
    root       8464   8462  0 01:10 ?        00:00:00 grep haproxy
controller:
    nobody     3005      1  0 14:12 ?        00:00:01 /usr/local/haproxy/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
    root       7316 124173 44 15:07 pts/1    00:00:00 /usr/bin/python /usr/bin/salt * cmd.run ps -ef|grep haproxy
    root       7334   7333  0 15:07 ?        00:00:00 /bin/sh -c ps -ef|grep haproxy
    root       7336   7334  0 15:07 ?        00:00:00 grep haproxy
[[email protected] prod]# 
[[email protected] prod]# salt ‘*‘ cmd.run ‘ps -ef|grep keepali‘
controller:
    root       7339 124173  0 15:07 pts/1    00:00:00 /usr/bin/python /usr/bin/salt * cmd.run ps -ef|grep keepali
    root       7357   7356  0 15:07 ?        00:00:00 /bin/sh -c ps -ef|grep keepali
    root       7359   7357  0 15:07 ?        00:00:00 grep keepali
fonsview:
    root       7560      1  0 00:46 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
    root       7562   7560  0 00:46 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
    root       7563   7560  0 00:46 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
    root       8470   8469  0 01:10 ?        00:00:00 /bin/sh -c ps -ef|grep keepali
    root       8472   8470  0 01:10 ?        00:00:00 /bin/sh -c ps -ef|grep keepali


技术分享

本文出自 “厚德载物” 博客,谢绝转载!

以上是关于saltstack实践haproxy+keepalived的主要内容,如果未能解决你的问题,请参考以下文章

haproxy+keepalive负载均衡环境部署(主主模式)

saltstack配置安装haproxy

阿里云ecs能搭建haproxy+keepalive吗

实现 KeepAlive + Haproxy + Nginx 七层负载均衡 + 动静分离

keepalive高可用haproxy实现URL资源的动静分离

Saltstack生产案例之Haproxy安装