首页 > 代码库 > Saltstack
Saltstack
Saltstack是Python开发的,上千台的服务器都可以管理。
运维重复性工作:系统安装、环境部署、添加监控、代码发布(基于git或svn二次开发)、项目迁移、计划任务。
salt是一个新的基础平台管理工具。只需花费数分钟即可运行起来,扩展性足以支撑管理上万台服务器,数秒即可完成数据传递。
salt可以做配置管理、远程命令、包管理。
salt配置
准备3台虚拟机,按照规范修改主机名:test-c2c-console01、test-c2c-php01、test-c2c-php02。
- [root@test-c2c-console01 ~]# cat /etc/sysconfig/network
- NETWORKING=yes
- HOSTNAME=test-c2c-console01.bj
- [root@test-c2c-console01 ~]# cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 oldboylinux
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 oldboylinux
- 192.168.31.138 test-c2c-php01
- 192.168.31.137 test-c2c-php02
- 192.168.31.128 test-c2c-console01.bj
配置yum源
- [root@test-c2c-console01 ~]# cd /etc/yum.repos.d/
- [root@test-c2c-console01 yum.repos.d]# ls
- CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo
- CentOS-Base.repo.20161216.oldboy CentOS-fasttrack.repo CentOS-Vault.repo
- rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
- wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
- [root@test-c2c-console01 yum.repos.d]# ls
- CentOS6-Base-163.repo CentOS-Debuginfo.repo CentOS-Vault.repo
- CentOS-Base.repo CentOS-fasttrack.repo epel.repo
- CentOS-Base.repo.20161216.oldboy CentOS-Media.repo epel-testing.repo
服务端
yum install salt-master –y
/etc/init.d/salt-master start
chkconfig salt-master on
客户端
yum install salt-minion -y
vim /etc/salt/minion
master: 192.168.31.128 #master端地址
cachedir: /etc/salt/modules #模块目录
log_file: /var/log/salt/minion.log #日志路径
log_level: warning #日志级别
/etc/init.d/salt-minion start
chkconfig salt-minion on
key管理
- [root@test-c2c-console01 ~]# salt-key -L
- Accepted Keys: #已认证
- Denied Keys: #未认证
- Unaccepted Keys:
- test-c2c-php01
- test-c2c-php02
- Rejected Keys: #被吊销
- [root@test-c2c-console01 ~]# salt-key -A
- The following keys are going to be accepted:
- Unaccepted Keys:
- test-c2c-php01
- test-c2c-php02
- Proceed? [n/Y] y
- Key for minion test-c2c-php01 accepted.
- Key for minion test-c2c-php02 accepted.
- [root@test-c2c-console01 ~]# salt-key -L
- Accepted Keys:
- test-c2c-php01
- test-c2c-php02
- Denied Keys:
- Unaccepted Keys:
- Rejected Keys:
- [root@test-c2c-console01 ~]# salt ‘*‘ test.ping
- test-c2c-php02:
- True
- test-c2c-php01:
- True
常用参数:
-L:查看key状态
-A:允许所有
-D:删除所有
-a:认证指定的key
-d:删除指定的key
-r:注销指定的key(该key状态为未认证)
管理
分组
[root@test-c2c-console01 salt]# pwd
/etc/salt
[root@test-c2c-console01 salt]# vim master
nodegroups:
#dev:‘L@ops-dev01.bj,ops-dev02.bj‘ #列表匹配
dev:‘E@ops-dev0[1-9].bj‘ #正则匹配
- [root@test-c2c-console01 salt]# salt -N ‘php‘ test.ping #ping php组的机器
- test-c2c-php02:
- True
- test-c2c-php01:
- True
- [root@test-c2c-console01 salt]# salt -N ‘php‘ cmd.run ‘uptime‘ #查看php组机器的负载
- test-c2c-php01:
- 11:45:01 up 1:45, 2 users, load average: 0.00, 0.00, 0.00
- test-c2c-php02:
- 11:44:20 up 1:46, 2 users, load average: 0.00, 0.00, 0.00
环境配置
file_roots:
base: #测试环境
-/srv/salt
dev: #开发环境
- /srv/salt/dev/services
- /srv/salt/dev/states
prod: #生产环境
- /srv/salt/prod/services
- /srv/salt/prod/states
即时管理
salt -N ‘dev‘ test.ping #匹配分组主机,即时ping
salt -N ‘dev‘ cmd.run ‘uptime‘ #执行命令
salt -N ‘ops-dev(02|03)‘ test.ping #正则匹配主机,即时ping
salt ‘*‘ cmd.run "ab -n 10 -c 2 http://www.google.com/" #匹配所有机器做压力测试
salt -N ‘dev‘ sys.doc cmd #查看模块文档
salt -N ‘dev‘ saltutil.sync_all #同步到dev分组
salt -N ‘dev‘ sys.doc mi #查看模块使用帮助
salt -N ‘dev‘ mi.sshkey #执行该模块
salt -N ‘dev‘ state.sls yum -v test=true #同步指定配置模块
salt -N ‘dev‘ state.hightstate -v test=true #同步所有模块
Saltstack