2015-01-20 02:43:38 +00:00
|
|
|
---
|
2020-09-01 15:14:13 +00:00
|
|
|
layout: commands
|
2020-04-07 18:55:19 +00:00
|
|
|
page_title: 'Commands: Lock'
|
|
|
|
description: >-
|
|
|
|
The lock command provides a mechanism for leader election, mutual exclusion,
|
|
|
|
or worker pools. For example, this can be used to ensure a maximum number of
|
|
|
|
services running at once across a cluster.
|
2015-01-20 02:43:38 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# Consul Lock
|
|
|
|
|
|
|
|
Command: `consul lock`
|
|
|
|
|
|
|
|
The `lock` command provides a mechanism for simple distributed locking.
|
2017-04-04 16:33:22 +00:00
|
|
|
A lock (or semaphore) is created at a given prefix in the KV store,
|
2015-01-20 02:43:38 +00:00
|
|
|
and only when held, is a child process invoked. If the lock is lost or
|
2015-01-25 10:42:26 +00:00
|
|
|
communication is disrupted, the child process is terminated.
|
2015-01-20 02:43:38 +00:00
|
|
|
|
2015-01-25 10:42:26 +00:00
|
|
|
The number of lock holders is configurable with the `-n` flag. By default,
|
2015-01-20 02:43:38 +00:00
|
|
|
a single holder is allowed, and a lock is used for mutual exclusion. This
|
2022-08-26 05:49:29 +00:00
|
|
|
uses the [leader election algorithm](/consul/tutorials/developer-configuration/application-leader-elections?utm_source=docs).
|
2015-01-20 02:43:38 +00:00
|
|
|
|
|
|
|
If the lock holder count is more than one, then a semaphore is used instead.
|
2015-06-01 13:34:54 +00:00
|
|
|
A semaphore allows more than a single holder, but this is less efficient than
|
2022-08-26 05:49:29 +00:00
|
|
|
a simple lock. This follows the [semaphore algorithm](/consul/tutorials/developer-configuration/distributed-semaphore?utm_source=docs).
|
2015-01-20 02:43:38 +00:00
|
|
|
|
2015-01-23 21:11:15 +00:00
|
|
|
All locks using the same prefix must agree on the value of `-n`. If conflicting
|
2015-01-22 01:35:06 +00:00
|
|
|
values of `-n` are provided, an error will be returned.
|
|
|
|
|
2015-01-20 02:43:38 +00:00
|
|
|
An example use case is for highly-available N+1 deployments. In these
|
|
|
|
cases, if N instances of a service are required, N+1 are deployed and use
|
|
|
|
consul lock with `-n=N` to ensure only N instances are running. For singleton
|
|
|
|
services, a hot standby waits until the current leader fails to take over.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Usage: `consul lock [options] prefix child...`
|
|
|
|
|
|
|
|
The only required options are the key prefix and the command to execute.
|
|
|
|
The prefix must be writable. The child is invoked only when the lock is held,
|
|
|
|
and the `CONSUL_LOCK_HELD` environment variable will be set to `true`.
|
|
|
|
|
2015-01-25 10:42:26 +00:00
|
|
|
If the lock is lost, communication is disrupted, or the parent process
|
2015-05-28 19:24:32 +00:00
|
|
|
interrupted, the child process will receive a `SIGTERM`. After a grace period
|
|
|
|
of 5 seconds, a `SIGKILL` will be used to force termination. For Consul agents
|
|
|
|
on Windows, the child process is always terminated with a `SIGKILL`, since
|
|
|
|
Windows has no POSIX compatible notion for `SIGTERM`.
|
2015-01-20 02:43:38 +00:00
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
#### Command Options
|
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
- `-child-exit-code` - Exit 2 if the child process exited with an error
|
2017-07-27 05:09:19 +00:00
|
|
|
if this is true, otherwise this doesn't propagate an error from the
|
|
|
|
child. The default value is false.
|
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
- `-monitor-retry` - Retry up to this number of times if Consul returns a 500 error
|
|
|
|
while monitoring the lock. This allows riding out brief periods of unavailability
|
|
|
|
without causing leader elections, but increases the amount of time required
|
|
|
|
to detect a lost lock in some cases. Defaults to 3, with a 1s wait between retries.
|
|
|
|
Set to 0 to disable.
|
2015-01-20 02:43:38 +00:00
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
- `-n` - Optional, limit of lock holders. Defaults to 1. The underlying
|
2015-01-20 02:43:38 +00:00
|
|
|
implementation switches from a lock to a semaphore when increased past
|
2015-01-22 01:35:06 +00:00
|
|
|
one. All locks on the same prefix must use the same value.
|
2015-01-20 02:43:38 +00:00
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
- `-name` - Optional name to associate with the underlying session.
|
2015-01-20 02:43:38 +00:00
|
|
|
If not provided, one is generated based on the child command.
|
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
- `-shell` - Optional, use a shell to run the command (can set a custom shell via the
|
2017-10-04 23:48:00 +00:00
|
|
|
SHELL environment variable). The default value is true.
|
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
- `-pass-stdin` - Pass stdin to child process.
|
2015-09-01 02:00:26 +00:00
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
- `-timeout` - Attempt to acquire the lock up to the given timeout. The timeout is a
|
2016-01-06 17:40:20 +00:00
|
|
|
positive decimal number, with unit suffix, such as "500ms". Valid time units
|
2020-07-01 12:59:07 +00:00
|
|
|
are "ns", "us" (or "µs"), "ms", "s", "m", "h". The default value is 0.
|
2016-01-06 00:40:35 +00:00
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
- `-verbose` - Enables verbose output.
|
2015-01-20 02:43:38 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
#### API Options
|
|
|
|
|
|
|
|
@include 'http_api_options_client.mdx'
|
|
|
|
|
|
|
|
@include 'http_api_options_server.mdx'
|
|
|
|
|
2016-06-04 00:01:05 +00:00
|
|
|
## SHELL
|
2020-04-07 18:55:19 +00:00
|
|
|
|
2016-06-04 00:01:05 +00:00
|
|
|
Consul lock launches its children in a shell. By default, Consul will use the shell
|
|
|
|
defined in the environment variable `SHELL`. If `SHELL` is not defined, it will
|
|
|
|
default to `/bin/sh`. It should be noted that not all shells terminate child
|
|
|
|
processes when they receive `SIGTERM`. Under Ubuntu, `/bin/sh` is linked to `dash`,
|
|
|
|
which does **not** terminate its children. In order to ensure that child processes
|
|
|
|
are killed when the lock is lost, be sure to set the `SHELL` environment variable
|
2017-10-04 23:48:00 +00:00
|
|
|
appropriately, or run without a shell by setting `-shell=false`.
|