zoukankan      html  css  js  c++  java
  • 1.saltstack基础笔记

    环境:

    master:

    节点node1:阿里云:121.42.195.15 centos6.6

    minion:

    节点node2:腾讯云:182.254.157.19 centos6.6

    一、saltstack快速配置

    1. saltstack基本介绍
      SaltStack是一个服务器基础架构集中化管理平台,具备配置管理、远程执行、监控等功能,一般可以理解为简化版的puppet和加强版的func。SaltStack基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)构建。通过部署SaltStack环境,我们可以在成千上万台服务器上做到批量执行命令,根据不同业务特性进行配置集中化管理、分发文件、采集服务器数据、操作系统基础及软件包管理等,SaltStack是运维人员提高工作效率、规范业务配置与操作的利器。

    2. saltstack相关网站
      https://github.com/saltstack/
      http://docs.saltstack.com/en/latest/ref/modules/all/index.html
      http://docs.saltstack.com/en/latest/ref/modules/index.html

    3. saltstack三种运行方式

      1. 本地
      2. 客户端(奴才)运行
      3. saltstackSSH
    4. 运行的操作系统

    5. 特点
      (1)、部署简单、方便;
      (2)、支持大部分UNIX/Linux及Windows环境;
      (3)、主从集中化管理;
      (4)、配置简单、功能强大、扩展性强;
      (5)、主控端(master)和被控端(minion)基于证书认证,安全可靠;
      (6)、支持API及自定义模块,可通过Python轻松扩展。

    安装:

    1.master安装

    节点:node1

    #yum install salt-master -y
    
    2.minion安装
    #yum install salt-minion -y
    
    3.启动:
     [root@node1 ~]#/etc/init.d/salt-master start
    
    4.配置文件
    [root@node1 ~]# ll /etc/salt/
    total 36
    -rw-r----- 1 root root 29543 Dec  2 05:06 master
    drwxr-xr-x 3 root root  4096 Mar 13 17:20 pki
    

    我是华丽的分隔符

    [root@node2 ~]# ll /etc/salt/
    total 28
    -rw-r----- 1 root root 26229 Dec  2 05:06 minion
    
    5.minion修改配置文件连接到master
    [root@node2 ~]# vim /etc/salt/minion
    修改16行:ip地址为master的节点ip
    16 master: 121.42.195.15
    修改74行minion主机名,方便master管理
    74 id: node2.minion
    重启
    /etc/init.d/salt-minion restart
    

    master端查看要链接的minion

    [root@node1 ~]# salt-key
    Accepted Keys:
    Denied Keys:
    Unaccepted Keys:
    centos66 #没修改之前的minion主机名
    node2.minion #修改之后的minion主机名
    Rejected Keys:
    

    开始链接

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

    查看已经认证的minion列表:

    [root@node1 ~]# salt-key
    Accepted Keys: #已经认证的minion主机名
    node2.minion
    Denied Keys:
    Unaccepted Keys: #等待认证的主机名
    centos66
    Rejected Keys:
    

    查看链接是否正常

    [root@node1 ~]# salt '*' test.ping
    node2.minion:
    True
    *号代表所有主机
    或:
    [root@node1 ~]# salt 'node2.minion' test.ping
    node2.minion:
    True
    

    防火墙:

    iptables -I INPUT -m state --state new -m tcp -p tcp --dport 4505 -j ACCEPT
    iptables -I INPUT -m state --state new -m tcp -p tcp --dport 4506 -j ACCEPT
    

    6.配置管理##

    1.配置安装模板
    修改master配置文件

    #引入配置文件
    12 default_include: master.d/*.conf
    15 interface: 0.0.0.0
    saltstack状态文件
    406 file_roots:
    407   base:   #开头两个空格   #基础环境
    408     - /etc/salt/states  #开头四个空格 #基础环境目录
    409   prod:   #生产环境
    410     - /etc/salt/states/prod  #生产环境目录
    329 state_top: top.sls #配置文件名 在base下的目录下 默认
    保存退出
    创建模板所在目录
    [root@node1 ~]# mkdir /etc/salt/states/prod -p
    重启master
    [root@node1 ~]# /etc/init.d/salt-master restart
    Stopping salt-master daemon:                               [  OK  ]
    Starting salt-master daemon:                               [  OK  ]
    

    日志文件:

    [root@node1 ~]# tail /var/log/salt/master 
    

    创建配置文件

    [root@node1 ~]# cd /etc/salt/states/
    [root@node1 states]# vim top.sls
    

    内容如下

    base:             #基本配置
     'node2.minion':  #对这个主机进行操作
       - init.pkg      #使用init目录下的pkg配置文件
    保存退出
    

    创建pkg配置文件

    [root@node1 states]# mkdir init
    [root@node1 states]# cd init/
    [root@node1 init]# vim pkg.sls
    

    写入如下内容(空格必须要正确 否则会失败)

    pkg.init:  #配置文件名
      pkg.installed: #进行安装操作(2空格)
        - names:                #(4空格)
          - lrzsz     #软件包名  
          - htop      #软件包名
          - nmap      #软件包名
    保存退出
    

    总结:
    定义base基础环境和prod生成环境目录
    开启top.sls
    salt工具会根据你定义的基础环境目录下的top.sls进行操作

    使用此模板进行操作:

    [root@node1 init]# salt '*' state.sls init.pkg
    #state是模块 sls是state模块的方法 init.pkg是自定义的模板
    

    运行结果:

    node2.minion:
    ----------
              ID: pkg.init
    Function: pkg.installed
        Name: nmap
      Result: True
     Comment: The following packages were installed/updated: nmap
     Started: 22:01:31.299862
    Duration: 22179.522 ms
     Changes:   
              ----------
              nmap:
                  ----------
                  new:
                      5.51-4.el6
                  old:
    ----------
          ID: pkg.init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed.
     Started: 22:01:53.485854
    Duration: 0.489 ms
     Changes:   
    Summary
    ------------
    Succeeded: 2 (changed=1)
    Failed:    0
    ------------
    Total states run:     2
    

    方法案例2:管理文件
    使用salt管理 /etc/security/limits.conf

    [root@node1 salt]# cd /etc/salt/states/init/
    [root@node1 init]# vim limit.sls
    

    内容如下:

    limit.conf.config:
      file.managed:
       - name: /etc/security/limits.conf
       - source: salt://init/files/limits.conf
       - user: root
       - group: root
       - mode: 644
    保存退出
    

    配置所需目录和文件

    [root@node1 init]# mkdir files
    [root@node1 init]# cp /etc/security/limits.conf .files/
    

    在top.sls里增加

    base:
     'node2.minion':
       - init.pkg
       - init.limit
    

    执行命令

    [root@node1 init]# salt '*' state.highstate
    

    结果:

    node2.minion:
    ----------
          ID: pkg.init
    Function: pkg.installed
        Name: nmap
      Result: True
     Comment: Package nmap is already installed.
     Started: 22:51:09.699088
    Duration: 568.249 ms
     Changes:   
    ----------
          ID: pkg.init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed.
     Started: 22:51:10.267485
    Duration: 0.409 ms
     Changes:   
    ----------
          ID: limit..conf.config
    Function: file.managed
        Name: /etc/security/limits.conf
      Result: True
     Comment: File /etc/security/limits.conf updated
     Started: 22:51:10.269850
    Duration: 138.081 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -6,7 +6,7 @@
                   #
                   #Where:
                   #<domain> can be:
                  -#        - a user name
                  +#        - an user name
                   #        - a group name, with @group syntax
                   #        - the wildcard *, for default entry
                   #        - the wildcard %, can be also used with %group syntax,
                  @@ -21,7 +21,7 @@
                   #        - data - max data size (KB)
                   #        - fsize - maximum filesize (KB)
                   #        - memlock - max locked-in-memory address space (KB)
                  -#        - nofile - max number of open file descriptors
                  +#        - nofile - max number of open files
                   #        - rss - max resident set size (KB)
                   #        - stack - max stack size (KB)
                   #        - cpu - max CPU time (MIN)
                  @@ -48,11 +48,5 @@
                   #@student        -       maxlogins       4
                   
                   # End of file
                  -@users soft nofile 100001
                  -@users hard nofile 100002
                  -@root  soft nofile 100001
                  -@root  hard nofile 100002
                  -* soft nproc 65535
                  -* hard nproc 65535
                   * soft nofile 65535
                   * hard nofile 65535
    
    Summary
     -----------
    Succeeded: 3 (changed=1)
    Failed:    0
    ------------
    Total states run:     3
    

    7.master和minion认证过程
    (1)、minion在第一次启动时,会在/etc/salt/pki/minion/(该路径在/etc/salt/minion里面设置)下自动生成minion.pem(private key)和 minion.pub(public key),然后将 minion.pub发送给master。
    (2)、master在接收到minion的public key后,通过salt-key命令accept minion public key,这样在master的/etc/salt/pki/master/minions下的将会存放以minion id命名的 public key,然后master就能对minion发送指令了。
    8.Master与Minion的连接
    (1)、SaltStack master启动后默认监听4505和4506两个端口。4505(publish_port)为saltstack的消息发布系统,4506(ret_port)为saltstack客户端与服务端通信的端口。如果使用lsof 查看4505端口,会发现所有的minion在4505端口持续保持在ESTABLISHED状态。

    9.基本使用

    查看当前的salt key信息

    salt-key
    

    当/etc/salt/master没有配置auto_accept:True时,
    需要通过salt-key命令来进行证书认证操作,具体操作如下:

    1. salt-key–L,显示已经或未认证的被控端id,
    2. salt-key–D,删除所有认证主机id证书
    3. salt-key-d id,删除单个id证书;
    4. salt-key–A,接受所有id证书请求;
    5. salt-key-a id,接受单个id证书请求。

    测试被控主机的连通性

    salt '*' test.ping
    

    远程命令执行测试
    cmd.run 这个命令可以执行所有linux下的命令
    例:

    [root@node1 ~]# salt '*' cmd.run 'df -h'
    node2.minion:
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1       7.9G  3.5G  4.1G  46% /
    

    注:这个不建议使用。因为功能过大。可以通过删除这个模块或者acl策略

    **显示被控主机的操作系统类型++

    salt '*' grains.item os
    

    远程代码执行测试

    salt '*' cmd.exec_code python 'import sys; print sys.version'
    结果:
    node2.minion:
        2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
        [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
    

    10.常用模块介绍
    下一章

  • 相关阅读:
    Tomcat学习笔记(九)
    在Windows上编译最新的CURL,含有zlib,openssl
    【转】测试LibreOffice SDK 开发环境配置(Windows)
    [转]LibreOffice-SDK 开发实战:嵌入MFC-View 和 C# Winform
    C++内存分配及变长数组的动态分配
    C++ 取得系统当前时间
    C# Winform程序获取外网IP地址
    【转】让Chrome化身成为摸鱼神器,利用Chorme运行布卡漫画以及其他安卓APK应用教程
    tesseract-ocr 出现 错误 Please make sure the TESSDATA_PREFIX environment variable is set to the parent d irectory of your "tessdata" directory.解决方案
    使用adb 查询data/data下的数据库
  • 原文地址:https://www.cnblogs.com/liu-yao/p/5523381.html
Copyright © 2011-2022 走看看