最近一直在研究saltstack的同步文件和批量執行命令,隨着架構的變大,批量部署的需求也變得明顯起來了,我需要用一條命令就部署好nginx和tomcat,並且符合我所有的環境需求,可以直接投入生產環境使用,這就需要用到saltstack的批量安裝部署功能了。這篇文章主要介紹nginx的批量部署,下篇講解tomcat多實例的批量部署方法。
環境介紹:
Centos 6.5
salt 2015.5.10
nginx 1.12.0
minion:test
1.修改master配置文件,修改後重啓服務,因爲saltstack是用python寫的,所以所有配置文件縮進和空格必須和文中一致!
mkdir -p /srv/salt
vim /etc/salt/master
file_roots: #前面必須有倆空格,下面縮進同理 base: - /srv/salt/
2.主入口文件top.sls
vim /srv/salt/top.sls
base: 'test': #只作用於test這臺minion - nginx.init #使用nginx下的init.sls文件進行初始化
3.創建nginx目錄
mkdir -p /srv/salt/nginx/files #用於存放模塊和配置文件
mkdir -p /srv/salt/soft/ #用於存放模塊的安裝方法
cd /srv/salt/nginx && tree
4.編輯初始化文件init.sls
vim /srv/salt/nginx/init.sls
include: - nginx.install #包含nginx下的install.sls
5.編輯安裝部署文件install.sls
vim /srv/salt/nginx/install.sls
include: - soft.modules #包含模塊配置文件 #nginx.tar.gz nginx_source: file.managed: - name: /tmp/nginx-1.12.0.tar.gz #標識文件所在位置 - unless: test -e /tmp/nginx-1.12.0.tar.gz #檢測文件是否存在,只在第一次檢測 - source: salt://nginx/files/nginx-1.12.0.tar.gz #把maser上的文件傳過去 #extract extract_nginx: cmd.run: - cwd: /tmp #進入/tmp目錄 - names: - tar zxvf nginx-1.12.0.tar.gz #解壓 - unless: test -d /tmp/nginx-1.12.0.tar.gz - require: - file: nginx_source #這個命令必須在上面的函數執行成功後才繼續執行 #user nginx_user: user.present: #用戶創建 - name: nginx - createhome: False #不用家目錄 - gid_from_name: True - shell: /sbin/nologin #指定shell #nginx_pkgs nginx_pkg: pkg.installed: #安裝必備組件 - pkgs: - gcc - gcc-c++ - epel-release - openssl-devel - pcre-devel - zlib-devel - gd-devel - lua-devel #nginx_compile nginx_compile: #nginx進行編譯 cmd.run: - cwd: /tmp/nginx-1.12.0 - names: - ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_p_w_picpath_filter_module --with-http_gzip_static_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-pcre --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --with-http_sub_module --add-module=/soft/ngx_cache_purge-2.3 --add-module=/soft/ngx_devel_kit-0.3.0rc1 --add-module=/soft/echo-nginx-module-master --add-module=/soft/lua-nginx-module-master && make && make install - require: - cmd: extract_nginx - pkg: nginx_pkg #- unless: test -d /usr/local/nginx/ #檢測或不檢測目錄是否存在 #cache_dir cache_dir: cmd.run: - names: #創建虛擬主機目錄,把nginx目錄權限給相關用戶 - mkdir -p /usr/local/nginx/conf/vhosts && chown -R nginx.nginx /usr/local/nginx/ - require: - cmd: nginx_compile - unless: test -d /usr/local/nginx/conf/vhosts/ #vhosts file.managed: - name: /usr/local/nginx/conf/nginx.conf #修改後的配置文件複製過去 - source: salt://nginx/files/nginx.conf #- unless: test -e /usr/local/nginx/conf/nginx.conf #建議不進行檢測,如果檢測,有這個文件將不會進行更新 /usr/local/nginx/conf/proxy.conf: file.managed: - name: /usr/local/nginx/conf/proxy.conf - source: salt://nginx/files/proxy.conf #- unless: test -e /usr/local/nginx/conf/proxy.conf
5.編輯模塊配置文件
vim /srv/salt/soft/modules.sls
echo-install: #每個文件管理必須配置不同的名字,縮進必須一致,name:和source:後面的空格必須有! file.managed: - name: /soft/echo-nginx-module-master.zip - source: salt://nginx/files/echo-nginx-module-master.zip - user: root #用戶指定可有可無 - group: root - mode: 755 #權限指定可有可無 cmd.run: - name: cd /soft && unzip echo-nginx-module-master.zip - unless: test -d /soft/echo-nginx-module-master lua-install: file.managed: - name: /soft/lua-nginx-module-master.zip - source: salt://nginx/files/lua-nginx-module-master.zip cmd.run: - name: cd /soft && unzip lua-nginx-module-master.zip - unless: test -d /soft/lua-nginx-module-master purge-install: file.managed: - name: /soft/ngx_cache_purge-2.3.tar.gz - source: salt://nginx/files/ngx_cache_purge-2.3.tar.gz cmd.run: - name: cd /soft && tar xvf ngx_cache_purge-2.3.tar.gz - unless: test -d /soft/ngx_cache_purge-2.3 devel-install: file.managed: - name: /soft/ngx_devel_kit-0.3.0rc1.tar.gz - source: salt://nginx/files/ngx_devel_kit-0.3.0rc1.tar.gz cmd.run: - name: cd /soft && tar xvf ngx_devel_kit-0.3.0rc1.tar.gz - unless: test -d /soft/ngx_devel_kit-0.3.0rc1
6.執行同步推送
salt 'test' state.highstate
觀察是否全部爲success,如果有fail,查看具體失敗原因,解決問題後可繼續推送