zoukankan      html  css  js  c++  java
  • saltstack

    一、安装&&配置Saltstack二、配置认证三、Saltstack的几种模块介绍

    一、安装&&配置Saltstack

    Saltstack的优势:

    有master端和minion端,执行的信息比较稳定,不容易丢失信息,或者出现失联主机的情况

    有封装好的http-api,我们可以直接启动salt-api就可以通过http协议进行调用。不需要自己进行第二次的封装。

    IP 地址    作用
    192.168.161.128    Server
    192.168.161.129    Client
    设置hostname以及hosts
    vim /etc/hosts
    //增加如下:

    192.168.161.128 python
    192.168.161.129 python2

    当然你也许会有疑问,如果我们管理的是成千上万台机器,如何操作?
    首先我们需要配置DNS把ip绑定在DNS上面,写个shell脚本分发到各个客户端即可!
    两台机器全部安装saltstack yum源

    yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
    128上执行
    yum install -y salt-master salt-minion
    129上执行
    yum install -y salt-minion
    3.2 配置启动选项
    分别在两台测试记上编辑:

    vim /etc/salt/minion
    //python修改为:
    master: python

    //python2修改为:
    master: python

    注意如上的python2 client上面一定要配置master的 salt!!!

    启动服务:

    //python上启动:
    # systemctl start salt-master; systemctl start salt-minion

    //python2上启动:
    # systemctl start salt-minion

    # ps aux | grep salt

    服务端监听4505和4506两个端口,4505为消息发布的端口,4506为和客户端通信的端口。

    二、配置认证

    master端和minion端通信需要建立一个安全通道,传输过程需要加密,所以得配置认证,也是通过密钥对来加密解密的。

    [root@python ~]# ls /etc/salt/pki/master/
    master.pem  master.pub  minions  minions_autosign  minions_denied  minions_pre  minions_rejected

    [root@python ~]# ls /etc/salt/pki/minion/
    minion.pem  minion.pub

    //其中minion.pem是私钥,minion.pub是公钥
    说明:

    • a 后面跟主机名,认证指定主机
    • A 认证所有主机
    • r 跟主机名,拒绝指定主机
    • R 拒绝所有主机
    • d 跟主机名,删除指定主机认证
    • D 删除全部主机认证
    • y 省略掉交互,相当于直接按了y

    2.1 认证一台client

    [root@python ~]# salt-key -a python2
    The following keys are going to be accepted:
    Unaccepted Keys:
    python2
    Proceed? [n/Y] y
    Key for minion python2 accepted.

    查看当前key状态:

    [root@python ~]# salt-key 
    Accepted Keys:
    python2
    Denied Keys:
    Unaccepted Keys:
    Rejected Keys:

    [root@python minions]# ls /etc/salt/pki/master/minions
    python  python2

    当然如果你认为不通过允许就可以直接加入:

    vi /etc/salt/master

    修改auto_accept 自动接收minion的key:

    auto_accept: Ture

    把本机也允许一下:

    [root@python minions]# salt-key -A

    [root@python minions]# salt-key
    Accepted Keys:
    python
    python2
    Denied Keys:
    Unaccepted Keys:
    Rejected Keys:

    模拟场景:
    删除所有的认证client

    [root@python minions]# salt-key -D
    The following keys are going to be deleted:
    Accepted Keys:
    python
    python2
    Denied Keys:
    python2
    Proceed? [N/y] Y
    Key for minion python2 deleted.
    Key for minion python deleted.
    Key for minion python2 deleted.

    [root@python minions]# salt-key
    Accepted Keys:
    Denied Keys:
    Unaccepted Keys:
    Rejected Keys:

    然后再次去添加:(出错了。。)

    [root@python minions]# salt-key -A
    The key glob '*' does not match any unaccepted keys.

    解决方案:
    在全部的server和client上面重启salt-minion服务

    [root@python minions]# systemctl restart salt-minion

    再次check:

    [root@python minions]# salt-key
    Accepted Keys:
    python
    python2
    Denied Keys:
    Unaccepted Keys:
    Rejected Keys:

    手动删除一个client

    [root@python minions]# salt-key -d python2 -y
    The following keys are going to be deleted:
    Accepted Keys:
    python2
    Key for minion python2 deleted.

    三、Saltstack的几种模块介绍

    • Runner 模块
      在master端执行的 salt-run
      master压力会很大(如果机器比较多)
    • Module 模块
      通过master同步到minion端, 在minion执行
      salt-call saltutil.sync_modules
      salt-call saltutil.sync_all:包括:beacons:
      clouds: engines: grains: log_handlers: modules:
      output: proxymodules: renderers: returners: sdb:
      states: utils:
    • Grins 模块
      记录minion的属性key:value
    • Pillar模块
      记录所有minion通用的属性,然后同步到minion端
      salt-call saltutil.refresh_pillar
      salt ‘’ saltutil.refresh_pillar 查看客户端上的模块 [root@python etc]# salt '' saltutil.sync_all
      cmd模块
      salt ‘*’ cmd.run “df -h”
    • ping模块
      salt ‘*’ test.ping –t 5 ## -t 指定等待时间
    • cp 模块
      如需设置基础文件目录需要进入:
    [root@python etc]# vim /etc/salt/master

    修改如下路径:

    # file_roots:
    #   base:
    #     - /srv/salt/      ##默认位置
    #   dev:
    #     - /srv/salt/dev/services
    #     - /srv/salt/dev/states
    #   prod:
    #     - /srv/salt/prod/services
    #     - /srv/salt/prod/states
    file_roots: 
    base:
    - /export/salt/root

    salt根目录:在master中file_roots定义的路径。

    salt://test.txt相当于/srv/salt/root/test.txt
    copy一个文件到指定主机:

    [root@python salt]# salt "*" cp.get_file salt://1.txt /tmp/22.txt
    python2:
        /tmp/22.txt

    [root@python2 ~]# ls /tmp/
    22.txt  mysql.sock
    cron模块:
    salt '*' cron.raw_cron root     (查看定时任务)
    salt '*' cron.set_job root '*' '*' '*' '*' 1 /export/scripts/rm_log.sh 
    salt '*' cron.rm_job root /export/scripts/rm_log.sh   (写全没效果)
    dnsutil模块
    salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 xiang.com
    salt '*' dnsutil.hosts_remove /etc/hosts xiang.com
    file模块:
    salt '*' file.chown /etc/passwd root root
    salt '*' file.copy /path/to/src /path/to/dst
    salt '*' file.file_exists /etc/hosts
    salt '*' file.directory_exists /etc/
    salt '*' file.get_mod /etc/passwd
    salt '*' file.set_mod /etc/passwd 0644
    salt '*' file.mkdir /tmp/test
    salt '*' file.sed /export/servers/nginx/conf/nginx.conf 'debug' 'warn'
    salt '*' file.append /tmp/test.txt "welcome xiang"
    salt '*' file.remove /tmp/1.txt
    network模块:
    salt '*' network.dig www.qq.com
    salt '*' network.ping www.qq.com
    salt '*' network.ip_addrs
    pkg包管理模块:
    管理yum, apt-get等
    salt '*' pkg.install php
    salt '*' pkg.remove php
    salt '*' pkg.upgrade    (升级所有的软件包)

    自定义模块:
    1、首先在默认目录中,创建一个modules模块的目录:

    [root@python salt]# mkdir _modules

    2、进入 _modules 并编写 module文件:

    [root@python _modules]# vim hello.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    def hello():
            result = dict()
            result.update({"code"231})
            result.update({"messages""successful"})
            result.update({"Get""YES"})
            return result

    3、刷新模块

    [root@python _modules]# salt '*' saltutil.sync_modules
    python2:
        - modules.hello

    4、引用模块

    [root@python _modules]# salt "*" hello.hello
    python2:
        ----------
        Get:
            YES
        code:
            231
        messages:
            successful

    或者以json的格式输出:
    [root@python _modules]# salt "*" hello.hello --output json
    {
        "python2": {
            "code"231,
            "messages""successful",
            "Get""YES"
        }
    }

    假如所写的函数 需要传参:

    [root@python _modules]# cat hello.py
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    def hello(aaa):
        result = dict()
        result.update({"code"231})
        result.update({"messages"'successful'})
        result.update({"Get": aaa})
        return result

    1、更新

    [root@python _modules]# salt '*' saltutil.sync_modules
    python2:
        - modules.hello

    2、传入参数

    [root@python _modules]# salt "*" hello.hello zhdy --output json
    {
        "python2": {
            "code"231,
            "messages""successful",
            "Get""zhdy"
        }

    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    python变量和作用域
    模块
    装饰器
    转git取消commit
    RTP
    ffmpeg编译
    win7开启wifi
    LIVE555
    【FFmpeg】ffplay播放rtsp视频流花屏问题
    Windows下编译SDL
  • 原文地址:https://www.cnblogs.com/jiangshanwang/p/9236995.html
Copyright © 2011-2022 走看看