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: file.managed: - 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 - require: - pkg: pkg-init - file: haproxy-install /etc/init.d/haproxy: 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] files]# mkdir -p /srv/salt/prod/cluster/files [[email protected] files]# cd /srv/salt/prod/cluster/files/ [[email protected] 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 # server web-node2 10.0.0.8:8080 check inter 2000 rise 30 fall 15 [[email protected] files]#cd .. [[email protected] 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] ~]# cd /srv/salt/base/ [[email protected] base]# vim top.sls base: ‘*‘: - init.env_init prod: ‘linux-node[1-2].example.com‘: - cluster.haproxy-outside [[email protected] 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] base]# grep ‘auth‘ /srv/salt/prod/cluster/files/haproxy-outside.cfg stats auth haproxy:saltstack
时间: 2024-11-13 00:26:22