Generate CockroachDB Resources

The cockroach gen command can generate command-line interface (CLI) utilities (man pages and abash autocompletion script), example SQL data suitable to populate test databases, and an HAProxy configuration file for load balancing a running cluster.

Subcommands

SubcommandUsage
manGenerate man pages for CockroachDB.
autocompleteGenerate bash or zsh autocompletion script for CockroachDB.Default: bash
example-dataGenerate example SQL datasets. You can also use the cockroach workload command to generate these sample datasets in a persistent cluster and the cockroach demo <dataset> command to generate these datasets in a temporary, in-memory cluster.
haproxyGenerate an HAProxy config file for a running CockroachDB cluster. The node addresses included in the config are those advertised by the nodes. Make sure hostnames are resolvable and IP addresses are routable from HAProxy.

Synopsis

Generate man pages:

  1. $ cockroach gen man

Generate bash autocompletion script:

  1. $ cockroach gen autocomplete

Generate example SQL data:

  1. $ cockroach gen example-data intro | cockroach sql
  1. $ cockroach gen example-data startrek | cockroach sql

Generate an HAProxy config file for a running cluster:

  1. $ cockroach gen haproxy

View help:

  1. $ cockroach gen --help
  1. $ cockroach gen man --help
  1. $ cockroach gen autocomplete --help
  1. $ cockroach gen example-data --help
  1. $ cockroach gen haproxy --help

Flags

The gen subcommands supports the following general-use and logging flags.

General

man

FlagDescription
—pathThe path where man pages will be generated.Default: man/man1 under the current directory

autocomplete

FlagDescription
—outThe path where the autocomplete file will be generated.Default: cockroach.bash in the current directory

example-data

No flags are supported. See the Generate Example Data example for guidance.

haproxy

FlagDescription
—hostThe server host and port number to connect to. This can be the address of any node in the cluster. Env Variable: COCKROACH_HOSTDefault: localhost:26257
—port-pThe server port to connect to. Note: The port number can also be specified via —host. Env Variable: COCKROACH_PORTDefault: 26257
—insecureUse an insecure connection.Env Variable: COCKROACH_INSECUREDefault: false
—certs-dirThe path to the certificate directory containing the CA and client certificates and client key.Env Variable: COCKROACH_CERTS_DIRDefault: ${HOME}/.cockroach-certs/
—urlA connection URL to use instead of the other arguments.Env Variable: COCKROACH_URLDefault: no URL
—outThe path where the haproxy.cfg file will be generated. If an haproxy.cfg file already exists in the directory, it will be overwritten.Default: haproxy.cfg in the current directory
—localityIf nodes were started with locality details, you can use the —locality flag here to filter the nodes included in the HAProxy config file, specifying the explicit locality tier(s) or a regular expression to match against. This is useful in cases where you want specific instances of HAProxy to route to specific nodes. See the Generate an HAProxy configuration file example for more details.

Logging

By default, the gen command logs errors to stderr.

If you need to troubleshoot this command's behavior, you can change its logging behavior.

Examples

Generate man pages

Generate man pages:

  1. $ cockroach gen man

Move the man pages to the man directory:

  1. $ sudo mv man/man1/* /usr/share/man/man1

Access man pages:

  1. $ man cockroach

Generate a bash autocompletion script

Generate bash autocompletion script:

  1. $ cockroach gen autocomplete

Add the script to your .bashrc and .bash_profle:

  1. $ printf "\n\n#cockroach bash autocomplete\nsource '<path to>cockroach.bash'" >> ~/.bashrc
  1. $ printf "\n\n#cockroach bash autocomplete\nsource '<path to>cockroach.bash'" >> ~/.bash_profile

You can now use tab to autocomplete cockroach commands.

Generate example data

Tip:

You can also use the cockroach workload command to generate these sample datasets in a persistent cluster and the cockroach demo <dataset> command to generate these datasets in a temporary, in-memory cluster.

To test out CockroachDB, you can generate an example startrek database, which contains 2 tables, episodes and quotes.

  1. $ cockroach gen example-data startrek | cockroach sql --insecure
  1. CREATE DATABASE
  2. SET
  3. DROP TABLE
  4. DROP TABLE
  5. CREATE TABLE
  6. INSERT 79
  7. CREATE TABLE
  8. INSERT 200

Launch the built-in SQL client to view it:

  1. $ cockroach sql --insecure
  1. > SHOW TABLES FROM startrek;
  1. +------------+
  2. | table_name |
  3. +------------+
  4. | episodes |
  5. | quotes |
  6. +------------+
  7. (2 rows)

You can also generate an example intro database, which contains 1 table, mytable, with a hidden message:

  1. $ cockroach gen example-data intro | cockroach sql --insecure
  1. CREATE DATABASE
  2. SET
  3. DROP TABLE
  4. CREATE TABLE
  5. INSERT 1
  6. INSERT 1
  7. INSERT 1
  8. INSERT 1
  9. ...
  1. # Launch the built-in SQL client to view it:
  2. $ cockroach sql --insecure
  1. > SHOW TABLES FROM intro;
  1. +-------------+
  2. | table_name |
  3. +-------------+
  4. | mytable |
  5. +-------------+
  6. (1 row)
  1. > SELECT * FROM intro.mytable WHERE (l % 2) = 0;
  1. +----+------------------------------------------------------+
  2. | l | v |
  3. +----+------------------------------------------------------+
  4. | 0 | !__aaawwmqmqmwwwaas,,_ .__aaawwwmqmqmwwaaa,, |
  5. | 2 | !"VT?!"""^~~^"""??T$Wmqaa,_auqmWBT?!"""^~~^^""??YV^ |
  6. | 4 | ! "?##mW##?"- |
  7. | 6 | ! C O N G R A T S _am#Z??A#ma, Y |
  8. | 8 | ! _ummY" "9#ma, A |
  9. | 10 | ! vm#Z( )Xmms Y |
  10. | 12 | ! .j####mmm#####mm#m##6. |
  11. | 14 | ! W O W ! jmm###mm######m#mmm##6 |
  12. | 16 | ! ]#me*Xm#m#mm##m#m##SX##c |
  13. | 18 | ! dm#||+*$##m#mm#m#Svvn##m |
  14. | 20 | ! :mmE=|+||S##m##m#1nvnnX##; A |
  15. | 22 | ! :m#h+|+++=Xmm#m#1nvnnvdmm; M |
  16. | 24 | ! Y $#m>+|+|||##m#1nvnnnnmm# A |
  17. | 26 | ! O ]##z+|+|+|3#mEnnnnvnd##f Z |
  18. | 28 | ! U D 4##c|+|+|]m#kvnvnno##P E |
  19. | 30 | ! I 4#ma+|++]mmhvnnvq##P` ! |
  20. | 32 | ! D I ?$#q%+|dmmmvnnm##! |
  21. | 34 | ! T -4##wu#mm#pw##7' |
  22. | 36 | ! -?$##m####Y' |
  23. | 38 | ! !! "Y##Y"- |
  24. | 40 | ! |
  25. +----+------------------------------------------------------+
  26. (21 rows)

Generate an HAProxy config file

HAProxy is one of the most popular open-source TCP load balancers, and CockroachDB includes a built-in command for generating a configuration file that is preset to work with your running cluster.

To generate an HAProxy config file for an entire secure cluster, run the cockroach gen haproxy command, specifying the location of certificate directory and the address of any instance running a CockroachDB node:

  1. $ cockroach gen haproxy \
  2. --certs-dir=<path to certs directory> \
  3. --host=<address of any node in the cluster>

To limit the HAProxy config file to nodes matching specific "localities", use the —localities flag, specifying the explicit locality tier(s) or a regular expression to match against:

  1. $ cockroach gen haproxy \
  2. --certs-dir=<path to certs directory> \
  3. --host=<address of any node in the cluster>
  4. --locality=region=us.*

To generate an HAProxy config file for an entire insecure cluster, run the cockroach gen haproxy command, specifying the address of any instance running a CockroachDB node:

  1. $ cockroach gen haproxy \
  2. --insecure \
  3. --host=<address of any node in the cluster>

To limit the HAProxy config file to nodes matching specific "localities", use the —localities flag, specifying the explicit locality tier(s) or a regular expression to match against:

  1. $ cockroach gen haproxy \
  2. --insecure \
  3. --host=<address of any node in the cluster>
  4. --locality=region=us.*

By default, the generated configuration file is called haproxy.cfg and looks as follows, with the server addresses pre-populated correctly:

  1. global
  2. maxconn 4096
  3. defaults
  4. mode tcp
  5. # Timeout values should be configured for your specific use.
  6. # See: https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#4-timeout%20connect
  7. timeout connect 10s
  8. timeout client 1m
  9. timeout server 1m
  10. # TCP keep-alive on client side. Server already enables them.
  11. option clitcpka
  12. listen psql
  13. bind :26257
  14. mode tcp
  15. balance roundrobin
  16. option httpchk GET /health?ready=1
  17. server cockroach1 <node1 address>:26257 check port 8080
  18. server cockroach2 <node2 address>:26257 check port 8080
  19. server cockroach3 <node3 address>:26257 check port 8080

The file is preset with the minimal configurations needed to work with your running cluster:

FieldDescription
timeout connecttimeout clienttimeout serverTimeout values that should be suitable for most deployments.
bindThe port that HAProxy listens on. This is the port clients will connect to and thus needs to be allowed by your network configuration.This tutorial assumes HAProxy is running on a separate machine from CockroachDB nodes. If you run HAProxy on the same machine as a node (not recommended), you'll need to change this port, as 26257 is likely already being used by the CockroachDB node.
balanceThe balancing algorithm. This is set to roundrobin to ensure that connections get rotated amongst nodes (connection 1 on node 1, connection 2 on node 2, etc.). Check the HAProxy Configuration Manual for details about this and other balancing algorithms.
option httpchkThe HTTP endpoint that HAProxy uses to check node health. /health?ready=1 ensures that HAProxy doesn't direct traffic to nodes that are live but not ready to receive requests.
serverFor each included node, this field specifies the address the node advertises to other nodes in the cluster, i.e., the addressed pass in the —advertise-addr flag on node startup. Make sure hostnames are resolvable and IP addresses are routable from HAProxy.

Note:

For full details on these and other configuration settings, see the HAProxy Configuration Manual.

See also

Was this page helpful?
YesNo