ETCD-01_etcd cluster installation and configuration

1. Goal

Install and configure etcd cluster on centos7.6

2. Architecture

ETCD cluster 3 node architecture
ip hostname os & module Cluster Name
10.1.1.5 etcd5 centos 7.6 etcd-v3.3.2-linux-amd64 etcd-1
10.1.1.6 etcd6 centos 7.6 etcd-v3.3.2-linux-amd64 etcd-2
10.1.1.7 etcd7 centos 7.6 etcd-v3.3.2-linux-amd64 etcd-3

Three, initialize the environment

The following must be operated on all nodes

1. Turn off the firewall and selinux

systemctl restart ntpd
systemctl enable ntpd
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

2. Download the etcd package

wget -P /usr/local/src/ https://github.com/coreos/etcd/releases/download/v3.3.2/etcd-v3.3.2-linux-amd64.tar.gz

cd /usr/local/src
tar -zxf etcd-v3.3.2-linux-amd64.tar.gz
mv etcd-v3.3.2-linux-amd64 /usr/local/etcd-v3.3.2

3. Modify system environment variables

vim ~/.bashrc

 Append the following two lines to the end of the ~/.bashrc file

export PATH=$PATH:/usr/local/etcd-v3.3.2/
export ETCDCTL_API=3

Let the configuration take effect

source ~/.bashrc

Fourth, configure etcd

The following operations need to be performed on three nodes, the configuration files are slightly different, pay attention to

1. Create the configuration file required for etcd startup

Note: The configuration files are basically the same, except that advertise-client-urls and initial-advertise-peer-urls must write their own ip addresses.

  ● Create the configuration file etcd.conf on node 1 (10.1.1.5)

vim /usr/local/etcd-v3.3.2/etcd.conf
name: etcd-1
data-dir: /usr/local/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.1.1.5:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.1.1.5:2380
initial-cluster: etcd-1=http://10.1.1.5:2380,etcd-2=http://10.1.1.6:2380,etcd-3=http://10.1.1.7:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new

  ● Create the configuration file etcd.conf on node 2 (10.1.1.6)

vim /usr/local/etcd-v3.3.2/etcd.conf
name: etcd-2
data-dir: /usr/local/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.1.1.6:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.1.1.6:2380
initial-cluster: etcd-1=http://10.1.1.5:2380,etcd-2=http://10.1.1.6:2380,etcd-3=http://10.1.1.7:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new

  ● Create configuration file etcd.conf on node 3 (10.1.1.7)

vim /usr/local/etcd-v3.3.2/etcd.conf
name: etcd-3
data-dir: /usr/local/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.1.1.7:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.1.1.7:2380
initial-cluster: etcd-1=http://10.1.1.5:2380,etcd-2=http://10.1.1.6:2380,etcd-3=http://10.1.1.7:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new

Notes:

  • name: etcd-1 --->etcd cluster node name (each host cannot be repeated)
  • data-dir: /usr/local/etcd-v3.3.2/data --->The main directory where data is stored
  • listen-client-urls: http://0.0.0.0:2379 --->Monitor the url of the client, 0.0.0.0 means to listen to all the ip addresses of the machine
  • advertise-client-urls: http://10.1.1.5:2379 --->Announce your own ip and port to etcd cluster, write the local ip
  • listen-peer-urls: http://0.0.0.0:2380 --->Listen to the ips of other members in the cluster, 0.0.0.0 means all ips can become members
  • initial-advertise-peer-urls: http://10.1.1.5:2380 ---> announce to other members in the cluster their own ip and port, write the local ip
  • initial-cluster: etcd-1=http://10.1.1.5:2380,etcd-2=http://10.1.1.6:2380,etcd-3=http://10.1.1.7:2380 --->will All cluster names of cluster members (name value in etcd.conf) and their ip+ports are written here, and each member is separated by a comma.
  • initial-cluster-token: etcd-cluster-my --->the token of the cluster, only the same token can be eligible to become an etcd member
  • initial-cluster-state: new --->The initial state of the cluster. new means to create a new cluster. When anyone wants to join an existing cluster, its status should be written as existing

2. Create data storage directories on all nodes

mkdir /usr/local/etcd-v3.3.2/data

3. Create a system boot menu on all nodes

vim /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/usr/local/etcd-v3.3.2
# User=etcd
ExecStart=/usr/local/etcd-v3.3.2/etcd --config-file /usr/local/etcd-v3.3.2/etcd.conf
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

4. Start etcd cluster nodes for the first time

Start-up general principle: start any node first, after the first node is successfully started, then start the remaining two

  ● Start etcd of node 1, and set it to start automatically after booting

systemctl daemon-reload
systemctl restart etcd && systemctl enable etcd

  ● Start etcd of node 2 and node 3, and set it to start automatically after booting

systemctl daemon-reload
systemctl restart etcd && systemctl enable etcd

5. Test the ETCD cluster

  ● Execute on any node and observe whether ports 2379 and 2380 are started

[root@etcd5 ~]# netstat -ntlup |grep etcd
tcp6       0      0 :::2379                 :::*                    LISTEN      6417/etcd           
tcp6       0      0 :::2380                 :::*                    LISTEN      6417/etcd

  ● Execute on any connection to view the list of ETCD cluster members

[root@etcd5 ~]# etcdctl member list
71b9fac8482ec139, started, etcd-1, http://10.1.1.5:2380, http://10.1.1.5:2379
89d4b75907acdf9a, started, etcd-3, http://10.1.1.7:2380, http://10.1.1.7:2379
b22a0934e0760a75, started, etcd-2, http://10.1.1.6:2380, http://10.1.1.6:2379

Note: 3 lines are found, and it is activated. It shows that the etcd on our 3 nodes have been started successfully this time, and the successful components have been clustered.

  ● Execute on any connection to check the health status of the node

[root@etcd5 ~]# etcdctl endpoint health --endpoints=10.1.1.5:2379,10.1.1.6:2379,10.1.1.7:2379
10.1.1.5:2379 is healthy: successfully committed proposal: took = 1.79041ms
10.1.1.7:2379 is healthy: successfully committed proposal: took = 3.425099ms
10.1.1.6:2379 is healthy: successfully committed proposal: took = 3.544992ms

 6. Summary

No matter how you restart any node, the cluster is still monitored.

No matter how you restart a node server, after restarting, it can still join the ETCD cluster smoothly.

 

Five, the basic operating commands of ETCD

1. Create key-value pairs

Create a key name value of serena

etcdctl put name serena

2. Query the value based on the key

Query the information whose key is name

[root@etcd5 ~]# etcdctl get name
name
serena

3. Fuzzy query key-value pair information based on the prefix of the key

Query all key values ​​whose keys start with n

[root@etcd5 ~]# etcdctl get n --prefix
name
serena
name2
kahn.xiao

4. Delete the specified key

#当键存在时,成功删除返回1
[root@etcd5 ~]# etcdctl del name4
1

#当键不存在时,删除返回0
[root@etcd5 ~]# etcdctl del name666
0

5. Use URL to store key values

[root@etcd5 ~]# curl http://10.1.1.5:2379/v2/keys/name5 -XPUT -d value="kahn.xiao"
{"action":"set","node":{"key":"/name5","value":"kahn.xiao","modifiedIndex":19,"createdIndex":19}}

6. Use URL to query key value

[root@etcd5 ~]# curl -s http://10.1.1.5:2379/v2/keys/name5
{"action":"get","node":{"key":"/name5","value":"kahn.xiao","modifiedIndex":19,"createdIndex":19}}

7. Use URL to query the version of ETCD

[root@etcd5 ~]# curl -s http://10.1.1.5:2379/version
{"etcdserver":"3.3.2","etcdcluster":"3.3.0"}

8. Use URL to delete the key value

[root@etcd5 ~]# curl -s http://10.1.1.5:2379/v2/keys/name5 -XDELETE
{"action":"delete","node":{"key":"/name5","modifiedIndex":20,"createdIndex":19},"prevNode":{"key":"/name5","value":"kahn.xiao","modifiedIndex":19,"createdIndex":19}}

9. Use URL to create a directory

Create a directory called kahnDIR

[root@etcd5 ~]# curl -s http://10.1.1.5:2379/v2/keys/kahnDIR -XPUT -d dir=true
{"action":"set","node":{"key":"/kahnDIR","dir":true,"modifiedIndex":21,"createdIndex":21}}

10. Use URL to query all directories

[root@etcd5 ~]# curl -s http://10.1.1.5:2379/v2/keys/?recursive=true
{"action":"get","node":{"dir":true,"nodes":[{"key":"/kahnDIR","dir":true,"modifiedIndex":21,"createdIndex":21},
{"key":"/kahnDIR2","dir":true,"modifiedIndex":22,"createdIndex":22},
{"key":"/kahnDIR3","dir":true,"modifiedIndex":23,"createdIndex":23}]}}

Note: There are 3 directories found here, the directory names are kahnDIR, kahnDIR2, kahnDIR3

11. Use URL to delete a directory

Delete a directory called kahnDIR3

[root@etcd5 ~]# curl -s 'http://10.1.1.5:2379/v2/keys/kahnDIR3?dir=true' -XDELETE
{"action":"delete","node":{"key":"/kahnDIR3","dir":true,"modifiedIndex":24,"createdIndex":23},"prevNode":{"key":"/kahnDIR3","dir":true,"modifiedIndex":23,"createdIndex":23}}

-------------END-------------------November 3, 2020 22:59:06------ -------------------

Writing a post is very time-consuming and very tiring. If you think this post is helpful to you, then I hope to get your rewards and encouragement to encourage me to continue to share more dry goods to you

Guess you like

Origin blog.csdn.net/xoofly/article/details/109470269