Saltstack 安裝應用筆記一

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

wKioL1gRr-yhuRZvAAAeHjrDSSA788.png


二、提示問題:

# 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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章