[Translated]20150106 Managing Linux server configs with the SaltStack.md

This commit is contained in:
GOLinux 2015-01-14 10:24:56 +08:00
parent bf6fbe0179
commit b59c18cbfc

View File

@ -1,55 +1,54 @@
Translating by GOLinux!
Managing Linux server configs with the SaltStack
SaltStackLinux服务器配置管理神器
================================================================================
![](http://techarena51.com/wp-content/uploads/2015/01/SaltStack+logo+-+black+on+white.png)
I came across Salt while searching for an alternative to [Puppet][1]. I like puppet, but I am falling in love with Salt :). This maybe a personal opinion but I found Salt easier to configure and get started with as compared to Puppet. Another reason I like Salt is that it lets you manage your server configurations from the command line, for example:
我在搜索[Puppet][1]的替代品时偶然间碰到了Salt。我喜欢puppet但是我又爱上Salt了:)。我发现Salt在配置和使用上都要比Puppet简单当然这只是一家之言你大可不必介怀。另外一个爱上Salt的理由是它可以让你从命令行管理服务器配置比如
To update all your servers with Salt, just run
要通过Salt来更新所有服务器你只需运行以下命令
salt * pkg.upgrade
**Installing the SaltStack on Linux.**
**安装SaltStack到Linux上。**
Salt is available in the EPEL repo if you are installing it on CentOS 6/7, Pi and Ubuntu linux users can add the Salt Repository from [here][2]. Since Salt is python based you can also use pip to install it but you have take care of dependencies like yum-utils and other packages yourself.
如果你是在CentOS 6/7上安装的话那么Salt可以通过EPEL仓库获取到。而对于Pi和Ubuntu Linux用户你可以从[这里][2]添加Salt仓库。Salt是基于python的所以你也可以使用pip来安装但是你得用yum-utils或是其它包管理器来自己处理它的依赖关系哦。
Salt follows the Server-Client model, The Server is known as the master whereas clients are called minions.
Salt遵循服务器-客户端模式,服务器端称为领主,而客户端则称为下属。
**Installation and Configuration of a Salt Master**
**安装并配置Salt领主**
[root@salt-master~]# yum install salt-master
Salt configurations files are stored in /etc/salt and /srv/salt. Salt is good to go out of the box, but I would recommend you configure a bit more verbose logging to help your troubleshoot.
Salt配置文件位于/etc/salt和/srv/salt。Salt虽然可以开箱即用但我还是建议你将日志配置得更详细点以方便日后排除故障。
[root@salt-master ~]# vim /etc/salt/master
#Default is warning change to the following
# 默认是warning修改如下
log_level: debug
log_level_logfile: debug
[root@salt-master ~]# systemctl start salt-master
**Installation and Configuration of a Salt minion**
**安装并配置Salt下属**
[root@salt-minion~]#yum install salt-minion
#Add the hostname of your Salt Master
# 添加你的Salt领主的主机名
[root@salt-minion~]#vim /etc/salt/minion
master: salt-master.com
#start the minion
# 启动下属
[root@salt-minion~] systemctl start salt-minion
On Startup, a minion will generate a cryptographic key and an id. It will then connect to the Salt Master and identify itself. The Salt Master must accept the minions key before allowing the minion to download a configuration.
在启动时下属客户机会生成一个密钥和一个id。然后它会连接到Salt领主服务器并验证自己的身份。Salt领主服务器在允许下属客户机下载配置之前必须接受下属的密钥。
**Listing and Accepting keys on the Salt Master**
**在Salt领主服务器上列出并接受密钥**
#List all keys
# 列出所有密钥
[root@salt-master~] salt-key -L
Accepted Keys:
Unaccepted Keys:
minion.com
Rejected Keys:
#Accept key with id minion.com
# 使用id 'minion.com'命令接受密钥
[root@salt-master~]salt-key -a minion.com
[root@salt-master~] salt-key -L
@ -58,43 +57,45 @@ On Startup, a minion will generate a cryptographic key and an id. It will then c
Unaccepted Keys:
Rejected Keys:
Once you have accepted a minions keys, you can get information on it immediately using the salt command.
在接受下属客户机的密钥后你可以使用salt命令来立即获取信息。
**Salt command line examples**
**Salt命令行实例**
#Check if a minion is up and running
# 检查下属是否启动并运行
[root@salt-master~] salt 'minion.com' test.ping
minion.com:
True
# run shell commands on the minion
# 在下属客户机上运行shell命令
[root@salt-master~]# salt 'minion.com' cmd.run 'ls -l'
minion.com:
total 2988
-rw-r--r--. 1 root root 1024 Jul 31 08:24 1g.img
-rw-------. 1 root root 940 Jul 14 15:04 anaconda-ks.cfg
-rw-r--r--. 1 root root 1024 Aug 14 17:21 test
#install/update a software on all your servers
# 安装/更新所有服务器上的软件
[root@salt-master ~]# salt '*' pkg.install git
The salt command needs a few components to send information. One of these components is the minion id and another is the function to be called on the minion.
In the first example I used the ping function of the test module to check if the system is up. This function does not perform an actual ping, it just returns true if the minion responds.
cmd.run is used to execute remote commands and pkg module contains functions for package management. The full list of builin modules is at the end of this post.
salt命令需要一些组件来发送信息其中之一是mimion id而另一个是下属客户机上要调用的函数。
**Grains example**
在第一个实例中我使用test模块的ping函数来检查系统是否启动。该函数并不是真的实施一次ping它仅仅是在下属客户机作出回应时返回
Salt uses an interface called **Grains** to get system information. You can use grains to run commands on systems with particular properties.
cmd.run用于执行远程命令pkg模块包含了包管理的函数。本文结尾提供了全部内建模块的列表。
**颗粒实例**
Salt使用一个名为**颗粒**的界面来获取系统信息。你可以使用颗粒在指定属性的系统上运行命令。
[root@vps4544 ~]# salt -G 'os:Centos' test.ping
minion:
True
More grain examples are available at http://docs.saltstack.com/en/latest/topics/targeting/grains.html
更多颗粒实例,请访问http://docs.saltstack.com/en/latest/topics/targeting/grains.html
**Package Management via the State File System.**
**通过状态文件系统进行包管理。**
In order to automate software configurations you will need to use the state system and create a state file. These files use the YAML format and python dictionaries, lists, strings and numbers for data structure. Reading up on them will help you understand the configurations better.
为了是软件配置自动化你需要使用状态系统并创建状态文件。这些文件使用YAML格式和python字典、列表、字符串以及编号来构成数据结构。将这些文件从头到尾研读一遍这将有助于你更好地理解它的配置。
**VIM state file example**
**VIM状态文件实例**
[root@salt-master~]# vim /srv/salt/vim.sls
vim-enhanced:
@ -105,11 +106,10 @@ In order to automate software configurations you will need to use the state syst
- user: root
- group: root
- mode: 644
The first and third line in this file are called state id. They must contain the exact name or path of the package or file to be managed. After the state ids are state and function declaration. pkg and file are state declarations whereas installed and managed are function declarations. Functions accept arguments, user,group,mode and source are all arguments to the function managed.
该文件的第一和第三行成为状态id它们必须包含有需要管理的包或文件的确切名称或路径。在状态id之后是状态和函数声明pkgfile是状态声明installedmanaged是函数声明。函数接受参数用户、组、模式和源都是函数managed的参数。
To apply this configuration to a minion move your vimrc file to /srv/salt and run.
要将该配置应用到下属客户端请移动你的vimrc文件到/src/salt然后运行以下命令。
[root@salt-master~]# salt 'minion.com' state.sls vim
minion.com:
@ -135,8 +135,8 @@ To apply this configuration to a minion move your vimrc file to /srv/sa
Failed: 0
------------
Total states run: 1
You can also add dependencies to your configurations.
你也可以添加依赖关系到你的配置中。
[root@salt-master~]# vim /srv/salt/ssh.sls
openssh-server:
@ -155,9 +155,9 @@ You can also add dependencies to your configurations.
- require:
- pkg: openssh-server
The require statement here is a requisite declaration, it creates a dependency between the service and pkg states. This declaration will first check if the package is installed and then run the service.
这里的require声明是必须的它在servicepkg状态之间创建依赖关系。该声明将首先检查包是否安装然后运行服务。
However, I prefer using the watch statement as it also checks for file modifications and restarts the service.
但是我更偏向于使用watch声明因为它也可以检查文件是否修改和重启服务。
[root@salt-master~]# vim /srv/salt/ssh.sls
openssh-server:
@ -229,7 +229,7 @@ However, I prefer using the watch statement as it also checks for file mod
------------
Total states run: 4
Maintaining all config files in single directory can make scaling a complex task, hence you can create sub-directories and add your configuration in them with a init.sls file
在单一目录中维护所有的配置文件是一项复杂的大工程因此你可以创建子目录并在其中添加配置文件init.sls文件。
[root@salt-master~]# mkdir /srv/salt/ssh
[root@salt-master~]# vim /srv/salt/ssh/init.sls
@ -253,13 +253,13 @@ Maintaining all config files in single directory can make scaling a complex task
[root@vps4544 ssh]# cp /etc/ssh/sshd_config /srv/salt/ssh/
[root@vps4544 ssh]# salt 'minion.com' state.sls ssh
**Top File and Environments.**
**Top文件和环境。**
A Top file (top.sls) is where you define your environments. A top file allows you to map minions to packages. The default environment is base. You need to define which packages will be installed on which server under the base environment.
top文件top.sls是用来定义你的环境的文件它允许你映射下属客户机到包默认环境是base。你需要定义在基本环境下哪个包会被安装到哪台服务器。
If there are multiple environments and more than one definitions for a particular minion is used then by default the base environment will supersede the others.
如果对于一台特定的下属客户机而言,有多个环境,并且有多于一个的定义,那么默认情况下,基本环境将取代其它环境。
To define an environment you need to add it to the file_roots directive in the master configuration file.
要定义环境你需要将它添加到领主配置文件的file_roots指针。
[root@salt-master ~]# vim /etc/salt/master
file_roots:
@ -268,7 +268,7 @@ To define an environment you need to add it to the file_roots directive in
dev:
- /srv/salt/dev
Now add a top.sls file in /srv/salt
现在添加一个top.sls文件到/src/salt
[root@salt-master ~]# vim /srv/salt/top.sls
base:
@ -277,7 +277,7 @@ Now add a top.sls file in /srv/salt
'minion.com':
- ssh
Apply the top file configuration with
应用top文件配置
[root@salt-master~]# salt '*' state.highstate
minion.com:
@ -297,29 +297,29 @@ Apply the top file configuration with
Started: 13:10:55.
Duration: 2.156 ms
The minion will download the top file and search for its configuration. It will also apply the configuration for all minions.
下属客户机将下载top文件并搜索用于它的配置领主服务器也会将配置应用到所有下属客户机。
This is just a brief introduction to Salt, for in depth understanding you can go through the links below and if you are already using Salt and have any recommendations do let me know.
这仅仅是一个Salt的简明教程如果你想要深入学习并理解你可以访问以下链接。如果你已经在使用Salt那么请告诉我你的建议和意见吧。
Update: [Foreman][3] has support for salt via [plugins][4].
更新: [Foreman][3] 已经通过[插件][4]支持salt。
Read
阅读链接
- http://docs.saltstack.com/en/latest/ref/states/top.html#how-top-files-are-compiled
- http://docs.saltstack.com/en/latest/topics/tutorials/states_pt1.html
- http://docs.saltstack.com/en/latest/ref/states/highstate.html#state-declaration
Grains
颗粒
- http://docs.saltstack.com/en/latest/topics/targeting/grains.html
List of Salt Modules
Salt模块列表
Good comparison of Salt and puppet
Salt和Puppet的充分比较
- https://mywushublog.com/2013/03/configuration-management-with-salt-stack/
Full list of builtin execution modules
内建执行模块的完全列表
- http://docs.saltstack.com/en/latest/ref/modules/all/
@ -328,7 +328,7 @@ Full list of builtin execution modules
via: http://techarena51.com/index.php/getting-started-with-saltstack/
作者:[Leo G][a]
译者:[译者ID](https://github.com/译者ID)
译者:[GOLinux](https://github.com/GOLinux)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出