您的位置:首页 > 运维架构 > 网站架构

集群管理系统 Saltstack的资源配置及性能测试 推荐

2013-08-02 00:27 513 查看
SaltStack是继 Puppet、Chef 之后新出现的配置管理及远程执行工具, 目前,SaltStack 正得到越来越多的瞩目。

与 Puppet 相比,SaltStack 没有那么笨重,感觉较为轻量;不像 Puppet 有 一套自己的 DSL 用来写配置,SaltStack 使用 YAML 作为配置文件格式,写 起来既简单又容易,同时也便于动态生成;此外,SaltStack 在远程执行命令 时的速度非常快,也包含丰富的模块。

SaltStack 是开源软件,其源代码托管于 GitHub 上,安装及使用尽可 参考其官方文档。
安装就很简单了,下载个epel的源,然后直接yum 就偶了~

原文:http://rfyiamcool.blog.51cto.com/1030776/1262537

拿一台服务器做master,直接yum安装

yum install salt-master


然后启动

salt-master -d


客户端 client
安装

yum install salt-minion


需要在客户端的/etc/salt/minion 里面填写主的ip地址.
需要注意的是,配置要遵守yaml的格式。 master:空格10.10.10.66





然后启动minion客户端

salt-minion-d


我们可以在master 查看下客户端是否请求认证!





确认客户端的请求可以用
salt-key -a 65.ruifengyun.com
也可以全部认证
salt-key -A

认证后 我们就可以测试下拉~ 下面的意思是, 执行所有的注册主机,用run模块,执行 ip a 这个命令,并返回结果。




salt详细的帮助文档:

[root@66 ~]# salt -h
Usage: salt [options] '<target>' <function> [arguments]
Options:
--version             show program's version number and exit
--versions-report     show program's dependencies version number and exit
-h, --help            show this help message and exit
-c CONFIG_DIR, --config-dir=CONFIG_DIR
Pass in an alternative configuration directory.
Default: /etc/salt
-t TIMEOUT, --timeout=TIMEOUT
Change the timeout, if applicable, for the running
command; default=5
-s, --static          Return the data from minions as a group after they all
return.
--async               Run the salt command but don't wait for a reply
-v, --verbose         Turn on command verbosity, display jid and active job
queries
-b BATCH, --batch=BATCH, --batch-size=BATCH
Execute the salt job in batch mode, pass either the
number of minions to batch at a time, or the
percentage of minions to have running
-a EAUTH, --auth=EAUTH, --eauth=EAUTH, --extended-auth=EAUTH
Specify an extended authentication system to use.
-T, --make-token      Generate and save an authentication token for re-use.
Thetoken is generated and made available for the
period defined in the Salt Master.
--return=RETURNER     Set an alternative return method. By default salt will
send the return data from the command back to the
master, but the return data can be redirected into any
number of systems, databases or applications.
-d, --doc, --documentation
Return the documentation for the specified module or
for all modules if none are specified.
Target Options:
Target Selection Options
-E, --pcre          Instead of using shell globs to evaluate the target
servers, use pcre regular expressions
-L, --list          Instead of using shell globs to evaluate the target
servers, take a comma delimited list of servers.
-G, --grain         Instead of using shell globs to evaluate the target
use a grain value to identify targets, the syntax for
the target is the grain key followed by a
globexpression: "os:Arch*"
--grain-pcre        Instead of using shell globs to evaluate the target
use a grain value to identify targets, the syntax for
the target is the grain key followed by a pcre regular
expression: "os:Arch.*"
-N, --nodegroup     Instead of using shell globs to evaluate the target
use one of the predefined nodegroups to identify a
list of targets.
-R, --range         Instead of using shell globs to evaluate the target
use a range expression to identify targets. Range
expressions look like %cluster
-C, --compound      The compound target option allows for multiple target
types to be evaluated, allowing for greater
granularity in target matching. The compound target is
space delimited, targets other than globs are preceded
with an identifier matching the specific targets
argument type: salt 'G@os:RedHat and webser* or
E@database.*'
-X, --exsel         Instead of using shell globs use the return code of a
function.
-I, --pillar        Instead of using shell globs to evaluate the target
use a pillar value to identify targets, the syntax for
the target is the pillar key followed by a
globexpression: "role:production*"
-S, --ipcidr        Match based on Subnet (CIDR notation) or IPv4 address.
Output Options:
Configure your preferred output format
--raw-out           DEPRECATED. Print the output from the 'salt' command
in raw python form, this is suitable for re-reading
the output into an executing python script with eval.
--yaml-out          DEPRECATED. Print the output from the 'salt' command
in yaml.
--json-out          DEPRECATED. Print the output from the 'salt' command
in json.
--text-out          DEPRECATED. Print the output from the 'salt' command
in the same form the shell would.
--out=OUTPUT, --output=OUTPUT
Print the output from the 'salt' command using the
specified outputter. The builtins are 'no_return',
'grains', 'yaml', 'overstatestage', 'json', 'pprint',
'nested', 'raw', 'highstate', 'quiet', 'key', 'txt',
'virt_query'.
--out-indent=OUTPUT_INDENT, --output-indent=OUTPUT_INDENT
Print the output indented by the provided value in
spaces. Negative values disables indentation. Only
applicable in outputters that support indentation.
--no-color, --no-colour
Disable all colored output


salt 在语句的后面加个 -t 时间,就可以做超时控制:





这里咱们再详细的说下,如何用salt的targeting匹配打算执行的主机
刚才咱们用的是 salt \* 意思是所有的在线客户端都要执行。

下面是通过shell通配符的方式匹配




下面是通过grains来匹配主机,grains又是什么尼? grains可以理解为saltstack客户端的一些特征,是saltstack客户端minion自己在服务器收集的,他可以获取ip,mac,系统,内存,cpu。。。。。
-G 就是匹配grains的意思, 我这里只是匹配系统为Centos的主机,用ping模块,测试他通不通。




下面是通过正则的方式:





为了方便起见,salt也支持组的概念, 你可以把自己常用的主机,放在一个组里面,这样你以后可以选定这个组,做针对的操作。

vim /etc/salt/master

nodegroups:
group1: 'L@10.200.89.67,10.200.89.69'
group2: 'G@os:Debian and foo.domain.com'


使用-N选项:

salt -N group1 test.ping

好了,在这里咱们再说下,salt的文件推送。 salt的文件传输,用的是zeromq的机制,这东西做mq速度够快,但是做文件的传输,性能真心不咋低。。。 可以说很一般,大家可以用rsync来扩展下性能。

master端的操作



我们再到客户端看看,文件是否同步过来? 有 1 这个文件,说明无问题。。。




刚才咱们只是简单的说了通过grains定位主机,在这里我们再详细的描述下,该如何查看添加删除grains的信息。
grains.items参数,我们可以通过grains 来查看客户端的基本信息 。




grains也是可以定制的,其实也好简单~




如何添加grains数据,vim /srv/salt/_grains/r.py 然后按照下面的格式,return grains就行了。写完了模块,再用 saltutil.sync_all把数据推下去。




这样就成了~ 这个东西我们可以用来做定位主机的信息。

saltsatck的模块是个很重要的东西,我们现在开始写模块:

1 编辑模块
vim /srv/salt/_modules/nima.py
2 同步
salt \* saltutil.sync_all
3 测试结果





咱们再执行以下




这样我们就可以看到自己定义的模块执行结果了。。。
咱们可以简单加个接受的参数就可以实现:



你自己可以随意的定义,想执行linux命令的话,你可以调用os subprocess的模块



大家看,这个是不是和cmd.run是一个效果的~,最起码看起来是那样的~

有时候大家会发现salt的执行速度有点慢,这样的问题有可能是你认证的主机大量的down机或者是不存在,网络有问题啥的,反正就是不稳定的因素
这个时候 咱们可以把那主机给踢出出去。




原文: xiaorui.cc
再说下saltstack的安全问题 saltstack自带了一个acl控制
可以定义对每个主机的使用哪个模块。。。

vim /etc/salt/master
增加以下内容
client_acl:
66.xiaorui.com:
- test.ping
- status.uptime


这个意思是说 针对66这个人,咱们只能运维 test.ping和status.uptime ,要是运行 cmd.run rm -f / 的话 会提示权限导致不能运行的。

Failed to authenticate, is this user permitted to execute commands?'

state状态的管理:
写来个top.sls的例子

base:
'*':
- ldap-client
- networking
- salt.minion
'salt-master*':
- salt.master
'^(memcache|web).(qa|prod).loc$':
- match: pcre
- nagios.client
- apache.server
'os:Ubuntu':
- match: grain
- repos.ubuntu
'os:(RedHat|CentOS)':
- match: grain_pcre
- repos.epel
'foo,bar,baz':
- match: list
- database
'somekey:abc':
- match: pillar
- xyz
'nag1* or G@role:monitoring':
- match: compound
- nagios.server


这里简单讲讲 job management 任务管理:
要知道salt针对每次的操作,他都会存起来,所有通过jobs可以获取以前的记录,甚至可以做做审计啥的。
salt-run jobs.list_jobs 是显示最近的操作
salt-run jobs.lookup_jid id号码 是显示具体的返回值

[root@devops-ruifengyun ~ ]$ salt-run jobs.list_jobs|tail -n 20
Function: saltutil.find_job
StartTime: 2014, Mar 16 08:21:24.036011
Target: '*'
Target-type: glob
User: root
'20140316082318997104':
Arguments:
- dir
Function: cmd.run
StartTime: 2014, Mar 16 08:23:18.997104
Target: 10.200.89.*
Target-type: glob
User: root
'20140316084139556527':
Arguments: []
Function: saltutil.running
StartTime: 2014, Mar 16 08:41:39.556527
Target: '*'
Target-type: glob
User: root
[root@devops-ruifengyun ~ ]$
[root@devops-ruifengyun ~ ]$
[root@devops-ruifengyun ~ ]$ salt-run jobs.lookup_jid 20140316082318997104
10.200.89.67:
beifen       install.log.syslog  keepalived-1.2.12.tar.gz  vs_wan.conf
install.log  keepalived-1.2.12   vip_wan.conf
10.200.89.69:
install.log  install.log.syslog  keepalived-1.2.12  keepalived-1.2.12.tar.gz




通过salt-run jobs.active 获取当前还在运行的任务。




测试他是同步还是异步
结果是异步的~ 那就说明 没有同步堵塞这一说~
master对于整个过程支持pub和sub的过程,剩下的东西都是客户端自己玩,只是在最后给他return一个结果罢了~





这里要感谢沈灿的帮助:
他的saltstack讲的比我的要细致。。。 更加牛逼点~ 推荐大家去瞅瞅
http://www.shencan.net/

需要说明的是他的网站真jiba慢~

这边再总结分享下,关于saltstack的教程,及我已经用saltstack做的案例 ,供大家瞅瞅!

运维平台化saltstack和jinja2模板构建高可用集群配置平台 http://rfyiamcool.blog.51cto.com/1030776/1351068
用saltstack cp模块实现文件管理、拉取和回滚下发 http://rfyiamcool.blog.51cto.com/1030776/1360468
saltsatck基础入门教程 http://rfyiamcool.blog.51cto.com/1030776/1262537
借用subprocess和gevent、multiprocess解决saltstack超时不准的问题 http://rfyiamcool.blog.51cto.com/1030776/1377429
简单实现海量数据的管理平台 [saltstack hadoop] http://rfyiamcool.blog.51cto.com/1030776/1258561
利用saltstack的api接口和modules实现实时监控 http://rfyiamcool.blog.51cto.com/1030776/1345518
saltstack 关于ssh key http://rfyiamcool.blog.51cto.com/1030776/1300204
saltstack web ui的配置及简单的二次开发
http://rfyiamcool.blog.51cto.com/1030776/1275443
saltstack websocket监控开发 http://rfyiamcool.blog.51cto.com/1030776/1269232
用Saltstack的modules和grains实现实时监控平台 http://rfyiamcool.blog.51cto.com/1030776/1266437
用Saltstack的returners实现批量监控和数据存储
http://rfyiamcool.blog.51cto.com/1030776/1264438
利用saltstack的salt-ssh进行集群管理【无客户端下】 http://rfyiamcool.blog.51cto.com/1030776/1305710
salt-api的接口文档 http://rfyiamcool.blog.51cto.com/1030776/1362979
大家还想看关于saltstack内容,可以提需求哈~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息