SaltStack Job管理

本文翻译自SaltStack 2017.7.2版本的官网英文资料。

1. Job管理

0.9.7版本的新功能。

由于Salt在许多系统上执行jobs,Salt需要能够管理在多个系统上运行的jobs。

1.1. Minion proc文件系统

Salt Minions在Salt cachedir中维护了一个proc目录。proc目录中维护着以执行的job ID命名的文件。 这些文件包含关于当前正在运行的job的信息,并允许查找job。 它位于cachedir的proc目录下,默认配置是/var/cache/salt/ proc。

1.2. saltutil模块中的功能函数

Salt 0.9.7为saltutil模块引入了一些新功能来管理jobs。

这些功能函数是:

    1. running, 返回在proc目录中找到的所有正在运行的jobs的数据。

    2. find_job, 基于job ID返回关于某个job的特定数据。

    3. signal_job, 允许给定的jid发送一个信号。

    4. term_job, 向控制指定job的进程发送终止信号(SIGTERM,15)。

    5. kill_job, 向控制指定job的进程发送kill信号(SIGKILL,9)。

这些功能函数构成了在minion级别上对jobs进行管理的核心。

1.3. Jobs Runner前端

增加了一个便利的jobs Runner前端和reporting系统。Jobs Runner包含一些可以更简单和更清晰的查看数据的功能。

Jobs Runner包含了许多功能...

active

active函数可以在所有minions上执行saltutil.running,并以更加可用和紧凑的格式格式化返回数据。 active函数还会比较已返回的job和仍在运行的job,从而更容易了解哪些系统已完成作业以及仍在等待哪些系统。

# salt-run jobs.active

lookup_jid

当作业执行时,返回数据会被发送回master并进行缓存。 默认情况下它会被缓存24小时,但可以通过master配置中的keep_jobs选项进行配置。 使用lookup_jid runner将显示与使用salt命令进行的初始作业调用时显示的返回数据相同的数据。

# salt-run jobs.lookup_jid <job id number>

list_jobs

在找到历史性job之前,可能需要找到Job ID。 list_jobs将解析缓存的执行数据,并显示已经或部分返回的作业的所有作业数据。

# salt-run jobs.list_jobs

1.4. 定时调度Jobs

Salt的调度系统允许minion或master进行增量执行。 调度系统对外提供了执行minions上的任何执行函数或在master执行任何runner的功能。

有多种方法启动调度系统的功能:

  •     在master或minion的配置文件中配置schedule选项。 这要求master或minion程序需要重新启动以生效。
  •     Minion pillar数据。 Schedule可以通过刷新minion的pillar数据来实现,例如通过使用saltutil.refresh_pillar。
  •     使用salt.states.schedule功能或salt.modules.schedule功能。

注意:调度程序对master和minions执行不同的功能函数。 当在master上运行时,是引用runner函数,当在minion上运行时,是使用execution函数。

除非将日志配置参数设置为info或更高级别,否则一个scheduled run任务没有任何输出信息。 请参阅minion-logging-settings。

像所有的states模块一样,States都是在minions上执行的。 您可以传递位置参数并提供一个YAML字典作为参数。

schedule:
  job1:
    function: state.sls
    seconds: 3600
    args:
      - httpd
    kwargs:
      test: True

这将安排定时执行命令:state.sls httpd test =True ,每间隔3600秒执行一次。

schedule:
  job1:
    function: state.sls
    seconds: 3600
    args:
      - httpd
    kwargs:
     test: True
     splay: 15

这将安排定时执行命令:state.sls httpd test =True ,每间隔3600秒执行一次。在0到15秒之间执行。

schedule:
  job1:
    function: state.sls
    seconds: 3600
    args:
      - httpd
    kwargs:
      test: True
      splay: 
        start:10
        end: 15

这将安排定时执行命令:state.sls httpd test =True ,每间隔3600秒执行一次。在10到15秒之间执行。

Schedule by Date and Time

New in version 2014.7.0.

作业的频率也可以使用Python dateutil库支持的日期字符串指定。 这需要安装Python dateutil库。

schedule:
  job1:
  function: state.sls
  args:
   - httpd
  kwargs:
    test: True
    when: 5:00pm

这将安排命令:state.sls httpd test = True在minion本地时间下午5:00执行 。

schedule:
  job1:
    function: state.sls
    args:
     - httpd
    kwargs:
      test: True
    when:
      - Monday 5:00pm
      - Tuesday 5:00pm
      - Wednesday 5:00pm
      - Thurday 5:00pm
      - Friday 5:00pm

这将按周一至周五的指定时间安排执行state.sls httpd test=True命令。

schedule:
  job1:
    function: state.sls
    Seconds: 3600
    args:
     - httpd
    kwargs:
      test: True
    range:
      start 8:00am
      end 5:00pm

这将在8点至17点间,每间隔1小时执行state.sls httpd test=True命令。这里要求range参数必须使用dateutil支持的格式。

schedule:
  job1:
    function: state.sls
    seconds: 3600
    args:
     - httpd
    kwargs:
      test: True
    range:
      invert: True
      start 8:00am
      end 5:00pm

使用invert参数,在指定的时间段之外按设定的频率执行命令。这里要求range参数必须使用dateutil支持的格式。

schedule:
  job1:
    function: pkg.install
    seconds: 3600
    kwargs:
      pkgs: [{‘bar’: ‘>1.2.3’}]
      refresh: true
      once: ‘2016-01-07T14:30:00’

这将安排函数pkg.install在指定的时间执行一次。 计划输入作业job1在作业完成后不会被删除,因此请使用schedule.delete手动删除它。

默认的日期格式是ISO 8601,但也可以通过指定once_fmt选项来覆盖,如下所示:

schedule:
  job1:
    function: test.ping
    once: 2015-04-22T20:21:00
    once_fmt: '%Y-%m-%dT%H:%M:%S'

Maximum Parallel Jobs Running

New in version 2014.7.0.

调度程序还支持确保运行特定例行程序的拷贝不超过N个。 适用于管理可能在长期时间范围内多次运行的工作,这些jobs可能会在基础设施停机的情况下发生启动多个程序实例的事件。

maxrunning的默认值为1。

schedule:
  long_running_job:
    function: big_file_transfer
    jid_include: True
    maxrunning: 1

Cron-like Schedule

New in version 2014.7.0.

schedule:
  job1:
    function: state.sls
    cron: '*/15 * * * *'
    args:
     - httpd
    kwargs:
    test: True

调度程序还支持使用cron 风格的调度作业。 这需要使用到Python croniter库。

Job Data Return

New in version 2015.5.0.

默认情况下,从Salt调度程序运行的作业的数据将返回给Master。 将return_job参数设置为False可以阻止将数据发送回Salt Master主机。

schedule:
  job1:
    function: scheduled_job_function
    return_job: False

Job Metadata

New in version 2015.5.0.

可以为一个job配置一些自定义的标签数据,用于帮助和其它jobs区分开来。 使用元数据参数可以与预定的作业相关联。 这些参数值并不会用于作业的执行,但可用于稍后搜索特定的作业,与return_job参数结合使用。 元数据参数必须指定为字典格式,否则将被忽略。

schedule:
  job1:
    function: scheduled_job_function
    metadata:
      foo: bar

Run on Start

New in version 2015.5.0.

默认情况下,任务计划会根据minion的启动时间将在minion启动时就第一次运行预定作业。 有时候这不是理想的情况。 可以将run_on_start参数设置为False,这会导致调度程序跳过第一次运行并等待下一次预定运行:

schedule:
  job1:
    function: state.sls
    seconds: 3600
    run_on_start: False
    args:
     - httpd
    kwargs:
      test: True

Until and After

New in version 2015.8.0.

schedule:
  job1:
    function: state.sls
    seconds: 15
    until: '12/31/2015 11:59pm'
    args:
     - httpd
    kwargs:
      test: True

使用until参数,Salt调度程序允许您为预定作业指定结束时间。 如果指定了此参数,则在指定时间过后,作业将不会运行。 时间应该以dateutil库支持的格式指定。 这需要安装Python dateutil库。

schedule:
  job1:
    function: state.sls
    seconds: 15
    After: '12/31/2015 11:59pm'
    args:
     - httpd
    kwargs:
      test: True

使用after参数,Salt调度程序允许您为计划作业指定开始时间。 如果指定了此参数,则作业将在指定时间过后才会运行。 时间应该以dateutil库支持的格式指定。 这需要安装Python dateutil库。

Scheduling States

schedule:
  log-loadavg:
    function: cmd.run
    seconds: 3660
    args:
     - 'logger -t salt < /proc/loadavg'
    kwargs:
      stateful: False
      shell: /bin/sh

Scheduling Highstates

要设置一个highstate,每60分钟在一个minion上运行,则可以在minion配置或pillar中设置:

schedule:
  highstate:
    function: state.highstate
    minutes: 60

时间间隔支持按秒、分钟、小时或天进行配置。

Scheduling Runners

Runner执行的调度也可以在Master配置文件中指定:

schedule:
  run_my_orch:
    function: state.orchestrate
    hours: 6
    splay: 600
    args:
      - orchestration.my_orch

上述配置的效果类似于每6小时运行一次salt-run state.orch orchestration.my_orch。

Scheduler With Returner

调度程序对于收集关于minion的监视数据等任务也很有用,此调度选项将收集状态数据并将其发送到一个存储返回值的MySQL数据库:

schedule:
  uptime:
    function: status.uptime
    seconds: 60
    returner: mysql
  meminfo:
    function: status.meminfo
    minutes: 5
    returner: mysql

由于反复指定returner可能会令人厌烦,因此可以使用schedule_returner选项来指定一个或全部returner列表,以便在安排调度时由minions使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Take charge of SaltStack to automate and configure enterprise-grade environments About This Book Automate tasks effectively, so that your infrastructure can run itself Take advantage of cloud-based services that can expand the capabilities of your own data centers Tackle real-world problems that appear in everyday situations In Detail SaltStack is known as a popular configuration management system, but that barely scratches the surface. It is, in fact, a powerful automation suite, which is designed not only to help you manage your servers, but to help them manage themselves. SaltStack is used worldwide by organizations ranging from just a few servers, to tens of thousands of nodes across data centers in multiple continents. This award-winning software is fast becoming the standard for systems management in the cloud world. This book will take you through the advanced features of SaltStack, bringing forward capabilities that will help you excel in the management of your servers. You will be taken through the the mind of the modern systems engineer, and discover how they use Salt to manage their infrastructures, and why those design decisions are so important. The inner workings of Salt will be explored, so that as you advance your knowledge of Salt, you will be able to swim with the current, rather than against it. Various subsystems of Salt are explained in detail, including Salt SSH, Salt Cloud, and external pillars, filesystems, and job caches. You will be taken through an in-depth discussion of how to effectively scale Salt to manage thousands of machines, and how to troubleshoot issues when things don't go exactly the way you expect them to. You will also be taken through an overview of RAET, Salt's new transport protocol, and given an insight into how this technology improves Salt, and the possibilities that it brings with it.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值