在服务端master机器执行(m01机器)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #下载新repo 源到/etc/yum.repos.d/
然后:
[iyunv@m01 yum.repos.d]# cd /etc/yum.repos.d/
yum install salt-master -y #安装主程序
[iyunv@m01 yum.repos.d]# chkconfig salt-master on #设置开机启动
[iyunv@m01 yum.repos.d]# vim /etc/salt/master
在416,417,418,529,530,531行中的#注释去掉,保存退出。
416 #file_roots:
417 # base:
418 # - /srv/salt
529 #pillar_roots:
530 # base:
531 # - /srv/pillar
修改为:
416 file_roots:
417 base:
418 - /srv/salt
529 pillar_roots:
530 base:
531 - /srv/pillar
执行如下命令启动salt-master主程序
[iyunv@m01 yum.repos.d]# /etc/init.d/salt-master start
在客户端机器上面(nfs机器):
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ##下载新repo 源到/etc/yum.repos.d/
yum install salt-minion -y #安装从程序
[iyunv@nfs01 ~]# chkconfig salt-minion on #设置开机启动
[iyunv@nfs01 ~]# vim /etc/salt/minion #修改16行把#注释取消,把salt修改为master的ip(不要改为主机名,因为要做解析,麻烦) 78行注释取消,然后后面添加为当前主机名,方便后期管理。
16 #master: salt
78 #id:
修改为如下:
16 master: 172.16.1.61
78 id: nfs01
然后再开启salt-minion服务
[iyunv@nfs01 ~]# /etc/init.d/salt-minion start
在服务端m01机器上面查看是否监控了nfs客户端的机器
[iyunv@m01 yum.repos.d]#salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
Rejected Keys:
上面查看没有nfs客户端的机器名说明没有监控到。检查客户端的salt-minion配置文件ip有没有配置错误。然后在客户端restart salt服务,或者客户端restart salt服务。
[iyunv@m01 ~]# /etc/init.d/salt-master restart
或者
[iyunv@nfs01 ~]# /etc/init.d/salt-minion restart
再到salt服务端输入salt-key就可以看到监控到的客户端机器id:nfs01了
[iyunv@m01 ~]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
nfs01
Rejected Keys:
上面出现客户id为nfs01说明已经安装好了。
在salt服务端执行:salt-key -A #-A意思是管理所有salt客户端,如果是a,代表只管理某个salt客户端,生产环境都是A。
[iyunv@m01 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
nfs01
Proceed? [n/Y] y
Key for minion nfs01 accepted.
管理成功。
salt服务端命令:
1)检查主机是否存活
[iyunv@m01 ~]# salt "*" test.ping #*代表所有主机,test是模块,ping是命令
nfs01:
True
2)查看salt客户端挂载磁盘
[iyunv@m01 ~]# salt "*" cmd.run 'df -hT'
nfs01:
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 ext4 18G 1.6G 15G 10% /
tmpfs tmpfs 931M 12K 931M 1% /dev/shm
/dev/sda1 ext4 190M 38M 142M 22% /boot
3)让salt客户端返回hehe
[iyunv@m01 ~]# salt "*" cmd.run 'echo "hehe"'
nfs01:
hehe
下发1:从salt服务端下发/etc/hosts到客户端的/etc/中
m01机器上面新建两个文件
[iyunv@m01 ~]# mkdir /srv/{salt,pillar}
[iyunv@m01 ~]# cd /srv/salt/
[iyunv@m01 salt]# vim host_file.sls
vim host_file.sls输入如下内容:
[iyunv@m01 salt]# cat host_file.sls
/etc/hosts:
file.managed:
- source: salt://files/hosts
- user: root
- group: root
- mode: 644
[iyunv@m01 salt]# pwd
/srv/salt
[iyunv@m01 salt]# mkdir files
[iyunv@m01 salt]# cd files
[iyunv@m01 files]# cp /etc/hosts
[iyunv@m01 files]# pwd
/srv/salt/files
[iyunv@m01 files]# ll
total 4
-rw-r--r-- 1 root root 349 Aug 17 21:09 hosts
[iyunv@m01 salt]# ls
files host_file.sls
[iyunv@m01 salt]# pwd
/srv/salt
[iyunv@m01 salt]# ls
files host_file.sls
[iyunv@m01 salt]# salt '*' state.sls host_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
nfs01:
----------
ID: /etc/hosts
Function: file.managed
Result: True
Comment: File /etc/hosts updated
Started: 21:19:23.167355
Duration: 95.798 ms
Changes:
----------
diff:
---
+++
@@ -8,4 +8,3 @@
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.61 m01
-#####21170815#################
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run:
salt服务端向客户端下发/etc/hosts文件,去salt客户端查看/etc/hosts文件是不是和salt服务端一样,如果一样说明下发成功。
[iyunv@m01 salt]# salt-cp '*' /etc/hosts /etc/
{'nfs01': {'/etc/hosts': True}}
下发2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@m01 salt]# pwd
/srv/salt
[iyunv@m01 salt]# vim nginx_install.sls
nginx-install:
pkg.installed:
- name:
- nginx
/etc/hosts:
file.managed:
- source: salt://files/hosts
- user: root
- group: root
- mode: 644
- require:
- pkg: nginx-install
service.running:
- names:
- nginx
下发3:查看salt客户端所有机器root用户下面的计划任务
1
2
3
4
5
6
7
8
9
[iyunv@m01 salt]# salt '*' cron.list_tab root
nfs01:
----------
crons:
env:
pre:
- #time sync by oldboy at 2010-2-1
- */5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
special:
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
[iyunv@m01 salt]# pwd
/srv/salt
[iyunv@m01 salt]# ls
crontab.sls files host_file.sls nginx_install.sls
[iyunv@m01 salt]# vim crontab.sls
[iyunv@m01 salt]# cat crontab.sls
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
cron.present:
- user: root
- minute: '*/5'
#下发定时任务到salt客户端
[iyunv@m01 salt]# salt '*' state.sls crontab
nfs01:
----------
ID: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
Function: cron.present
Result: True
Comment: Cron /usr/sbin/ntpdate times.aliyun.com >>/dev/null already present
Started: 22:04:33.979863
Duration: 7.671 ms
Changes:
Summary
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
到salt客户端查看下发的定时任务是否生效:
1
2
3
4
5
6
[iyunv@nfs01 ~]# crontab -l
#time sync by oldboy at 2010-2-1
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
# 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
从salt客户端查看阿里云的定时任务已生效。
下发4:删除阿里云时间同步计划任务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[iyunv@m01 salt]# vim del_cron.sls
[iyunv@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
#如下是执行salt服务端向客户端删除阿里云时间同步脚本。
[iyunv@m01 salt]# salt '*' state.sls 'del_cron'
nfs01:
----------
ID: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
Function: cron.absent
Result: True
Comment: Cron /usr/sbin/ntpdate times.aliyun.com >>/dev/null already absent
Started: 22:30:17.293428
Duration: 14.29 ms
Changes:
Summary
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
上面可以看到salt客户端删除阿里云时间同步成功。
到salt客户端查看阿里云时间同步计划已被salt服务端取消了,如下所示
[iyunv@nfs01 ~]# crontab -l
#time sync by oldboy at 2010-2-1
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
# Lines below here are managed by Salt, do not edit
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com