saltstack1

saltstack三种运行模式:

  local本地、master/minion(类似于agent)、salt ssh

saltstack三大功能:

  远程执行、配置管理、云管理

saltstack安装:1、使用官方进行yum安装 2、自建yum源进行安装

node1:

wget https://repo.saltstack.com/yum/redhat/salt-repo-2017.7-1.el6.noarch.rpm
yum install salt-repo-2017.7-1.el6.noarch.rpm
yum clean expire-cache
yum install salt-master salt-minion

修改minion配置:/etc/salt/minion

master: 指向master节点ip

node2:

wget https://repo.saltstack.com/yum/redhat/salt-repo-2017.7-1.el6.noarch.rpm
yum install salt-repo-2017.7-1.el6.noarch.rpm
yum clean expire-cache
yum install salt-minion

修改minion配置:/etc/salt/minion

master: 指向master节点ip

2、由于yum安装下载官方rpm包比较慢,所以自己自建了yum源,从https://repo.saltstack.com/yum/redhat/6/x86_64/2017.7/此处将需要以来安装的rpm包下载下来

查看下配置文件:

[[email protected] ~]# egrep -v "^#|^$" /etc/salt/master
[[email protected] ~]# egrep -v "^#|^$" /etc/salt/minion
master: 192.168.44.134
[[email protected] ~]# egrep -v "^#|^$" /etc/salt/minion
master: 192.168.44.134

启动两节点上的服务:

[[email protected] ~]# /etc/init.d/salt-master start
Starting salt-master daemon:                               [确定]
[[email protected] ~]# /etc/init.d/salt-minion start
Starting salt-minion:root:node1 daemon: OK
[[email protected] ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1265/sshd
tcp        0      0 0.0.0.0:4505                0.0.0.0:*                   LISTEN      2797/python2.7
tcp        0      0 0.0.0.0:4506                0.0.0.0:*                   LISTEN      2803/python2.7   

[[email protected] ~]# /etc/init.d/salt-minion start
Starting salt-minion:root:node2 daemon: OK

显示当前未连接上master的minion节点:

[[email protected] master]# tree
.
├── master.pem
├── master.pub
├── minions
├── minions_autosign
├── minions_denied
├── minions_pre           还没有成为master的minion节点
│   ├── node1
│   └── node2
└── minions_rejected
[[email protected] master]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:         未同意的key有两个,node1和node2上的minion
node1
node2
Rejected Keys:

将未同意的minion进行加入到master来:

salt-key -A:同意所有未同意的minion节点

salt-key -a node*:匹配模式的同意

[[email protected] master]# salt-key -a node*
The following keys are going to be accepted:
Unaccepted Keys:
node1
node2
Proceed? [n/Y] Y
Key for minion node1 accepted.
Key for minion node2 accepted.
[[email protected] master]# tree
.
├── master.pem
├── master.pub
├── minions
│   ├── node1
│   └── node2
├── minions_autosign
├── minions_denied
├── minions_pre
└── minions_rejected

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

1、test.ping:类似于zabbix的agent的ping,test是一个模块,ping是模块中的方法

[[email protected] ~]# salt ‘*‘ test.ping
node2:
    True
node1:
    True

2、cmd.run:执行所有命令的模块

[[email protected] ~]# salt "*" cmd.run "uptime"
node1:
     12:04:22 up  2:32,  2 users,  load average: 0.00, 0.00, 0.00
node2:
     12:04:22 up 23:28,  1 user,  load average: 0.07, 0.02, 0.00

[[email protected] ~]# salt "*" cmd.run "df -h"
node2:
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/vg_node2-lv_root
                           16G  3.5G   12G  24% /
    tmpfs                 932M   12K  932M   1% /dev/shm
    /dev/sda1             485M   32M  429M   7% /boot
node1:
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/vg_node1-lv_root
                           16G  2.9G   12G  20% /
    tmpfs                 932M   28K  932M   1% /dev/shm
    /dev/sda1             485M   32M  429M   7% /boot

grains:salt的一个组件,存储在minion上

  信息收集:收集minion端的信息

  匹配某个minion端执行某个命令

  匹配top file执行某些命令

[[email protected] salt]# salt ‘node1‘ grains.ls          列出所有的grains信息
node1:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns

[[email protected] salt]# salt ‘node1‘ grains.items 列出详细的grains的key和value

显示单个item的值:

[[email protected] salt]# salt ‘node1‘ grains.item fqdn     获取某一个grains的item
node1:
    ----------
    fqdn:
        node1

或者使用get方法:

[[email protected] salt]# salt ‘node1‘ grains.get fqdn      获取某一个grains的值
node1:
    node1

显示minion端的操作系统:

[[email protected] salt]# salt ‘node1‘ grains.get os       获取某一个grains的值
node1:
    CentOS

在操作系统是CentOS上执行某命令:

[[email protected] salt]# salt -G os:CentOS cmd.run ‘w‘     -G:以grains类型作匹配
node1:
     13:04:59 up  3:32,  2 users,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              [email protected]   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.44.1     11:42    0.00s  0.54s  0.35s /usr/bin/python
    root     pts/1    192.168.44.1     11:49   28:30   0.04s  0.04s -bash
node2:
     13:04:59 up 1 day, 29 min,  1 user,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              [email protected]   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.44.1     11:42   28:36   0.08s  0.08s -bash
[[email protected] salt]# salt "*" grains.item roles          获取roles这个item的grains信息
node1:
    ----------
    roles:
        - webserver
        - memcache
node2:
    ----------
    roles:

grains存在于minion端的配置文件中,可以使用配置grains进行将minion打一个标签,然后通过使用grains进行匹配,执行某操作:

[[email protected] salt]# egrep -v "^$|^#" /etc/salt/minion
master: 192.168.44.134
grains:
  roles:
    - webserver
    - memcache
修改了配置文件,需要重启才能生效:
[[email protected] salt]# /etc/init.d/salt-minion restart
Stopping salt-minion:root:node1 daemon: OK
Starting salt-minion:root:node1 daemon: OK
执行操作:
[[email protected] salt]# salt -G "roles:memcache" cmd.run ‘echo "hello node1"‘
node1:
    hello node1
-G "roles:memcache":匹配刚刚在minion端配置文件中的roles定义

当然如果不想将grains配置写在minion的配置文件中,那么可以将grains配置单独写在/etc/salt/grains中:匹配某一个minion端执行某一个命令

[[email protected] salt]# ll /etc/salt/grains,该grains需要事先进行创建
比如:
[[email protected] salt]# cat /etc/salt/grains
roles: nginx

[[email protected] salt]# /etc/init.d/salt-minion restart
Stopping salt-minion:root:node1 daemon: OK
Starting salt-minion:root:node1 daemon: OK
[[email protected] salt]# salt -G "roles:nginx" cmd.run ‘echo "node1 nginx"‘
No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received

报错原因:/etc/salt/grains 改配置文件中的key为roles与minion中的key值roles一致,导致冲突了,修改该key就可以解决

[[email protected] salt]# cat /etc/salt/grains
web: nginx
[[email protected] salt]# /etc/init.d/salt-minion restart
Stopping salt-minion:root:node1 daemon: OK
Starting salt-minion:root:node1 daemon: OK
[[email protected] salt]# salt -G "web:nginx" cmd.run ‘echo "node1 nginx"‘
node1:
    node1 nginx

pillar:只有在master配置文件中才会用到设置pillar

默认pillar没有打开

[[email protected] salt]# salt "*" pillar.items
node1:
    ----------
node2:
    ----------

需要在master配置文件中开启:

[[email protected] ~]# egrep -v "^#|^$" /etc/salt/master
file_roots:
  base:
    - /srv/salt
pillar_opts: True
[[email protected] ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [确定]
Starting salt-master daemon:                               [确定]
[[email protected] ~]# salt "*" pillar.items

node2:
    ----------
    master:
        ----------
        __role:
            master
        allow_minion_key_revoke:

pillar支持环境,base环境或其他,pillar也有一个入口目录,入口文件top file,top file必须放在base环境下面,打开base环境设置:

[[email protected] ~]# egrep -v "^#|^$" /etc/salt/master
pillar_roots:
  base:
    - /srv/pillar
创建该文件:[[email protected] ~]# mkdir /srv/pillar
重启master服务:[[email protected] ~]# /etc/init.d/salt-master restart
[[email protected] pillar]# cat apache.sls
{% if grains[‘os‘] == ‘CentOS‘ %}
apache: httpd
{% elif grains[‘os‘] == ‘Debian‘ %}
apache: apache2
{% endif %}
[[email protected] pillar]# cat top.sls
base:
  ‘*‘:
    - apache
将pillar的True再次修改为False,重启master,查看上面编写的sls文件是否已经生效
[[email protected] pillar]# salt ‘*‘ pillar.items
node2:
    ----------
    apache:
        httpd
node1:
    ----------
    apache:
        httpd
设置完了pillar,需要进行刷新才能够使用:
[[email protected] pillar]# salt ‘*‘ saltutil.refresh_pillar
node2:
    True
node1:
    True
[[email protected] pillar]# salt -I ‘apache:httpd‘ test.ping 刷新了才能调用pillar的设置,-I表示使用pillar匹配
node2:
    True
node1:
    True

时间: 2024-08-29 05:26:55

saltstack1的相关文章

saltstack1 安装及基础操作

saltstack常用网址: 官方网站:http://www.saltstack.com 官方文档:http://docs.saltstack.com GitHub:http://github.com/saltstack 中国SaltStack用户组:http://www.saltstack.cn saltstack运行方式: local mater/minion salt ssh saltstack三个功能: 远程执行 配置管理 云管理 master端:10.20.23.144 minion端

自动化运维工具之SaltStack-1、SaltStack介绍及安装

1.SaltStack简介 官方网址:http://www.saltstack.com官方文档:http://docs.saltstack.comGitHub:https:github.com/saltstack SaltStack是一个服务器基础架构集中化管理平台,具备配置管理.远程执行.监控等功能,一般可以理解为简化版的puppet和加强版的func.SaltStack基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq.PyCrypto.Pyji

SaltStack 实践课程二 PHP+NGINX

1.查看file_roots file_roots: base: - /srv/salt/ dev: - /srv/salt/dev/ prod: - /srv/salt/prod/ 2.目录结构如下 [[email protected] prod]# pwd /srv/salt/prod [[email protected] prod]# tree . |-- nginx | `-- files | `-- nginx-1.9.15.tar.gz |-- pcre | |-- files |