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

SaltStack是继 Puppet、Chef 以后新出现的配置管理及远程执行工具, 目前,SaltStack 正获得愈来愈多的瞩目。
node


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


SaltStack 是开源软件,其源代码托管于 GitHub 上,安装及使用尽可 参考其官方文档。linux

安装就很简单了,下载个epel的源,而后直接yum  就偶了~ios


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


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

yum install salt-master


而后启动express

salt-master -d


客户端 clientapache

安装json

yum install salt-minion


须要在客户端的/etc/salt/minion 里面填写主的ip地址.ubuntu

须要注意的是,配置要遵照yaml的格式。  master:空格10.10.10.66


214959296.jpg


而后启动minion客户端

salt-minion-d


咱们能够在master 查看下客户端是否请求认证!

215142391.jpg


确认客户端的请求能够用

salt-key -a 65.ruifengyun.com

也能够所有认证

salt-key -A

认证后 咱们就能够测试下拉~ 下面的意思是, 执行全部的注册主机,用run模块,执行 ip a 这个命令,并返回结果。

215514929.jpg


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 时间,就能够作超时控制:

220050637.jpg


这里我们再详细的说下,如何用salt的targeting匹配打算执行的主机

刚才我们用的是 salt \*     意思是全部的在线客户端都要执行。


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

wKiom1Mk79CwemdPAAFQP0l_fic084.jpg


下面是经过grains来匹配主机,grains又是什么尼?   grains能够理解为saltstack客户端的一些特征,是saltstack客户端minion本身在服务器收集的,他能够获取ip,mac,系统,内存,cpu。。。。。

 -G 就是匹配grains的意思,  我这里只是匹配系统为Centos的主机,用ping模块,测试他通不通。

wKioL1Mk7m_gDa8hAAD2DQ9_POQ795.jpg


下面是经过正则的方式:  

220226440.jpg


为了方便起见,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端的操做

222932743.jpg

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

223014752.jpg



刚才我们只是简单的说了经过grains定位主机,在这里咱们再详细的描述下,该如何查看添加删除grains的信息。

grains.items参数,咱们能够经过grains 来查看客户端的基本信息 。


234833543.jpg

grains也是能够定制的,其实也好简单~


010853751.jpg

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

010853648.jpg



这样就成了~  这个东西咱们能够用来作定位主机的信息。



saltsatck的模块是个很重要的东西,咱们如今开始写模块:




1  编辑模块

vim /srv/salt/_modules/nima.py

2  同步

salt \* saltutil.sync_all

3  测试结果


003716786.jpg


我们再执行如下

003838630.jpg


这样咱们就能够看到本身定义的模块执行结果了。。。

我们能够简单加个接受的参数就能够实现:

004447879.jpg

你本身能够随意的定义,想执行linux命令的话,你能够调用os subprocess的模块

005029113.jpg

你们看,这个是否是和cmd.run是一个效果的~,最起码看起来是那样的~


有时候你们会发现salt的执行速度有点慢,这样的问题有多是你认证的主机大量的down机或者是不存在,网络有问题啥的,反正就是不稳定的因素

这个时候 我们能够把那主机给踢出出去。

011327372.jpg

原文: 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

wKioL1Mk9eeSRdDYAAQZfTEtmXw920.jpg


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

wKiom1Mk91XAMN0JAAGGRZfYt2I678.jpg


测试他是同步仍是异步

结果是异步的~  那就说明 没有同步堵塞这一说~

master对于整个过程支持pub和sub的过程,剩下的东西都是客户端本身玩,只是在最后给他return一个结果罢了~


011904947.jpg


这里要感谢沈灿的帮助:

他的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内容,能够提需求哈~

相关文章
相关标签/搜索