saltstack
简介
参考文档 http://docs.saltstack.cn/ http://blog.cunss.com/?p=255 http://www.iyunv.com/forum-142-1.html http://www.0550go.com/automation ... k-file-recurse.html 比puppet出来晚几年,基于python开发,如果你仔细对比会发现saltstack和puppet很像,随着saltstack的使用量越来越多,它的发展也是越来越迅猛,可以说saltstatck可以实现puppet的所有功能,它比puppet容易配置。 三种工作方式: 本地, c/s, 基于ssh(不用安装agent) 三大功能: 远程命令执行,配置管理(服务,文件,cron,用户,组),云管理 支持系统:大多数都支持,windows上不支持安装master
1.准备工作
两台机器:
139.129.119.104(服务端) CentOS release 6.8
112.74.97.66 (客户端) CentOS release 6.8
关闭防火墙
两台机器关闭selinux,清空iptables规则,并保存
设置hostname
vi /etc/sysconfig/network
服务端:hy1
客户端:hy2
服务端和客户端/etc/hosts文件手动添加
139.129.119.104 hy1
112.74.97.66 hy2
测试
[root@hy1 ~]# ping hy2
PING hy2 (112.74.97.66) 56(84) bytes of data.
[root@hy2 ~]# ping hy1
PING hy1 (139.129.119.104) 56(84) bytes of data.
2. 服务端安装
yum install -y epel-release
yum install -y salt-master salt-minion
/etc/init.d/salt-master start
/etc/init.d/salt-minion start
chkconfig salt-master on
chkconfig salt-minion on
3.客户端安装
yum install -y epel-release
yum install -y salt-minion
vim /etc/salt/minion //指定server的ip
大概是16行,修改或增加
master: 139.129.119.104
id: client
说明,这个id虽然不定义也没有关系,如果不定义,那么master会以客户端的hostname来显示,一定定义了就按id定义的名字来显示了,这个id可以不和hostname一致,但定义了id后,也应该在/etc/hosts里面定义记录
/etc/init.d/salt-minion start
chkconfig salt-minion on
4. 认证
添加签名
server上:
salt-key -a hy2
此时我们在client的 /etc/salt/pki/minion 目录下面会多出一个minion_master.pub 文件
查看已签名的客户端
[root@hy1 ~]# salt-key
Accepted Keys:
hy2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
salt-key 可以使用-A签名所有主机,也可以使用-d删除指定主机的key
5.远程执行
命令
示例1: salt '*' test.ping 这里的*表示所以已经签名的客户端,也可以指定其中一个
示例2: salt '*' cmd.run 'df -h'
[root@hy1 ~]# salt 'hy*' cmd.run 'df -h' hy2: Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 1.1G 16G 7% / tmpfs 504M 12K 504M 1% /dev/shm /dev/sda1 190M 28M 153M 16% /boot [root@hy1 ~]# salt '*' test.ping hy2: True [root@hy1 ~]# salt 'hy*' test.ping hy2: True
说明1: 这里的*必须是在master上已经被接受过的客户端,可以通过salt-key查到,通常是我们已经设定的id值。关于这部分内容,它支持通配、列表以及正则。 比如两台客户端 web10 web11, 那我们可以写成 salt 'web*' salt 'web1[02]' salt -L 'web10,web11' salt -E 'web(10|11) 等形式,使用列表,即多个机器用逗号分隔,而且需要加-L,使用正则必须要带-E选项。 它还支持grains,加-G选项,下面会介绍到。
6. grains
grains是在minion启动时收集到的一些信息,比如操作系统类型、网卡ip等。 使用命令
salt 'client.aming.com' grains.ls 列出所有的grains项目名字
salt 'client.aming.com' grains.items 列出所有grains项目以及值
[root@hy1 ~]# salt 'hy2' grains.ls hy2: - SSDs - cpu_flags - cpu_model - cpuarch - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gpus - host - hwaddr_interfaces - id - init - ip4_interfaces - ip6_interfaces - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - locale_info - localhost - lsb_distrib_codename - lsb_distrib_id - lsb_distrib_release - machine_id - master - mdadm - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - osrelease_info - path - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - server_id - shell - virtual - zmqversion [root@hy1 ~]# salt 'hy2' grains.items hy2: ---------- SSDs: 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 - nx - pdpe1gb - rdtscp - lm - constant_tsc - up - arch_perfmon - pebs - bts - xtopology - tsc_reliable - nonstop_tsc - aperfmperf - unfair_spinlock - pni - pclmulqdq - ssse3 - fma - cx16 - pcid - sse4_1 - sse4_2 - x2apic - movbe - popcnt - tsc_deadline_timer - aes - xsave - avx - f16c - rdrand - hypervisor - lahf_lm - abm - 3dnowprefetch - ida - arat - epb - xsaveopt - pln - pts - dtherm - hwp - hwp_noitfy - hwp_act_window - hwp_epp - fsgsbase - bmi1 - hle - avx2 - smep - bmi2 - invpcid - rtm - rdseed - adx cpu_model: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz cpuarch: i686 domain: fqdn: hy2 fqdn_ip4: - 192.168.1.101 fqdn_ip6: gpus: |_ ---------- model: SVGA II Adapter vendor: unknown host: hy2 hwaddr_interfaces: ---------- eth0: 00:0c:29:99:c8:31 lo: 00:00:00:00:00:00 id: hy2 init: upstart ip4_interfaces: ---------- eth0: - 192.168.1.101 lo: - 127.0.0.1 ip6_interfaces: ---------- eth0: - fe80::20c:29ff:fe99:c831 lo: - ::1 ip_interfaces: ---------- eth0: - 192.168.1.101 - fe80::20c:29ff:fe99:c831 lo: - 127.0.0.1 - ::1 ipv4: - 127.0.0.1 - 192.168.1.101 ipv6: - ::1 - fe80::20c:29ff:fe99:c831 kernel: Linux kernelrelease: 2.6.32-642.el6.i686 locale_info: ---------- defaultencoding: UTF8 defaultlanguage: en_US detectedencoding: UTF-8 localhost: hy2 lsb_distrib_codename: Final lsb_distrib_id: CentOS lsb_distrib_release: 6.8 machine_id: d9b154423d19355980b585c700000008 master: 192.168.1.100 mdadm: mem_total: 1006 nodename: hy2 num_cpus: 1 num_gpus: 1 os: CentOS os_family: RedHat osarch: i686 oscodename: Final osfinger: CentOS-6 osfullname: CentOS osmajorrelease: 6 osrelease: 6.8 osrelease_info: - 6 - 8 path: /sbin:/usr/sbin:/bin:/usr/bin ps: ps -efH pythonexecutable: /usr/bin/python2.6 pythonpath: - /usr/bin - /usr/lib/python26.zip - /usr/lib/python2.6 - /usr/lib/python2.6/plat-linux2 - /usr/lib/python2.6/lib-tk - /usr/lib/python2.6/lib-old - /usr/lib/python2.6/lib-dynload - /usr/lib/python2.6/site-packages - /usr/lib/python2.6/site-packages/gtk-2.0 pythonversion: - 2 - 6 - 6 - final - 0 saltpath: /usr/lib/python2.6/site-packages/salt saltversion: 2015.5.10 saltversioninfo: - 2015 - 5 - 10 - 0 selinux: ---------- enabled: False enforced: Disabled server_id: 1231409202 shell: /bin/sh virtual: VMware zmqversion: 3.2.5
grains的信息并不是动态的,并不会时时变更,它只是在minion启动时收集到的。
grains可以做配置管理。
自定义grains
vim /etc/salt/minion 添加或更改: grains: role: - nginx env: - test 或者 vim /etc/salt/grains 添加: role: nginx env: test
重启minion服务
获取grains:
salt '*' grains.item role env
salt 'hy2' grains.get role
[root@hy1 ~]# salt '*' grains.item role env hy2: ---------- env: test role: nginx [root@hy1 ~]# salt 'hy2' grains.get role hy2: nginx
grains其实在远程执行命令时,很方便。我们可以按照grains的一些指标来操作。比如把所有的web服务器的grains的role设置为nginx,那这样我们就可以批量对nginx的服务器进行操作了:
salt -G role:nginx cmd.run 'hostname'
salt -G os:CentOS cmd.run 'hostname'
[root@hy1 ~]# salt -G role:nginx cmd.run 'hostname'
hy2:
hy2
7. pillar
pillar和grains不一样,是在master上定义的,并且pillar其实在远程执行命令时,很方便。是针对minion定义的一些信息。像一些比较重要的数据(密码)可以存在pillar里,还可以定义变量等。
查看指定minion的pillar值(我测试的版本为空):
salt 'hy2' pillar.items
配置自定义pillar
vim /etc/salt/master找到如下配置: pillar_roots: base: - /srv/pillar 去掉前面的警号 mkdir /srv/pillar vi /srv/pillar/test.sls //内容如下 conf: /etc/123.conf myname: hy1 system: CentOS vi /srv/pillar/top.sls //总入口配置文件 base: 'hy2': - test
/etc/init.d/salt-master restart
当更改完pillar配置文件后,我们可以通过刷新pillar配置来获取新的pillar状态:
salt ‘*’ saltutil.refresh_pillar
验证:
[root@hy1 ~]# salt '*'.pillar.items hy2: ---------- conf: /etc/123.conf myname: hy2 system: CentOS [root@hy1 ~]# salt '*' pillar.item test hy2: ---------- [root@hy1 ~]# salt '*' pillar.item conf hy2: ---------- conf: /etc/123.conf [root@hy1 ~]# salt '*' pillar.item conf myname system hy2: ---------- conf: /etc/123.conf myname: hy2 system: CentOS [root@hy1 ~]# salt -I system:CentOS cmd.run 'w' hy2: 00:37:53 up 1:59, 1 user, load average: 0.08, 0.15, 0.13 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.1.1 22:43 51:01 0.04s 0.04s -bash
pillar同样可以用来作为salt的匹配对象。
8.配置管理
安装并启动服务
yum安装apache,启动并设置开机启动
server上: vim /etc/salt/master //搜索找到 file_roots 打开如下内容的注释: file_roots: base: - /srv/salt mkdir /srv/salt cd /srv/salt vim /srv/salt/top.sls //加入如下内容 base: '*': - apache 意思是,在所有的客户端上执行 apache模块 vim /srv/salt/apache.sls //加入如下内容,这个就是apache模块的内容 apache-service: pkg.installed: - names: - httpd - httpd-devel service.running: - name: httpd - enable: True 说明,该模块调用了pkg.installed 函数,下面是要安装的包的名字。service.running也是一个函数,来保证指定的服务启动,enable表示开机启动。 执行: salt 'hy2' state.highstate
hy2: ---------- ID: apache-service Function: pkg.installed Name: httpd Result: True Comment: Package httpd is already installed. Started: 17:50:50.833761 Duration: 724.248 ms Changes: ---------- ID: apache-service Function: pkg.installed Name: httpd-devel Result: True Comment: Package httpd-devel is already installed. Started: 17:50:51.558377 Duration: 0.76 ms Changes: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service httpd is already enabled, and is in the desired state Started: 17:50:51.559782 Duration: 31.318 ms Changes: Summary ------------ Succeeded: 3 Failed: 0 ------------ Total states run: 3
[root@hy2 ~]# rpm -q httpd httpd-2.2.15-56.el6.centos.3.i686 root 2145 0.0 0.3 11048 3324 ? Ss 17:48 0:00 /usr/sbin/httpd apache 2148 0.0 0.2 11048 2160 ? S 17:48 0:00 /usr/sbin/httpd apache 2149 0.0 0.2 11048 2160 ? S 17:48 0:00 /usr/sbin/httpd apache 2150 0.0 0.2 11048 2188 ? S 17:48 0:00 /usr/sbin/httpd apache 2151 0.0 0.2 11048 2160 ? S 17:48 0:00 /usr/sbin/httpd apache 2152 0.0 0.2 11048 2160 ? S 17:48 0:00 /usr/sbin/httpd apache 2153 0.0 0.2 11048 2160 ? S 17:48 0:00 /usr/sbin/httpd apache 2155 0.0 0.2 11048 2160 ? S 17:48 0:00 /usr/sbin/httpd apache 2156 0.0 0.2 11048 2160 ? S 17:48 0:00 /usr/sbin/httpd
9.同步文件
操作目录在/srv/salt下
cd /srv/salt/ [root@hy1 salt]# ls apache.sls top.sls mkdir -p test/123 echo 123 > test/123/1.txt vim filetest.sls file_test: file.managed: - name: /tmp/hy2.txt - source: salt://test/123/1.txt - user: root - group: root - mode: 644 vim top.sls //加入- filetest base: '*': - filetest
salt 'hy2' state.highstate
10.同步目录
vim dirtest.sls //加入 file_dir: file.recurse: - name: /tmp/123 - source: salt://test/123 - user: root - group: root - file_mode: 644 - dir_mode: 755 - mkdir: True - clean: True [root@hy1 salt]# vim top.sls base: 'hy2': - dirtest salt 'hy2' state.highstate
11.远程执行命令(更规范的形式)
vim cmdtest.sls cmd_test: cmd.run: - onlyif: test -f /tmp/111.txt - names: - rm -rf /tmp/111.txt /tmp/1233 - user: root vim top.sls base: 'hy2': - cmdtest salt 'hy2' state.highstate
12.远程执行shell脚本
vim test/1.sh #!/bin/bash touch /tmp/111.txt if [ -d /tmp/1233 ] then rm -rf /tmp/1233 fi vim cmdtest.sls cmd_test: cmd.script: - source: salt://test/1.sh - user: root vim top.sls base: 'hy2': - cmdtest
13. 任务计划
cron_test: cron.present: - name: /bin/touch /tmp/111.txt - user: root - minute: '*' - hoinur: 20 - daymonth: '*' - month: '*' - dayweek: '*' 注意,*需要用单引号引起来。 当然我们还可以使用file.managed模块来管理cron,因为系统的cron都是以配置文件的形式存在的。 删除该cron: cron.absent: - name: /bin/touch /tmp/111.txt 两者不能共存,要想删除一个cron,那之前的present就得删除掉。
14. 一些可能会用到的命令
cp.get_file 拷贝master上的文件到客户端
salt '*' cp.get_file salt://test/1.txt /tmp/123.txt
cp.get_dir 拷贝目录
salt '*' cp.get_dir salt://test/conf /tmp/ //会自动在客户端创建conf目录,所以后面不要加conf,如果写成 /tmp/conf/ 则会在/tmp/conf/目录下又创建conf
salt-run manage.up 显示存活的minion
salt '*' cmd.script salt://test/1.sh 命令行下执行master上的shell脚本