Saltstack-部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Saltstack-部署相关的知识,希望对你有一定的参考价值。

saltstack部署过程

一.服务端:
Epel源安装

[[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

Salt-master安装
[[email protected] ~]# yum install salt-master -y
添加master启动服务
[[email protected] ~]# chkconfig salt-master on
修改master
[[email protected] ~]# vim /etc/salt/master
修改配置文件/etc/salt/master,打开以下6行的注释

416 file_roots:
417   base:
418     - /srv/salt
529 pillar_roots:
530   base:
531     - /srv/pillar

添加master启动
[[email protected] ~]# /etc/init.d/salt-master start

查看可被管理的主机

[[email protected] ~]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
Rejected Keys:

[[email protected] ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
Rejected Keys:

添加所有被管理端
[[email protected] ~]# salt-key –A

删除所有被管理端
[[email protected] ~]# salt-key –D

重新启动master服务
[[email protected] ~]# /etc/init.d/salt-master restart

Saltstack远程执行命令
Saltstack 目标 模块.方法 返回信息

[[email protected] ~]# salt "*" test.ping          #运行salt模块里的ping#
backup:
    True
web01:
    True
nfs01:
True

[[email protected] ~]# salt "*" sys.doc ‘test‘ #查看test模块的使用帮助#

查看所有被管理端的磁盘使用率
[[email protected] ~]# salt "*" cmd.run "df -h"

远程执行命令查看客户端的内存使用情况
[[email protected] ~]# salt "*" cmd.run "free -h"

远程系统cpu负载情况
[[email protected] ~]# salt "*" cmd.run ‘uptime‘

查看被控端操作系统类型

[[email protected] ~]# salt "*" cmd.run "cat /etc/redhat-release"

[[email protected] ~]# salt ‘*‘ grains.item os
backup:
    ----------
    os:
        CentOS
nfs01:
    ----------
    os:
        CentOS
web01:
    ----------
    os:
        CentOS

Saltstack的配置管理
搭建目录

[[email protected] ~]# mkdir /srv/{salt,pillar}

[[email protected] salt]# vim host_file.sls
/etc/hosts:
  file.managed:
    - source: salt://files/hosts
    - user: root
    - group: root
    - mode: 644
~                 

[[email protected] salt]# mkdir files
[[email protected] salt]# cd files/
[[email protected] files]# cp /etc/hosts .
[[email protected] files]# ll
总用量 4
-rw-r--r-- 1 root root 333 11月 15 23:38 hosts
[[email protected] files]# pwd
/srv/salt/files
null

执行salt脚本 host_file.sls
[[email protected] files]# salt ‘*‘ state.sls host_file

[[email protected] 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

查看定时任务
[[email protected] salt]# salt ‘*‘ cron.list_tab root

创建定时任务
[[email protected] salt]# vim crontab.sls
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
  cron.present:
      user: root
      minute: ‘*/5‘

[[email protected] salt]# salt ‘*‘ state.sls crontab

[[email protected] salt]# vim crontab.sls           
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
  cron.present:
      user: root
      minute: ‘*/3‘

[[email protected] salt]# salt ‘*‘ state.sls crontab

删除定时任务
[[email protected] salt]# vim del_cron.sls
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
  cron.absent:
- name: /usr/sbin/ntpdate times.aliyun.com >>/dev/null

[[email protected] salt]# salt ‘*‘ state.sls ‘del_cron‘

[[email protected] salt]# salt ‘*‘ state.highstate


二.客户端:

Epel源安装 
[[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

Minion安装
[[email protected] ~]# yum install salt-minion –y

查看salt-minion版本
[[email protected] ~]# rpm -aq salt-minion
salt-minion-2015.5.10-2.el6.noarch

编辑minion
[[email protected] ~]# vim /etc/salt/minion
修改配置文件/etc/salt/minion,修改以下内容
16 master: 172.16.1.61    <------这个ip地址是master的ip地址(或者写主机名也行)
78 id: web01    <----------这个id是给本机起的一个名字,尽量起的有意义

设置salt-minion开机启动
[[email protected] ~]# chkconfig salt-minion on

启动salt-minion
[[email protected] ~]# /etc/init.d/salt-minion start

查看密钥
[[email protected] ~]# ll /etc/salt/pki/minion/       
总用量 12
-r-------- 1 root root 1675 11月 15 21:51 minion.pem
-rw-r--r-- 1 root root  451 11月 15 21:51 minion.pub

重启salt-minion
[[email protected] ~]# /etc/init.d/salt-minion restart

以上是关于Saltstack-部署的主要内容,如果未能解决你的问题,请参考以下文章

saltstack详解+部署apache服务

SaltStack部署及使用实践

saltstack安装部署以及简单实用

SaltStack部署

SaltStack部署

saltstack 安装部署