标签:
一、三种运行方式
二、安装
安装master
yum -y install salt-master
安装minion
yum -y install salt-minion
在minion节点修改minion配置文件,指定master,修改自己的ID
vim /etc/salt/minion master: 192.168.0.185 id: minion.saltstack.com
在master上给minion授权证书
salt-key #查看未被授权的minion节点 salt-key -a minion.saltstack.ocom #授权 salt-key #查看被授权的minion节点
三、salt远程执行
远程执行三大功能
四、saltstack对象
概念:
在Master上可以通过不同的Target去管理不同的Minion
匹配方式
1.正则匹配
在操作与管理Minion时可以通过正则表达式来匹配Minion ID的方式去管理它们.
[root@master ~]# salt -E ‘sal*‘ test.ping salt: True
2.列表匹配
[root@master ~]# salt -L ‘minion,minion1‘ test.ping minion: True minion1: True
3.Grians匹配
[root@master ~]# salt -G ‘os:CentOS‘ test.ping salt: True
#其中os:CentOS是一组键值对
4.Pillar匹配
[root@master ~]# salt -I ‘key:value‘ test.ping minion: True minion1: True #其中key:value是一组键值对,跟Grians的键值类似
5.组匹配
[root@master ~]# salt -N groups test.ping minion: True minion1: True #在saltstack系统中也可以提前给minion定义组角色,但需要提前知道minion ID信息才能把它定义到某个组中.groups是我们在master配置文件中定义的组名称
6.复合匹配
[root@master ~]# salt -c ‘G@os:MacOS or L@Minion1’ test.ping minion: True minion1: True #os:MacOS or L@Minion1是复合组合,支持使用and和or 关联多个条件
7.CIDR匹配
[root@master ~]# salt -c ‘192.168.1.0/24’ test.ping minion: True minion1: True #192.168.1.0/24是一个指定的CIDR网段,这里CIDR匹配的IP地址是Minion连接Master 4505端口的来源地址
五、Master和Minion认证流程
在Master的/etc/salt/pki/master目录下和Minion的/etc/salt/pki/minion目录下都有minion.pem(私钥)和minion.pub(公钥)
认证流程就是互相交换公钥:
Master把Minion的公钥放在/etc/salt/pki/master/minions的目录下
[root@master minions]# ls minion.saltstack.com #minion的公钥名称,以minion的ID命名
Minion把Master的公钥放在/etc/salt/pki/minion目录下
└── minion ├── minion_master.pub #master的公钥名称 ├── minion.pem └── minion.pub
六、salt数据系统
Grains
概念:
测试grains
[root@master ~]# salt ‘*‘ grains.items #显示所有minion的信息 minion.saltstack.com: ---------- SSDs: biosreleasedate: 07/02/2015 biosversion: 6.00 cpu_flags: - fpu - vme - de - pse - tsc - msr - pae - mce - cx8 - apic - sep - mtrr - pge - mca - cmov - pat - pse36 - clflush - dts - mmx - fxsr - sse - sse2 - ss - syscall - nx - rdtscp - lm - constant_tsc - arch_perfmon - pebs - bts - nopl - xtopology - tsc_reliable - nonstop_tsc - aperfmperf - pni - pclmulqdq - ssse3 - cx16 - pcid - sse4_1 - sse4_2 - x2apic - popcnt - tsc_deadline_timer - aes - xsave - avx - f16c - rdrand - hypervisor - lahf_lm - ida - arat - epb - pln - pts - dtherm - fsgsbase - tsc_adjust - smep cpu_model: Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz cpuarch: x86_64 domain: fqdn: minion fqdn_ip4: - 221.192.153.46 fqdn_ip6: - fe80::20c:29ff:fe14:16f9 gpus: |_ ---------- model: SVGA II Adapter vendor: unknown host: minion hwaddr_interfaces: ---------- eno16777736: 00:0c:29:14:16:f9 lo: 00:00:00:00:00:00 id: minion.saltstack.com init: systemd ip4_interfaces: ---------- eno16777736: - 192.168.0.9 lo: - 127.0.0.1 ip6_interfaces: ---------- eno16777736: - fe80::20c:29ff:fe14:16f9 lo: - ::1 ip_interfaces: ---------- eno16777736: - 192.168.0.9 - fe80::20c:29ff:fe14:16f9 lo: - 127.0.0.1 - ::1 ipv4: - 127.0.0.1 - 192.168.0.9 ipv6: - ::1 - fe80::20c:29ff:fe14:16f9 kernel: Linux kernelrelease: 3.10.0-229.el7.x86_64 locale_info: ---------- defaultencoding: UTF-8 defaultlanguage: en_US detectedencoding: UTF-8 localhost: minion lsb_distrib_id: CentOS Linux machine_id: af38bc2fa53342ab90e8fd3b887abb2d manufacturer: VMware, Inc. master: 192.168.0.116 mdadm: mem_total: 475 nodename: minion num_cpus: 1 num_gpus: 1 os: CentOS os_family: RedHat osarch: x86_64 oscodename: Core osfinger: CentOS Linux-7 osfullname: CentOS Linux osmajorrelease: 7 osrelease: 7.1.1503 osrelease_info: - 7 - 1 - 1503 path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin productname: VMware Virtual Platform ps: ps -efH pythonexecutable: /usr/bin/python pythonpath: - /usr/bin - /usr/lib64/python27.zip - /usr/lib64/python2.7 - /usr/lib64/python2.7/plat-linux2 - /usr/lib64/python2.7/lib-tk - /usr/lib64/python2.7/lib-old - /usr/lib64/python2.7/lib-dynload - /usr/lib64/python2.7/site-packages - /usr/lib64/python2.7/site-packages/gtk-2.0 - /usr/lib/python2.7/site-packages pythonversion: - 2 - 7 - 5 - final - 0 saltpath: /usr/lib/python2.7/site-packages/salt saltversion: 2015.5.10 saltversioninfo: - 2015 - 5 - 10 - 0 selinux: ---------- enabled: True enforced: Permissive serialnumber: VMware-56 4d c5 82 37 d8 1e 0a-87 fd 88 f6 6c 14 16 f9 server_id: 748666745 shell: /bin/sh systemd: ---------- features: +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN version: 219 virtual: VMware zmqversion: 3.2.5 [root@master ~]# salt -G ‘os:CentOS‘ test.ping #在所有minion的信息中过滤出系统为centos的执行ping操作 minion.saltstack.com: True 注意: 所有的minion grains信息都可以作为过滤条件
自定义Grains的方法:
通过Minion配置文件定义
Minion端
vim /etc/salt/grains #自定义的grains都写在这个配置文件里 cloud: openstack #自定义一个grains,相当于key:value的形式
Master端
由于minion做了修改所以得在master端更新一下信息,才能获取到 [root@master ~]# salt ‘*‘ saltutil.sync_grains #更新
测试
[root@master ~]# salt -G ‘cloud:openstack‘ test.ping minion.saltstack.com: True
Pillar
概念:
主要作用就是存储和定义配置管理中需要的一些数据,比如软件版本号、用户名密码等信息,只有指定的Minion可以看到.它的定义存储格式跟grains类似
七、执行结果处理
1.概念:
通过Return我们可以对SaltStack的每次操作进行记录,mysql目前已经支持30种数据存储与接口
2.Return流程
Return是在Master端触发任务,然后Minion端接受处理任务后直接与Return岑楚服务器建立连接,然后把数据Return存到存储服务器.
注意:因为这个过程都是Minion端操作存储服务器,所以要确保Minion端的配置跟依赖包是正确的
3.使用Mysql作为Return存储方式
安装mysql(这里把master作为mysql数据库)
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-release-el7-5.noarch.rpm yum install mysql-server systemctl start mysql
配置mysql
#创建salt数据库 CREATE DATABASE `salt` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; #使用salt数据库 USE `salt`; #删除原来的jids表 DROP TABLE IF EXISTS `jids`; #创建jids表 CREATE TABLE `jids` ( `jid` varchar(255) NOT NULL, `load` mediumtext NOT NULL, UNIQUE KEY `jid` (`jid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 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; #删除salt_events表 DROP TABLE IF EXISTS `salt_events`; #创建salt_events表 CREATE TABLE `salt_events` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `tag` varchar(255) NOT NULL, `data` mediumtext NOT NULL, `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `master_id` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `tag` (`tag`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; #授权 grant all on salt.* to salt@‘%‘ identified by ‘salt‘;
测试MySQL
mysql -u salt -p -h 192.168.0.116 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | salt | #salt需要的数据库 +--------------------+ mysql> use salt; mysql> show tables; #salt数据库下面的表结构 +----------------+ | Tables_in_salt | +----------------+ | jids | #JobID | salt_events | #salt事件 | salt_returns | #salt执行命令的返回结果 +----------------+
配置Master
vim /etc/salt/master #在最后一行添加: mysql.host: ‘192.168.0.116‘ mysql.user: ‘salt‘ mysql.pass: ‘salt‘ mysql.db: ‘salt‘ mysql.port: 3306 systemctl restart salt-master
配置Minion(暂时需要在minion端也得配置)
vim /etc/salt/minion #在最后一行添加 mysql.host: ‘192.168.0.116‘ mysql.user: ‘salt‘ mysql.pass: ‘salt‘ mysql.db: ‘salt‘ mysql.port: 3306 systemctl restart salt-minion
测试执行命令的结果是否存到数据库里
Master: [root@master ~]#mysql mysql> use salt; mysql> select * from salt_returns Empty set (0.01 sec) #可以看到现在是空的
执行命令:
[root@master ~]# salt ‘*‘ cmd.run ‘df -h‘ --return mysql> select * from salt_returns | cmd.run | 20161004173011028071 | "Filesystem Size Used Avail Use% Mounted on\n/dev/sda5 17G 17G 996M 95% /\ndevtmpfs 230M 0 230M 0% /dev\ntmpfs 238M 92K 238M 1% /dev/shm\ntmpfs 238M 8.9M 229M 4% /run\ntmpfs 238M 0 238M 0% /sys/fs/cgroup\n/dev/sda2 97M 97M 20K 100% /boot\ntmpfs 48M 12K 48M 1% /run/user/42\ntmpfs 48M 0 48M 0% /run/user/0" | minion.saltstack.com | 1 | {"fun_args": ["df -h"], "jid": "20161004173011028071", "return": "Filesystem Size Used Avail Use% Mounted on\n/dev/sda5 17G 17G 996M 95% /\ndevtmpfs 230M 0 230M 0% /dev\ntmpfs 238M 92K 238M 1% /dev/shm\ntmpfs 238M 8.9M 229M 4% /run\ntmpfs 238M 0 238M 0% /sys/fs/cgroup\n/dev/sda2 97M 97M 20K 100% /boot\ntmpfs 48M 12K 48M 1% /run/user/42\ntmpfs 48M 0 48M 0% /run/user/0", "retcode": 0, "success": true, "fun": "cmd.run", "id": "minion.saltstack.com"} | 2016-10-04 17:30:13 | 1 row in set (0.00 sec) #可以看到 已经有一条数据了
八、Job管理
概念:
在saltstack里面执行任何一个操作都会在master上产生一个jid号.
原理:
通过salt-run来管理job
1.查看salt-run对job管理的一些用法
[root@master ~]# salt-run -d |grep jobs ‘jobs.active:‘ #查看正在运行的jobs Return a report on all actively running jobs from a job id centric salt-run jobs.active ‘jobs.list_job:‘ #指定Jid查看jobs详细信息 salt-run jobs.list_job 20130916125524463507 ‘jobs.list_jobs:‘ #查看所有jobs信息 List all detectable jobs and associated functions salt-run jobs.list_jobs ‘jobs.lookup_jid:‘ #指定Jid查看jobs结果 salt-run jobs.lookup_jid 20130916125524463507 salt-run jobs.lookup_jid 20130916125524463507 outputter=highstate ‘jobs.print_job:‘ #指定Jid查询jobs详细信息 salt-run jobs.print_job 20130916125524463507 Note:可以通过salt-run -d jobs查看关于每个参数的解释
2.salt-run操作job实例
执行一条命令 [root@master ~]# salt ‘salt‘ cmd.run ‘sleep 100;whoami‘ ^CExiting on Ctrl-C #由于时间太长所以Ctrl-C了 This job‘s jid is: 20161008144119107339 #这是jobID The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later run: salt-run jobs.lookup_jid 20161008144119107339
3.查看这个job的详细记录
[root@master ~]# salt-run jobs.list_job 20161008144119107339 Arguments: - sleep 100;whoami Function: cmd.run Minions: - salt Result: ---------- salt: ---------- return: root StartTime: 2016, Oct 08 14:41:19.107339 Target: salt Target-type: glob User: root jid: 20161008144119107339
通过SaltStack Module来管理job
概念:
由于salt-run对job管理有些局限,不支持kill掉某个job,所以介绍用saltstack 自带的module来管理job
查看相关module用法
[root@master ~]# salt \* sys.doc saltutil |grep job ‘saltutil.find_cached_job:‘ #查询job cache信息 Return the data for a specific cached job id salt ‘*‘ saltutil.find_cached_job <job id> ‘saltutil.find_job:‘ #查看job信息 Return the data for a specific job id salt ‘*‘ saltutil.find_job <job id> ‘saltutil.kill_job:‘ #kill掉Job(发送SIGTERM 9信号方式) Sends a kill signal (SIGKILL 9) to the named salt job‘s process salt ‘*‘ saltutil.kill_job <job id> salt ‘*‘ saltutil.runner jobs.list_jobs ‘saltutil.signal_job:‘ #发送指定信号 Sends a signal to the named salt job‘s process salt ‘*‘ saltutil.signal_job <job id> 15 ‘saltutil.term_job:‘ #删除job(发送SIGTERM 15信号方式) Sends a termination signal (SIGTERM 15) to the named salt job‘s process salt ‘*‘ saltutil.term_job <job id>
九、Event和Reactor系统
Event系统
概念:
Event是SaltStack里面对每个时间的一个记录,它相比job更加底层,Event能记录更加详细的saltstack事件
比如:Minion服务启动后请求Master签发证书或者证书校验的过程,都能通过Event事件来查看整个过程
查看Event事件
1.打开两个Master终端 第一个终端执行查看事件命令并等待: [root@master ~]# salt-run state.event pretty=True 第二个终端执行一个salt任务: [root@master ~]# salt ‘salt‘ cmd.run ‘ls‘ salt: anaconda-ks.cfg 这时再切到第一个终端查看: salt/event/new_client { "_stamp": "2016-10-08T07:02:27.488312" } 20161008150227502370 { "_stamp": "2016-10-08T07:02:27.502657", "minions": [ "salt" ] } salt/job/20161008150227502370/new { "_stamp": "2016-10-08T07:02:27.502827", "arg": [ "ls" ], "fun": "cmd.run", "jid": "20161008150227502370", "minions": [ "salt" ], "tgt": "salt", "tgt_type": "glob", "user": "root" } salt/job/20161008150227502370/ret/salt { "_stamp": "2016-10-08T07:02:27.630653", "cmd": "_return", "fun": "cmd.run", "fun_args": [ "ls" ], "id": "salt", "jid": "20161008150227502370", "retcode": 0, "return": "anaconda-ks.cfg", "success": true } Note:出现了刚才任务的详细信息
Reactor系统
概念:
Reactor是基于Event的每个事件来做相应的操作(states).
我们可以理解为Reactor一直监听着Event然后触发一些states操作.
实例:
在我们日常 大规模新机器上线或者初始化机器的时候,都希望Minion第一次服务起来的时候就能完成所有配置,这个时候需要自动完成Minion的证书签发和根据不同的业务完成不同的states配置.
后续再补··········
标签:
原文地址:http://www.cnblogs.com/zhaijunming5/p/5939437.html