Centos7 installation etcd

Download and unzip the installation package from Github

wget https://github.com/coreos/etcd/releases/download/v3.4.10/etcd-v3.4.10-linux-amd64.tar.gz
tar xzvf etcd-v3.4.10-linux-amd64.tar.gz
mv etcd-v3.4.10-linux-amd64 /opt/etcd

After decompression are some documents and two binary files etcd and etcdctl. etcd is the server side, etcdctl is the client side.

In the test environment, to start a single-node etcd service, just run the etcd command.

./etcd

Configuration information

In order to run etcd using system commands, copy the binary files in the folder to bin.

cp /opt/etcd/etcd* /usr/local/bin/

Set etcd configuration file

mkdir -p /var/lib/etcd/
mkdir -p /opt/etcd/config/
chmod 700 /var/lib/etcd #Be careful to modify permissions, otherwise it will not start

Create etcd configuration file

cat <<EOF | sudo tee /opt/etcd/config/etcd.conf
#Node name
ETCD_NAME=$(hostname -s)
#Data storage location
ETCD_DATA_DIR=/var/lib/etcd
EOF

Create systemd configuration file

cat <<EOF | sudo tee /etc/systemd/system/etcd.service[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target[Service]
User=root
Type=notify
EnvironmentFile=-/opt/etcd/config/etcd.conf
ExecStart=/opt/etcd/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000[Install]
WantedBy=multi-user.target
EOF

Start etcd

systemctl daemon-reload & amp; & amp; systemctl enable etcd & amp; & amp; systemctl start etcd

Basic operations

etcdctl -h
NAME:etcdctl - A simple command line client for etcd3.USAGE:etcdctl [flags]VERSION:3.4.10API VERSION:3.4COMMANDS:alarm disarm Disarms all alarmsalarm list Lists all alarmsauth disable Disables authenticationauth enable Enables authenticationcheck datascale Check the memory usage of holding data for different workloads on a given server endpoint.check perf Check the performance of the etcd clustercompaction Compacts the event history in etcddefrag Defragments the storage of the etcd members with given endpointsdel Removes the specified key or range of keys [key, range_end)elect Observes and participates in leader electionendpoint hashkv Prints the KV history hash for each endpoint in --endpointsendpoint health Checks the healthiness of endpoints specified in `--endpoints` flagendpoint status Prints out the status of endpoints specified in `--endpoints` flagget Gets the key or a range of keyshelp Help about any commandlease grant Creates leaseslease keep-alive Keeps leases alive (renew)lease list List all active leaseslease revoke Revokes leaseslease timetolive Get lease informationlock Acquires a named lockmake-mirror Makes a mirror at the destination etcd clustermember add Adds a member into the clustermember list Lists all members in the clustermember promote Promotes a non-voting member in the clustermember remove Removes a member from the clustermember update Updates a member in the clustermigrate Migrates keys in a v2 store to a mvcc storemove-leader Transfers leadership to another etcd cluster member.put Puts the given key into the storerole add Adds a new rolerole delete Deletes a rolerole get Gets detailed information of a rolerole grant-permission Grants a key to a rolerole list Lists all rolesrole revoke-permission Revokes a key from a rolesnapshot restore Restores an etcd member snapshot to an etcd directorysnapshot save Stores an etcd node backend snapshot to a given filesnapshot status Gets backend snapshot status of a given filetxn Txn processes all the requests in one transactionuser add Adds a new useruser delete Deletes a useruser get Gets detailed information of a useruser grant-role Grants a role to a useruser list Lists all usersuser passwd Changes password of useruser revoke-role Revokes a role from a userversion Prints the version of etcdctlwatch Watches events stream on keys or prefixesOPTIONS:--cacert= "" verify certificates of TLS-enabled secure servers using this CA bundle--cert="" identify secure client using this TLS certificate file--command-timeout=5s timeout for short running command (excluding dial timeout)--debug[= false] enable client-side debug logging--dial-timeout=2s dial timeout for client connections-d, --discovery-srv="" domain name to query for SRV records describing cluster endpoints--discovery-srv-name=" " service name to query when using DNS discovery--endpoints=[127.0.0.1:2379] gRPC endpoints-h, --help[=false] help for etcdctl--hex[=false] print byte strings as hex encoded strings- -insecure-discovery[=true] accept insecure SRV records describing cluster endpoints--insecure-skip-tls-verify[=false] skip server certificate verification (CAUTION: this option should be enabled only for testing purposes)--insecure-transport [=true] disable transport security for client connections--keepalive-time=2s keepalive time for client connections--keepalive-timeout=6s keepalive timeout for client connections--key="" identify secure client using this TLS key file-- password="" password for authentication (if this option is used, --user option shouldn't include password)--user="" username[:password] for authentication (prompt if password is not supplied)-w, -- write-out="simple" set the output format (fields, json, protobuf, simple, table)

Enter, view, update, delete k-v:

[root@localhost ~] etcdctl put /testkey "Hello world"
OK
[root@localhost ~] etcdctl get /testkey "Hello world"
/testkey
Hello world
[root@localhost ~] etcdctl put /testkey "Hello"
OK
[root@localhost ~] etcdctl get /testkey "Hello"
/testkey
Hello
[root@localhost ~] etcdctl del /testkey
1

Get json output:

[root@localhost ~] etcdctl get key1 -w json
{"header":{"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":6,"raft_term":2},"kvs":[{"key":"a2V5MQ==","create_revision" :5,"mod_revision":5,"version":1,"value":"dmFsdWUx"}],"count":1}
[root@localhost ~] etcdctl get key1
key1
value1
[root@localhost ~] echo dmFsdWUx|base64 -d
value1[root@localhost ~]#

watch operation: run in a terminal:

[root@localhost etcd] etcdctl watch key1
PUT
key1
valuex
PUT
key1
valuez

In another terminal:

[root@localhost ~] etcdctl put key1 valuex
OK
[root@localhost ~] etcdctl put key1 valuez
OK

Lease
lease. etcd supports applying for timers. Applying for a lease will return a lease ID identifying the timer. If you put a key and carry the lease ID, an automatically expired key will be implemented. In etcd, a lease can be associated with any number of keys. When the lease expires, all associated keys will be automatically deleted.

#Generate
[root@localhost etcd] etcdctl lease grant 300
lease 694d73749a9d0515 granted with TTL(300s)
#Associated to key
[root@localhost etcd] etcdctl put key3 300 --lease=694d73749a9d0515
OK
#maintainlease
[root@localhost etcd] etcdctl lease keep-alive 694d73749a9d0515
lease 694d73749a9d0515 keepalived with TTL(300)
#revokelease
[root@localhost ~] etcdctl lease revoke 694d73749a9d0515
lease 694d73749a9d0515 revoked

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 16743 people are learning the system