### Put keys and get by prefix example Source: https://etcd.io/docs/v3.8/tasks/developer/how-to-get-key-by-prefix Shows a complete example: put three keys (web1, web2, web3) and then get all keys with the prefix 'web' using the --prefix option. ```shell etcdctl --endpoints=$ENDPOINTS put web1 value1 etcdctl --endpoints=$ENDPOINTS put web2 value2 etcdctl --endpoints=$ENDPOINTS put web3 value3 etcdctl --endpoints=$ENDPOINTS get web --prefix ``` -------------------------------- ### Set up authentication with etcdctl (role and user management) Source: https://etcd.io/docs/v3.8/demo Commands to configure authentication in etcd. Starts by setting the API version and endpoints, then creates a root role and user, grants permissions, and enables auth. After enabling, all client requests require authentication. Includes examples of authenticated and unauthenticated requests, showing permission denied when no user is provided. ```shell export ETCDCTL_API=3 ENDPOINTS=localhost:2379 etcdctl --endpoints=${ENDPOINTS} role add root etcdctl --endpoints=${ENDPOINTS} role get root etcdctl --endpoints=${ENDPOINTS} user add root etcdctl --endpoints=${ENDPOINTS} user grant-role root root etcdctl --endpoints=${ENDPOINTS} user get root etcdctl --endpoints=${ENDPOINTS} role add role0 etcdctl --endpoints=${ENDPOINTS} role grant-permission role0 readwrite foo etcdctl --endpoints=${ENDPOINTS} user add user0 etcdctl --endpoints=${ENDPOINTS} user grant-role user0 role0 etcdctl --endpoints=${ENDPOINTS} auth enable # now all client requests go through auth etcdctl --endpoints=${ENDPOINTS} --user=user0:123 put foo bar etcdctl --endpoints=${ENDPOINTS} get foo # permission denied, user name is empty because the request does not issue an authentication request etcdctl --endpoints=${ENDPOINTS} --user=user0:123 get foo # user0 can read the key foo etcdctl --endpoints=${ENDPOINTS} --user=user0:123 get foo1 ``` -------------------------------- ### Initial keys with revisions for past version examples Source: https://etcd.io/docs/v3.8/dev-guide/interacting_v3 Shows the key-value pairs and their revisions used in the past version examples. The keys foo and foo1 have been modified, with revisions 2 through 5. ```text foo = bar # revision = 2 foo1 = bar1 # revision = 3 foo = bar_new # revision = 4 foo1 = bar1_new # revision = 5 ``` -------------------------------- ### Start etcd gateway with DNS SRV discovery Source: https://etcd.io/docs/v3.8/op-guide/gateway Starts the etcd gateway using DNS SRV discovery. First, the SRV records for _etcd-client._tcp.example.com are queried, then the A records for the hostnames are resolved, and finally the gateway is started with --discovery-srv=example.com. The output confirms the gateway is ready to proxy client requests. ```shell $ dig +noall +answer SRV _etcd-client._tcp.example.com _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra0.example.com. _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra1.example.com. _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra2.example.com. ``` ```shell $ dig +noall +answer infra0.example.com infra1.example.com infra2.example.com infra0.example.com. 300 IN A 10.0.1.10 infra1.example.com. 300 IN A 10.0.1.11 infra2.example.com. 300 IN A 10.0.1.12 ``` ```shell $ etcd gateway start --discovery-srv=example.com 2016-08-16 11:21:18.867350 I | tcpproxy: ready to proxy client requests to [...] ``` -------------------------------- ### Install Prometheus 2.0.0 Source: https://etcd.io/docs/v3.8/op-guide/monitoring Downloads and extracts Prometheus version 2.0.0, then verifies the installation by running the version command. ```bash PROMETHEUS_VERSION="2.0.0" wget https://github.com/prometheus/prometheus/releases/download/v$PROMETHEUS_VERSION/prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz -O /tmp/prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz tar -xvzf /tmp/prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz --directory /tmp/ --strip-components=1 /tmp/prometheus -version ``` -------------------------------- ### Verify etcd Installation with etcd --version Source: https://etcd.io/docs/v3.8/install This command verifies that the etcd binary is in the system path and displays its version. The expected output starts with 'etcd Version: ain' indicating the version number is shown. ```shell $ etcd --version etcd Version: ain ... ``` -------------------------------- ### Start Prometheus with etcd configuration Source: https://etcd.io/docs/v3.8/op-guide/monitoring Starts Prometheus in the background with the specified configuration, listening on port 9090, and logs output to /tmp/test-etcd.log. ```bash nohup /tmp/prometheus \ -config.file /tmp/test-etcd.yaml \ -web.listen-address ":9090" \ -storage.local.path "test-etcd.data" >> /tmp/test-etcd.log 2>&1 & ``` -------------------------------- ### Start etcd members with restored data directories Source: https://etcd.io/docs/v3.8/op-guide/recovery Starts three etcd members (m1, m2, m3) using the restored data directories. Each member is launched in the background with its own client and peer URLs. After starting, the restored cluster should be available and serving the keyspace from the snapshot. ```shell $ etcd \ --name m1 \ --data-dir m1_data_dir.etcd \ --listen-client-urls http://host1:2379 \ --advertise-client-urls http://host1:2379 \ --listen-peer-urls http://host1:2380 & $ etcd \ --name m2 \ --data-dir m2_data_dir.etcd \ --listen-client-urls http://host2:2379 \ --advertise-client-urls http://host2:2379 \ --listen-peer-urls http://host2:2380 & $ etcd \ --name m3 \ --data-dir m3_data_dir.etcd \ --listen-client-urls http://host3:2379 \ --advertise-client-urls http://host3:2379 \ --listen-peer-urls http://host3:2380 & ``` -------------------------------- ### Initial keys for etcdctl read examples Source: https://etcd.io/docs/v3.8/dev-guide/interacting_v3 Shows the initial key-value pairs used in the read examples. The keys are foo, foo1, foo2, and foo3 with corresponding values bar, bar1, bar2, and bar3. ```text foo = bar foo1 = bar1 foo2 = bar2 foo3 = bar3 ``` -------------------------------- ### Add member with etcdctl and start new etcd process Source: https://etcd.io/docs/v3.8/op-guide/runtime-configuration Adds a new member by name and peer URL, then starts the new etcd process with the printed environment variables and flags. The new member immediately begins catching up. Adding multiple members should be done one at a time, verifying each starts correctly. In a 1-node cluster, progress halts until the new member connects. ```shell $ etcdctl member add infra3 --peer-urls=http://10.0.1.13:2380 added 9bf1b35fc7761a23 to cluster ETCD_NAME="infra3" ETCD_INITIAL_CLUSTER="infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380,infra3=http://10.0.1.13:2380" ETCD_INITIAL_CLUSTER_STATE=existing ``` ```shell $ export ETCD_NAME="infra3" $ export ETCD_INITIAL_CLUSTER="infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380,infra3=http://10.0.1.13:2380" $ export ETCD_INITIAL_CLUSTER_STATE=existing $ etcd --listen-client-urls http://10.0.1.13:2379 --advertise-client-urls http://10.0.1.13:2379 --listen-peer-urls http://10.0.1.13:2380 --initial-advertise-peer-urls http://10.0.1.13:2380 --data-dir %data_dir% ``` -------------------------------- ### Install goreman Source: https://etcd.io/docs/v3.8/dev-guide/local_cluster Install goreman to control Procfile-based applications. This is required to start a multi-member cluster using the provided Procfile. ```bash $ go install github.com/mattn/goreman@latest ``` -------------------------------- ### Example: etcdctl endpoint hashkv with table output Source: https://etcd.io/docs/v3.8/tasks/operator/how-to-check-cluster-status Example of checking KV hash with table output. Shows the hash and hash revision for each endpoint in the cluster. ```shell etcdctl --cluster endpoint hashkv --write-out=table +------------------+------------+---------------+ | ENDPOINT | HASH | HASH REVISION | +------------------+------------+---------------+ | 10.240.0.17:2379 | 3892279174 | 3 | | 10.240.0.18:2379 | 3892279174 | 3 | | 10.240.0.19:2379 | 3892279174 | 3 | +------------------+------------+---------------+ ``` -------------------------------- ### Start etcd gateway with static endpoints Source: https://etcd.io/docs/v3.8/op-guide/gateway Starts the etcd gateway with static endpoints. The command lists three endpoints separated by commas, and the output confirms the gateway is ready to proxy client requests. ```shell $ etcd gateway start --endpoints=infra0.example.com:2379,infra1.example.com:2379,infra2.example.com:2379 2016-08-16 11:21:18.867350 I | tcpproxy: ready to proxy client requests to [...] ``` -------------------------------- ### Custom etcd discovery service: create size key and start members Source: https://etcd.io/docs/v3.8/op-guide/clustering Set up a custom etcd discovery service by creating a size key with curl, then start two etcd members using the --discovery flag. Each member must have a unique --name flag or discovery fails due to duplicated names. ```shell $ curl -X PUT https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size -d value=3 ``` ```shell $ etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.10:2380 \ --listen-peer-urls http://10.0.1.10:2380 \ --listen-client-urls http://10.0.1.10:2379,http://127.0.0.1:2379 \ --advertise-client-urls http://10.0.1.10:2379 \ --discovery https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83 ``` ```shell $ etcd --name infra2 --initial-advertise-peer-urls http://10.0.1.12:2380 \ --listen-peer-urls http://10.0.1.12:2380 \ --listen-client-urls http://10.0.1.12:2379,http://127.0.0.1:2379 \ --advertise-client-urls http://10.0.1.12:2379 \ --discovery https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83 ``` -------------------------------- ### Start a multi-member cluster with goreman Source: https://etcd.io/docs/v3.8/dev-guide/local_cluster Start a multi-member cluster using goreman with etcd's stock Procfile. Members listen on localhost:2379, localhost:22379, and localhost:32379 for client requests. ```bash $ goreman -f Procfile start ``` -------------------------------- ### Install and run etcd-dump-logs Source: https://etcd.io/docs/v3.8/learning/persistent-storage-files Install and run the etcd-dump-logs tool to read WAL logs. The tool prints only Entries, applies overrides to show final values, and prints uncommitted entries without HardState.commitIndex information. ```shell % go install go.etcd.io/etcd/v3/tools/etcd-dump-logs@latest % go run go.etcd.io/etcd/v3/tools/etcd-dump-logs --start-index=0 aname.etcd ``` -------------------------------- ### Start a three-node etcd cluster with initial-cluster-state new Source: https://etcd.io/docs/v3.8/tasks/operator/how-to-deal-with-membership Initializes a three-node etcd cluster with a shared token and cluster state. Each node is started with the same peer and client URLs, and the initial cluster configuration is passed via environment variables. ```shell # For each machine TOKEN=my-etcd-token-1 CLUSTER_STATE=new NAME_1=etcd-node-1 NAME_2=etcd-node-2 NAME_3=etcd-node-3 HOST_1=10.240.0.13 HOST_2=10.240.0.14 HOST_3=10.240.0.15 CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380 # For node 1 THIS_NAME=${NAME_1} THIS_IP=${HOST_1} etcd --data-dir=data.etcd --name ${THIS_NAME} \ --initial-advertise-peer-urls http://${THIS_IP}:2380 \ --listen-peer-urls http://${THIS_IP}:2380 \ --advertise-client-urls http://${THIS_IP}:2379 \ --listen-client-urls http://${THIS_IP}:2379 \ --initial-cluster ${CLUSTER} \ --initial-cluster-state ${CLUSTER_STATE} \ --initial-cluster-token ${TOKEN} # For node 2 THIS_NAME=${NAME_2} THIS_IP=${HOST_2} etcd --data-dir=data.etcd --name ${THIS_NAME} \ --initial-advertise-peer-urls http://${THIS_IP}:2380 \ --listen-peer-urls http://${THIS_IP}:2380 \ --advertise-client-urls http://${THIS_IP}:2379 \ --listen-client-urls http://${THIS_IP}:2379 \ --initial-cluster ${CLUSTER} \ --initial-cluster-state ${CLUSTER_STATE} \ --initial-cluster-token ${TOKEN} # For node 3 THIS_NAME=${NAME_3} THIS_IP=${HOST_3} etcd --data-dir=data.etcd --name ${THIS_NAME} \ --initial-advertise-peer-urls http://${THIS_IP}:2380 \ --listen-peer-urls http://${THIS_IP}:2380 \ --advertise-client-urls http://${THIS_IP}:2379 \ --listen-client-urls http://${THIS_IP}:2379 \ --initial-cluster ${CLUSTER} \ --initial-cluster-state ${CLUSTER_STATE} \ --initial-cluster-token ${TOKEN} ``` -------------------------------- ### Removed embed.Config.SetupLogging Source: https://etcd.io/docs/v3.8/upgrades/upgrade_3_4 Shows that `embed.Config.SetupLogging` has been removed. The example shows the old call being deleted; logging is now set up automatically. ```go import "github.com/coreos/etcd/embed" cfg := &embed.Config{Debug: false} -cfg.SetupLogging() ``` -------------------------------- ### Start etcd members infra1 and infra2 with static cluster flags Source: https://etcd.io/docs/v3.8/op-guide/clustering Start etcd on the second and third machines of a static cluster. Each command sets the member's name, peer and client URLs, and the shared initial-cluster token and state. The initial-cluster parameters are ignored on subsequent runs, so they can be removed after bootstrap. ```bash $ etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.11:2380 \ --listen-peer-urls http://10.0.1.11:2380 \ --listen-client-urls http://10.0.1.11:2379,http://127.0.0.1:2379 \ --advertise-client-urls http://10.0.1.11:2379 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380 \ --initial-cluster-state new ``` ```bash $ etcd --name infra2 --initial-advertise-peer-urls http://10.0.1.12:2380 \ --listen-peer-urls http://10.0.1.12:2380 \ --listen-client-urls http://10.0.1.12:2379,http://127.0.0.1:2379 \ --advertise-client-urls http://10.0.1.12:2379 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380 \ --initial-cluster-state new ``` -------------------------------- ### Start gRPC proxy with --namespace and verify prefixed access Source: https://etcd.io/docs/v3.8/op-guide/grpc_proxy Start a gRPC proxy with the --namespace flag to partition the keyspace. The second block shows that client requests are transparently prefixed on the etcd cluster and responses strip the prefix. ```shell $ etcd grpc-proxy start --endpoints=localhost:2379 \ --listen-addr=127.0.0.1:23790 \ --namespace=my-prefix/ ``` ```shell $ ETCDCTL_API=3 etcdctl --endpoints=localhost:23790 put my-key abc # OK $ ETCDCTL_API=3 etcdctl --endpoints=localhost:23790 get my-key # my-key # abc $ ETCDCTL_API=3 etcdctl --endpoints=localhost:2379 get my-prefix/my-key # my-prefix/my-key # abc ``` -------------------------------- ### Install etcd using Homebrew on Linux Source: https://etcd.io/docs/v3.8/install Installs etcd with Homebrew on Linux, providing recent software versions. This is a recommended alternative to using official Linux repositories, which may be outdated. Includes prerequisite and result verification steps. ```shell $ brew update ``` ```shell $ brew install etcd ``` ```shell $ etcd --version etcd Version: ain ... ``` -------------------------------- ### etcdutl snapshot status example Source: https://etcd.io/docs/v3.8/tasks/operator/how-to-save-database Example of checking snapshot status with etcdutl. The command uses --write-out=table to display the snapshot details in a table format, showing the hash, revision, total keys, and total size. ```shell etcdutl --write-out=table snapshot status my.db +---------+----------+------------+------------+ | HASH | REVISION | TOTAL KEYS | TOTAL SIZE | +---------+----------+------------+------------+ | c55e8b8 | 9 | 13 | 25 kB | +---------+----------+------------+------------+ ``` -------------------------------- ### Start etcd with client certificate authentication Source: https://etcd.io/docs/v3.8/op-guide/security Starts etcd with client certificate authentication enabled. Requires a CA certificate, server key pair, and a client key pair signed by the same CA. The server will reject requests from clients without a valid certificate. ```shell $ etcd --name infra0 --data-dir infra0 \ --client-cert-auth --trusted-ca-file=/path/to/ca.crt --cert-file=/path/to/server.crt --key-file=/path/to/server.key \ --advertise-client-urls https://127.0.0.1:2379 --listen-client-urls https://127.0.0.1:2379 ``` -------------------------------- ### Start a standalone etcd cluster Source: https://etcd.io/docs/v3.8/dev-guide/local_cluster Run ./etcd to start a standalone etcd cluster. The etcd binary may be located at $GOPATH/bin/etcd or /usr/local/bin/etcd if not in the current directory. The member listens on localhost:2379 for client requests. ```bash $ ./etcd ... ``` -------------------------------- ### etcdctl snapshot save example Source: https://etcd.io/docs/v3.8/tasks/operator/how-to-save-database Example of saving a snapshot with etcdctl. The command sets the ENDPOINTS variable and saves the snapshot to my.db, producing the output "Snapshot saved at my.db". ```shell ENDPOINTS=$HOST_1:2379 etcdctl --endpoints=$ENDPOINTS snapshot save my.db Snapshot saved at my.db ``` -------------------------------- ### Start new etcd member with --initial-cluster-state existing Source: https://etcd.io/docs/v3.8/tasks/operator/how-to-deal-with-membership Starts the newly added member (node 4) with --initial-cluster-state existing. Warns to remove the old member's data directory if reusing the same disk space. ```shell # [WARNING] If the new member starts from the same disk space, # make sure to remove the data directory of the old member # # restart with 'existing' flag TOKEN=my-etcd-token-1 CLUSTER_STATE=existing NAME_1=etcd-node-1 NAME_2=etcd-node-2 NAME_4=etcd-node-4 HOST_1=10.240.0.13 HOST_2=10.240.0.14 HOST_4=10.240.0.16 # new member CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_4}=http://${HOST_4}:2380 THIS_NAME=${NAME_4} THIS_IP=${HOST_4} etcd --data-dir=data.etcd --name ${THIS_NAME} \ --initial-advertise-peer-urls http://${THIS_IP}:2380 \ --listen-peer-urls http://${THIS_IP}:2380 \ --advertise-client-urls http://${THIS_IP}:2379 \ --listen-client-urls http://${THIS_IP}:2379 \ --initial-cluster ${CLUSTER} \ --initial-cluster-state ${CLUSTER_STATE} \ --initial-cluster-token ${TOKEN} ``` -------------------------------- ### Enable v2 API in etcd 3.4 Source: https://etcd.io/docs/v3.8/upgrades/upgrade_3_4 To serve v2 API requests, start etcd with --enable-v2=true. ```bash -etcd +etcd --enable-v2=true ``` -------------------------------- ### Example: etcdctl endpoint status with table output Source: https://etcd.io/docs/v3.8/tasks/operator/how-to-check-cluster-status Example of checking overall status with table output. Shows endpoint details including ID, version, DB size, leader status, and raft information. ```shell etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status +------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | +------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | 10.240.0.17:2379 | 4917a7ab173fae7 | 3.5.0 | 45 kB | true | false | 4 | 16726 | 16726 | | | 10.240.0.18:2379 | 59796ba9cd1bcd72 | 3.5.0 | 45 kB | false | false | 4 | 16726 | 16726 | | | 10.240.0.19:2379 | 94df724b66343e6c | 3.5.0 | 45 kB | false | false | 4 | 16726 | 16726 | | +------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ ``` -------------------------------- ### Put and get keys with /v3/kv/put and /v3/kv/range Source: https://etcd.io/docs/v3.8/dev-guide/api_grpc_gateway Shows how to write and read keys using the gRPC gateway. The key and value fields must be base64 encoded (foo is 'Zm9v', bar is 'YmFy'). The first curl puts a key, the second ranges it, and the third gets all keys prefixed with 'foo' using range_end. ```bash </dev/null 2>&1 # {"result":{"header":{"cluster_id":"12585971608760269493","member_id":"13847567121247652255","revision":"2","raft_term":"2"},"events":[{"kv":{"key":"Zm9v","create_revision":"2","mod_revision":"2","version":"1","value":"YmFy"}}]}} ``` -------------------------------- ### clientv3.SetLogger with grpclog.LoggerV2 (Before/After) Source: https://etcd.io/docs/v3.8/upgrades/upgrade_3_3 Shows the change from grpclog.Logger to grpclog.LoggerV2 for clientv3.SetLogger. The old log.New does not implement the new interface. ```go import "github.com/coreos/etcd/clientv3" clientv3.SetLogger(log.New(os.Stderr, "grpc: ", 0)) ``` ```go import "github.com/coreos/etcd/clientv3" import "google.golang.org/grpc/grpclog" clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr)) // log.New above cannot be used (not implement grpclog.LoggerV2 interface) ``` -------------------------------- ### Start etcd gRPC proxy without resolver prefix Source: https://etcd.io/docs/v3.8/op-guide/grpc_proxy Starts a proxy without a resolver prefix. The member list API returns its own advertise-client-url as shown in the following output. ```shell $ etcd grpc-proxy start --endpoints=localhost:2379 \ --listen-addr=127.0.0.1:23792 \ --advertise-client-url=127.0.0.1:23792 ``` -------------------------------- ### Get keys by prefix command Source: https://etcd.io/docs/v3.8/tasks/developer/how-to-get-key-by-prefix Basic etcdctl command to get all keys with a given prefix. Requires the --prefix option. Uses the $ENDPOINTS environment variable for gRPC endpoints. ```shell $ etcdctl --endpoints=$ENDPOINTS get PREFIX --prefix ```