使用saltstack的前提是PPT 服务、流程、工具和技术
安装
rpm -Uvh http://mirrors.yun-idc.com/epel/6Server/x86_64/epel-release-6-8.noarch.rpm
yum install salt-master salt-minion -y
/etc/init.d/salt-master start
/etc/init.d/salt-minion start
[root@linux-node1 master]# grep '^[a-z]' /etc/salt/minion
master: 10.0.0.7 #master的ip
1:三种运行方式
local
master/minion
salt ssh
2:三大功能
远程执行、配置管理、云管理
2.1:配置管理
分为三个环境,base、dev、prod
[root@linux-node1 salt]# vim /etc/salt/master
[root@linux-node1 master]# mkdir -p /srv/salt
[root@linux-node1 master]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [确定]
Starting salt-master daemon: [确定]
首先创建top.sls文件:是一个入口文件
[root@linux-node1 master]# cd /srv/salt/
base: #定义使用base环境
'*': #定义主机,可以使用*,也可以直接指定主机名
- apache #状态文件名称,建议分目录
在/srv/salt/下面创建一个文件夹如init,把相同类型的功能的文件都放在同一个目录下面,在起下面创建一个apache.sls文件。
调用的时候使用这种方式: - init.apache 不需要加上后缀名
apache-install: #ID声明,声明是干什么用的
pkg.installed: #PKG是一个软件包状态模块,installed是其中的方法
- names: #表明要装的包的名字,因为是装多个包
- httpd
- httpd-devel
[root@linux-node1 master]# salt '*' state.sls apache
#使用salt对所有的机器使用state这个模块下的sls方法来执行apache这个状态模块
#如果使用的分目录的方式,执行的方法是:salt '*' state.sls init.apache
实例:
[root@linux-node1 salt]# cd /srv/salt/
[root@linux-node1 salt]# mkdir init
[root@linux-node1 salt]# cd /srv/salt/
[root@linux-node1 salt]# cat top.sls
base:
'*':
- init.tree
[root@linux-node1 salt]# cd /srv/salt/init/
[root@linux-node1 init]# cat tree.sls
tree-install:
pkg.installed:
- names:
- tree
- cmake
[root@linux-node1 salt]# salt '*' state.sls init.tree
linux-node2.example.com:
----------
ID: tree-install
Function: pkg.installed
Name: cmake
Result: True
Comment: The following packages were installed/updated: cmake
Started: 00:07:37.492640
Duration: 44100.373 ms
Changes:
----------
cmake:
----------
new:
2.8.12.2-4.el6
old:
libarchive:
----------
new:
2.8.3-4.el6_2
old:
----------
ID: tree-install
Function: pkg.installed
Name: tree
Result: True
Comment: Package tree is already installed.
Started: 00:08:21.607298
Duration: 1.081 ms
Changes:
Summary
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
使用salt来管理文件
[root@linux-node1 files]# cat /srv/salt/top.sls
base:
'*':
- init.tree
- init.limit-conf
[root@linux-node1 files]# mkdir /srv/salt/init/files
[root@linux-node1 files]# cp /etc/security/limits.conf /srv/salt/init/files/
[root@linux-node1 files]# cat /srv/salt/init/limit-conf.sls
limit-conf-config:
file.managed:
- name: //etc/security/limits.conf
- source: salt://init/files/limits.conf
- user: root
- group: root
- mode: 644
[root@linux-node1 files]# salt '*' state.highstate
linux-node2.example.com:
----------
ID: tree-install
Function: pkg.installed
Name: cmake
Result: True
Comment: Package cmake is already installed.
Started: 00:31:45.787593
Duration: 1483.04 ms
Changes:
----------
ID: tree-install
Function: pkg.installed
Name: tree
Result: True
Comment: Package tree is already installed.
Started: 00:31:47.270901
Duration: 0.796 ms
Changes:
----------
ID: limit-conf-config
Function: file.managed
Name: //etc/security/limits.conf
Result: True
Comment: File //etc/security/limits.conf updated
Started: 00:31:47.326445
Duration: 21.848 ms
Changes:
----------
diff:
---
+++
@@ -39,8 +39,8 @@
#<domain> <type> <item> <value>
#
-#* soft core 0
-#* hard rss 10000
+* soft core 0
+* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
Summary
------------
Succeeded: 3 (changed=1)
Failed: 0
------------
Total states run: 3
2.1:远程控制
[root@linux-node1 master]#salt '*' cmd.run 'df -h'
grains
收集信息
匹配minion
[root@linux-node1 master]# salt '*' grains.get os
[root@linux-node1 master]# salt '*' grains.ls
[root@linux-node1 master]# salt '*' grains.items
[root@linux-node1 master]# salt '*' grains.get fqdn
[root@linux-node1 master]# salt '*' grains.get ip_interfaces:eth0
[root@linux-node1 master]#salt '*' grains.get os
[root@linux-node1 master]# salt -G os:CentOS cmd.run 'uptime'
vim /etc/salt/minion
/etc/init.d/salt-minion restart
salt -G 'roles:memcache' cmd.run 'echo hehe'
vim /etc/salt/grains
web: nginx
/etc/init.d/salt-minion restart
[root@linux-node1 salt]# vim /srv/salt/top.sls
pillar
给minion指定它想要的数据,在master端来进行操作
vim /etc/salt/master
[root@linux-node1 salt]# mkdir /srv/pillar
[root@linux-node1 salt]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [确定]
Starting salt-master daemon: [确定]
[root@linux-node1 salt]# cd /srv/pillar/
[root@linux-node1 pillar]# vim apache.sls
[root@linux-node1 pillar]# salt '*' pillar.items
linux-node2.example.com:
----------
apache:
httpd
linux-node1.example.com:
----------
apache:
httpd
[root@linux-node1 pillar]# salt -I 'apache:httpd' test.ping
linux-node1.example.com:
Minion did not return. [No response]
linux-node2.example.com:
Minion did not return. [No response]
[root@linux-node1 pillar]# salt '*' saltutil.refresh_pillar #刷新一下pillar
linux-node2.example.com:
True
linux-node1.example.com:
True
[root@linux-node1 pillar]# salt -I 'apache:httpd' test.ping
linux-node2.example.com:
True
linux-node1.example.com:
True
salt 目标 模块 返回
目标的匹配
通配符
https://docs.saltstack.com/en/latest/topics/targeting/globbing.html#targeting-glob
salt '*..example.com ' test.ping
salt '*.example.*' test.ping
salt 'web?.example.net' test.ping
salt 'web[1-5]' test.ping
salt 'web[1,3]' test.ping
salt 'web-[x-z]' test.ping
主机名命名例子:nginx-node01.web.idc01.oldboy.com
正则表达式
salt -E 'salt -E 'linux-node(1|2).example.com' test.ping
在top.sls中这写
base:
'linux-node(1|2).example.com':
- match: pcre
- webserver
list列表
salt -L 'linux-node(1|2).example.com' test.ping
IP地址(SUBNET/IP ADDRESS MATCHING)
https://docs.saltstack.com/en/latest/topics/targeting/ipcidr.html
salt -S 192.168.40.20 test.ping
salt -S 10.0.0.0/24 test.ping
NODE GROUPS
https://docs.saltstack.com/en/latest/topics/targeting/nodegroups.html
grains
https://docs.saltstack.com/en/latest/topics/targeting/grains.html
pillar
https://docs.saltstack.com/en/latest/topics/targeting/pillar.html
模块
服务的模块
salt '*' service.available sshd
salt '*' service.get_all
salt '*' service.reload <service name>
salt '*' service.restart <service name>
salt '*' service.start <service name>
salt '*' service.status <service name> [service signature]
salt '*' service.stop <service name>
网络的模块
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.network.html
磁盘的模块(salt.modules.disk)
salt '*' network.active_tcp
salt '*' network.arp
salt '*' network.connect archlinux.org 80
salt '*' network.connect archlinux.org 80 timeout=3
salt '*' network.connect archlinux.org 80 timeout=3 family=ipv4
salt '*' network.connect google-public-dns-a.google.com port=53 proto=udp timeout=3
salt '*' network.default_route
salt '*' network.dig archlinux.org
salt '*' network.getbufsize #Return network buffer sizes as a dict
salt '*' network.get_hostname
salt '*' network.hw_addr eth0
salt '*' network.in_subnet 10.0.0.0/16
salt '*' network.interface eth0
salt '*' network.interface_ip eth0
salt '*' network.interfaces
salt '*' network.mod_hostname master.saltstack.com #Modify hostname
ACL认证
https://docs.saltstack.com/en/latest/ref/clientacl.html
[root@linux-node1 pillar]# vim /etc/salt/master
[root@linux-node1 ~]# chown -R oldboy.oldboy /var/log/salt/master
[root@linux-node1 ~]# su - oldboy
[oldboy@linux-node1 ~]$ salt '*' test.ping
linux-node2.example.com:
True
linux-node1.example.com:
True
返回
返回到数据库
这个数据库可以安装在任意的一台机器上,
[root@linux-node1 ~]# vim yum -y install mysql-server
在所有的minion端都要安装,因为是从客户端返回数据的
yum -y install MySQL-python
http://docs.saltstack.cn/zh_CN/latest/ref/returners/all/salt.returners.mysql.html?highlight=return
[root@linux-node1 ~]# vim /etc/salt/master
master_job_cache: mysql
mysql.host: '10.0.0.7'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
在mysql数据库中进行操作:
CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE `salt`;
--
-- Table structure for table `jids`
--
DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid` varchar(255) NOT NULL,
`load` mediumtext NOT NULL,
UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for table `salt_returns`
--
DROP TABLE IF EXISTS `salt_returns`;
CREATE TABLE `salt_returns` (
`fun` varchar(50) NOT NULL,
`jid` varchar(255) NOT NULL,
`return` mediumtext NOT NULL,
`id` varchar(255) NOT NULL,
`success` varchar(10) NOT NULL,
`full_ret` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
KEY `id` (`id`),
KEY `jid` (`jid`),
KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for table `salt_events`
--
DROP TABLE IF EXISTS `salt_events`;
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varchar(255) NOT NULL,
`data` varchar(1024) NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
grant all on salt.* to salt@'%' identified by 'salt';
[root@linux-node1 ~]# /etc/init.d/salt-master restart
接下来执行命令,查看记录在数据库中
mysql> use salt;
mysql> select * from salt_returns;
配置管理
可以在/etc/salt/master中看top.sls的信息,默认是在base环境下的。
[root@linux-node1 salt]# vim /etc/salt/master
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
prod:
- /srv/salt/prod
[root@linux-node1 salt]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [确定]
Starting salt-master daemon: [确定]
[root@linux-node1 salt]# mkdir /srv/salt/base
[root@linux-node1 salt]# mkdir /srv/salt/test
[root@linux-node1 salt]# mkdir /srv/salt/prod
[root@linux-node1 salt]# ll /srv/salt/
总用量 12
drwxr-xr-x 2 root root 4096 11月 7 10:14 base
drwxr-xr-x 2 root root 4096 11月 7 10:13 prod
drwxr-xr-x 2 root root 4096 11月 7 10:13 test
[root@linux-node1 salt]# tree
.
├── base
│ ├── apache.sls
│ └── top.sls
├── prod
└── test