目录
1 saltstack
1.1 saltstack简介
SaltStack管理工具允许管理员对多个操作系统创建一个一致的管理系统,包括VMware
vSphere环境。
salt特点:
并行,管理的工具,Python开发的。可二次开发。
salt远程执行
salt配置管理
salt云管理(阿里云升级,salt还没有升级,故salt不能管理阿里云)
salt三种管理模式:
1、Local
2、Master,Minion(类似主从,最常用的)
3、Salt SSH
salt支持的操作系统:
CentOS;Redhat;Fedora;Gentoo;Debian;MAC OS X;Ubuntu;suse;FreeBSD……
Winodows(只能当Minion)
1.2 saltstack安装及配置
1.2.1 环境准备
# 准备至少两台虚拟机,本次实验是四台
1、m01(master)
eth0:192.168.90.61
eth1:172.16.1.61
2、nfs01(minion)
eth0:192.168.90.31
eth1:172.16.1.31
3、backup(minion)
eth0:192.168.90.41
eth1:172.16.1.41
4、web01(minion)
eth0:192.168.90.8
eth1:172.16.1.8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
master和minion上都安装epel源
# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm
- 1
- 2
- 3
1.2.2 master安装及配置
1、在master上安装salt-master
# yum install salt-master -y
- 1
- 2
2、在master上把salt-master加入开机启动
# chkconfig salt-master on
- 1
- 2
3、在master上修改配置文件(/etc/salt/master)
#找到以下内容,注释全部去掉,并且注意下面base前面和下一行前面的空格数量,一个为2,一个为4
# vim /etc/salt/master
file_roots:
base:
- /srv/salt
pillar_roots:
base:
- /srv/pillar
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
4、启动master的salt-master
[root@m01 ~]# /etc/init.d/salt-master start
Starting salt-master daemon: [确定]
- 1
- 2
- 3
1.2.3 minion安装及配置
1、在minion上安装salt-minion
# yum install salt-minion -y
- 1
- 2
2、在minion上把salt-minion加入开机启动
# chkconfig salt-minion on
- 1
- 2
3、修改minion的配置文件(/etc/salt/minion)
#在大概16行的master上添加master主机ip地址或主机名(需要地址解析),冒号和ip之间有一空格
# vim /etc/salt/minion
master: 192.168.90.61
# 找到#id,把注释去掉,给每一台虚拟机起一个有意义的id名称
# 这里三台minion都取各自类型的名称,如果此项不修改,则会使用每台虚拟机的主机名
id: nfs
id: backup
id: web
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
4、启动minion的salt-minion
[root@nfs01 ~]# /etc/init.d/salt-minion start
Starting salt-minion daemon: [确定]
- 1
- 2
- 3
1.2.4 后续操作
# 在以上master端和minion端配置完成启动之后继续以下操作
# master上salt-key查看是否有以下情况,有即成功
[root@m01 ~]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
backup
nfs
web
Rejected Keys:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
# salt-key -A全部管理,-a指定管理机器
[root@m01 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
backup
nfs
web
Proceed? [n/Y] Y
Key for minion backup accepted.
Key for minion nfs accepted.
Key for minion web accepted.
[root@m01 ~]# salt-key
Accepted Keys:
backup
nfs
web
Denied Keys:
Unaccepted Keys:
Rejected Keys:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
# 到此为止salt-master已经成功管理minion机器了,下面开始测试
1.3 测试
1.3.1 测试minion主机是否存活
# salt命令语法格式
salt ‘目标’ 模块(命令) 方法(参数) 返回
# 检查主机是存活
[root@m01 ~]# salt '*' test.ping
web:
True
backup:
True
nfs:
True
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
# 测试单独主机是否存活
[root@m01 ~]# salt 'backup' test.ping
backup:
True
- 1
- 2
- 3
- 4
1.3.2 测试运行命令
# 运行命令,检查所有主机磁盘大小
[root@m01 ~]# salt '*' cmd.run 'df -h'
web:
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.6G 6.9G 19% /
tmpfs 491M 12K 491M 1% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
172.16.1.31:/data 8.8G 1.6G 6.8G 19% /mnt
nfs:
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.6G 6.8G 19% /
tmpfs 491M 12K 491M 1% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
backup:
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.6G 6.9G 19% /
tmpfs 491M 12K 491M 1% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
1.3.3 管理minion文件
# 创建需要的目录并创建配置文件来管理minion
[root@m01 ~]# mkdir /srv/{salt,pillar}
[root@m01 ~]# cd /srv/salt/
[root@m01 salt]# cat host_file.sls
/etc/hosts:
file.managed:
- source: salt://files/hosts
- user: root
- group: root
- mode: 644
[root@m01 salt]# mkdir files
[root@m01 salt]# cd files
[root@m01 files]# pwd
/srv/salt/files
[root@m01 files]# cp /etc/hosts .
[root@m01 files]# ll
总用量 4
-rw-r--r-- 1 root root 349 3月 8 10:40 hosts
[root@m01 salt]# salt '*' state.sls host_file
#指定成功后会有以下信息出现,由于信息过长故只列出一台成功信息。
nfs:
----------
ID: /etc/hosts
Function: file.managed
Result: True
Comment: File /etc/hosts updated
Started: 10:48:05.517696
Duration: 1430.111 ms
Changes:
----------
diff:
---
+++
@@ -8,4 +8,4 @@
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.61 m01
-#############rsq#############
+########rsq########
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
1.3.4 测试salt-cp拷贝文件
# salt拷贝文件(salt-cp)
[root@m01 salt]# salt-cp '*' /etc/hosts /etc/
{'backup': {'/etc/hosts': True},
'nfs': {'/etc/hosts': True},
'web': {'/etc/hosts': True}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
1.3.5 测试统一安装Nginx
# 测试统一安装Nginx
[root@m01 salt]# vim nginx_install.sls
nginx_install:
pkg.installed:
- names:
- nginx
/etc/hosts:
file.managed:
- source: salt://files/hosts
- user: root
- group: root
- mode: 644
- require:
- pkg: nginx_install
service.running:
- names:
- nginx
[root@m01 salt]# salt 'web' state.sls nginx_install
#出现以下信息则证明对端成功安装nginx服务并启动成功
web:
----------
ID: nginx_install
Function: pkg.installed
Name: nginx
Result: True
Comment: The following packages were installed/updated: nginx
Started: 11:09:50.922773
Duration: 11045.613 ms
Changes:
----------
nginx:
----------
new:
1.10.2-1.el6
old:
nginx-all-modules:
----------
new:
1.10.2-1.el6
old:
nginx-mod-http-geoip:
----------
new:
1.10.2-1.el6
old:
nginx-mod-http-image-filter:
----------
new:
1.10.2-1.el6
old:
nginx-mod-http-perl:
----------
new:
1.10.2-1.el6
old:
nginx-mod-http-xslt-filter:
----------
new:
1.10.2-1.el6
old:
nginx-mod-mail:
----------
new:
1.10.2-1.el6
old:
nginx-mod-stream:
----------
new:
1.10.2-1.el6
old:
----------
ID: /etc/hosts
Function: file.managed
Result: True
Comment: File /etc/hosts updated
Started: 11:10:01.974038
Duration: 12.497 ms
Changes:
----------
diff:
---
+++
@@ -8,3 +8,4 @@
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.61 m01
+########rsq########
----------
ID: /etc/hosts
Function: service.running
Name: nginx
Result: True
Comment: Started Service nginx
Started: 11:10:01.988382
Duration: 244.989 ms
Changes:
----------
nginx:
True
Summary
------------
Succeeded: 3 (changed=3)
Failed: 0
------------
Total states run: 3
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
1.3.6 salt配置管理定时任务
# 利用salt配置管理定时任务,其格式为如下:
minute(分)
hour(时)
daymonth(天)
month(月)
dayweek(周)
# 进入到/srv/salt目录下创建crontab.sls文件,每5分钟执行一下同步,内容如下
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
cron.present:
- user: root
- minute: '*/5'
- 1
- 2
- 3
- 4
- 5
# 其它不写的情况代表定时任务中的”*”号
# 测试
[root@m01 salt]# salt '*' state.sls crontab
nfs:
----------
ID: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
Function: cron.present
Result: True
Comment: Cron /usr/sbin/ntpdate times.aliyun.com >>/dev/null added to root's crontab
Started: 14:59:43.288623
Duration: 27.281 ms
Changes:
----------
root:
/usr/sbin/ntpdate times.aliyun.com >>/dev/null
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
backup:
----------
ID: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
Function: cron.present
Result: True
Comment: Cron /usr/sbin/ntpdate times.aliyun.com >>/dev/null added to root's crontab
Started: 14:59:43.372986
Duration: 39.923 ms
Changes:
----------
root:
/usr/sbin/ntpdate times.aliyun.com >>/dev/null
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
web:
----------
ID: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
Function: cron.present
Result: True
Comment: Cron /usr/sbin/ntpdate times.aliyun.com >>/dev/null added to root's crontab
Started: 14:59:43.468049
Duration: 30.553 ms
Changes:
----------
root:
/usr/sbin/ntpdate times.aliyun.com >>/dev/null
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
# 可以去minion中查看显示内容
[root@nfs01 ~]# crontab -l
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:/usr/sbin/ntpdate times.aliyun.com >>/dev/null
*/5 * * * * /usr/sbin/ntpdate times.aliyun.com >>/dev/null
- 1
- 2
- 3
- 4
- 5
# 若想要修改成每3分钟执行,则直接修改配置文件即可
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
cron.present:
- user: root
- minute: '*/3'
- 1
- 2
- 3
- 4
- 5
# 再次执行
[root@m01 salt]# salt '*' state.sls crontab
- 1
- 2
# 去minion中查看
[root@nfs01 ~]# crontab -l
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:/usr/sbin/ntpdate times.aliyun.com >>/dev/null
*/3 * * * * /usr/sbin/ntpdate times.aliyun.com >>/dev/null
- 1
- 2
- 3
- 4
- 5
# 删除定时任务
[root@m01 salt]# cat del_cron.sls
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
cron.absent:
- name: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
- 1
- 2
- 3
- 4
- 5
# 测试:
[root@nfs01 ~]# crontab -l
# Lines below here are managed by Salt, do not edit
- 1
- 2
- 3
# 想看某一模块的帮助信息
[root@m01 salt]# salt '*' sys.doc 'test'
- 1
- 2
1.4 saltstack小结
以上只是简单利用saltstack来进行一些简单的测试。下面总结一下:
1、在修改master和minion配置文件的时候一定要注意空格数量
2、在写管理文件的时候要在/srv/salt目录下写,并且文件后缀名必须为”.sls”
3、在写sls文件的时候需要注意以下问题,以下面文件为例:
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
cron.present:
- user: root
- 1
- 2
- 3
- 4
1)第一行结尾一定要加“:”冒号
2)第二行前面一定是空两个空格
3)第三行前面一定空四个空格
4)“-”后边一定要有空格;user:后也一定要有一空格,结尾不加冒号。
4、saltstack对空格要求极为严格,少些多写都会报错。
转载至https://blog.csdn.net/mr_rsq/article/details/79486680