zoukankan      html  css  js  c++  java
  • Saltstack 安装与常用模块

    一、介绍

    saltstack是基于C/S服务模式,在该架构中,服务器端叫做Master,客户端叫做Minion。传统的C/S模式我们这样理解,客户端发送请求给服务器端,服务器端接受到来自客户端的请求并处理完成后再返回客户端。 在saltstack架构中,不仅有传统的C/S服务模式,而且有消息队列中的发布与订阅(pub/sub)服务模式。目前我们一般用其C/S架构做批量管理。

    1、Master:控制中心,salt命令运行和资源状态管理
    2、Minion : 需要管理的客户端机器,会主动去连接Mater端,并从Master端得到资源状态
    3、信息,同步资源管理信息
    4、States:配置管理的指令集
    5、Modules:在命令行中和配置文件中使用的指令模块,可以在命令行中运行
    6、Grains:minion端的变量,静态的
    7、Pillar:minion端的变量,动态的比较私密的变量,可以通过配置文件实现同步minions定义
    8、highstate:为minion端下发永久添加状态,从sls配置文件读取.即同步状态配置
    9、salt_schedule:会自动保持客户端配置

    二、安装

    #1、在master上安装master端
    yum -y install salt-master
    #配置salt-master
    [root@master ~]# egrep -v "^$|^#" /etc/salt/master
    publish_port: 4505 #master的管理端口
    user: root   # salt运行的用户,影响到salt的执行权限
    worker_threads: 10 #salt的运行线程,开的线程越多一般处理的速度越快,但一般不要超过CPU的个数
    ret_port: 4506 # master跟minion的通讯端口,用于文件服务,认证,接受返回结果等
    root_dir: / # saltstack 可以控制的文件系统的开始位置
    file_roots: # salt state执行时候的根目录
      base:
        - /etc/salt/file 
    pillar_roots: # 设置pillar 的根目录
      base:
       - /etc/salt/pillar 
    syndic_master_port: 4506 # 如果这个master运行的salt-syndic连接到了一个更高层级的master,那么这个参数需要配置成连接到的这个高层级master的监听端口
    syndic_pidfile: /var/run/salt-syndic.pid # 指定pid文件位置
    syndic_log_file: /var/log/salt_master.log  # 日志文件地址
    nodegroups:
      group_all: '*' # 分组设置
    
    
    #创建file和pillar目录
    [root@master ~]# mkdir  /etc/salt/pillar
    [root@master ~]# mkdir  /etc/salt/file
    #启动
    systemctl  start  salt-master.service
    
    2、在node上安装salt-minion
    yum -y install salt-minion
    #配置salt-minion
    [root@node ~]# egrep -v "^$|^#" /etc/salt/minion
    master: 172.31.17.148
    master_port: 4506  # master通讯端口
    user: root  # salt运行的用户权限
    id: 172.31.17.149 # minion的识别ID,可以是IP,域名,或是可以通过DNS解析的字符串
    output: nested   # 执行salt-call时候的输出方式
    backup_mode: minion # 备份模式,minion是本地备份,当进行文件管理时的文件备份模式
    acceptance_wait_time: 10  # minion等待master接受认证的时间
    acceptance_wait_time_max: 0 # 失败重连次数,0表示无限次,非零会不断尝试到设置值后停止尝试
    random_reauth_delay: 60 # 重新认证延迟时间,可以避免因为master的key改变导致minion需要重新认证的syn风暴
    file_roots:  # 文件路径基本位置
      base:
        - /etc/salt/min/file
    pillar_roots:  # pillar基本位置
      base:
        - /etc/salt/min/pillar
    log_file: /var/log/salt_minion.log # 日志文件位置
    
    #创建目录
    [root@node ~]# mkdir  -p /etc/salt/min/file
    [root@node ~]# mkdir  -p /etc/salt/min/pillar
    
    #启动
    systemctl  start salt-minion.service
    
    
    3、认证
    #查看认证信息
    [root@master ~]# salt-key 
    Accepted Keys:
    Denied Keys:
    Unaccepted Keys:
    172.31.17.149
    Rejected Keys:
    
    #接受172.31.17.149的认证
    [root@master ~]# salt-key  -y -a 172.31.17.149
    The following keys are going to be accepted:
    Unaccepted Keys:
    172.31.17.149
    Key for minion 172.31.17.149 accepted.
    
    #查看
    [root@master ~]# salt-key 
    Accepted Keys:
    172.31.17.149
    Denied Keys:
    Unaccepted Keys:
    Rejected Keys:
    
    #删除认证的客户端
    [root@master ~]# salt-key  -y -d 172.31.17.149
    Deleting the following keys:
    Accepted Keys:
    172.31.17.149
    Key for minion 172.31.17.149 deleted.
    #查看
    [root@master ~]# salt-key 
    Accepted Keys:
    Denied Keys:
    Unaccepted Keys:
    Rejected Keys:
    
    4、测试salt-minion是否连接
    #如果返回true则成功,如果无结果,则说明连接为失败,可以检查防火墙是否开放了4506,4505端口,或是重启salt-minion再试试,有时候master跟minion版本不同的问题,也会导致连接失败
    [root@master ~]# salt  '*' test.ping
    172.31.17.149:
        True
    172.31.17.148:
        True

    三、salt常用命令

    #salt常用命令
    salt '*' test.ping   #测试salt-minion是否连接
    salt-run manage.status   ##查看所有minion状态(up或者down)
    salt-run manage.down     ##查看所有没在线minion
    salt-run manged.up       ##查看所有在线minion
    #认证
    salt-key  参数
    -a    接受指定minion     
    -A    接受所有minion     
    -r    拒绝指定minion     
    -R    拒绝所有minion     
    -d    删除指定minion     
    -D    删除所有minion     
    -y    默认yes     
    
    #salt-cp 分发文件到minion上,不支持目录分发,通常在master运行
    例如:将master上/etc/hosts文件复制到所有节点的/tmp下
    [root@master file]# salt-cp '*'  /etc/hosts   /tmp/
    {'192.168.1.144': {'/tmp/hosts': True}, '192.168.1.145': {'/tmp/hosts': True}}
    ##复制过去并改名改成lala的文件名
    [root@master file]# salt-cp '*'  /etc/hosts   /tmp/lala
    {'192.168.1.144': {'/tmp/lala': True}, '192.168.1.145': {'/tmp/lala': True}}

    四、slatstack  选择器 

    1、通配符选择

    1、选择所有
    salt '*'  test.ping
    2、选择某一台
    salt '10.0.0.141'  test.ping
    3、选择以10开头的所有
    salt "10*"  test.ping
    5、选择以141结尾的所有
    salt "*141"  test.ping
    6、其它通过通配符选择
    salt "10.0.[0|1|2]*"  test.ping

    2、列表匹配方式

    #选择多台
    salt -L  "10.0.0.141,10.0.0.140"  test.ping

    3、正则表达式

    #匹配以10、20、30开头的
    salt -E  '(1|2|3)0*' test.ping

    4、IP匹配方式

    #匹配ip为10.0.0.0/24以内的
    salt  -S '10.0.0.0/24' test.ping
    #单个ip匹配
    salt  -S '10.0.0.141' test.ping

    5、分组匹配方式

    #在master配置文件中添加组
    nodegroups:
      web: '10.0.0.141,10.0.0.144'
      test: '10*'
      ip:  'S@10.0.0.0/24'
      aaa: 'E@(1|2|3)0*'
    #重启salt-master
    systemctl  restart salt-master.service
    
    #使用
    salt -N  ip test.ping

    五、常用模块详解与使用

    1、cmd 执行远程命令模块

    1、cmd.run   #最常用的(批量执行shell命令)
    #例如:
    [root@master ~]# salt '*' cmd.run  "ip a|awk NR==3|egrep -o [0-9.]+ "
    172.31.0.182:
        127.0.0.1
        8
    2、retcode  #在minion端执行一个shell命令并返回命令的返回码。0表示成功,0以外表示失败有问题(不能执行复杂一点的操作)
    例如:
    [root@master ~]# salt '*'  cmd.retcode  "ip a"
    172.31.0.182:
        0
    [root@master ~]# salt '*'  cmd.retcode  "ip a|awk NR==3|egrep -o [0-9.]+"
    172.31.0.182:
        1
    ERROR: Minions returned with non-zero exit code
    
    3、script (master上file_roots的目录) #从远程服务器下载脚本到并本地执行
    
    #环境
    [root@master ~]# chmod +x  /etc/salt/file/test.sh
    [root@master ~]# cat   /etc/salt/file/test.sh
    #!/bin/bash
    date  +%Y-%m-%d:%T >/tmp/test.txt
    
    例如:
    [root@master file]# salt '*'  cmd.script   salt://test.sh     
    192.168.1.144:
        ----------
        pid:
            8337
        retcode:
            0
        stderr:
        stdout:
    192.168.1.145:
        ----------
        pid:
            3671
        retcode:
            0
        stderr:
        stdout:
    [root@master file]# ll /tmp/test.txt 
    -rw-r--r-- 1 root root 20 Jan 17 15:05 /tmp/test.txt
    [root@master file]# cat  /tmp/test.txt 
    2019-01-17:15:05:02
    
    
    4、 cmd.which(查看命令的位置)
    测试:
    [root@master ~]# salt  '*' cmd.which  'cat'
    172.31.0.182:
        /usr/bin/cat
    [root@master ~]# salt  '*' cmd.which  'catd'
    172.31.0.182:
        None

    2、cp 复制模块

    #cp 模块是小型文件服务器操作的主页。 cp 模块由 Salt 状态系统 salt-cp 使用,可用于分发 Salt 文件服务器提供的文件。
     1、get_file (master上file_roots的目录) #可以从一个URL地址下载文件,URL可以是msater上的路径(salt://),也可以是http网址
     例如:
     [root@master file]# seq 10 > /etc/salt/file/test.txt
    [root@master file]# ll /etc/salt/file/test.txt
    -rw-r--r-- 1 root root 21 Jan 17 15:00 /etc/salt/file/test.txt
    [root@master file]# salt '*'   cp.get_file  salt://test.txt    /tmp/test.txt 
    192.168.1.144:
        /tmp/test.txt
    192.168.1.145:
        /tmp/test.txt
    #查看
    [root@master file]# ll /tmp/test.txt 
    -rw-r--r-- 1 root root 21 Jan 17 15:02 /tmp/test.txt
    
    2、get_dir #复制目录(空目录不会被复制)
    例如:
    #创建目录与文件
    [root@master aaa]# mkdir /etc/salt/file/aaa/{a,b,c} -p
    [root@master aaa]# seq 100 >/etc/salt/file/aaa/b/a.txt
    [root@master aaa]# salt '*'  cp.get_dir   salt://aaa    /mnt/
    192.168.1.144:
        - /mnt//aaa/b/a.txt
    192.168.1.145:
        - /mnt//aaa/b/a.txt
    #查看
    [root@node ~]# ll /mnt/aaa/
    total 4
    drwxr-xr-x 2 root root 4096 Jan 17 15:33 b
    
    3、get_url #用于从URL获取单个文件
    例子:把baidu的index下载下来并放入/mnt目录下改成baidu.index的文件
    [root@master aaa]# salt '*' cp.get_url  https://www.baidu.com/    /mnt/baidu.index
    192.168.1.144:
        /mnt/baidu.index
    192.168.1.145:
        /mnt/baidu.index
    
    4、cp.list_master和cp.list_master_dirs(查看salt master本地的file服务器有哪些文件或者目录)    
    例子:
    [root@master aaa]# salt '192.168.1.145' cp.list_master
    192.168.1.145:
        - aaa/b/a.txt
        - test.sh
        - test.txt
    [root@master aaa]# salt '192.168.1.145' cp.list_master_dirs
    192.168.1.145:
        - .
        - aaa
        - aaa/a
        - aaa/b
        - aaa/c
        - b

    3、cron 定时任务模块

    1、cron.raw_cron #查看定时任务列表
    例如:
    [root@master file]# salt '*' cron.raw_cron root
    192.168.1.144:
        * * * * * date >/tmp/date.log
    192.168.1.145:
        * * * * * date >/tmp/date.log
    cron.set_job  #添加定时任务
    例如:
    [root@master file]# salt '*' cron.set_job root '*' '*' '*' '*' '*' 'cat /etc/hosts >/tmp/hosts.log'  cat_hosts_cron
    192.168.1.144:
        new
    192.168.1.145:
        new
    #查看
    [root@node ~]# crontab  -l
    # cat_hosts_cron
    * * * * * cat /etc/hosts >/tmp/hosts.log
    
    2、cron.rm_job #删除定时任务
    例如:删除刚才添加的定时任务
    [root@master file]# salt '*'   cron.rm_job root    cat_hosts_cron
    192.168.1.144:
        absent
    192.168.1.145:
        absent

    4、service 服务模块

    1、available #查看某个服务是否存在
    例子:
    [root@master aaa]# salt '*'  service.available sshd
    192.168.1.144:
        True
    192.168.1.145:
        True
    2、disable  #禁止某个服务开机自启
    例子:
    [root@master aaa]# salt '*'  service.disable  sshd
    192.168.1.144:
        True
    192.168.1.145:
        True
    3、enable #设置某个服务开机自启
    [root@master aaa]# salt '*'  service.enable  sshd
    192.168.1.144:
        True
    192.168.1.145:
        True
    4、get_all #查看所有服务项
    例子:
    [root@master aaa]# salt '*' service.get_all |head 
    192.168.1.144:
        - -.mount
        - CmsGoAgent
        - NetworkManager
        - NetworkManager-dispatcher
        - NetworkManager-wait-online
        - README
        - aegis
        - agentwatch
        - aliyun
        
    5、get_enabled #查看所有开机启动的服务
    例子:
    [root@master aaa]# salt '*' service.get_enabled |head 
    192.168.1.144:
        - CmsGoAgent
        - atd
        - auditd
        - autovt@
        - chronyd
        - cloud-config
        - cloud-final
        - cloud-init
        - cloud-init-local
    6、start #启动某服务
    7、stop #关闭某服务
    8、restart  #重启某服务
    9、reload  #重载某服务
    10、status #查看某服务状态

    5、Grains 资产管理模块

    1、ls 列出所有可打印的状态模块
    例如:
    [root@localhostmnt]#salt '*' grains.ls|head -3
    192.168.1.144:
        - SSDs
        - biosreleasedate
    2、items 打印所有的状态信息
    
    例如:
    [root@localhostmnt]#salt '*' grains.items|head  -5
    10.0.0.141:
        ----------
        SSDs:
        biosreleasedate:
            07/02/2015
    
    
    3、item 打印单个的状态信息
    例如:
    [root@localhostmnt]#salt '*' grains.item ip4_gw
    10.0.0.141:
        ----------
        ip4_gw:
            10.0.0.254

    6、pkg 安装模块

    1、install 安装
    2、remove 卸载
    3、latest_version 安装最新版本
    例子:
    [root@localhostmnt]#salt '*' pkg.install 'iperf'
    10.0.0.141:
        ----------
        iperf:
            ----------
            new:
                2.0.12-4.el7
            old:
    
    [root@localhostmnt]#salt '*' pkg.remove 'iperf'
    10.0.0.141:
        ----------
        iperf:
            ----------
            new:
            old:
                2.0.12-4.el7

    7、file 文件操作模块

    1、stats 查看文件状态信息(类似stat命令)
    例:salt '*' file.stats /etc/passwd
    2、touch  创建文件
    例:salt '*'  file.touch /mnt/aaaa
    3、symlink  创建软连接
    例:
    [root@localhostmnt]#salt '*'  file.symlink  /etc/hosts    /mnt/link_hosts
    10.0.0.141:
        True
    [root@localhostmnt]#ls -l /mnt/link_hosts 
    lrwxrwxrwx 1 root root 10 1月  18 09:34 /mnt/link_hosts -> /etc/host
    4、rename  改名
    例:salt '*'  file.rename /mnt/aaaa   /mnt/aaaab
    5、chown 修改所有者(如同linux的chown命令)
    例:
    [root@localhostmnt]ll  /mnt/aaaab
    -rw-r--r-- 1 root root 0 1月  18 09:33 /mnt/aaaab
    [root@localhostmnt]#salt '*' file.chown   /mnt/aaaab  www www
    10.0.0.141:
        None
    [root@localhostmnt]ll  /mnt/aaaabw
    -rw-r--r-- 1 www www 0 1月  18 09:33 /mnt/aaaab
  • 相关阅读:
    BZOJ3745 / SP22343 NORMA2
    Luogu P4169 [Violet]天使玩偶/SJY摆棋子
    Luogu P3170 [CQOI2015]标识设计 状态压缩,轮廓线,插头DP,动态规划
    CQOI2013 棋盘游戏
    HAOI2008 硬币购物
    九省联考2018 一双木棋
    Codeforces Round #560 Div. 3
    算法竞赛入门经典 写题笔记(第五章 图论算法与模型4)
    算法竞赛入门经典 写题笔记(第五章 图论算法与模型3)
    算法竞赛入门经典 写题笔记(第五章 图论算法与模型2)
  • 原文地址:https://www.cnblogs.com/zhangb8042/p/10286136.html
Copyright © 2011-2022 走看看