Saltstack 安装应用

    master 10.10.10.96

    minion 10.10.10.97

一、系统准备

[root@master ~]# uname -r

2.6.32-504.el6.x86_64

# salt --version

salt 2016.3.3 (Boron)


1、设置关闭 selinux

[root@master ~]# cat /etc/sysconfig/selinux |grep -v ^#

    SELINUX=disabled

    SELINUXTYPE=targeted 


2、设置iptables

# iptables -A INPUT -p tcp --dport 4505 -j ACCEPT

# iptables -A INPUT -p tcp --dport 4506 -j ACCEPT

# /etc/init.d/iptables save


3、安装 gcc c++

# yum install gcc gcc-c++ -y 


4、配置yum 源

# cat /etc/yum.repos.d/saltstack.repo

    [saltstack]

    name=saltstack

    baseurl=https://repo.saltstack.com/yum/rhel6/

    enabled=1

    gpgcheck=0


# rhel7 可以以此更改URL 安装源;


5、安装配置

master:

# yum install salt-master salt-minion salt-ssh salt-syndic salt-cloud -y


6、配置master

# mkdir /srv/{salt,pillar}

# cat /etc/salt/master

    interface: 10.10.10.96

    # 绑定master通信IP

    publish_port: 4505

    master_id: master

    auto_accept: True 

    # 打开key的自动验证

    pidfile: /var/run/salt-master.pid

    pki_dir: /etc/salt/pki

    file_roots:

      base:

        - /srv/salt

    # 指定saltstack文件根目录位置


    pillar_roots:

      base:

        - /srv/pillar

    # 指定pillar的配置目录

#  

当/etc/salt/master没有配置auto_accept: True时,需要通过salt-key命令来进行证书认证操作

salt-key -L 显示已认证或未认证的被控端id

salt-key -D 删除所有认证主机id证书

salt-key -d id 删除单个id证书

salt-key -A 接受所有id证书

salt-key -a id 接受单个id证书

salt-key -j id 拒绝单个id证书

salt-key –J 拒绝所有id证书

#

7、启动

# service salt-master start

# chkconfig salt-master on


8、minion 端:

# yum install salt-minion -y

    配置 minion

[root@client ~]# cat /etc/salt/minion

    master: 10.10.10.96

    id: client


9、启动

# service salt-minion start

# chkconfig salt-minion on


10、向客户端发送命令检测;

[root@master ~]# salt-key -L 

    # 查看minion 列表

[root@master ~]# salt 'client' test.ping

Saltstack 安装应用笔记一_安装测试


二、提示问题:

# salt '*' test.ping

    [ERROR] Salt request timed out. If this error persists, worker_threads may need to be increased.

Failed to authenticate!  This is most likely because this user is not permitted to execute commands, but there is a small possibility that a disk error occurred (check disk/inode usage).

Salt因为握手不成功报错[要求增加线程]解决办法?一般都是配置问题

解决方法:

检测配置文件 /etc/salt/master里面的worker_threads 参数

检测 IPtables和4506 4505 端口;


三、常用模块及API

通过sys模块列出当前版本支持的模块

#salt '*' sys.list_modules


1、archive模块:实现系统层面的压缩包调用

# salt ‘*’ archive.gzip /tmp/sourcefile.txt

# salt ‘*’ archive.gunzip /tmp/sourcefile.txt.gz


2、cmd模块:实现远程的命令行调用执行(默认具有root权限)

# salt ‘*’ cmd.run ‘free -m’

在远程主机运行test.sh脚本,其中script/test.sh存放在file_roots指定的目录,该命令会有两个动作,首先同步test.sh到minion的cache目录(/var/cache/salt/minion/files/base/script/),其次运行该脚本

# salt ‘nginx_update’ cmd.script salt://script/test.sh


3、cp模块:实现远程文件、目录的复制,以及下载URL文件等操作

将指定被控主机的/etc/hosts文件复制到被控主机本地的salt cache目录/var/cache/salt/minion/localfiles

# salt ‘*’ cp.cache_local_file /etc/hosts

将主服务器file_roots指定位置下的目录复制到被控主机

# salt ‘*’ cp.get_dir salt://path/to/dir /minion/dest

将主服务器file_roots指定位置下的文件复制到被控主机

# salt ‘*’ cp.get_file salt://path/to/file /minion/dest

下载URL内容到被控主机指定位置

# salt ‘*’ cp.get_url http://www.slashdot.org /tmp/index.html


 删除 文件

 salt '*' file.remove /tmp/test


4、cron模块:实现被控主机的crontab操作

查看指定被控主机root用户的crontab清单

# salt ‘nginx_update’ cron.raw_cron root

为指定的被控主机root用户添加/usr/local/weekly任务作业

# salt ‘nginx_update’ cron.set_job root ‘*’ ‘*’ ‘*’ ‘*’ 1 /usr/local/weekly

删除指定的被控主机root用户的crontab的/usr/local/weekly任务作业

# salt ‘nginx_update’ cron.rm_job root /usr/local/weekly


5、dnsutil模块:实现被控主机通用DNS相关操作

添加指定被控主机hosts的主机配置项

# salt ‘nginx_update’ dnsutil.hosts_append /etc/hosts 127.0.0.1 localhost.com

删除指定被控主机hosts的主机配置项

# salt ‘nginx_update’ dnsutil.hosts_remove /etc/hosts localhost.com


6、file模块:被控主机文件常见操作,包括文件读写,权限,查找,校验等

# salt ‘*’ file.chown /etc/passwd root root

# salt ‘*’ file.copy /path/to/src /path/to/dst

# salt '*' file.directory_exists /etc/

# salt '*' file.file_exists /etc/passwd

# salt '*' file.stats /etc/passwd

# salt '*' file.get_mode /etc/passwd

# salt '*' file.set_mode /etc/passwd 0644

# salt '*' file.mkdir /tmp/test

# salt ‘*’ file.sed /etc/httpd/httpd.conf ‘LogLevel warn’ ‘LogLevel info’

# salt '*' file.append /tmp/test/testfile 'maxclient 100'

# salt '*' file.remove /tmp/test/testfile


7、pkg包管理模块:被控主机程序包管理,如yum

# salt ‘*’ pkg.install php

# salt ‘*’ pkg.remove php

# salt ‘*’ pkg.upgrade


8、service服务模块:被控主机程序包服务管理

# salt ‘*’ service.enable nginx

# salt ‘*’ service.disable nginx

# salt ‘*’ service.reload nginx

# salt ‘*’ service.restart nginx

# salt ‘*’ service.start nginx

# salt ‘*’ service.stop nginx

# salt ‘*’ service.status nginx


9、iptables模块

# salt ‘*’ iptables.append filter INPUT rule=’-m state -state RELATED,ESTABLISHED -j ACCEPT’

# salt ‘*’ iptables.insert filter INPUT position=3 rule=’-m state -state RELATED,ESTABLISHED -j ACCEPT’

# salt ‘*’ iptables.delete filter INPUT position=3

# salt ‘*’ iptables.delete filter INPUT rule=’ -m state -state RELATED,ESTABLISHED -j ACCEPT’

# salt ‘*’ iptables.save /etcsysconfig/iptables


四、官方参考:

http://docs.saltstack.cn/topics/installation/rhel.html