saltstack学习-9:批量安装nginx服务并定时更新配置(pillar)
環(huán)境介紹:
slatmaster:10.80.0.162
minion01:10.80.0.163
minion02:10.80.0.164
目標:在兩臺minion上安裝nginx服務(wù),并定時同步master的nginx配置文件。
第一步,編寫sls文件
[root@study02 salt]# tree . ├── etc │?? ├── file │?? │?? └── passwd │?? ├── nginx │?? │?? ├── conf.d │?? │?? │?? └── default.conf │?? │?? └── nginx.conf │?? └── script │?? └── test.sh ├── sls │?? ├── init.sls │?? ├── nginx.sls │?? └── test.sls ├── test.sls └── top.sls6 directories, 9 files cat sls/nginx.sls nginx:pkg:- installedservice:- running- enable: True- reload: True- watch:- pkg: nginx- file: nginx.conf- file: default.conf nginx.conf:file.managed:- source: salt://etc/nginx/nginx.conf- user: root- group: root- mode: 644- name: /etc/nginx/nginx.confdefault.conf:file.managed:- source: salt://etc/nginx/conf.d/default.conf- user: root- group: root- mode: 644- name: /etc/nginx/conf.d/default.con第二步,創(chuàng)建配置文件源目錄,并將nginx的配置文件拷貝到對應(yīng)路徑下
[root@study02 salt]# cd /srv/salt/ [root@study02 salt]# mkdir etc/nginx/conf.d -p [root@study02 salt]# cp /etc/nginx/nginx.conf etc/nginx/ [root@study02 salt]# cp /etc/nginx/conf.d/default.conf etc/nginx/conf.d/第三步:使用salt批量安裝和并同步配置文件 [root@study02 salt]# salt 'study0[34]' state.sls sls.nginx study03: . . .Summary ------------ Succeeded: 4 Failed: 0 ------------ Total states run: 4study04: ---------- . . .Summary ------------ Succeeded: 4 (changed=3) Failed: 0 ------------ Total states run: 4第四步在客戶端驗證nginx是否安裝,啟動成功
[root@study02 salt]# salt 'study0[34]' cmd.run 'rpm -qa|grep nginx' study04:nginx-mod-mail-1.10.2-1.el6.x86_64nginx-filesystem-1.10.2-1.el6.noarchnginx-mod-http-image-filter-1.10.2-1.el6.x86_64nginx-mod-http-perl-1.10.2-1.el6.x86_64nginx-mod-http-geoip-1.10.2-1.el6.x86_64nginx-mod-stream-1.10.2-1.el6.x86_64nginx-1.10.2-1.el6.x86_64nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64nginx-all-modules-1.10.2-1.el6.noarch study03:nginx-filesystem-1.10.2-1.el6.noarchnginx-mod-http-image-filter-1.10.2-1.el6.x86_64nginx-mod-http-geoip-1.10.2-1.el6.x86_64nginx-mod-stream-1.10.2-1.el6.x86_64nginx-1.10.2-1.el6.x86_64nginx-mod-mail-1.10.2-1.el6.x86_64nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64nginx-all-modules-1.10.2-1.el6.noarchnginx-mod-http-perl-1.10.2-1.el6.x86_64 [root@study02 salt]# salt 'study0[34]' cmd.run 'netstat -nltup|grep "8080"' study04:tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 31246/nginx tcp 0 0 :::80 :::* LISTEN 31246/nginx study03:tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32487/nginx tcp 0 0 :::80 :::* LISTEN 32487/nginx第五步:修改配置文件,測試配置文件同步,minion同步成功后,重啟nginx(reload)
- 修改配置文件,將端口改為8080
- 使用salt同步配置文件,并重啟nginx
- 驗證minion的nginx配置文件是否同步成功,端口是否更改
擴展:在minion端執(zhí)行sls
- 修改配置文件,將端口改回 80
- 在minion01執(zhí)行文件同步sls,并驗證端口是否修改成功
- minion02沒有執(zhí)行,端口任然為8080
?
定時同步方法一:在minion端將salt-call state.sls sls.nginx 寫入到定時任務(wù)中去。
?
Pillar
Pillar是salt非常重要的一個組件,它用于給特定的minion定義任何你需要的數(shù)據(jù),這些數(shù)據(jù)可以被salt的其它組件使用。Salt在0.98版本中引入了Pillar。
Pillar在解析完成后,是一個嵌套的字典結(jié)構(gòu);最上層的key是minion ID,其value是改minion所擁有的Pillar數(shù)據(jù);每一個value也都是key:value。
這里可以看出一個特點,Pillar數(shù)據(jù)是與特定minion關(guān)聯(lián)的,也就是說每一個minion都只能看到自己的Pillar數(shù)據(jù),所以可以用Pillar傳遞敏感數(shù)據(jù)(在salt的設(shè)計中,Pillar使用獨立的加密session,也是為了保證敏感數(shù)據(jù)的安全性)
Pillar使用場景
?
Pillar示例
官網(wǎng)地址:http://docs.saltstack.cn/topics/jobs/index.html
第一步:修改master配置文件
[root@study02 srv]# vim /etc/salt/master ##### Pillar settings ##### ########################################## # Salt Pillars allow for the building of global data that can be made selectively # available to different minions based on minion grain filtering. The Salt # Pillar is laid out in the same fashion as the file server, with environments, # a top file and sls files. However, pillar data does not need to be in the # highstate format, and is generally just key/value pairs. pillar_roots:base:- /srv/pillar第二步:創(chuàng)建top.sls和nginx.sls
[root@study02 srv]# tree pillar/ pillar/ ├── nginx │?? └── nginx.sls └── top.sls [root@study02 pillar]# cat top.sls base:'*':- 'nginx.nginx' [root@study02 pillar]# cat nginx/nginx.sls schedule:test:function: state.slsminutes: 3600args:- 'nginx.nginx'第三步:下發(fā)pillar數(shù)據(jù),查看是否生效
[root@study02 srv]# salt 'study0[34]' pillar.data study04:----------schedule:----------test:----------args:- nginx.nginxfunction:state.slsminutes:1 study03:----------schedule:----------test:----------args:- nginx.nginxfunction:state.slsminutes:1第四步:pillar數(shù)據(jù)雖然已經(jīng)下發(fā)給minion但是還沒有生效,需要刷新pillar數(shù)據(jù),執(zhí)行如下命令:
[root@study02 srv]# salt 'study0[34]' saltutil.refresh_pillar study03:True study04:True第五步:驗證端口,是否更新
[root@study02 pillar]# salt 'study0[34]' cmd.run 'netstat -lntup|grep -E ":80|:8080"' study04:tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 711/nginx study03:tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2196/nginx轉(zhuǎn)載于:https://www.cnblogs.com/snailshadow/p/8228022.html
總結(jié)
以上是生活随笔為你收集整理的saltstack学习-9:批量安装nginx服务并定时更新配置(pillar)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 著名站点的爬虫 —— 豆瓣
- 下一篇: jquery 前台分页插件总结(1 前台