当前位置:网站首页>Introduction: from the origin of etcd name to installation, deployment and usage posture
Introduction: from the origin of etcd name to installation, deployment and usage posture
2022-06-10 11:42:00 【Li XXXI】
Hello everyone , I am a Thirty-one [0], This time, we share the distributed artifact etcd[1] Of 5 Deployment methods and 10 Three use postures , Estimated cost of reading the full text 16 minute . If it helps you , You are also welcome to like the collection .
I'm learning etcd Before , Let's talk about it first etcd The origin of the name [2].etcd Medium etc Taken from the unix Systematic /etc Catalog , Add one more d representative distributed system It makes up etcd. stay unix In the system /etc The directory is used to store the configuration data of the system , Just by name etcd It can be used to store configuration data of distributed systems , Sometimes I put etcd It is simply understood as distributed /etc The configuration directory .
etcd brief introduction
etcd It's a reliable distributed key-value The storage system , It is mainly used for Configure sharing and Service registration and discovery , It has the following characteristics :
Simple : be based on gRPC Clearly defined 、 User oriented API. Security : Support for optional clients TLS Automatic certificate authentication feature . Fast : Support per second 10000 Second write . reliable : be based on Raft The algorithm protocol guarantees consistency .
etcd Use Go Language development , Bottom based Raft The consensus algorithm manages highly available replication logs . It has been used by many companies for key production projects , such as :Kubernetes、locksmith、vulcand、Doorman etc. .
Of course , There are also other components that can provide the functions of configuration sharing and service registration and discovery , For example, the most extensive and well-known Zookeeper, And a lot of Java Recognized and used by well-known open source projects of the Department , such as :Hadoop、HBase、Kafka etc. .
but etcd Is the only one that can rival or even surpass Zookeeper The components of .
By contrast ,Zookeeper It has the following disadvantages [3]:
complex :Zookeeper be based on ZAB agreement , Belong to category Paxos agreement , and Paxos Algorithms are known for their complexity ;Zookeeper The use of is also complex , The client needs to be installed , At present, the official only provides Java and C Two language interface . Slow development : Due to the huge structure and loose management of the foundation , This leads to the slow development of the project .
and etcd As a rising star , Its advantages are obvious :
Simple : Use Go The language is easy to write and deploy ; Use gRPC Defining interfaces , Support for cross language 、 Cross-platform feature ; The user-friendly Raft The algorithm guarantees consistency , be better than Paxos Algorithm . Fast development :etcd In high-speed iterative development . Superior performance : Official benchmark data ,etcd Clusters can support... Per second 10000+ Second write , Better performance than the Zookeeper. Security :etcd Support TLS visit , and ZooKeeper It is slightly rough in terms of permission control .
Environment building
One 、 Local installation package deployment
「 download 」: Download the latest installation package ( Current latest :v3.5.4), Download address :https://github.com/etcd-io/etcd/releases/

「 install 」: In the unzipped file directory etcd、etcdctl These are the compiled execution files of the installation package and the client , There are three ways to run the configuration .
Method 1 : Unzip the directory and run directly Method 2 : hold etcd、etcdctlFile copy toGOBINUnder the table of contents .Method 3 : Add... To the environment variable etcd、etcdctlDirectory of files .
notes : In operation , Permission issues may be involved , Authorization is enough .
「 verification 」:
# verification etcd edition
$ etcd --version
etcd Version: 3.5.4
Git SHA: 08407ff76
Go Version: go1.16.15
Go OS/Arch: darwin/amd64
# verification etcdctl edition
$ etcdctl version
etcdctl version: 3.5.4
API version: 3.5
Two 、 Compile and deploy locally
「 download 」: Use the following command to clone the code
# Download the latest version
$ git clone https://github.com/etcd-io/etcd.git
# Download the specified version
$ git clone -b v3.5.4 https://github.com/etcd-io/etcd.git
「 Compilation and installation 」:
# compile
$ cd etcd
$ make build
# install
$ export PATH="$PATH:`pwd`/bin"
「 verification 」:
# verification etcd edition
$ etcd --version
# verification etcdctl edition
$ etcdctl version
# verification etcdutl edition
$ etcdutl version
3、 ... and 、 Local cluster deployment
Installation is required first goreman Components , It's based on Procfile Profile management etcd Application process .
$ go install github.com/mattn/[email protected]
「 Start cluster 」: Source directory Procfile The script has built the local demo cluster , Just run and start directly
$ goreman start
「 verification 」
$ etcdctl member list
8211f1d0f64f3269, started, infra1, http://127.0.0.1:12380, http://127.0.0.1:2379, false
91bc3c398fb3c146, started, infra2, http://127.0.0.1:22380, http://127.0.0.1:22379, false
fd422379fda50e48, started, infra3, http://127.0.0.1:32380, http://127.0.0.1:32379, false
This script creates a script that contains 3 individual etcd Cluster of member nodes , Each cluster member receives read and write of key values . You can also follow Procfile.learner Script guidance , Learn how to add nodes to the cluster .
Four 、Docker Single deployment
Here through docker-compose Carry out the experimental configuration .
「 Mirror pull 」
$ docker pull bitnami/etcd:3.5.2
「 edit docker-compose.yml」
version: '3.5'
services:
etcd:
container_name: builder-etcd
image: bitnami/etcd:3.5.2
ports:
- 2379:2379
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1002
- ETCD_QUOTA_BACKEND_BYTES=4294967296
volumes:
- ${DOCKER_ROOT_DIR:-.}/volumes/etcd/data:/bitnami/etcd
networks:
default:
name: builder_dev
「 Start the service 」
$ docker-compose -f docker-compose.yml up
「 verification 」: Verify the version of the cluster node
$ docker exec -it builder-etcd /bin/bash -c "etcd --version"
etcd Version: 3.5.2
Git SHA: 99018a77b
Go Version: go1.16.3
Go OS/Arch: linux/amd64
5、 ... and 、Docker Cluster deployment
「 edit docker-compose.yml」
version: '3.5'
services:
etcd1:
container_name: builder-etcd1
image: bitnami/etcd:3.5.2
ports:
- 12379:2379
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1002
- ETCD_QUOTA_BACKEND_BYTES=4294967296
- ETCD_NAME=etcd1
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd1:2380
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd1:2379
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_INITIAL_CLUSTER_STATE=new
volumes:
- ${DOCKER_ROOT_DIR:-.}/volumes/etcd/data1:/bitnami/etcd
etcd2:
container_name: builder-etcd2
image: bitnami/etcd:3.5.2
ports:
- 22379:2379
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1002
- ETCD_QUOTA_BACKEND_BYTES=4294967296
- ETCD_NAME=etcd2
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd2:2380
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd2:2379
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_INITIAL_CLUSTER_STATE=new
volumes:
- ${DOCKER_ROOT_DIR:-.}/volumes/etcd/data2:/bitnami/etcd
etcd3:
container_name: builder-etcd3
image: bitnami/etcd:3.5.2
ports:
- 32379:2379
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1002
- ETCD_QUOTA_BACKEND_BYTES=4294967296
- ETCD_NAME=etcd3
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd3:2380
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd3:2379
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_INITIAL_CLUSTER_STATE=new
volumes:
- ${DOCKER_ROOT_DIR:-.}/volumes/etcd/data3:/bitnami/etcd
networks:
default:
name: builder_dev
Deployment profile docker-compose.yml details , See etcd docker-compose yml[4]
「 Start the service 」
$ docker-compose -f docker-compose.yml up
「 verification 」: Verify the version of the cluster node
$ docker exec -it builder-etcd1 /bin/bash -c "etcd --version"
$ docker exec -it builder-etcd2 /bin/bash -c "etcd --version"
$ docker exec -it builder-etcd3 /bin/bash -c "etcd --version"
# Output
etcd Version: 3.5.2
Git SHA: 99018a77b
Go Version: go1.16.3
Go OS/Arch: linux/amd64
API Study
here , By using etcdctl[5] Conduct API Learning verification ,etcdctl It's one used to communicate with etcd Command line tools for server interaction .
「1. View version 」
$ etcdctl version
etcdctl version: 3.6.0-alpha.0
API version: 3.6
「2. write in key」
$ etcdctl put foo bar
OK
「3. Read key」
$ etcdctl get foo
foo
bar
# Just get value
$ etcdctl get foo --print-value-only
bar
「4. Batch values 」
$ etcdctl put foo1 bar1
$ etcdctl put foo3 bar2
$ etcdctl put foo3 bar3
# Get from foo To foo3 Value , barring foo3
$ etcdctl get foo foo3 --print-value-only
bar
bar1
bar2
# Get the prefix foo Value
$ etcdctl get --prefix foo --print-value-only
bar
bar1
bar2
bar3
# Get the first two values that match the prefix
$ etcdctl get --prefix --limit=2 foo --print-value-only
bar
bar1
「5. Delete key」
# Delete foo Value
$ etcdctl del foo
1
# Delete foo To foo2 And it doesn't include foo2 Value
$ etcdctl del foo foo2
1
# Delete prefix foo All values
$ etcdctl del --prefix foo
2
「6. monitor 」
# monitor foo Single key
$ etcdctl watch foo
# Another console performs : etcdctl put foo bar
PUT
foo
bar
# Monitor multiple values at the same time
$ etcdctl watch -i
$ watch foo
$ watch zoo
# Another console performs : etcdctl put foo bar
PUT
foo
bar
# Another console performs : etcdctl put zoo val
PUT
zoo
val
# monitor foo Prefix hit key
$ etcdctl watch --prefix foo
# Another console performs : etcdctl put foo1 bar1
PUT
foo1
bar1
# Another console performs : etcdctl put fooz1 barz1
PUT
fooz1
barz1
「7. Set up a lease 」 When one key When bound to a lease , Its life cycle is bound to the life cycle of the lease .
# Set up 10 Expiration time in seconds
$ etcdctl lease grant 10
lease 32698142c52a170a granted with TTL(10s)
# hold foo Bound to lease , Set to 10 Seconds after expired
$ etcdctl put --lease=32698142c52a170a foo bar
OK
$ etcdctl get foo
foo
bar
# 10 Seconds later , Can't get foo
$ etcdctl get foo
# Returns an empty
「8. Cancel the lease 」 Through the lease ID Cancel the lease , Revoking a lease will delete all its bound key.
$ etcdctl lease grant 10
lease 32698142c52a170c granted with TTL(10s)
$ etcdctl put --lease=32698142c52a170c foo bar
OK
# Cancel the lease
$ etcdctl lease revoke 32698142c52a170c
lease 32698142c52a170c revoked
$ etcdctl get foo
# Returns an empty
「9. The contract 」 By refreshing TTL Value to keep the lease valid , Keep it from expiring .
# Set up 10 The lease expires in seconds
$ etcdctl lease grant 10
lease 32698142c52a170f granted with TTL(10s)
# hold foo Bound to lease , Set to 10 Seconds after expired
$ etcdctl put foo bar --lease=32698142c52a170f
# Automatic scheduled renewal , After the successful renewal, each lease is 10 second
$ etcdctl lease keep-alive 32698142c52a170f
lease 32695410dcc0ca06 keepalived with TTL(10)
lease 32695410dcc0ca06 keepalived with TTL(10)
lease 32695410dcc0ca06 keepalived with TTL(10)
...
「10. View lease 」 View lease information , To renew the lease or to see if the lease still exists or has expired
# Set up 50 second TTL
$ etcdctl lease grant 50
lease 32698142c52a1711 granted with TTL(50s)
# zoo1 binding 32698142c52a1711 lease
$ etcdctl put --lease=32698142c52a1711 zoo1 val1
OK
# View lease ,remaining(32s) Remaining valid time 32 second ;--keys Get the lease bound key
$ etcdctl lease timetolive --keys 32698142c52a1711
lease 32698142c52a1711 granted with TTL(50s), remaining(32s), attached keys([zoo1])
notes : One lease supports binding multiple key
$ etcdctl lease grant 50
lease 32698142c52a1713 granted with TTL(50s)
$ etcdctl put --lease=32698142c52a1713 zoo1 val1
OK
$ etcdctl put --lease=32698142c52a1713 zoo2 val2
OK
$ etcdctl put --lease=32698142c52a1713 zoo3 val3
OK
After the lease expires , all key Values will be deleted , therefore :
When the lease is bound to only one key when , Want to delete this key, The best way is to cancel its lease , Instead of deleting this key. When the lease is not bound key when , It should be cancelled voluntarily , Simply delete key after , The renewal operation continues , Can cause memory leaks .
# Method 1 : Delete directly `key`
# Set the lease and bind zoo1
$ etcdctl lease grant 50
lease 32698142c52a1715 granted with TTL(50s)
$ etcdctl --lease=32698142c52a1715 put zoo1 val1
OK
# The contract
$ etcdctl lease keep-alive 32698142c52a1715
lease 32698142c52a1715 keepalived with TTL(50)
# Another console performs :etcdctl del zoo1
# Simply delete key after , The renewal operation continues , Can cause memory leaks
lease 32698142c52a1715 keepalived with TTL(50)
lease 32698142c52a1715 keepalived with TTL(50)
lease 32698142c52a1715 keepalived with TTL(50)
...
# Method 2 : revoke `key` Lease of
# Set the lease and bind zoo1
$ etcdctl lease grant 50
lease 32698142c52a1717 granted with TTL(50s)
$ etcdctl --lease=32698142c52a1717 put zoo1 val1
OK
# The contract
$ etcdctl lease keep-alive 32698142c52a1717
lease 32698142c52a1717 keepalived with TTL(50)
lease 32698142c52a1717 keepalived with TTL(50)
# Another console performs :etcdctl lease revoke 32698142c52a1717
# Renew, cancel and exit
lease 32698142c52a1717 expired or revoked.
$ etcdctl get zoo1
# Returns an empty
above , That's all for today , It almost includes etcd Various postures for installation and use , You are welcome to try .
️️️ Every reader's love is the driving force of the author's progress ! I'm thirty-one , Thank you for : Please thumb up 、 Ask for comment 、 Please forward , See you next time !
References
[0] Thirty-one : http://www.lee31.cn/assets/image/ThirtyOneLee.jpeg [1] etcd: https://github.com/etcd-io/etcd [2] etcd The origin of the name : https://etcd.io/docs/v3.5/faq/ [3] https://www.infoq.cn/article/etcd-interpretation-application-scenario-implement-principle/ [4] etcd docker-compose yml: https://github.com/liyaodev/docker-compose [5] etcdctl: https://etcd.io/docs/v3.5/dev-guide/interacting_v3/
The articles
How to see Python(PyScript) Can do browser front-end development language ? The depth of the summary , Take you around NVIDIA GPU Still living in the last era ,Etcd 3.0 Implementing distributed locks is so simple from 0 To 1, How to roll one with your bare hands Python Plug in system ? One stop machine learning business platform MLflow What about? ? Amateurs don't ask for people ,30 second AI Make fast LOGO
边栏推荐
猜你喜欢
随机推荐
Tmux使用
[PaperNote] Confidential Computing Direction
'getWidth()' is deprecated,'getHeight()' is deprecated
[WIP] Openstack Masakari (by quqi99)
SqlSessionFactory和SqlSession详解
Remote access to tensorboard
Linked list, stack, queue
Cvpr22 oral | Hong Kong Chinese proposed transrank: sequencing loss + self supervision =sota
Testing ovn manually based on LXD (by quqi99)
线性代数的本质6 逆矩阵、列空间与零空间
2022 CISCN 初赛pwn完整wp
如何编写产品营销策划方案
并发bug之源(一)-可见性
为你推荐一款高效的IO组件——okio
十二、进程地址空间(pmap;vdso;mmap)
flutter websocket示例
Transfomer components and pytoch
陆金所:对外技术输出赋能,助力金融企业实现数据库自主可控
[extensive reading of papers] distilling the knowledge in a neural network
两个BigDecimal数据类型比较、加减乘除、格式化









