官方手册

https://docs.saltstack.com/en/pdf/Salt-2019.2.1.pdf

快速入门

SALTSTACK是什么?

Salt是一种和以往不同的基础设施管理方法,它是建立在大规模系统高速通讯能力可以大幅提升的想法上。这种方法使得Salt成为一个强大的能够解决基础设施中许多特定问题的多任务系统。远程执行引擎是Salt的核心,它能够为多组系统创建高速、安全的双向通讯网络。基于这个通许系统,Salt提供了一个非常快速、灵活并且容易使用的配置管理系统,称之为“Salt States”。

The backbone of Salt is the remote execution engine, which creates a high-speed, secure and bi-directional communication net for groups of systems. On top of this communication system, Salt provides an extremely fast, flexible, and easy-to-use configuration management system called Salt States.

安装SALT

SaltStack has been made to be very easy to install and get started. The installation documents contain instructions for all supported platforms.

SALT入门

Salt functions on a master/minion topology. A master server acts as a central control bus for the clients, which are called minions. The minions connect back to the master.

设置SALT MASTER

运行Salt Master很容易,就是执行它!默认配置适用于大多数不同安装。Salt Master能够由Linux/Unix本地服务管理器控制。

On Systemd based platforms (newer Debian, OpenSuse, Fedora):

systemctl start salt-master

在基于Upstart的系统上(Ubuntu, Older Fedora/RHEL):

service salt-master start

On SysV Init systems (Gentoo, older Debian etc.):

/etc/init.d/salt-master start

另一种方式,Master可以直接在命令行启动:

salt-master -d

Salt Master也能够在前台以debug模式启动,这样会极大增加命令输出:

salt-master -l debug

Salt Master需要绑定系统上2个TCP端口,分别是4505和4506。更多这些端口更深入的关于防火墙信息,参见防火墙教程。here.

FINDING THE SALT MASTER

When a minion starts, by default it searches for a system that resolves to the salt hostname`` on the network. If found, the minion initiates the handshake and key authentication process with the Salt master. This means that the easiest configuration approach is to set internal DNS to resolve the name salt back to the Salt Master IP.

否则,需要编辑minion配置文件配置 master 选项指向Salt Master的DNS名或IP:

注解

默认配置文件路径位于/etc/salt下。大多数平台会遵守这个约定,但是像FreeBSD和Microsoft Windows这样的平台会将这个文件放在不同的路径。

/etc/salt/minion:

master: saltmaster.example.com

设置SALT MINION

注解

Salt Minion有无Salt Master时都可以运作。本演练将假定minion可以连接到master,想了解如何运行一个无master的minion的资料请参考master-less quick-start guide:

Masterless Minion 快速入门

现在已经能够找到master了,同master一样以相同方式启动minion;使用平台init系统或者直接通过命令行。

以daemon模式运行

salt-minion -d

在前台以debug模式运行

salt-minion -l debug

当minion启动后,它会产生一个 id 值,除非已经在之前的运行过程中产生过并且缓存在配置路径下,默认是 /etc/salt 。minion用这个值作为名称尝试去master进行验证。尝试下面几步操作,以便找到一个不是 localhost 的值:

  1. 运行Python函数"socket.getfqdn()"

  2. 核对"/etc/hostname"(仅针对非Windows系统)

  3. 核对"/etc/hosts"(在Windows主机上是"%WINDIR%system32driversetchosts") 上的包括"127.0.0.0/8"在内的所有主机名。

如果以上都不能产生除"localhost"以外的id,那么就会按顺序检测minion上的IP地址列表(排除"127.0.0.0/8"在内)。如果存在,就会使用第一个公网路由IP地址,否则就会使用第一个私网路由IP地址。

如果所有这些都失败了,那么就会使用"localhost"作为备选。

注解

覆盖"id"值

minion的id也可以通过minion配置文件中 :conf_minion:`id`选项手动指定。如果指定这个配置值,它会覆盖所有其他来源的"id"值。

现在minion已经运行了,它会产生秘钥对并且尝试连接master。下一步就是折回master服务器接受新minion的公钥。

使用SALT-KEY

Salt通过公钥加密和认证minions。想要让minion从master端接受命令,minions的密钥需要被master接受。

salt-key 命令时用来管理master上所有的密钥的。列出master上的密钥:

salt-key -L

The keys that have been rejected, accepted, and pending acceptance are listed. The easiest way to accept the minion key is to accept all pending keys:

salt-key -A

注解

Keys should be verified! Print the master key fingerprint by running salt-key -F master on the Salt master. Copy the master.pub fingerprint from the Local Keys section, and then set this value as the master_finger in the minion configuration file. Restart the Salt minion.

On the master, run salt-key -f minion-id to print the fingerprint of the minion's public key that was received by the master. On the minion, run salt-call key.finger --local to print the fingerprint of the minion key.

On the master:

# salt-key -f foo.domain.com
Unaccepted Keys:
foo.domain.com: 39:f9:e4:8a:aa:74:8d:52:1a:ec:92:03:82:09:c8:f9

On the minion:

# salt-call key.finger --local
local:
39:f9:e4:8a:aa:74:8d:52:1a:ec:92:03:82:09:c8:f9

If they match, approve the key with salt-key -a foo.domain.com.

发送第一个命令

现在minion已经连接到master并且通过认证,master可以发送命令到minion。

Salt命令允许执行海量的函数库,并且可以针对特殊的minions和minions组为目标执行。

salt 命令包含命令选项,目标说明,要执行的函数,和函数的参数。

一个简单的入门级命令看起来像是这样:

salt '*' test.ping

* 是指向所有minions的目标。

test.ping 告诉minon运行 test.ping 函数。

In the case of test.pingtest refers to a execution moduleping refers to the ping function contained in the aforementioned test module.

注解

Execution modules are the workhorses of Salt. They do the work on the system to perform various tasks, such as manipulating files and restarting services.

运行这条命令的结果将会是master指示所有的minions并行执行 test.ping 并返回结果。

这不是真正的ICMP ping,而是一个简单的函数返回 True。使用 test.ping 是确认一个minion是否连接正常的好方法。

注解

每个minion使用唯一的minion ID注册自身,但是也能够通过使用minion配置中的 id 选项来明确定义。

Of course, there are hundreds of other modules that can be called just as test.ping can. For example, the following would return disk usage on all targeted minions:

salt '*' disk.usage

函数概况

Salt拥有一个巨大的函数库可用于执行,而且Salt函数是自带文档说明的。在minions上执行 sys.doc 函数可以查看哪些函数可用:

salt '*' sys.doc

这会显示一个非常大的可用函数和函数文档列表。

注解

模块文档也可以 在线 查看。

这些函数覆盖从shell命令到包管理到数据库服务器操作等所有内容。它们包含强大的系统管理API,而这则是Salt配置管理和很多其他部分的核心。

注解

Salt拥有很多插件系统。这些函数通过文档:`执行模块 </ref/modules/all/index>`的"salt"命令可用。

了解一些有帮助的函数

文档`cmd </ref/modules/all/salt.modules.cmdmod>`模块包含在minions上执行shell命令的函数,比如模块`cmd.run <salt.modules.cmdmod.run>`和模块`cmd.run_all <salt.modules.cmdmod.run_all>`:

salt '*' cmd.run 'ls -l /etc'

pkg 函数会自动将本地系统包管理器映射到相同的salt函数。这意味着 pkg.install 在基于Red Hat系统上将使用 yum 而在Debian系统上则使用 apt 来安装包,等等。

salt '*' pkg.install vim

注解

一些自定义的Linux和其他发行版的衍生版可能不能被Salt正确检测。如果上述命令返回 pkg.install is not available的错误信息,那么你可能就需要重写pkg provider。这个过程在 这里 有详解。

模块函数`network.interfaces <salt.modules.network.interfaces>` 将会列出minion上的所有接口,以及它们的IP地址,子网掩码,MAC地址等:

salt '*' network.interfaces

CHANGING THE OUTPUT FORMAT

The default output format used for most Salt commands is called the nested outputter, but there are several other outputters that can be used to change the way the output is displayed. For instance, the pprint outputter can be used to display the return data using Python's pprint module:

root@saltmaster:~# salt myminion grains.item pythonpath --out=pprint
{'myminion': {'pythonpath': ['/usr/lib64/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib64/python2.7/lib-tk',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/gst-0.10',
'/usr/lib/python2.7/site-packages/gtk-2.0']}}

The full list of Salt outputters, as well as example output, can be found here.

SALT-CALL

The examples so far have described running commands from the Master using the salt command, but when troubleshooting it can be more beneficial to login to the minion directly and use salt-call.

Doing so allows you to see the minion log messages specific to the command you are running (which are not part of the return data you see when running the command from the Master using salt), making it unnecessary to tail the minion log. More information on salt-call and how to use it can be found here.

GRAINS是MINION启动时加载的,在运行过程中不会发生变化,所以是静态数据。GRAINS中包含诸如运行的内核版本,操作系统等信息。

Salt使用一个叫做 :doc:`Grains <../targeting/grains>`的系统来建立关于minions的静态数据。这个数据包含了关于操作系统运行状态,CPU架构等信息。grains系统贯穿Salt用于发送平台数据到许多组件和用户。

Grains can also be statically set, this makes it easy to assign values to minions for grouping and managing.

A common practice is to assign grains to minions to specify what the role or roles a minion might be. These static grains can be set in the minion configuration file or via the grains.setval function.

TARGETING

Salt allows for minions to be targeted based on a wide range of criteria. The default targeting system uses globular expressions to match minions, hence if there are minions named larry1larry2curly1, and curly2, a glob of larry* will match larry1 and larry2, and a glob of *1 will match larry1 and curly1.

除了通配符之外还有许多其他的目标系统可以使用,这些系统包括:

正则表达式

使用PCRE引擎的正则表达式的目标

grains是minion启动时加载的,在运行过程中不会发生变化,所以是静态数据。grains中包含诸如运行的内核版本,操作系统等信息。

基于grains数据的目标: Targeting with Grains

Pilar

基于pilar数据的目标: Targeting with Pillar

IP

基于IP地址/子网/范围的目标

杂合

创建基于多个目标的逻辑目标规则: Targeting with Compound

节点组

节点组目标: Targeting with Nodegroup

目标的概念不仅在可以Salt命令行上使用,而且在很多其他的区域同样可以运行,包括state系统和用于ACLs和用户权限的系统。

传递参数

很多函数可以通过命令行接收参数:

salt '*' pkg.install vim

This example passes the argument vim to the pkg.install function. Since many functions can accept more complex input than just a string, the arguments are parsed through YAML, allowing for more complex data to be sent on the command line:

salt '*' test.echo 'foo: bar'

一般Salt将这种字符串'foo: bar'翻译为字典"{'foo': 'bar'}"

注解

任何包含一个换行符的行不会通过YAML解析。

SALT STATES

Now that the basics are covered the time has come to evaluate States. Salt States, or the State System is the component of Salt made for configuration management.

The state system is already available with a basic Salt setup, no additional configuration is required. States can be set up immediately.

注解

Before diving into the state system, a brief overview of how states are constructed will make many of the concepts clearer. Salt states are based on data modeling and build on a low level data structure that is used to execute each state function. Then more logical layers are built on top of each other.

The high layers of the state system which this tutorial will cover consists of everything that needs to be known to use states, the two high layers covered here are the sls layer and the highest layer highstate.

Understanding the layers of data management in the State System will help with understanding states, but they never need to be used. Just as understanding how a compiler functions assists when learning a programming language, understanding what is going on under the hood of a configuration management system will also prove to be a valuable asset.

第一个SLS公式

The state system is built on SLS formulas. These formulas are built out in files on Salt's file server. To make a very basic SLS formula open up a file under /srv/salt named vim.sls. The following state ensures that vim is installed on a system to which that state has been applied.

/srv/salt/vim.sls:

vim:
pkg.installed

Now install vim on the minions by calling the SLS directly:

salt '*' state.sls vim

This command will invoke the state system and run the vim SLS.

Now, to beef up the vim SLS formula, a vimrc can be added:

/srv/salt/vim.sls:

vim:
pkg.installed: [] /etc/vimrc:
file.managed:
- source: salt://vimrc
- mode: 644
- user: root
- group: root

Now the desired vimrc needs to be copied into the Salt file server to /srv/salt/vimrc. In Salt, everything is a file, so no path redirection needs to be accounted for. The vimrc file is placed right next to the vim.sls file. The same command as above can be executed to all the vim SLS formulas and now include managing the file.

注解

Salt does not need to be restarted/reloaded or have the master manipulated in any way when changing SLS formulas. They are instantly available.

增加一些深度

Obviously maintaining SLS formulas right in a single directory at the root of the file server will not scale out to reasonably sized deployments. This is why more depth is required. Start by making an nginx formula a better way, make an nginx subdirectory and add an init.sls file:

/srv/salt/nginx/init.sls:

nginx:
pkg.installed: []
service.running:
- require:
- pkg: nginx

A few concepts are introduced in this SLS formula.

First is the service statement which ensures that the nginx service is running.

Of course, the nginx service can't be started unless the package is installed -- hence the require statement which sets up a dependency between the two.

The require statement makes sure that the required component is executed before and that it results in success.

注解

The require option belongs to a family of options called requisites. Requisites are a powerful component of Salt States, for more information on how requisites work and what is available see: Requisites

Also evaluation ordering is available in Salt as well: Ordering States

This new sls formula has a special name -- init.sls. When an SLS formula is named init.sls it inherits the name of the directory path that contains it. This formula can be referenced via the following command:

salt '*' state.sls nginx

注解

Reminder!

Just as one could call the test.ping or disk.usage execution modules, state.sls is simply another execution module. It simply takes the name of an SLS file as an argument.

Now that subdirectories can be used, the vim.sls formula can be cleaned up. To make things more flexible, move the vim.sls and vimrc into a new subdirectory called edit and change the vim.sls file to reflect the change:

/srv/salt/edit/vim.sls:

vim:
pkg.installed /etc/vimrc:
file.managed:
- source: salt://edit/vimrc
- mode: 644
- user: root
- group: root

Only the source path to the vimrc file has changed. Now the formula is referenced as edit.vim because it resides in the edit subdirectory. Now the edit subdirectory can contain formulas for emacs, nano, joe or any other editor that may need to be deployed.

接下来阅读

Two walk-throughs are specifically recommended at this point. First, a deeper run through States, followed by an explanation of Pillar.

  1. States 入门

  2. Pillar 演练

一个对于理解Pilar的非常有用的方式是使用States。

更加深入STATES

两个更深入的States教程已经存在,用以更加深入学习States功能。

  1. How Do I Use Salt States?, covers much more to get off the ground with States.
  2. The States Tutorial also provides a fantastic introduction.

These tutorials include much more in-depth information including templating SLS formulas etc.

还有更多!

This concludes the initial Salt walk-through, but there are many more things still to learn! These documents will cover important core aspects of Salt:

更多教程可以参考:

SALT手册目录

saltstack手册(含官方pdf)的更多相关文章

  1. Navi.Soft31.WinCE框架.开发手册(含下载地址)

    1.概述 1.1应用场景 随着物联网的普及,越来越多的制造商对货品从原料配备,加工生产,销售出库等环节的要求和把控越来越高.在此情况之下,传统的ERP软件已经无法满足现有的操作流程. 移动设备的应用, ...

  2. Learn Rails5.2-- rails base(含官方指导Debugging 摘录)

    豆知识扩展: <meta>  https://www.cnblogs.com/chentianwei/p/9183799.html css selector div > p 选择所有 ...

  3. Logstash生产环境实践手册(含grok规则示例和ELKF应用场景)

    ELKF应用场景: 1) datasource->logstash->elasticsearch->kibana 2) datasource->filebeat->log ...

  4. Python科学计算——前期准备

    1.开发环境搭建 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公 ...

  5. 推荐《R数据可视化手册》高清英文版PDF+中文版PDF+源代码

    绝大多数的绘图案例都是以强大.灵活制图而著称的R包ggplot2实现的,充分展现了ggplot2生动.翔实的一面.从如何画点图.线图.柱状图,到如何添加注解.修改坐标轴和图例,再到分面的使用和颜色的选 ...

  6. 转:Egret社区翻译的《TypeScript语言手册》

      <TyptScript语言手册>第1章-介绍<TypeScript语言手册>第2章-基本概念<TypeScript语言手册>第3章-类型<TypeScri ...

  7. Python 官方中文教程(简)

    Python 官方教程 前言 这是一次系统学习Python官方教程的学习笔记 整个教程一共16章, 在学习过程中记录自己不知道的和一些重要的知识, 水平有限, 请指正. Python3.7 官方教程. ...

  8. java web 开发手册

    W3School离线手册(2017.03)               提取密码: b2fo JavaScript高级程序设计第三版             提取密码: cscv CSS4.2.4 参 ...

  9. 304902阿里巴巴Java开发手册1.4.0

    转自官网 前言 <阿里巴巴Java开发手册>是阿里巴巴集团技术团队的集体智慧结晶和经验总结,经历了多次大规模一线实战的检验及不断完善,系统化地整理成册,回馈给广大开发者.现代软件行业的高速 ...

随机推荐

  1. Linux目录管理

    Linux文件目录管理 1:目录管理 1)切换目录 # cd  [ 目录名称] 2)退到上一目录 # cd .. 2:创建目录 mkdir  [文件名称] mkdir -p  [文件名称] 递归创建目 ...

  2. Paxos算法—前世

    Paxos算法是基于消息传递且具有高度容错特性的一致性算法.我们将从一个简单的问题开始,逐步的改进我们的设计方案,最终得到Paxos,一个可以在逆境下工作的协议. 一.客户端-服务器模型 我们从最小的 ...

  3. MATLAB之指定文件读取与读取地址输出

    一.读取指定文件夹下的指定格式文件 (1) 利用命令 uigetdir('','') 参数解释: uigetdir('所要打开的盘地址','对打开的弹出框进行描述') 例如:uigetdir('C:\ ...

  4. 2.3 Scala面向对象编程基础

    一.类 1.类的定义 Unit表示什么都不返回 方法体最后一句的值,就是方法的返回值. 2.类成员的可见性 3.方法的定义方式 定义方法的时候加圆括号,调用时可以加圆括号c.getValue()也可以 ...

  5. python结巴分词余弦相似度算法实现

    过余弦相似度算法计算两个字符串之间的相关度,来对关键词进行归类.重写标题.文章伪原创等功能, 让你目瞪口呆.以下案例使用的母词文件均为txt文件,两种格式:一种内容是纯关键词的txt,每行一个关键词就 ...

  6. 论文阅读笔记六十二:RePr: Improved Training of Convolutional Filters(CVPR2019)

    论文原址:https://arxiv.org/abs/1811.07275 摘要 一个训练好的网络模型由于其模型捕捉的特征中存在大量的重叠,可以在不过多的降低其性能的条件下进行压缩剪枝.一些skip/ ...

  7. openlayers绘制点,线,圆等

    由于我的业务需求是可以在底图上进行一些操作,比如绘制电子围栏等功能,于是需要使用openlayers中的画笔功能,接下来开始一波操作 还是上一篇的html页面, 直接上代码 <!doctype ...

  8. 数据结构——栈与递归(recursion)

    /* recursion.c */ /* 递归 */ #include <stdio.h> void interface(void); /* 斐波那契数列以及阶乘函数声明 */ long ...

  9. SPOJ31428 FIBONOMIAL(斐波那契数列)

    神鱼推题,必是好题. 前几天刚做过[BJOI2019]勘破神机,于是就会这题了.(BJ人民强啊……%鱼) 首先要求是 $$\sum\limits_{i=0}^nx^if_i$$ 应该很明显能想到把 $ ...

  10. MySQL实战45讲学习笔记:第三十讲

    一.复习一下加锁规则 在第20和21篇文章中,我和你介绍了 InnoDB 的间隙锁.next-key lock,以及加锁规则.在这两篇文章的评论区,出现了很多高质量的留言.我觉得通过分析这些问题,可以 ...