saltstack功能模組
阿新 • • 發佈:2018-11-30
[[email protected] ~]# mkdir /srv/salt/prod/pkg
[[email protected] ~]# mkdir /srv/salt/prod/haproxy
[[email protected] ~]# mkdir /srv/salt/prod/haproxy/files
[[email protected] pkg]# pwd
/srv/salt/prod/pkg
[[email protected] pkg]# vim pkg-init.sls
[[email protected] pkg]# cat pkg-init.sls
pkg-init:
pkg.installed:
- names:
- gcc
- gcc-c++
- glibe
- make
- autoconf
- openssl
- openssl-devel
[ [email protected] prod]# cd haproxy/files/
[[email protected] files]# wget https://fossies.org/linux/misc/haproxy-1.8.14.tar.gz
[[email protected] files]# cp haproxy-1.8.14.tar.gz /usr/local/src
[[email protected] src]# pwd
/usr/local/src
[[email protected] src]# tar -zxf haproxy-1.8.14.tar.gz
[ [email protected] src]# cd haproxy-1.8.14
[[email protected] haproxy-1.8.14]# less README
[[email protected] haproxy-1.8.14]# uname -a
Linux master.com 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] haproxy-1.8.14]# make TARGET=linux26 PREEFIX=/usr/local/haproxy &&make install PREFIX=/usr/local/haproxy
[ [email protected] haproxy-1.8.14]# cd examples/
[[email protected] examples]# vim haproxy.init
BIN=/usr/local/haproxy/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]# cd ..
[[email protected] haproxy]# ls
files
狀態模組:條件判斷,用於cmd狀態模組
onlyif: 檢查的命令,僅當onlyif選項指向的命令返回true時才執行name定義的命令。
unless: 用於檢查的命令,僅當unless選項指向的命令返回false時才執行name指向的命令。
require: 解決依賴
[[email protected] haproxy]# vim install.sls
[[email protected] haproxy]# cat install.sls
include:
- pkg.pkg-init #pkg模組呼叫自定義的pkg-init
haproxy-install: #id
file.managed: #file模組的managed的方法
- name: /usr/local/src/haproxy-1.8.14.tar.gz
- source: salt://haproxy/files/haproxy-1.8.14.tar.gz #等於/srv/salt/prod/haproxy/files/haproxy-1.8.14.tar.gz
- user: root
- group: root
- mode: 755 #許可權
cmd.run: #cmd模組的run方法
- name: cd /usr/local/src && tar -zxf haproxy-1.8.14.tar.gz && cd haproxy-1.8.14 && make TARGET=linux26 PREEFIX=/usr/local/haproxy &&make install PREFIX=/usr/local/haproxy
- unless: test -d /usr/local/haproxy #unless用於檢測, test -d /usr/local/haproxy 檢測檔案是否存在,並返回,返回值
- require: #依賴
- pkg: pkg-init #執行了pkg-init才能執行cmd.run
- file: haproxy-install #執行了id為haproy-install的命令才能執行cmd.run
haproxy-init:
file.managed: #file模組的managed的方法
- name: /etc/init.d/haproxy
- source: salt://haproxy/files/haproxy.init #同上
- user: root
- group: root
- mode: 755
- require: #依賴
- cmd: haproxy-install #執行了id為haproxy-install的命令才能執行file.managed
cmd.run:
- name: chkconfig --add haproxy
- unles: chkconfig --list |grep haproxy #判斷是否有haproxy,並返回,返回值
- require:
- file: haproxy-init 執行了id為haproy-init的命令才能執行cmd.run
net.ipv4.ip_nonlocal_bind: #開啟自己的ipv4監聽
sysctl.present:
- value: 1
haproxy-config-dir:
file.directory:
- name: /etc/haproxy
- user: root
- group: root
- mode: 755
[[email protected] haproxy]# salt '*' state.sls haproxy.install env=prod #env=prod 預設是載入的base檔案,haproxy.install 為轉態可以自定義名稱
一個id下不能寫相同的模組
[[email protected] prod]# mkdir /srv/salt/prod/cluster
[[email protected] prod]# mkdir /srv/salt/prod/cluster/files
[[email protected] prod]# cd /srv/salt/prod/cluster/files
[[email protected] files]# vim haproxy-outside.cfg
[[email protected] files]# cat 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
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 192.168.43.119:80
mode http
option httplog
log global
default_backend backend_www_example_com
backend backend_www_example_com
option forwardfor header X-REA-IP
option httpchk HEAD / HTTP/1.0
balance source
server web-node1 192.168.43.118:8080 check inter 2000 rise 30 fall 15
server web-node2 192.168.43.71:8080 check inter 2000 rise 30 fall 15
[[email protected] cluster]# pwd
/srv/salt/prod/cluster
[[email protected] cluster]# tree
.
└── files
└── haproxy-outside.cfg
1 directory, 1 file
[[email protected] cluster]# vim haproxy-outside.sls
[[email protected] 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
- mdoe: 644
service.running:
- nmae: haproxy
- enable: True
- reload: True
- require:
- cmd: haproxy-init
- watch: #監控配置檔案是否改變,變了直接reload
- file: haproxy-service
[[email protected] base]# pwd
/srv/salt/base
[[email protected] base]# vim top.sls
[[email protected] base]# cat top.sls
base:
'*':
- init.env_init
prod:
'*':
- cluster.haproxy-outside
[[email protected] base]# salt '*' state.highstate test=true
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# vim index.html
[[email protected] html]# cat index.html
q
w
q
w
q
w
q
w
[[email protected] base]# cd /var/www/
[[email protected] www]# cd html/
[[email protected] html]# vim index.html
[[email protected] html]# cat index.html
12313123132
訪問: http://192.168.43.71:8888/haproxy-status
vim /etc/http/conf/httpd.conf
listen 8080