Deploy etCD using Docker

Etcd in single-node deployment mode

docker run \ -d \ -p 2379:2379 \ -p 2380:2380 \ --mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \ - the name etcd - quay - v3.5.0 \ quay. IO/coreos/etcd: v3.5.0 \ / usr/local/bin/etcd \ - name s1 \ - data - dir/etcd - data \ --listen-client-urls http://0.0.0.0:2379 \ --advertise-client-urls http://0.0.0.0:2379 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-advertise-peer-urls http://0.0.0.0:2380 \ --initial-cluster s1=http://0.0.0.0:2380 \ --initial-cluster-token tkn \ --initial-cluster-state new \ --log-level info \ --logger zap \ --log-outputs stderrCopy the code

Key parameter analysis

parameter instructions
–name Etcd Node name
–initial-cluster When etcd starts, use this configuration to find the address list of other nodes in the format: ‘Node name:http://ip:2380’Case: s1=http://0.0.0.0:2380
–initial-cluster-state At initialization, the cluster can be in either “new” or “existing” states, with new representing a new cluster and existing representing joining an existing cluster.
–listen-client-urls List of addresses to listen for client requests in the format: ‘http://localhost:2379’, separated by commas.
–advertise-client-urls If –listen-client-urls is configured with multiple listening addresses for client requests, this parameter can be used to suggest which address the client should use to access etCD.
–listen-peer-urls Listening address for communication between nodes on the server, format: ‘http://localhost:2380’
–initial-advertise-peer-urls Recommended address list for communication between servers.

The installation process

Unable to find the image 'quay. IO/coreos etcd: v3.5.0' locally v3.5.0: Pulling the from coreos / 1813 d21adc01 etcd: Pull complete 6e96907ab677: Pull complete 444ed0ea8673: Pull complete 0fd2df5633f0: Pull complete 8cc22b9456bb: Pull complete 7ac70aecd290: Pull complete 4b376c64dfe4: Pull complete Digest: sha256:28759af54acd6924b2191dc1a1d096e2fa2e219717a21b9d8edf89717db3631b Status: Downloaded newer image for quay. IO/coreos/etcd: v3.5.0Copy the code

Out-of-container validation

MacBook -pro RPC % docker exec 9f62717b21a8 /bin/sh -c "/usr/local/bin/etcd --version" etcd version: 3.5.0 Git SHA: 946a5A6F2 Go1.16.3 Go OS/Arch: linux/amd64 MacBook-Pro rpc % docker exec 9f62717b21a8 /bin/sh -c "/usr/local/bin/etcdctl version" etcdctl version: 3.5.0 API version: 3.5 MacBook-Pro RPC % docker exec 9f62717b21a8 /bin/sh -c "/usr/local/bin/etcdctl endpoint health" 127.0.0.1:2379 is healthy: successfully committed proposal: Took = 5.6964ms MacBook-Pro RPC % docker exec 9f62717b21a8 /bin/sh -c "/usr/local/bin/etcdctl put foo bar" OK MacBook-Pro rpc % docker exec 9f62717b21a8 /bin/sh -c "/usr/local/bin/etcdctl get foo" foo barCopy the code

In-container verification

MacBook-Pro rpc % docker exec -it 66658ceef406 /bin/sh # cd /usr/local/bin # ls -al total 56240 drwxr-xr-x 1 root root 4096 Jun 15 21:53 . drwxr-xr-x 1 root root 4096 Jan 11 2021 .. -rwxr-xr-x 1 root root 1050 Jan 27 2021 clean-install -rwxr-xr-x 1 root root 23560192 Jun 15 21:52 etcd -rwxr-xr-x 1 root root 17969152 Jun 15 21:52 etcdctl -rwxr-xr-x 1 root root 16048128 Jun 15 21:52 etcdutl lrwxrwxrwx 1 root root 4 Jan 27 2021 invoke-rc.d -> noop -r-xr-xr-x 1 root root 0 Jan 27 2021 noop lrwxrwxrwx 1 root root 4 Jan 27 2021 runlevel -> noop LRWXRWXRWX 1 root root 4 Jan 27 2021 update-rc.d -> noop # etcd --version etcd version: 3.5.0 Git SHA: 946a5a6f2 go1.16.3 Go OS/Arch: Linux/AMD64 # etcdctl Version etcdctl Version: 3.5.0 API Version: 3.5 # etcdctl endpoint health 127.0.0.1:2379 is healthy: successfully committed proposal: Took = 4.1765ms # etcdctl get foo foo barCopy the code

Cluster pattern

Docker – compose. Yaml files

Version: '2' networks: etcdnet: services: etcd1: image: quay. IO/coreos/etcd: v3.5.0 container_name: etcd1 command: Etcd-name etcd1-advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 - the initial - cluster - token etcd - cluster - initial - cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new ports: - 2379-2380 networks: - etcdnet etcd2: image: quay. IO/coreos/etcd: v3.5.0 container_name: etcd2 command: Etcd-name etcd2-advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 - the initial - cluster - token etcd - cluster - initial - cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new ports: - 2379-2380 networks: - etcdnet etcd3: image: quay. IO/coreos/etcd: v3.5.0 container_name: etcd3 command: Etcd-name etcd3-advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 - the initial - cluster - token etcd - cluster - initial - cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new ports: - 2379 - 2380 networks: - etcdnetCopy the code

Cluster startup Service

MacBook-Pro etcd % docker-compose up -d Creating network "etcd_etcdnet" with the default driver Creating etcd2 ... done Creating etcd1 ... done Creating etcd3 ... done MacBook-Pro etcd % docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 85044bbb6d81 Quay. IO/coreos etcd: v3.5.0 "etcd -name etcd1 - a..." 12 seconds ago Up 10 seconds 0.0.0.0:64640->2379/ TCP, 0.0.0.0:64641-692 > 2380 / TCP etcd1 ae553cf5c quay. IO/coreos/etcd: v3.5.0 "etcd -name etcd2 - a..." 12 seconds Up 11 seconds 0.0.0.0:64638->2379/ TCP, 0.0.0.0:64639-911035389 > 2380 / TCP etcd2 db3 quay. IO/coreos/etcd: v3.5.0 "etcd -name etcd3 - a..." 12 seconds ago Up 10 seconds 0.0.0.0:64643->2379/ TCP, 0.0.0.0:64642->2380/ TCP ETCD3Copy the code

Verifying cluster Status

Golang connection

Golang connection you need to use the “go. Etcd. IO/etcd/client/v3” depend on the package

Verify that the connection is possible

package main

import (
	"log"
	"time"

	clientv3 "go.etcd.io/etcd/client/v3"
)

func main(a) {
	config := clientv3.Config{
		// Multiple node service addresses of etCD
		Endpoints: []string{"127.0.0.1:2379"},
		// Set the initial connection timeout time of the client
		DialTimeout: 5 * time.Second,
	}

	client, err := clientv3.New(config)
	iferr ! =nil {
		log.Panic(err)
		return
	}
	defer client.Close()
	log.Println("connect success")}Copy the code

Add, delete, change, search

package main

import (
	"context"
	"log"
	"time"

	clientv3 "go.etcd.io/etcd/client/v3"
)

func main(a) {
        / / configuration
	config := clientv3.Config{
		// Multiple node service addresses of etCD
		Endpoints: []string{"127.0.0.1:2379"},
		// Set the initial connection timeout time of the client
		DialTimeout: 5 * time.Second,
	}
        
        // Create etcd client
	client, err := clientv3.New(config)
	iferr ! =nil {
		log.Panic(err)
		return
	}
	defer client.Close()

        // add, delete, change, and check
	etcd := NewEtcdInfo(client)

	key := "/demo/key"
	etcd.Put(key, "value")
	etcd.Get(key)
	etcd.Update(key, "update_value")
	etcd.Get(key)
	etcd.Delete(key)
}

type EtcdInfo struct {
	ctx    context.Context
	client *clientv3.Client
}

func NewEtcdInfo(client *clientv3.Client) *EtcdInfo {
	etcd := new(EtcdInfo)
	etcd.client = client
	etcd.ctx = context.Background()
	return etcd
}

/ / set the value
func (e *EtcdInfo) Put(key, value string) {
	The added value of / /
	res, err := e.client.Put(e.ctx, key, value)

	iferr ! =nil {
		log.Panicf("put fail err:%v", err)
		return
	}
	log.Printf("pus res:%v", *res)
}

/ / get the value
func (e *EtcdInfo) Get(key string) {
	The added value of / /
	res, err := e.client.Get(e.ctx, key)

	iferr ! =nil {
		log.Panicf("put fail err:%v", err)
		return
	}

	for _, item := range res.Kvs {
		log.Printf("key:%v, value:%v \n".string(item.Key), string(item.Value))
	}
}

/ / modify the value
func (e *EtcdInfo) Update(key, value string) {
	res, err := e.client.Put(e.ctx, key, value, clientv3.WithPrevKV())
	iferr ! =nil {
		log.Panicf("update fail err:%v", err)
		return
	}
	log.Printf("update value:%v".string(res.PrevKv.Value))
}

func (e *EtcdInfo) Delete(key string) {
	resp, err := e.client.Delete(e.ctx, key)
	iferr ! =nil {
		log.Panicf("delete fail err:%v", err)
		return
	}
	log.Printf("delete value:%v \n", resp.PrevKvs)
}

Copy the code

reference

Running etcd under Docker | etcd Releases etcd – IO/etcd (github.com) coreos/etcd Quay Docker installation etcd_w3cschool etcd use: Golang Examples – Volume 9 – Blogland (CNblogs.com)