2332323 发表于 2017-8-22 08:54:33

saltstack 快速入门

三种模式
Local
Master/minion
Salt ssh


三大功能
远程执行
配置管理
云管理

配置系统环境

cat /etc/redhat-release
CentOS release 6.6 (Final)
setenforce 0
/etc/init.d/iptables stop
# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.129node1
192.168.10.128 node2
# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.129node1
192.168.10.128 node2

下载并配置slat-master 和salt-minion(Master/minion模式)
# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
# yum install -y salt-master salt-minion -y
# chkconfig salt-master on
# /etc/init.d/salt-master start

# vim /etc/salt/minion
master: 192.168.10.129# /etc/init.d/salt-minion start
# chkconfig salt-minion on

# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
# yum install -y salt-minion
# vim /etc/salt/minion
master: 192.168.10.129# /etc/init.d/salt-minion start

Master与Minion的连接
# salt-key -a node*
# salt-key -L
Accepted Keys:
node1
node2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
# salt '*' test.ping
node2:
    True
node1:
True
远程执行命令
# salt '*' cmd.run 'df -h'
node1:
    Filesystem            SizeUsed Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                        8.3G2.1G5.8G26% /
    tmpfs               242M   16K242M   1% /dev/shm
    /dev/sda1             477M   28M424M   7% /boot
node2:
    Filesystem            SizeUsed Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                        8.3G2.1G5.9G26% /
    tmpfs               242M   12K242M   1% /dev/shm
    /dev/sda1             477M   28M424M   7% /boot
# salt '*' cmd.run 'uptime'
node2:
   13:33:55 up 13:51,2 users,load average: 0.07, 0.02, 0.00
node1:
   05:35:41 up 13:51,2 users,load average: 0.00, 0.04, 0.10


配置管理(下载httpd并启动)
# vim /etc/salt/master (注意空格,必须要启动一个base才能配置管理)
file_roots:
   base:
   - /srv/salt
# mkdir /srv/salt
# /etc/init.d/salt-master restart
# cd /srv/salt/
# cat apache.sls(注意空格)
apache-install:
pkg.installed:
    - names:
      - httpd
      - httpd-devel

apache-service:
service.running:
    - name: httpd
    - enable: True
- reload: True
使用命令直接执行状态
# salt '*' state.sls apache(*代表所有salt-minion都执行)
# netstat -tunlp|grep httpd
tcp      0      0 :::80                     :::*                        LISTEN      9361/httpd
# netstat -tunlp|grep 80
tcp      0      0 :::80                     :::*                        LISTEN      23119/httpd         

使用top.sls指定minlion执行状态
# pwd
/srv/salt
# cat top.sls(base环境下指定node2要执行apache这个状态,top指定谁可以执行什么状态,一般所有机器要执行的状态放在base环境中)
base:
'node2' :仅仅node2执行了apache这个状态
    - apache

# salt 'node2' state.highstate

页: [1]
查看完整版本: saltstack 快速入门