SaltStack学习笔记

来源:互联网 发布:php获取连接端口号 编辑:程序博客网 时间:2024/05/16 04:54
1,安装启动、接收key
1)使用yum安装
2)修改minion端的配置文件"#master: salt" 为"master: $salt-master-IP",又需要就改一下minion的ID
3)centos7启动并添加开机启动,
master端:
systemctl enable salt-master
systemctl start salt-master
minion端:
systemctl enable salt-minion
systemctl start salt-mimion
4)接收minion的认证请求
在master端:
salt-key -L ###列出所有的key
salt-key -A ###接收没有认证的key
salt-key -a $key_name ###接收$key_name
salt-key -d $key_name ###删除$key_name
salt-key -D ###删除所有已认证的key


2,使用grains
grain中存储的minion端的基本信息
1)grain信息的来源
a,salt-minion服务在minion端获取的基本信息。
b,在minion端的/etc/salt/minion.d 添加以.conf结尾的文本文件ID要是grains,格式如下:
[root@node01 minion.d]# cat myself_grains.conf 
grains:
  node_type: webserver
2)salt-minion的加载
a,在salt-minion服务启动时会加载一次。
b,在salt-master端 执行 salt '*' saltutil.sync_grains
3)利用grains匹配主机
a,salt、salt-cp等命令
salt -G os:CentOS test.ping
b,grains的查看及在sls文件中使用
salt '*' grains.items
salt '*' grains.get systemd:version
## 利用grains 来区分机器(node_type这个需要在minion端写好)
base:
'node_type:webserver':
'os:CentOS':
- match: grain
- test

### 利用grains 获取的值做值
[root@server pillar]# cat test/init.sls
os: apple OS
abc: {{ salt['grains.get']('virtual') }}
[root@server pillar]# salt '*' saltutil.refresh_pillar
node02.door.com:
True
node01.door.com:
True
[root@server pillar]# salt '*' pillar.get abc
node01.door.com:
VMWare
node02.door.com:


3,使用pillar
pillar是存储在master端 minion机器的信息,通过自定义各主机的pillar信息可以灵活的配置各主机的信息和差异化和主机
1)pillar信息的来源
在master端 在/srv/pillar文件夹中编写sls文件进行配置
2)pillar的基本使用
salt '*' pillar.items
salt '*' pillar.get nginx:common
salt '*' saltutil.refresh_pillar
2)pillar的在sls文件中的使用
{% if grains['os'] == 'CentOS' %}
apache: httpd
git: git
{% elif grains['os'] == 'Debian' %}
apache: apache2
git: git-core
{% endif %}

[root@server pillar]# cat test/init.sls
os: apple OS
abc: {{ salt['grains.get']('virtual') }}
[root@server pillar]# salt '*' saltutil.refresh_pillar
node02.door.com:
True
node01.door.com:
True
[root@server pillar]# salt '*' pillar.get abc
node01.door.com:
VMWare
node02.door.com:
注意:sls文件在加载使用的时候 会自动进过jinja的调用

4,salt远程执行(远程执行只是saltstack的一个模块)目标
执行的格式 salt 【target】 【commond】
A:Targeting
支持:Globing、正则、List、Grains、Pillar、Node Group、混搭、Batch Size
salt '*' test.ping ###对所有的已注册的Key的机器执行test.ping
salt 'abc[d,f]' test.ping ###对abcd和abcf 执行test.ping
salt 'abc[d-f]' test.ping ###对abcd、abce、abcf执行test.ping

B:正则表达式
salt -E 'web1-(prod|devel)' test.ping ###匹配web1-prod和web1-devel
cat top.sls
base:
'node(01|02).door.com':
- match: pcre
- prod.nginx

C:List
salt -L 'node01.door.com,node02.door.com' test.ping

D:Grains
salt -G 'os:CentOS' test.ping
base:
'os:CentOS':
- match: grain
- prod.nginx

E:Pillar
salt -I 'nginx:common:nginx_port:8081' test.ping

F:Node Groups
要现在 配置文件中设置好 nodegroups才能用
root@server pillar]# cat /etc/salt/master.d/groups.conf
nodegroups:
web-cluster: 'node01*'
db-cluster: 'node02*'
CentOS-cluster: 'G@os:CentOS'
[root@server pillar]# salt -N 'web-cluster' test.ping
node01.door.com:
True
G:混搭(Compound)
[root@server ~]# salt -C 'G@os:CentOS and *02*' test.ping
node02.door.com:
True
H:Batch Size
通过-b(-batch-size)指定同时运行的minion数目
[root@server ~]# salt '*' -b 1 test.ping
Executing run on ['node01.door.com']
node01.door.com:
True
retcode:
0
Executing run on ['node02.door.com']
node02.door.com:
True
retcode:
0

5,远程执行实例
1)salt '*' test.ping ###测试minion是否可以连通(非icmp协议)
2)salt '*' pkg.install httpd ###在所有的minion机器上安装httpd服务
3) salt '*' cp.get_file salt://apache/files/httpd.conf /opt/httpd.conf
salt-cp '*' /srv/salt/apache/files/httpd.conf /opt/httpd2.conf
####拷贝master上的配置文件到minion机器上,用于下发配置文件 一般用第一种
4)salt '*' service.start httpd ###启动minion机器上的httpd服务


6,job管理
查看saltstack的执行的job的信息
可以通过 salt-run jobs.list_jobs 查询salt已经执行过的job记录 , 缓存时间默认 是24h,可以设置。
[root@salt-master master.d]# salt-run jobs.lookup_jid 20161103015443174183
module:
    ----------
    tree:
        ----------
        new:
            1.5.3-3.el6
        old:

7,salt配置管理
1)前置学习
A:YAML
salt 默认使用YAML作为sls文件描述格式
YAML Ain't Markup Language(Yet Another Markup Language)
使用空白字符为文件排缩来表示结构,不过不能使用TAB
注释用#号
字符串平常不适用引号,如果有需要,可以使用单引号或双引号。使用双引号表示字符串时,特殊字符可以通过(\)来进行转义
列表:短杆(-)+空白字符
- pengyao
- door
等价于python中的:['pengyao','oldboy']
字典:key和value通过冒号(:)+空白字符隔开
name: cssug
site: http://www.saltstack.cn
等价于python中的:{'name':'cssug','site':'http://www.saltstack.cn'}
多层结构:通过缩进来表示层级
- pengyao:
- site:
- blog: http://hehailiang.win
- github: http://github.com/hehailiang/
等价于python中的:[{'pengyao',[{'site':[{'blog':'http://hehailiang.win'},{'github':'http://github.com/hehailiang/'}]}]}]
在python中使用yaml库,可以直观的了解yaml中列表和字典的格式
解析出列表和字典
root@ubuntu:~/桌面/何海亮/SaltStack# cat test.yaml 
abc:
  - hehailiang
  - door
  - dologe:
    - a
    - b
>>> import yaml
>>> fd = open('test.yaml')
>>> print yaml.load(fd)
{'abc': ['hehailiang', 'door', {'dologe': ['a', 'b']}]}
解析yaml问价的脚本:
root@ubuntu:~# cat parse_yaml.py
#!/usr/bin/env python
import yaml
import sys
fd = open(sys.argv[1])
print yaml.load(fd)root@ubuntu:~# python parse_yaml.py test.yaml
{'abc': ['hehailiang', 'door', {'dologe': ['a', 'b']}]}

B:Jinja2
Salt默认使用Jinja2模板系统来生成YAML
Jinja2为Python语言开发的一个模板引擎,类似于Django模板系统
Site:http://jinja.pocoo.org/
Jinja2用法:
变量:{{ foo }},变量属性可以使用{{ foo.bar }}或{{ foo['bar'] }}
注释:{# comment #}
for:
{% for eachitem in items %}
{{ eachitem }}
{% endfor %}
if:
{% if GFW %}
Welcome to China!
{% elif not Internet %}
Welcome to North Korea!
{% else %}
Freedom!
{% endif %}
更多jinja用法请访问:http://jinja.pocoo.org/docs/templates/

C:State Tree
Top file: 配置管理入口文件,和pillar类似,指定minions需要完成哪些配置管理,默认为top.sls
sls模板使用点(.)来分割,如apache.install 等于
salt://apache/install.sls 或salt://apache/install/init.sls
Include/Extend: sls文件间可以通过Include及extend来引用和扩展
sls中ID必须唯一,且在该ID下的同一类的状态管理模块只能有一个
0.17.0 Salt增加了state_auto_order参数(默认值为True),使state在没有Requisites的情况下 按照从上到下的顺序进行执行
关于State Tree的更多内容请访问:
http://docs.saltstack.com/en/lastst/ref/states/highstate.html
注意:
top.sls中如果是这样的
会先找/srv/pillar/apache/init.sls 没有的话,再找/srv/pillar/apache.sls
D:Requestes
require: 本state执行是需要先执行哪些sate
requre_in: 与require位置相反
watch: 除了require外,也会监测依赖state的状态,如果状态发生变化,做出反应。
watch_in: 与watch位置相反
prereq: 0.16.0新增的功能,会通过test=True 接口检查所有的依赖的state得状态,如果状态发生变化,之下。
prereq: 与prereq位置相反
更多Requisites信息请访问:
http://docs.saltstack.com/en/lastst/ref/states/requisites.html
[root@server apache]# cat init.sls
apache:
pkg.installed:
- name: httpd
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- require:
- pkg: apache
- template: jinja
- defaults:
port: 52113
service.running:
- name: httpd
- enable: True
- watch:
- pkg: apache
- file: apache
Requisites使用案例:
A:每台主机的监听端口不一样
[root@salt-master apache]# cat init.sls 
apache:
  pkg.installed:
    - name: httpd
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://apache/files/httpd.conf
    - require:
      - pkg: apache
    - template: jinja
    - context:
        {% if grains['id'] == 'module' %}
        port: 8081
        {% elif grains['id'] == 'modulf' %}
        port: 8082
        {% else %}
        port: 8080
        {% endif %}
  service.running:
    - name: httpd
    - enable: True
    - watch:
      - pkg: apache
      - file: apache

[root@salt-master apache]# salt '*' state.highstate

B:有洁癖?
sls文件不应该参杂业务数据,业务应该独立存放
到了pillar登台的时候了
/srv/pillar/apache/init.sls

[root@salt-master pillar]# cat apache/init.sls 
apache:
  {% if grains['id'] == "module" %}
  port: 8181
  {% elif grains['id'] == "modulf" %}
  port: 8182
  {% else %}
  port: 80
  {% endif %}
[root@salt-master pillar]# cat top.sls 
base:
  '*':
  - packages
  - apache
[root@salt-master pillar]# salt '*' saltutil.refresh_pillar
module:
    True
[root@salt-master pillar]# salt '*' pillar.items
module:
    ----------
    apache:
        ----------
        port:
            8181
    git:
        git

[root@salt-master apache]# pwd
/srv/salt/apache
[root@salt-master apache]# cat init.sls 
apache:
  pkg.installed:
    - name: httpd
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://apache/files/httpd.conf
    - require:
      - pkg: apache
    - template: jinja
    - context:
        port: {{ salt['pillar.get']('apache:port',80)}}
  service.running:
    - name: httpd
    - enable: True
    - watch:
      - pkg: apache
      - file: apache

[root@salt-master apache]# salt '*' state.highstate

在执行salt '*' state.的时候  会自动先刷新 pillar

[root@salt-master salt]# cat top.sls 
base:
  '*':
    - apache
    - system
[root@salt-master salt]# cat system.sls 
helloworld:
  cmd.run:
    - name: echo hello world
saltstack官方参考样例:
https://github.com/SS-archive/salt-states

saltstack官方提供的解决方案:
https://github.com/saltstack-formulas



Salt高级应用
Job管理
A:job简介
jid: job id ,格式为%Y%m%d%H%M%S%f
master 在下发指令消息时,会附带上产生的jid。minion在接受到指令开始执行时,会在本地的cachedir(默认是/var/cache/salt/minion/)下的proc目录产生以该jid命名的文件,用于在执行过程中master查看当前任务的执行情况,指令执行完毕将结果传送给master后,删除该临时文件
master将minion的执行结果存放在本地/var/cache/salt/master/jobs目录,默认缓存24小时(可以通过修改master配置文件keep jobs选项调整)
salt '*' test.ping ###查询已经执行过的任务
salt '*' test.ping -v ###'-v' 参数可以显示当前job的id,只要执行salt后 echo $?为0都会有jid
minion端jid缓存文件,执行完毕之后 就会删掉
[root@salt-master salt]# salt '*' cmd.run 'sleep 20'
[root@lnmp conf]# cd /var/cache/salt/minion/proc/
[root@lnmp proc]# ll
总用量 4
-rw-r--r-- 1 root root 94 11月  3 02:39 20161104005939531606
master端job缓存的目录
上面里面缓存的是二进制的
故一般查看使用
[root@salt-master jobs]# salt-run jobs.list_jobs    获取jid
再用
[root@salt-master jobs]# salt-run jobs.lookup_jid 20161104010629925751
查看job信息
master配置文件中配置缓存时间的参数
B:Job基本管理
saltutil模块中的job管理方法
saltutil.running #查看minion当前正在运行的jobs
saltutil.find_job <jid> #查看知道jid的job(minion正在运行的jobs)
saltutil.signal_job <jid> <single> #给指定的jid进程发送信号
saltutil.term_job <jid> #终止指定的jid进程(信号为15)
saltutil.kill_job <job> #终止指定的jid进程(信号为9)
salt-run 中的job管理方法
salt-run jobs.active #查看所有的minion当前正在运行的jobs(在所有的minions上运行saltutil.running)
salt-run jobs.lookup_jid <jid> #从master jobs cache中查询指定jid的运行结果
salt-run jobs.list_jobs #列出当前master jobs cache中的所有job
更多job管理请访问:
http://docs.saltstack.com/en/latest/topics/jobs/index.html
[root@salt-master salt]# salt '*' saltutil.running     ###查看当前正在运行的任务
终止job
salt '*' saltutil.term_job 20161104011721356731
Schedule
Schedule简介
每30分钟minion执行一次highstate(配置在minion配置文件或pillar中)
[root@salt-master pillar]# cat top.sls 
base:
  '*':
  - packages
  - apache
  - system
  - schedule

[root@salt-master pillar]# cat schedule.sls   这个只要是sls结尾就OK
schedule:        ####这个健是必须是schedule
  helloword:
    function: cmd.run
    args: 
      - date >> /tmp/test.log
    minutes: 1

[root@salt-master pillar]# salt '*' saltutil.refresh_pillar
module:
    True

[root@lnmp proc]# tail -f /tmp/test.log 
Thu Nov  3 03:06:43 CST 2016
Thu Nov  3 03:07:43 CST 2016
Thu Nov  3 03:08:43 CST 2016
Thu Nov  3 03:09:43 CST 2016
Thu Nov  3 03:10:43 CST 2016
Thu Nov  3 03:11:43 CST 2016

###pillar中的东西 让minion端更新就会执行
0 0
原创粉丝点击