1、编写功能模块
1)首先编写依赖安装模块
[[email protected] ~]# mkdir -p /srv/salt/prod/pkg /srv/salt/prod/haproxy /srv/salt/prod/haproxy/files
[[email protected] pkg]# vim pkg-init.sls
pkg-init:
pkg.installed:
- names:
- gcc
- gcc-c++
- glibc
- make
- autoconf
- openssl
- openssl-devel
2)编写HAproxy状态模块
如何写状态模块?1、安装一遍,将安装步骤记录;2、将配置文件,启动文件等cp到/srv/salt/prod/*/files下
a)获取启动脚本,并copy到/srv/salt/prod/haproxy/files/
[[email protected] ~]# mv haproxy-1.6.2.tar.gz /srv/salt/prod/haproxy/files/
- [[email protected] ~]# cd /srv/salt/prod/haproxy/files/
[[email protected] files]# tar zxf haproxy-1.6.2.tar.gz
[[email protected] files]# cd haproxy-1.6.2/examples/
[[email protected] examples]# vim haproxy.init
35 BIN=/usr/local/haporxy/sbin/$BASENAME
[[email protected] examples]# cp haproxy.init /srv/salt/prod/haproxy/files/
[[email protected] examples]# cd /srv/salt/prod/haproxy/files
[[email protected] files]# rm -rf haproxy-1.6.2
b)编写install.sls
不在这里写配置文件,是为了解耦。因为安装和启动时原子操作,在哪都必须,但是配置文件,在不同环境下是不一样的
[[email protected] examples]# cd /srv/salt/prod/haproxy/
[[email protected] haproxy]# vim install.sls #顺序一定要正确,从上往下解析
include:
- pkg.pkg-init
haproxy-install: #id声明,id唯一,id名称没有意义,主要是为了方便标识
file.managed: #一个ID下一个模块只能使用一次,不能冲突
- name: /usr/local/src/haproxy-1.6.2.tar.gz
- source: salt://haproxy/files/haproxy-1.6.2.tar.gz
- user: root
- group: root
- mode: 755
cmd.run:
- name: cd /usr/local/src && tar zxf haproxy-1.6.2.tar.gz && cd haproxy-1.6.2 && make TARGET=linux26 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy
- unless: test -d /usr/local/haproxy # 当unless选项指向的命令返回false才执行name指向的命令。还有个onlyif,当onlyif指向的命令返回true才执行name指向的命令
- require: #依赖后续的状态
- pkg: pkg-init
- file: haproxy-install #依赖haproxy-install这个ID中的file模块
/etc/init.d/haproxy: #name声明
file.managed:
- source: salt://haproxy/files/haproxy.init
- user: root
- group: root
- mode: 755
- require:
- cmd: haproxy-install
cmd.run:
- name: chkconfig --add haproxy
- unless: chkconfig --list | grep haproxy
- require:
- file: /etc/init.d/haproxy
net.ipv4.ip_nonlocal_bind:
sysctl.present:
- value: 1
haproxy-config-dir:
file.directory:
- name: /etc/haproxy
- user: root
- group: root
- mode: 755
[[email protected] src]# salt ‘linux-node1.*‘ state.sls haproxy.install env=prod
linux-node1.example.com:
----------
- ......
Summary
-------------
Succeeded: 13 (changed=3)
Failed: 0
-------------
Total states run: 13
2、编写业务引用 - HAproxy配置文件
[[email protected]-node1 files]# mkdir -p /srv/salt/prod/cluster/files
[[email protected]-node1 files]# cd /srv/salt/prod/cluster/files/
[[email protected]-node1 files]# vim haproxy-outside.cfg
global
maxconn 100000
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
maxconn 100000
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen stats
mode http
bind 0.0.0.0:8888
stats enable
stats uri /haproxy-status
stats auth haproxy:saltstack
frontend frontend_www_example_com
bind 10.0.0.11:80
mode http
option httplog
log global
default_backend backend_www_example_com
backend backend_www_example_com
option forwardfor header X-REAL-IP
option httpchk HEAD / HTTP/1.0
balance source
server web-node1 10.0.0.7:8080 check inter 2000 rise 30 fall 15 #生产环境除了负载均衡,其他服务都不监听80端口,建议改成8080,好处是:1、只有负载均衡监听80;2、普通用户可以起服务,不需要root用户
server web-node2 10.0.0.8:8080 check inter 2000 rise 30 fall 15
[[email protected]-node1 files]#cd ..
[[email protected]-node1 cluster]# vim haproxy-outside.sls
include:
- haproxy.install
haproxy-service:
file.managed:
- name: /etc/haproxy/haproxy.cfg
- source: salt://cluster/files/haproxy-outside.cfg
- user: root
- group: root
- mode: 644
service.running:
- name: haproxy
- enable: True
- reload: True
- require:
- cmd: haproxy-init
- watch:
- file: haproxy-service
[[email protected]-node1 ~]# cd /srv/salt/base/
[[email protected]-node1 base]# vim top.sls
base:
‘*‘:
- init.env_init
prod:
‘linux-node[1-2].example.com‘:
- cluster.haproxy-outside
[[email protected]-node1 base]# salt ‘*‘ state.highstate
linux-node1.example.com:
----------
- ......
Summary
-------------
Succeeded: 21 (unchanged=2, changed=1)
Failed: 0
-------------
Total states run: 21
linux-node2.example.com:
----------
- ......
Summary
-------------
Succeeded: 21 (unchanged=9, changed=3)
Failed: 0
-------------
Total states run: 21
3、Web查看服务状态
从web登陆10.0.0.7:8888/haproxy-status,用户名和密码在/srv/salt/prod/cluster/files/haproxy-outside.cfg中
[[email protected]-node1 base]# grep ‘auth‘ /srv/salt/prod/cluster/files/haproxy-outside.cfg
stats auth haproxy:saltstack
时间: 2024-11-04 20:20:31