根据业务不同来安装haproxy
第一种方式:
使用jinja模板来实现:
第二种方式:
[root@node1-saltstack prod]# mkdir /srv/salt/prod/cluster
[root@node1-saltstack prod]# mkdir /srv/salt/prod/cluster/files
haproxy配置文件:
[root@node1-saltstack salt]# cat /srv/salt/prod/cluster/files/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
#开启Haproxy Status状态监控,增加验证
listen stats
mode http
bind 0.0.0.0:8888 #haproxy的状态
stats enable
stats uri /haproxy-status
stats auth haproxy:saltstack
#前端设置
frontend frontend_www_example_com
bind 192.168.1.6: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 192.168.1.6:8080 check inter 2000 rise 30 fall 15
server web-node2 192.168.1.14:8080 check inter 2000 rise 30 fall 15
haproxy模块配置:
[root@node1-saltstack cluster]# cat 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
- include:安装haproxy
- haproxy-service:拷贝配置文件
- service.running:启动服务
- watch:监控这个haproxy-service配置文件是否有变化,如果有就reload
入口文件:
[root@node1-saltstack base]# cat /srv/salt/base/top.sls
base:
'*':
- init.env_init
prod:
'node1-saltstack':
- cluster.haproxy-outside
'node2-saltstack':
- cluster.haproxy-outside
执行:
salt '*' state.highstate
访问其中一个节点http://IP:8888