例行吐槽:有年月没更新博客了,最近太浮躁了,以前一起工作的小伙伴都找到新的东家了,薪资条那叫一个长,不开森了,都不带我[]
#######################分隔线####################
一、简介
saltstack 是一个新基础设施管理工具,可以看做是强化的Func+弱化puppet的组合,间接的反映出了saltstack的两大功能:远程执行命令与配置管理,
saltstack是使用python开发的,非常简单易用和轻量级的管理工具,由master和minion构成,通过ZeroMQ进行通信
二、安装
安装时需要epel源的支持,请自行安装与当系统匹配的epel源
*:安装依赖包
yum install python-jinja2 -y
1、安装master端
yum -y install salt-master enablerepr=epel-testing
2、安装minion端
yum -y install salt-minion enablerepr=epel-testing
三、配置环境
a、master
1、修改本地绑定地址
sed -ie 's/^#.*interface:.*/\ interface: 192.168.2.65/g' /etc/salt/master
注:或是此处写上主机名,并绑定/etc/hosts文件
2、自动接收所有minion的请求
sed -ie 's@^#\(auto_accept: \)False@\ true@g' /etc/salt/master
b、minion
1、指向master
sed -ie 's@^#.*master:.*@\ master: salt@g' /etc/salt/minion
注:由于saltstack配置文件所限,当启用每一个配置参数时对格式有严格要求,书写时请注意
四、环境测试
1、分别在master/minion中启动服务
#service salt-master start #service salt-minion start
说明:
saltstack的master监听于4505,minion监听于4506;
可以将master与minion部署同一台服务器上(没有什么意义[哈哈~])。
2、salt 测试
[root@openapi php-fpm]# salt "*" test.ping 192.168.2.36-CentOS.backup.test.backend: True 192.168.2.30-centos.public.test: True
说明:salt的其它命令可用salt --help很简单,在此不多介绍
五、salt 常用的正则表达式(部分)
a、E:正则匹配
可以在每一个句后面加一个 -l debug来显示命令具体执行过程
[root@openapi self_userd]# salt -E '(backend81)' test.ping backend81: True [root@openapi self_userd]# salt -E '(backend81)' test.ping -l debug [DEBUG ] Reading configuration from /etc/salt/master [DEBUG ] Guessing ID. The id can be explicitly in set /etc/salt/minion [INFO ] Found minion id from generate_minion_id(): openapi.test.dns.com.cn [DEBUG ] Missing configuration file: ~/.saltrc [DEBUG ] Configuration file path: /etc/salt/master [DEBUG ] Reading configuration from /etc/salt/master [DEBUG ] Guessing ID. The id can be explicitly in set /etc/salt/minion [INFO ] Found minion id from generate_minion_id(): openapi.test.dns.com.cn [DEBUG ] Missing configuration file: ~/.saltrc [DEBUG ] MasterEvent PUB socket URI: ipc:///var/run/salt/master/master_event_pub.ipc [DEBUG ] MasterEvent PULL socket URI: ipc:///var/run/salt/master/master_event_pull.ipc [DEBUG ] LazyLoaded local_cache.get_load [DEBUG ] get_iter_returns for jid 20150727212822728009 sent to set(['backend81']) will timeout at 21:28:27.732956 [DEBUG ] jid 20150727212822728009 return from backend81 backend81: True [DEBUG ] jid 20150727212822728009 found all minions set(['backend81'])
b、-L :命令行里面一般是以列表的形式来指定对象的。
c、-G:这个参数很强大,会根据默认的grain的结果来过滤。(grains也可以自己定义)
d、-N:这个参数是基于分组的,前提是你得先分好组。(分组可以定义于主匹配文件/etc/salt/master中,也可以定义于/etc/salt/master.d/*.conf)
然后可以这样使用
e、-C :表示tagger可是一个复合语句
[root@openapi salt]# salt -C 'G@virtual:physical and E@backend81' test.ping backend81: True
f、-b :一次 *** 作多少台,也可以使使用百分比来 *** 作(--batch-size)
[root@openapi salt]# salt "*" -b 3 grains.item os #salt "*" --batch-size 25% grains.item os 36 Detected for this batch run backend81 Detected for this batch run backend84 Detected for this batch run zabbix.server.dns.com.cn Detected for this batch run backend83 Detected for this batch run webdata.backup Detected for this batch run Executing run on ['zabbix.server.dns.com.cn', 'webdata.backup', 'backend84'] webdata.backup: ---------- os: CentOS backend84: ---------- os: CentOS zabbix.server.dns.com.cn: ---------- os: CentOS Executing run on ['backend83', 'backend81', '36'] backend81: ---------- os: CentOS backend83: ---------- os: CentOS 36: ---------- os: CentOS
注:
更多的模块使用说明可以使用
salt \* sys.doc |grep <模块名称>
salt 的每一个子命令都可以用-d来查看具体的用法
六、自定义grains
1、grains的优先级
grains可以保持在minion端、通过master端下发等多个方式来分发。但不同的方法有不同的优先级的:
a. /etc/salt/grains
b. /etc/salt/minion
c./srv/salt/_grains/ master端_grains目录下
优先级顺序依次为存在在minion端/etc/salt/minion配置文件中的同名grains会覆盖/etc/salt/grains文件中的值,而通过master端_grains目录下grains文件下发的值可以会覆盖minion端的所有同名值。比较拗口,总之记得,通过master下发的grains优先级是最高的可,/etc/salt/minion次之,/etc/salt/grains最低(core grains不大懂,就不讨论了,这个比/etc/salt/grains还低)
2、自定义grains
注:
a、首先要在master端的主匹配文件中开启file_roots
file_roots: base: - /srv/salt
b、如果没有以上目录,创建即可(此处使用_grains目录)
[root@openapi _grains]# pwd /srv/salt/_grains
c、自定义grains字典
[root@openapi _grains]# cat ports.py #!/usr/bin/env python #coding:utf8 def open_moutil_port (): grains={} grains['web_port']='80' grains['mysql_path']='/data/mysql' grains['mysqld_port']='3307' grains['max_open_file']='65535' return grains
d、同步grains并重新加载grains(grains是“静态”的,是每一个minion接入到master自动提交的)
[root@openapi _grains]# salt "*" saltutil.sync_all 192.168.2.36-CentOS.backup.test.backend: ---------- beacons: grains: modules: outputters: renderers: returners: states: utils: 192.168.2.30-centos.public.test: ---------- beacons: grains: modules: outputters: renderers: returners: states: utils: [root@openapi _grains]# salt '*' sys.reload_modules 192.168.2.36-CentOS.backup.test.backend: True 192.168.2.30-centos.public.test: True
e、查询自定义的grains
[root@openapi _grains]# salt "*" grains.item web_port 192.168.2.36-CentOS.backup.test.backend: ---------- web_port: 80 192.168.2.30-centos.public.test: ---------- web_port: 80
七、LNMP环境
1、目录结构如下:
[root@openapi salt]# tree . ├── _grains │ └── ports.py ├── lnmp.sls ├── mysql │ ├── installed.sls │ └── my.cnf ├── nginx │ ├── default.conf │ └── installed.sls ├── php-fpm │ ├── index.php │ └── installed.sls └── top.sls
a、top文件(此文件必须存在,它定义了各minion的执行规划)
[root@openapi salt]# cat top.sls base: '192.168.2.30-centos.public.test': #此处可以写组、正则、或minion;匹配目标 - nginx.installed #这表示到那一个目录下的那一个文件 ,此部分表示在30安装nginx,php-fpm - php-fpm.installed '192.168.2.36-CentOS.backup.test.backend': - mysql.installed #在2.36上安装mysql-server
b、nginx目录下的文件
[root@openapi nginx]# ll total 8 -rw-r--r-- 1 sysadmin sysadmin 1102 Jul 29 21:50 default.conf #nginx 默认的配置文件,只改了root目录为/var/www/html,其它地方与/etc/nginx/conf.d/default完全一样 -rw-r--r-- 1 root root 360 Jul 29 23:23 installed.sls # *** 作文件 [root@openapi nginx]# cat installed.sls nginx: #id pkg.installed: #使用pkg安装包,installed表示要安装完成,也可以指定版本来保证所有环境使用同一版本软件包 - name: nginx #包名,如果没有将继承id名称 service.running: #服务状态 - enable: True #是否启动 - reload: True #重新载入 - watch: #监控文件 - file: /etc/nginx/conf.d/default.conf #如果此文件发生变化将文件重新装载到服务中 - require: #依赖 - pkg: nginx #包名 file.managed: #文件 *** 作 - name: /etc/nginx/conf.d/default.conf #文件名 - source: salt://nginx/default.conf #base下那一个文件 - user: nginx #用户 - group: root #组 - mode: 644 #权限 - backup: minion #将上一个版本的文件备份。(如果有的话) - require: #依赖 - pkg: nginx
c、mysql目录下的文件
[root@openapi mysql]# ll total 8 -rw-r--r-- 1 root root 434 Jul 29 23:07 installed.sls # *** 作文件 -rw-r--r-- 1 sysadmin sysadmin 775 Jul 29 22:26 my.cnf #默认的匹配文件 [root@openapi mysql]# cat installed.sls mysql-server: pkg.installed: - name: mysql-server service.running: - name: mysqld - enable: True - reload: True - watch: - file: /etc/my.cnf - require: - pkg: mysql-server file.managed: - name: /etc/my.cnf - source: salt://mysql/my.cnf - user: mysql - group: mysql - mode: 644 - backup: minion mysql: pkg.installed: - name: mysql myslq-devel: pkg.installed: - name: mysql-devel
d、php-fpm目录下的文件
[root@openapi php-fpm]# ll total 8 -rw-r--r-- 1 root root 22 Jul 29 22:35 index.php -rw-r--r-- 1 root root 351 Jul 29 23:34 installed.sls [root@openapi php-fpm]# cat index.php #php测试页面 <?php phpinfo() ?> [root@openapi php-fpm]# cat installed.sls php-xcache: pkg.installed: - name: php-xcache php-fpm: pkg.installed: - name: php-fpm service.running: - enable: Ture - require: - pkg: php-fpm file.managed: - name: /var/www/html/index.php - source: salt://php-fpm/index.php - user: nginx - group: root - mode: 644 - require: - pkg: php-fpm
注:在php-fpm中的installed.sls如果需要的模块不是php-fpm依赖的必需定义到phf-fpm之前,如果先安装完成php-fpm后安装所需要的php模块,还要重新reloadphp-fpm服务才能生效
e、定义顺序文件
lnmp环境一般要先行安装数据库-->web服务-->php-fpm所以要定义一下两台不同服务执行执行顺序。
[root@openapi salt]# pwd /srv/salt [root@openapi salt]# cat lnmp.sls db_setup: #id salt.state: #状态 - tgt: '192.168.2.36-CentOS.backup.test.backend' #目标与base定义的一样 - highstate: True web_setup: salt.state: - tgt: '192.168.2.30-centos.public.test' - highstate: True - require: #依赖 - salt: db_setup
2、执行 *** 作
salt-run state.orchestrate lnmp
注: 也可以使用如下命令测试
salt-run state.orchestrate lnmp test=Ture
剩下说等待 *** 作完成了
3、最络执行结果
[root@openapi nginx]# salt-run state.orchestrate lnmp openapi.test.dns.com.cn_master: ---------- ID: db_setup Function: salt.state Result: True Comment: States ran successfully. No changes made to 192.168.2.36-CentOS.backup.test.backend. Started: 23:27:11.401701 Duration: 3533.27 ms Changes: ---------- ID: web_setup Function: salt.state Result: True Comment: States ran successfully. Updating 192.168.2.30-centos.public.test. Started: 23:27:14.936140 Duration: 46763.368 ms Changes: 192.168.2.30-centos.public.test: ---------- ID: nginx Function: pkg.installed Result: True Comment: Package nginx is already installed. Started: 23:29:08.840872 Duration: 971.023 ms Changes: ---------- ID: nginx Function: file.managed Name: /etc/nginx/conf.d/default.conf Result: True Comment: File /etc/nginx/conf.d/default.conf is in the correct state Started: 23:29:09.816842 Duration: 5.82 ms Changes: ---------- ID: nginx Function: service.running Result: True Comment: Service nginx is already enabled, and is in the desired state Started: 23:29:09.822994 Duration: 273.52 ms Changes: ---------- ID: php-fpm Function: pkg.installed Result: True Comment: The following packages were installed/updated: php-fpm Started: 23:29:10.097200 Duration: 26085.157 ms Changes: ---------- php-fpm: ---------- new: 5.3.3-46.el6_6 old: ---------- ID: php-fpm Function: service.running Result: True Comment: Started Service php-fpm Started: 23:29:36.192137 Duration: 301.652 ms Changes: ---------- php-fpm: True ---------- ID: php-fpm Function: file.managed Name: /var/www/html/index.php Result: True Comment: File /var/www/html/index.php updated Started: 23:29:36.499508 Duration: 23.542 ms Changes: ---------- diff: New file mode: 0644 user: nginx ---------- ID: php-xcache Function: pkg.installed Result: True Comment: The following packages were installed/updated: php-xcache Started: 23:29:36.527960 Duration: 16844.534 ms Changes: ---------- php-xcache: ---------- new: 3.0.4-1.el6 old: Summary ------------ Succeeded: 7 (changed=4) Failed: 0 ------------ Total states run: 7 Summary ------------ Succeeded: 2 (changed=1) Failed: 0 ------------ Total states run: 2
温馨提示:在卸载软件包时,千万不要用yum、yum、yum.
a、查看服务状态
[root@openapi salt]# salt "192.168.2.36-CentOS.backup.test.backend" service.status mysqld 192.168.2.36-CentOS.backup.test.backend: Ture [root@openapi salt]# salt "192.168.2.30-centos.public.test" service.status nginx 192.168.2.30-centos.public.test: Ture [root@openapi salt]# salt "192.168.2.30-centos.public.test" service.status php-fpm 192.168.2.30-centos.public.test: Ture
b、测试php
#########################到此lnmp部署完成###############################
ps:
其它了服务也是同理,是重要的是注意 *** 作文件的格式及服务执行的先后顺序。
下次更新来个源码的及pillar的使用!回家,碎觉~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)