码迷,mamicode.com
首页 > 其他好文 > 详细

saltstack实践haproxy+keepalived

时间:2017-03-09 01:02:33      阅读:759      评论:0      收藏:0      [点我收藏+]

标签:自动化运维 saltstack ansible

172.16.10.199 fonsview     作为minion

172.16.10.128  controller   作为master


[root@controller 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


[root@controller cluster]# cd /srv/salt/

[root@controller 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


[root@controller salt]# cat base/top.sls 

base:

  ‘*‘:

    - init.init


prod:

  ‘*‘:

#    - cluster.haproxy-outside

    - cluster.haproxy-outside-keepalived


[root@controller 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


[root@controller salt]# cat base/init/init.sls 

include:

  - init.dns

  - init.history

  - init.audit

  - init.sysctl

#  - init.epel

  - init.zabbix-agent


[root@controller salt]# cd prod/

[root@controller 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

[root@controller 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

[root@controller prod]# cat modules/pkg/make.sls 
make-pkg:
  pkg.installed:
    - pkgs:
      - make
      - gcc
      - gcc-c++
      - autoconf
      - openssl
      - openssl-devel
      - pcre
      - pcre-devel
      
      
[root@controller 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安装

[root@controller 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
    
    
引入配置文件
[root@controller 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


执行高级状态

[root@controller cluster]# salt ‘*‘ state.highstate  

验证结果
[root@controller 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
[root@controller prod]# 
[root@controller 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

标签:自动化运维 saltstack ansible

原文地址:http://huaxin.blog.51cto.com/903026/1904572

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!