残缺极品 发表于 2018-7-31 09:17:36

saltstack-sandshell

  在服务端master机器执行(m01机器)
  wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #下载新repo 源到/etc/yum.repos.d/
  然后:
  # cd /etc/yum.repos.d/
  yum install salt-master -y #安装主程序
  # chkconfig salt-master on #设置开机启动
  # 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主程序
  # /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 #安装从程序
  # chkconfig salt-minion on #设置开机启动
  # vim /etc/salt/minion #修改16行把#注释取消,把salt修改为master的ip(不要改为主机名,因为要做解析,麻烦) 78行注释取消,然后后面添加为当前主机名,方便后期管理。
  16 #master: salt
  78 #id:
  修改为如下:
  16 master: 172.16.1.61

  78>  然后再开启salt-minion服务
  # /etc/init.d/salt-minion start
  在服务端m01机器上面查看是否监控了nfs客户端的机器
  #salt-key
  Accepted Keys:
  Denied Keys:
  Unaccepted Keys:
  Rejected Keys:
  上面查看没有nfs客户端的机器名说明没有监控到。检查客户端的salt-minion配置文件ip有没有配置错误。然后在客户端restart salt服务,或者客户端restart salt服务。
  # /etc/init.d/salt-master restart
  或者
  # /etc/init.d/salt-minion restart
  再到salt服务端输入salt-key就可以看到监控到的客户端机器id:nfs01了
  # salt-key
  Accepted Keys:
  Denied Keys:
  Unaccepted Keys:
  nfs01
  Rejected Keys:
  上面出现客户id为nfs01说明已经安装好了。
  在salt服务端执行:salt-key -A   #-A意思是管理所有salt客户端,如果是a,代表只管理某个salt客户端,生产环境都是A。
  # salt-key -A
  The following keys are going to be accepted:
  Unaccepted Keys:
  nfs01
  Proceed? y
  Key for minion nfs01 accepted.
  管理成功。
  salt服务端命令:
  1)检查主机是否存活
  # salt "*" test.ping #*代表所有主机,test是模块,ping是命令
  nfs01:
  True
  2)查看salt客户端挂载磁盘
  # salt "*" cmd.run 'df -hT'
  nfs01:

  Filesystem   Type>  /dev/sda3      ext4    18G1.6G   15G10% /
  tmpfs          tmpfs931M   12K931M   1% /dev/shm
  /dev/sda1      ext4   190M   38M142M22% /boot
  3)让salt客户端返回hehe
  # salt "*" cmd.run 'echo "hehe"'
  nfs01:
  hehe
  下发1:从salt服务端下发/etc/hosts到客户端的/etc/中
  m01机器上面新建两个文件
  # mkdir /srv/{salt,pillar}
  # cd /srv/salt/
  # vim host_file.sls
  vim host_file.sls输入如下内容:
  # cat host_file.sls
  /etc/hosts:
  file.managed:
  - source: salt://files/hosts
  - user: root
  - group: root
  - mode: 644
  # pwd
  /srv/salt
  # mkdir files
  # cd files
  # cp /etc/hosts
  # pwd
  /srv/salt/files
  # ll
  total 4
  -rw-r--r-- 1 root root 349 Aug 17 21:09 hosts
  # ls
  fileshost_file.sls
  # pwd
  /srv/salt
  # ls
  fileshost_file.sls
  # salt '*' state.sls host_file
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服务端一样,如果一样说明下发成功。
  # salt-cp '*' /etc/hosts /etc/
  {'nfs01': {'/etc/hosts': True}}
  下发2:
# pwd  
/srv/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用户下面的计划任务
# 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:
# pwd  
/srv/salt
  
# ls
  
crontab.slsfileshost_file.slsnginx_install.sls
  
# vim crontab.sls
  
# cat crontab.sls
  
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
  
cron.present:
  
    - user: root
  
    - minute: '*/5'
  
#下发定时任务到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客户端查看下发的定时任务是否生效:
# 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:删除阿里云时间同步计划任务
# vim del_cron.sls  
# cat del_cron.sls
  
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
  
cron.absent:
  
    - name: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
  
#如下是执行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服务端取消了,如下所示
  # 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]
查看完整版本: saltstack-sandshell