saltstack
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack相关的知识,希望对你有一定的参考价值。
saltstack搭建:
saltstack是一个基础设施管理工具,保持了输入输出,配置文件的一致性,所有文件均使用了YAML格式,主要负责配置管理和远程执行。
三大功能:
远程执行命令,配置管理,云管理
saltstack是一主多从结构。
server1为主,server3/4为从
首先下好依赖包,或者是在安装时一步一步解决依赖性,比较复杂。
[[email protected]1 salt]# ls
libyaml-0.1.3-4.el6.x86_64.rpm
pciutils-3.1.10-4.el6.x86_64.rpm
pciutils-libs-3.1.10-4.el6.x86_64.rpm
python-babel-0.9.4-5.1.el6.noarch.rpm
python-backports-1.0-5.el6.x86_64.rpm
python-backports-ssl_match_hostname-3.4.0.2-2.el6.noarch.rpm
python-chardet-2.2.1-1.el6.noarch.rpm
python-cherrypy-3.2.2-4.el6.noarch.rpm
python-crypto-2.6.1-3.el6.x86_64.rpm
python-futures-3.0.3-1.el6.noarch.rpm
python-jinja2-2.8.1-1.el6.noarch.rpm
python-libcloud-0.20.0-1.el6.noarch.rpm
python-markupsafe-0.11-10.el6.x86_64.rpm
python-msgpack-0.4.6-1.el6.x86_64.rpm
python-ordereddict-1.1-2.el6.noarch.rpm
python-requests-2.6.0-3.el6.noarch.rpm
python-six-1.9.0-2.el6.noarch.rpm
python-tornado-4.2.1-1.el6.x86_64.rpm
python-urllib3-1.10.2-1.el6.noarch.rpm
python-zmq-14.5.0-2.el6.x86_64.rpm
PyYAML-3.11-1.el6.x86_64.rpm
salt-2016.11.3-1.el6.noarch.rpm
salt-api-2016.11.3-1.el6.noarch.rpm
salt-cloud-2016.11.3-1.el6.noarch.rpm
salt-master-2016.11.3-1.el6.noarch.rpm
salt-minion-2016.11.3-1.el6.noarch.rpm
salt-ssh-2016.11.3-1.el6.noarch.rpm
salt-syndic-2016.11.3-1.el6.noarch.rpm
yum-3.2.29-60.el6.noarch.rpm
yum-utils-1.1.30-30.el6.noarch.rpm
zeromq-4.0.5-4.el6.x86_64.rpm
[[email protected] salt]#
解决依赖性之后就可以安装.
安装master
[[email protected] salt]# ls |wc -l
31
[[email protected] salt]# yum install -y *
安装minion
安装minion要把和主相关的文件删除,不然minion无法启动。
[[email protected] salt]# ls |wc -l
27
[[email protected] salt]# yum install -y *
************
注意:所有修改必须添加两个空格,这是格式,不然会报错
修改master配置:
vim /etc/salt/master
修改接口interface为自己的IP
[[email protected] salt]# cat /etc/salt/master|grep ‘interface‘
# The address of the interface to bind to:
interface: 172.25.33.1
# the interface option must be adjusted, too. (For example: "interface: ‘::‘")
# The port used by the communication interface. The ret (return) port is the
# interface used for the file server, authentication, job returns, etc.
# The publisher interface ZeroMQPubServerChannel
# masters‘ syndic interfaces.
# The example below enables minion foo.example.com to get ‘network.interfaces‘ mine
# - network.interfaces
修改minion配置:
vim /etc/salt/minion
修改master为master 的IP
[[email protected] salt]# cat /etc/salt/minion|grep ‘^ master‘
master: 172.25.33.1285
#auto_accept: False 是否接受自动认证。
启动服务。启动主服务。
[[email protected] salt]# service salt-master start
Starting salt-master daemon: [ OK ]
启动minino服务
[[email protected] salt]# service salt-minion start
Starting salt-minion:root:server3.example.com daemon: OK
此时可可以在master 端查看到minion列表
[[email protected] salt]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
server3.example.com
Rejected Keys:
minion在第一次启动时会在/etc/salt/pki/minion(可以在/etc/salt/minion里设置。)生成key文件,在启动时将minion.pub发送给master
total 12
-rw-r--r-- 1 root root 450 Mar 13 11:09 minion_master.pub
-r-------- 1 root root 1674 Mar 13 10:59 minion.pem
-rw-r--r-- 1 root root 450 Mar 13 10:59 minion.pub
master在接受到minino的public key 后通过salt-key命令接受minion的public key,这样,就可以对minion发送指令了。
接受所有的key
[[email protected] salt]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
server3.example.com
Proceed? [n/Y] Y
Key for minion server3.example.com accepted.
[[email protected] salt]# salt-key -L
Accepted Keys:
server3.example.com
Denied Keys:
Unaccepted Keys:
Rejected Keys:
-a :accept ,-A:accept-all,-d:delete,-D:delete-all。可以使用 salt-key 命令查看到已经签名的客户端
向Minion发送测试:
[[email protected] salt]# salt ‘*‘ test.ping
server3.example.com:
True
至此,简单的saltstack搭建已经完成。
saltstack master 启动后会默认监控4505和4506端口,4505是salt的消息发送端口。4506是master 和minion的通信端口。
[[email protected] salt]# netstat -antlp |grep 4505
tcp 0 0 172.25.33.3:42094 172.25.33.1:4505 ESTABLISHED 1485/python2.6
[[email protected] salt]# netstat -antlp |grep 4506
[[email protected] salt]#
[[email protected] salt]# netstat -antlp |grep 4505
tcp 0 0 172.25.33.1:4505 0.0.0.0:* LISTEN 1101/python2.6
tcp 0 0 172.25.33.1:4505 172.25.33.3:42094 ESTABLISHED 1101/python2.6
[[email protected] salt]# netstat -antlp |grep 4506
tcp 0 0 172.25.33.1:4506 0.0.0.0:* LISTEN 1108/python2.6
查看minion的磁盘使用情况:
[[email protected] salt]# salt ‘*‘ disk.usage
server3.example.com:
----------
/:
----------
1K-blocks:
19134332
available:
15249116
capacity:
17%
filesystem:
/dev/mapper/VolGroup-lv_root
used:
2913236
/boot:
----------
1K-blocks:
495844
available:
436777
capacity:
8%
filesystem:
/dev/vda1
used:
33467
/dev/shm:
----------
1K-blocks:
251136
available:
251120
capacity:
1%
filesystem:
tmpfs
used:
16
[[email protected] salt]#
查看minion的文件。
[[email protected] salt]# salt ‘*‘ cmd.run "cat /etc/passwd" //*是minion的id,可以在minion中的id项设定,同时支持正则匹配。
server3.example.com:
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/no
在master配置文件中还可以对Minion进行分组:
1005 ##### Node Groups ######
1006 ##########################################
1007 # Node groups allow for logical groupings of minion nodes. A group cons ists of
1008 # a group name and a compound target. Nodgroups can reference other nod egroups
1009 # with ‘[email protected]‘ classifier. Ensure that you do not have circular references .
1010 #
1011 #nodegroups:
1012 # group1: ‘[email protected],bar.domain.com,baz.domain.com or bl*.domai n.com‘
1013 # group2: ‘[email protected]:Debian and foo.domain.com‘
1014 # group3: ‘[email protected]:Debian and [email protected]‘
配置管理apache
[[email protected] salt]# vim /etc/salt/master
524 file_roots:
525 base:
526 - /srv/salt/
/srv/salt/目录在系统中并不存在,需要自己创建
[[email protected] salt]# mkdir /srv/salt
[[email protected] salt]# cd /srv/salt/
[[email protected] salt]# ls
[[email protected] salt]# vim /etc/salt/master
[[email protected] salt]# ls
[[email protected] salt]# pwd
/srv/salt
[[email protected] salt]# vim top.sls
[[email protected] salt]# vim apache.sls
[[email protected] salt]# cat top.sls
base:
‘server3.example.com‘
-apache
[[email protected] salt]# cat apache.sls
apache-service:
pkg.instlled:
-names://如果是一个服务,可以使用,-name:httpd
-httpd
-httpd-devel
service running:
-name:httpd
-enable:Ture
[[email protected] salt]# service salt-master restart
Stopping salt-master daemon: [FAILED]//报错原因:格式错误,未留两个空格
Starting salt-master daemon: [ OK ]
执行命令:
[[email protected] salt]# salt ‘server3.example.com‘ state.highstate
server3.example.com:
----------
ID: states
Function: no.None
Result: False
Comment: No Top file or external nodes data matches found.
Changes:
Summary for server3.example.com
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
ERROR: Minions returned with non-zero exit code
[[email protected] salt]# tree /srv/salt/
/srv/salt/
├── apache.sls
└── top.sls
0 directories, 2 files
[[email protected] salt]# cat apache.sls
apache-service:
pkg.installed:
- name: httpd
service.running:
- name: httpd
- enable: True
[[email protected] salt]# cat top.sls
base:
‘*‘:
- apache
[[email protected] ~]# salt ‘server3.example.com‘ state.highstate
server3.example.com:
----------
ID: apache-service
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 17:20:56.277491
Duration: 10208.904 ms
Changes:
----------
httpd:
----------
new:
2.2.15-29.el6_4
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 17:21:06.493284
Duration: 322.954 ms
Changes:
----------
httpd:
True
Summary for server3.example.com
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 10.532 s
[[email protected] ~]# service httpd status
httpd (pid 2055) is running...
以上是关于saltstack的主要内容,如果未能解决你的问题,请参考以下文章
saltstacksaltstack之job返回数据salt-sshsalt-syndic分布式架构salt-api部署