源碼編譯安裝nginx
1、在master端
mkdir /srv/salt/nginx/files -p
將nginx的源碼包放入files目錄
編寫源碼編譯安裝腳本:
cd /srv/salt/nginx
vim install.sls
nginx-install:
# 安裝源碼編譯nginx所需要的依賴
pkg.installed:
- pkgs:
- gcc
- pcre-devel
- openssl-devel
file.managed:
- name: /opt/nginx-1.10.1.tar.gz # 將源碼包推送至minion端後的存放位置
- source: salt://nginx/files/nginx-1.10.1.tar.gz # 源碼包的來源(在master端的位置)
cmd.run:
# 源碼編譯開始之前的操作,隱藏nginx版本號等,隨後進行源碼編譯安裝
- name: cd /opt && tar zxf nginx-1.10.1.tar.gz && cd nginx-1.10.1 && sed -i .
bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i .bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/cor
e/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-
http_stub_status_module --with-threads --with-file-aio &>/dev/null && make >/dev/null && make install >/dev/null
# 如果minion端沒有這個目錄,則創建出來
- creates: /usr/local/nginx
2、使用salt執行腳本
爲了方便以及後面的負載均衡集羣需要我們向server3中安裝nginx
salt server3 state.sls nginx.install
3、在server3中查看安裝情況
4、salt自動重新加載nginx的配置文件
a、編輯自動建立nginx用戶的sls文件:
mkdir /srv/salt/users
vim nginx.sls
nginx-group:
group.present:
- name: nginx
- gid: 800
nginx-user:
user.present:
- name: nginx
- uid: 800
- gid: 800
- shell: /sbin/nologin
- createhome: False
- home: /usr/local/nginx
b、編輯自動加載nginx配置文件的腳本
cd /srv/salt/nginx
vim service.sls
include:
- users.nginx # 調用nginx的建立用戶
- nginx.install # 調用nginx的安裝
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://nginx/files/nginx.conf # 將server3中的nginx.conf文件拷貝至master中
nginx-service:
file.managed:
- name: /etc/init.d/nginx
- source: salt://nginx/files/nginx # 編寫nginx啓動腳本,放在/srv/salt/nginx/files目錄中
- mode: 755
service.running:
- name: nginx
- reload: True
- watch:
- file: /usr/local/nginx/conf/nginx.conf
c、執行service.sls腳本
6、在server3中查看nginx用戶是否創建、是否可以通過/etc/init.d/nginx控制nginx服務
salt一鍵部署負載均衡
1、在master端安裝salt-minion,使server1也作爲一個minion,並接收server-minion的公鑰
2、在/srv/salt目錄下創建haproxy目錄,編寫haproxy的安裝腳本
mkdir /srv/salt/haproxy
cd /srv/salt/haproxy
vim install.sls
haproxy-install:
file.managed:
- name: /mnt/haproxy-1.4.24-1.x86_64.rpm
- source: salt://haproxy/files/haproxy-1.4.24-1.x86_64.rpm
cmd.run:
- name: yum install -y /mnt/haproxy-1.4.24-1.x86_64.rpm
- unless: rpm -q haproxy
/usr/share/haproxy:
file.directory:
- mode: 755
3、編輯自動配置負載均衡腳本:
cd /srv/salt/haproxy
vim service.sls
include:
- haproxy.install # 調用haproxy安裝腳本,安裝haproxy
haproxy-service:
file.managed:
- name: /etc/haproxy/haproxy.cfg
- source: salt://haproxy/files/haproxy.cfg # 配置負載均衡
service.running:
- name: haproxy
- enable: true
- reload: true
- watch:
- file: haproxy-service
4、將haproxy配置文件haproxy.cfg放入/srv/salt/haproxy/files中,並配置好負載均衡機制
vim /srv/salt/haproxy/files/haproxy.cfg
在最後寫入:
frontend main *:80
default_backend app
backend app
balance roundrobin
server app1 172.25.66.2:8080 check
server app2 172.25.66.3:80 check
5、在/srv/salt/目錄下編輯top.sls腳本,用來一鍵部署負載均衡,即在server1上部署haproxy,在server2上部署http,在server3上部署nginx
vim /srv/salt/top.sls
base:
'server1':
- haproxy.service # 調用haproxy的部署腳本
'server2':
- httpd.apache # 調用apache的部署腳本
'server3':
- nginx.service # 調用nginx的部署腳本
6、使用salt一鍵推送
**salt ‘*’ state.highstate **
7、在瀏覽器中訪問www.westos.org會出現負載均衡