什么是saltstack
• Saltstack是基于python开发的一套C/S架构配置管理工具
• 使用SSL证书签方的方式进行认证管理
• 底层使用ZeroMQ消息队列pub/sub方式通信
– 号称世界上最快的消息队列ZeroMQ能快速在成千上万台主机上进行各种操作
– 采用RSA Key方式确认身
主要功能
• Saltstack最主要的两个功能是:配置管理与远程执行
• Saltstack不只是一个配置管理工具,还是一个云计算与数据中心架构编排的利器
• Saltstack已经支持Docker相关模块
• 在友好地支持各大云平台之后,配合Saltstack的Mine实时发现功能可以实现各种云平台业务的自动扩展
Saltstack架构
• Saltstack基于C/S架构
– 服务器端称作Master
– 客户端称作Minion
• 可以实现传统处理方式,即:客户端发送请求给服务器,服务器收到请求后处理请求,再将结果返回
• 也可以使用消息队列中的发布与订阅(pub/sub)服务模式
Saltstack工作机制
• Master和Minion都以守护进程的方式运行
• Master监听配置文件里定义的ret_port(接收minion请求),和publish_port(发布消息)的端口
• 当Minion运行时,它会自动连接到配置文件里定义的Master地址ret_port端口进行连接认证
• 当Master和Minion可以正常通信后,就可以进行各种各样的配置管理工作了
[root@foundation26 rhel6]# createrepo -v /home/kiosk/Desktop/rhel6/
[root@foundation26 rhel6]# ls
[root@foundation26 rhel6]# cd repodata/
[root@foundation26 repodata]# ls
142fb62d6e3a6e6e4826f0769ca312e016b563e27bb5eb34c02dd038e6da0235-filelists.xml.gz
27dbf988a69f5d7d7056d04e7334f16f4d9eddc95bff851e650751344797275b-other.xml.gz
2d9dc375949c7038585f9b6163b29880ac22177809b7d2b22eda534c582d13e9-primary.xml.gz
992793bee89fbd9b85ba03ac7b300b5a4c1c608a5d4e2aea7b5f32bf835b0ac6-other.sqlite.bz2
e6bb3b094fee65f8e1f1225ce51b23a31811a67552196c5ff6bdf6eb1bf57f92-filelists.sqlite.bz2
f4d01a6eb61002c8670b318b4593cbf947126835e6a550185b8a0c27075be0cd-primary.sqlite.bz2
repomd.xml
[root@foundation26 repodata]# cp -r /home/kiosk/Desktop/rhel6 /var/www/html
[root@foundation26 html]# pwd
/var/www/html
[root@foundation26 html]# ls
rhel6 rhel6.5 rhel7.3
[root@server1 yum.repos.d]# yum clean all
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Cleaning repos: rhel-source saltstack
Cleaning up Everything
[root@server1 yum.repos.d]# yum repolist
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel-source | 3.9 kB 00:00
rhel-source/primary_db | 3.1 MB 00:00
saltstack | 2.9 kB 00:00
saltstack/primary_db | 16 kB 00:00
repo id repo name status
rhel-source Red Hat Enterprise Linux 6Server - x86_64 - Source 3,690
saltstack saltstack 29
repolist: 3,719
[root@server1 yum.repos.d]# yum install salt-master
[root@server1 yum.repos.d]# cd /etc/salt/
[root@server1 salt]# /etc/init.d/salt-master start
Starting salt-master daemon: [ OK ]
[root@server1 salt]#
[root@server1 salt]# netstat -antlp
[root@server2 yum.repos.d]# vim rhel-source.repo
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.26.250/source6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[saltstack]
name=saltstack
baseurl=http://172.25.26.250/rhel6
gpgcheck=0
[root@server2 ~]# yum install salt-minion -y
[root@server2 ~]# cd /etc/salt/
[root@server2 salt]# vim minion
17 master: 172.25.26.1
[root@server2 salt]# /etc/init.d/salt-minion start
Starting salt-minion:root:server2 daemon: OK
[root@server2 salt]# ls
cloud cloud.deploy.d cloud.profiles.d master minion minion_id proxy roster
cloud.conf.d cloud.maps.d cloud.providers.d master.d minion.d pki proxy.d
Salt-minion每次开启后,会生成minion_id文件,重启前需删掉
[root@server3 yum.repos.d]# vim rhel-source.repo
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.26.250/source6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[saltstack]
name=saltstack
baseurl=http://172.25.26.250/rhel6
gpgcheck=0
[root@server3 ~]# yum install salt-minion -y
[root@server3 ~]# cd /etc/salt/
[root@server3 salt]# vim minion
17 master: 172.25.26.1
[root@server3 salt]# /etc/init.d/salt-minion start
Starting salt-minion:root:server3 daemon: OK
[root@server1 salt]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
server2
server3
Rejected Keys:
[root@server1 salt]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
server2
server3
Proceed? [n/Y] y
Key for minion server2 accepted.
Key for minion server3 accepted.
[root@server1 salt]# salt-key -L
Accepted Keys:
server2
server3
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@server1 salt]# cd /etc/salt/pki/master/
[root@server1 master]# pwd
/etc/salt/pki/master
[root@server1 master]# md5sum master.pub
b6efb4b17c9f0a999b4a8e23956a7b21 master.pub
[root@server2 salt]# cd pki/minion/
[root@server2 minion]# md5sum minion_master.pub
b6efb4b17c9f0a999b4a8e23956a7b21 minion_master.pub
[root@server2 minion]#
[root@server3 salt]# cd pki/minion/
[root@server3 minion]# md5sum minion_master.pub
b6efb4b17c9f0a999b4a8e23956a7b21 minion_master.pub
[root@server3 minion]#
[root@server1 master]# yum install tree -y
[root@server1 master]# tree
.
├── master.pem
├── master.pub
├── minions
│ ├── server2
│ └── server3
├── minions_autosign
├── minions_denied
├── minions_pre
└── minions_rejected
5 directories, 4 files
[root@server2 minion]# yum install tree -y
[root@server2 minion]# tree
.
├── minion_master.pub
├── minion.pem
└── minion.pub
[root@server3 minion]# yum install tree -y
[root@server3 minion]# tree
.
├── minion_master.pub
├── minion.pem
└── minion.pub
[root@server1 master]# yum install lsof -y
[root@server1 master]# lsof -i :4505
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
salt-mast 1432 root 16u IPv4 14217 0t0 TCP server1:4505 (LISTEN)
salt-mast 1432 root 18u IPv4 18675 0t0 TCP server1:4505->server2:52115 (ESTABLISHED)
salt-mast 1432 root 19u IPv4 18688 0t0 TCP server1:4505->server3:34836 (ESTABLISHED)
[root@server1 master]# lsof -i :4506
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
salt-mast 1438 root 24u IPv4 14227 0t0 TCP server1:4506 (LISTEN)
[root@server1 master]# salt '*' test.ping
[root@server1 master]# salt '*' cmd.run hostname
[root@server1 salt]# salt server2 test.ping
server2:
True
[root@server1 salt]# salt server2 cmd.run hostname
server2:
server2
[root@server1 salt]# salt server2 cmd.run 'df -h'
server2:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 972M 17G 6% /
tmpfs 246M 16K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
[root@server1 ~]# vim /etc/salt/master
[root@server1 ~]# mkdir /srv/salt/
[root@server1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [ OK ]
Starting salt-master daemon: [ OK ]
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# mkdir httpd
[root@server1 salt]# cd httpd/
[root@server1 httpd]# vim install.sls (自动安装httpd和php)
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
[root@server1 httpd]# salt server2 state.sls httpd.install test=true
[root@server1 httpd]# salt server2 state.sls httpd.install
[root@server1 httpd]# salt server3 state.sls httpd.install test=true
[root@server2 ~]# rpm -q httpd
httpd-2.2.15-29.el6_4.x86_64
[root@server2 ~]# rpm -q php
php-5.3.3-38.el6.x86_64
[root@server3 minion]# rpm -q httpd
httpd-2.2.15-29.el6_4.x86_64
[root@server3 minion]# rpm -q php
php-5.3.3-26.el6.x86_64