saltstack快速入門##個人整理篇

寫在前面,本文百分之70來自saltstack官方文檔摘抄,百分之20來自網絡其他博文

saltstack官方文檔目錄:http://docs.saltstack.cn/zh_CN/latest/contents.html

入門篇

使用pip安裝

因爲Salt已經進入 PyPI , 所以可以使用pip安裝,儘管多數用戶優先使用RPMs安裝(可以從 EPEL 安裝)。從pip安裝很簡單:

pip install salt

警告

如果從pip安裝(或者使用 setup.pyinstall 從源碼安裝),需要注意 yum-utils 被Salt依賴,用於管理軟件包。同樣的,如果Python依賴沒有安裝,你需要額外安裝庫/工具來構建一些依賴。更多信息點這裏

從EPEL安裝

從0.9.4版本開始,Salt已經在 EPEL 中可用。使用yum即可安裝。Salt可以在所有主流的基於RHEL的發行版中使用,包括CentOS, Scientific Linux,Oracle Linux和Amazon Linux。報告任何bugs或者issues在issue tracker

在RHEL6上,Jinja包 'python-jinja2' 從EPEL移動到"RHEL Server Option Channel"中去. 在RHEL6平臺上安裝salt時請確保該倉庫處於enabled狀態.

1. RHEL中開啓EPEL

在你的系統中如果EPEL當前並不是enabled狀態,你可以通過如下命令啓用它.

對於RHEL 5:

rpm -Uvh http://mirror.pnl.gov/epel/5/i386/epel-release-5-4.noarch.rpm

對於RHEL 6:

rpm -Uvh http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release-6-8.noarch.rpm

2. 安裝穩定版

Salt的master和minion包是分開的。機器只需要安裝相應的包即可運行。通常情況下,會有一個master和多個minions。

在salt-master上,運行:

yum install salt-master

在salt-minion上,運行:

yum install salt-minion

3. 從 epel-testing 安裝

當一個新版本的Salt打包時,首先會加入到 epel-testing 倉庫,以後會移到stable repo。

想要從 epel-testing 安裝,使用yum帶上 enablerepo 參數:

yum --enablerepo=epel-testing install salt-minion

簡單配置

Master

配置master開機自動啓動:

chkconfig salt-master on

啓動Master:

service salt-master start

Minion

配置Minion開機自動啓動:

chkconfig salt-minion on

啓動Minion:

service salt-minion start

==========

Salt Minion僅僅需要知道一條信息就可以運行,Master的網絡位置

默認情況下minion將會查找DNS解析 salt 作爲master,最簡單的途徑是搭建一個內部的DNS將salt 解析到Salt Master IP上。

否則,需要編輯minion配置文件配置 master 選項指向Salt Master的DNS名或IP:

註解

默認配置文件路徑位於/etc/salt下。大多數平臺會遵守這個約定,但是像FreeBSD和Microsoft Windows這樣的平臺會將這個文件放在不同的路徑。

/etc/salt/minion:

master: saltmaster.example.com

現在已經能夠找到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! The secure thing to do before accepting a key isto runsalt-key -f minion-id to print the fingerprint of the minion'spublic key. This fingerprint can then be compared against the fingerprintgenerated on the minion.

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.ping, test refers to a execution module. ping refers to theping function contained in the aforementionedtestmodule.

註解

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

運行這條命令的結果將會是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 astest.ping can. For example, the following would return disk usage on alltargeted minions:

salt '*' disk.usage

函數

http://docs.saltstack.cn/zh_CN/latest/topics/tutorials/walkthrough.html

函數概況

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 nestedoutputter, but there are several other outputters that can be used to changethe way the output is displayed. For instance, thepprint outputter can beused to display the return data using Python'spprint 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 foundhere.

 salt-call

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

Doing so allows you to see the minion log messages specific to the command youare running (which arenot part of the return data you see when running thecommand from the Master usingsalt), making it unnecessary to tail theminion log. More information onsalt-call and how to use it can be foundhere.

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 tominions for grouping and managing.

A common practice is to assign grains to minions to specify what the role orroles a minion might be. These static grains can be set in the minionconfiguration file or via thegrains.setvalfunction.

Targeting

Salt allows for minions to be targeted based on a wide range of criteria. Thedefault targeting system uses globular expressions to match minions, hence ifthere are minions namedlarry1, larry2,curly1, and curly2, aglob oflarry* will match larry1 and larry2, and a glob of *1will 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

這個例子傳遞參數 vim 給pkg.install函數。因爲很多函數可以僅僅通過一個字符串接受更復雜的輸入,傳遞的參數通過YAML解析,允許更復雜的數據通過命令行發送:

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

一般Salt將這種字符串'foo: bar'翻譯爲字典"{'foo': 'bar'}"

註解

任何包含一個換行符的行不會通過YAML解析。


擴展篇

節點組管理

節點組是使用複合目標的規範聲明。複合目標的文檔可以在這裏找到:doc:here <compound>

nodegroups`master配置文件參數用於定義節點組。這裏有一個通過`/etc/salt/master``配置文件配置節點組的例子:

nodegroups:
  group1: '[email protected],bar.domain.com,baz.domain.com or bl*.domain.com'
  group2: 'G@os:Debian and foo.domain.com'

註解

The L within group1 is matching a list of minions, while theG ingroup2 is matching specific grains. See thecompound matchers documentation for more details.

通過 -N 參數在命令行指定運行的節點組:

salt -N group1 test.ping

To match a nodegroup in your top file, make sure to put- match:nodegroup on the line directly following the nodegroup name.

base:
  group1:
    - match: nodegroup
    - webserver

註解

When adding or modifying nodegroups to a master configuration file, the master must be restartedfor those changes to be fully recognized.

A limited amount of functionality, such as targeting with -N from the command-line may beavailable without a restart.


分組語法

nodegroup分組時可以用到的語法關鍵字有G、E、P、L、I、S、R、D幾個,幾者的意義和用法見下表:



此外,匹配中可以使用and、or及not等boolean型操作。例:

想匹配所有minion中主機名(minion id)以webserv開頭並且運行在Debian系統上或者minion的主機名(minion id)匹配正則表達web-dc1-srv.* ,就可以用下表方式表示: www.111cn.net

 代碼如下 複製代碼

salt -C 'webserv* and G@os:Debian or E@web-dc1-srv.*' test.ping

當然也可以在預先分組時將這個配置寫在分組規則裏。在top.sls中可以如下使用:

 代碼如下 複製代碼

base:
'webserv* and G@os:Debian or E@web-dc1-srv.*':
– match: compound
– webserver


Batch Size

The -b (or --batch-size) option allows commands to be executed on onlya specified number of minions at a time. Both percentages and finite numbers aresupported.

salt '*' -b 10 test.ping

salt -G 'os:RedHat' --batch-size 25% apache.signal restart

This will only run test.ping on 10 of the targeted minions at a time and thenrestart apache on 25% of the minions matchingos:RedHat at a time and workthrough them all until the task is complete. This makes jobs like rolling webserver restarts behind a load balancer or doing maintenance on BSD firewallsusing carp much easier with salt.

The batch system maintains a window of running minions, so, if there are atotal of 150 minions targeted and the batch size is 10, then the command issent to 10 minions, when one minion returns then the command is sent to oneadditional minion, so that the job is constantly running on 10 minions.

文件的傳輸

默認的傳輸根目錄/srv/salt,不存在的話需要手動穿件

cp.get_file用來從master下載文件到客戶端,可以外加幾個參數,比如沒有文件夾,創建文件夾的makedirs=True ,壓縮的gzip參數。  

語法如下:



salt '*'cp.get_file salt://rr/etc/rr

get_url


cp.get_url可以從一個URL地址下載文件,URL可以是msater上的路徑(salt://),也可以是http網址。



salt '*'cp.get_url salt://my/file /tmp/mine
salt '*'cp.get_url http://xiaorui.cc/good.txt  /tmp/good.txt


通過master分發文件

[root@cbs1 salt]# cd /srv/salt/
[root@cbs1 salt]# mkdir test001
[root@cbs1 salt]# cd test001/
[root@cbs1 test001]# echo `date` > time.txt
[root@cbs1 test001]# ls
time.txt
[root@cbs1 test001]# salt -N 'group2' cp.get_file salt://test001/time.txt /tmp/time.txt
cbs2.test.com:
    /tmp/time.txt
cbs3.test.com:
    /tmp/time.txt
[root@cbs1 test001]# salt -N 'group2' cmd.run 'cat /tmp/time.txt'
cbs3.test.com:
    Mon Apr 27 19:04:10 CST 2015
cbs2.test.com:
    Mon Apr 27 19:04:10 CST 2015
[root@cbs1 test001]# 
[root@cbs1 test001]# 


通過minion拉去文件

[root@cbs1 test001]# cat /etc/salt/master | grep file_rec
file_recv: Ture
#file_recv_max_size: 100
[root@cbs1 test001]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
[root@cbs1 test001]# salt -N 'group2' cp.push /tmp/push.txt
cbs3.test.com:
    False
cbs2.test.com:
    True
[root@cbs1 test001]# cat /var/cache/salt/master/minions/cbs2.test.com/files/tmp/push.txt 
test push
[root@cbs1 test001]# 





發佈了59 篇原創文章 · 獲贊 7 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章