In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the meaning of Etcd cluster, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Introduction to Etcd
Etcd is an open source, highly consistent distributed key-value storage system. It is realized by Go language and has good cross-platform. Mainly used for configuration sharing and service discovery. Through the raft algorithm to maintain the communication and data consistency of each node in the cluster, the relationship between nodes is peer-to-peer. Even if the leader node fails, a new leader will be elected quickly to ensure the normal operation of the system. At present, it has been widely used in kubernetes, ROOK, CoreDNS, M3, openstack and other fields.
Features: the interface is easy to operate and provides http+json and grpc interfaces. Optional ssl client authentication to support https access. Each instance supports 1000 QPS, which is suitable for storing data that is small but updated and accessed frequently. The data is stored in layers and persisted in the way of the file system. Monitor changes in specific keys or directories and respond to changes in values, applicable to the publication and subscription of messages.
Architecture and working principle of Etcd
Architecture
The architecture of Etcd is shown in the following figure and is mainly divided into four parts. HTTP server, Store, Raft and WAL.
HTTP server: the Api request provided to the user. Store: used to handle transactions for various functions supported by etcd, including data indexing, node state changes, monitoring and feedback, event handling and execution, and so on. Raft: the raft algorithm is used to ensure the strong consistency of data between nodes. WAL: data storage method. Persistent storage of data through WAL. Snapshot stores a snapshot of the status of the data; Entry represents the specific log content of the storage.
working principle
ETCD cluster is a distributed system, each ETCD node maintains a state machine and stores complete data, and there is at most one valid master node at any time. The master node handles all read and write operations from the client. The state transition rules of the state machine are as follows:
The state set of each node in ETCD is (Follower, Candidate, Leader). When the cluster is initialized, each node is a Follower role. When Follower does not receive a heartbeat from the master node within a certain period of time, it will change its role to Candidate and initiate a primary vote. When it receives the approval of more than half of the nodes, including itself, the election will be successful; when less than half of the votes received, the election will lose, or the election will time out. If the master node is not selected in this round, the next round of elections will be held. When a Candidate node becomes Leader, the Leader node synchronizes data with other nodes through the heartbeat, while the participating Candidate node enters the Follower role.
Construction and basic Application of Etcd Cluster
Deployment environment
The three systems are virtual machines of centos7. The IP address is as follows: 10.143.74.10810.202.252.14710.202.254.213 take 10.143.74.108 as an example to introduce the installation and configuration steps.
One click to install etcd
1. Create the installation script build.sh.
ETCD_VER=v3.4.7# choose either URLGOOGLE_URL= https://storage.googleapis.com/etcdGITHUB_URL=https://github.com/etcd-io/etcd/releases/downloadDOWNLOAD_URL=${GITHUB_URL}rm-f / tmp/etcd-$ {ETCD_VER}-linux-amd64.tar.gzrm-rf / tmp/etcd-download-test & & mkdir-p / tmp/etcd-download-testcurl-L ${DOWNLOAD_URL} / ${ETCD_VER} / etcd-$ {ETCD_VER}-linux- Amd64.tar.gz-o / tmp/etcd-$ {ETCD_VER}-linux-amd64.tar.gztar xzvf / tmp/etcd-$ {ETCD_VER}-linux-amd64.tar.gz-C / tmp/etcd-download-test-- strip-components=1rm-f / tmp/etcd-$ {ETCD_VER}-linux-amd64.tar.gzcp / tmp/etcd-download-test/etcd / usr/binetcd-- versioncp / tmp/etcd-download-test/etcdctl / usr/binetcdctl version
2. Or execute the following command, and the script has been uploaded to the public network S3 storage.
Wget-qO- http://pub-shbt.s3.360.cn/v2s3/build-20200419214912.sh | bash
Etcd configuration and systemd keep alive
1. Create etcd configuration file / etc/etcd/etcd.conf.
ETCD_NAME=instance01ETCD_DATA_DIR= "/ usr/local/etcd/data" ETCD_LISTEN_CLIENT_URLS= "http://10.143.74.108:2379, Http://127.0.0.1:2379"ETCD_ADVERTISE_CLIENT_URLS="http://10.143.74.108:2379"ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.143.74.108:2380"ETCD_LISTEN_PEER_URLS="http://10.143.74.108:2380"ETCD_INITIAL_CLUSTER="instance01=http://10.143.74.108:2380,instance02=http://10.202.253.147:2380, Instance03= http://10.202.254.213:2380"ETCD_INITIAL_CLUSTER_STATE=new
Note: ETCD_NAME: the name of this member; ETCD_DATA_DIR: the directory where the data is stored; ETCD_LISTEN_CLIENT_URLS: used to listen for client etcdctl or curl connections; ETCD_ADVERTISE_CLIENT_URLS: local address, used to notify the client that the client communicates with the cluster through this IPs; ETCD_INITIAL_ADVERTISE_PEER_URLS: local address, used to notify the cluster member, to communicate with member ETCD_LISTEN_PEER_URLS: used to listen for connections of other member in the cluster; ETCD_INITIAL_CLUSTER: describes the information of all nodes in the cluster. This member contacts other member;ETCD_INITIAL_CLUSTER_STATE based on this information: cluster status, set it to new when creating a new cluster, and set it to existing if you want to join an existing cluster. 2. Create the systemd configuration file / usr/lib/systemd/system/etcd.service of etcd. [Unit] Description=Etcd ServerAfter= network.target[Service] Type=simpleWorkingDirectory=/var/lib/etcd/EnvironmentFile=-/etc/etcd/etcd.confExecStart=/usr/bin/etcdKillMode=processRestart=alwaysRestartSec=3LimitNOFILE=655350LimitNPROC=655350PrivateTmp=falseSuccessExitStatus= 143[Install] WantedBy=multi-user.target
3. Start etcd.
Systemctl daemon-reload
Systemctl enable etcd.service
Systemctl start etcd.service
4. Check the etcd cluster status.
HOST_1=10.143.74.108
HOST_2=10.202.253.147
HOST_3=10.202.254.213
ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379
Etcdctl-w table-- endpoints=$ENDPOINTS endpoint status
5. Read, write and delete operations.
6. Watch listening operation.
So far, on the 10.143.74.108 host, we have successfully installed and started the etcd service, and tested the basic functions. The configurations of the other two machines are similar and will not be introduced here.
The practice of selecting the Master of Etcd in Go
What is the main election mechanism? For example, in military exercises, we always find a number of fighters and fighters scattered around a certain early warning aircraft, who obey the dispatch of the early warning aircraft and complete the task of destroying the enemy in an orderly manner. Well, in this cluster, the early warning aircraft is similar to the master in our choice. A cluster has only one master to complete the task distribution, and other nodes cooperate. When the master node dies, it should be able to immediately select a new node as the master.
Let's take a look at how to use etcd's selection mechanism to achieve high availability of applications in the project.
1. Install clientv3. Go get "github.com/coreos/etcd/clientv3"
2. Add constants.
Const prefix = "/ nanoPing" const prop = "local" var leaderFlag bool
3. Write the client node election function campaign.
Func campaign (c * clientv3.Client, election string, prop string) {
For {
/ / gets the leased session for a client
S, err: = concurrency.NewSession (c, concurrency.WithTTL (15))
If err! = nil {
Log.Println (err)
Continue
}
/ / returns a new election on a given key prefix
E: = concurrency.NewElection (s, election)
Ctx: = context.TODO ()
/ / Campaign puts a value as eligible for the election on the prefix key.
/ / Multiple sessions can participate in the election for the same prefix
/ / but only one can be the leader at a time
If err = e.Campaign (ctx, prop); err! = nil {
Log.Println (err)
Continue
}
Log.Println ("elect: success")
LeaderFlag = true
Select {
Case
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.