Introduction to etcd

Time:2020-10-5

What is etcd?

Etcd is written in go language. It is used for high-performance K-V storage, service discovery, load balancing, distributed lock, configuration management and other scenarios in distributed system. It is similar to zookeeper of Java. Based on raft protocol, the consistency of data can be guaranteed.

Official address

[etcd.io] https://etcd.io

[github.com]https://github.com/etcd-io/etcd

Installation of etcd

There are two ways to install, can be compiled through the binary or source installation, I use here is binary installation, another source installation I have not tried.

In addition, the installation here is not in the cluster mode. It uses a single node, but only some basic operations.

1、 Binary installation

  1. Click on the address below and select different compression packages according to your system, such as MacOS download etcd-v3.4.4-darwin-amd64.zip

[https://etcd.io/docs/v3.4.0/integrations/] https://etcd.io/docs/v3.4.0/integrations/
2. Unzip the downloaded package, as shown below

$ ls ~/Applications/etcd
Documentation       README-etcdctl.md   README.md           READMEv2-etcdctl.md etcd     etcdctl
  1. In the above file, etcd is the server; etcdctl is the client, which integrates some commonly used APIs.

2、 Source code installation

Because it’s written by go, it can be used directlygo get https://github.com/etcd-io/etcdDownload on the line, but this etcd package is a little big, if the network is not good, it may not be able to download down, where I am not whole down.

Therefore, if you can’t download it, you can try to use GitHub’s zip package to download. If it still doesn’t work, you can use domestic download acceleration.

I found one online[ https://www.newbe.pro/Mirrors/Mirrors-Etcd/ ] https://www.newbe.pro/Mirrors/Mirrors-Etcd/ Download the compressed package directly from here and put it in your gopath.

Next, go to the etcd directory and execute the ./buildJust build it.

Etcd start

Either way, to start etcd, first find the file etcd, and then execute it. The default is on port 2379 of your computer. If you want to open it on the public network, you can specify the following parameters

$ ~/Applications/etcd/etcd  --advertise-client-urls="https://123.33.44.34:2379"  --listen-client-urls="https://123.33.44.34:2379"

Start locally as follows

$ ~/Applications/etcd/etcd
[WARNING] Deprecated '--logger=capnslog' flag is set; use '--logger=zap' flag instead
2020-03-11 12:55:38.289362 I | etcdmain: etcd Version: 3.4.4
2020-03-11 12:55:38.289461 I | etcdmain: Git SHA: c65a9e2dd
2020-03-11 12:55:38.289468 I | etcdmain: Go Version: go1.12.12
2020-03-11 12:55:38.289476 I | etcdmain: Go OS/Arch: darwin/amd64
2020-03-11 12:55:38.289482 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4
2020-03-11 12:55:38.289492 N | etcdmain: failed to detect default host (default host not supported on darwin_amd64)
2020-03-11 12:55:38.289500 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
[WARNING] Deprecated '--logger=capnslog' flag is set; use '--logger=zap' flag instead
2020-03-11 12:55:38.292027 I | embed: name = default
2020-03-11 12:55:38.292038 I | embed: data dir = default.etcd
2020-03-11 12:55:38.292045 I | embed: member dir = default.etcd/member
2020-03-11 12:55:38.292050 I | embed: heartbeat = 100ms
2020-03-11 12:55:38.292054 I | embed: election = 1000ms
2020-03-11 12:55:38.292058 I | embed: snapshot count = 100000
2020-03-11 12:55:38.292079 I | embed: advertise client URLs = http://localhost:2379
2020-03-11 12:55:38.407474 I | etcdserver: starting member 8e9e05c52164694d in cluster cdf818194e3a8c32

Well, see the above output, the server will start. If you want to test, you can use etcdctl to test.

Etcdctl put "task / Task1" "task task"
OK

etcdctl get  "task/task1"
task/task1
Task task

Code operation

Here, go is used to perform some simple operations on etcd. The client of go can be found on the official website[ https://etcd.io/docs/v3.4.0/integrations/ ] https://etcd.io/docs/v3.4.0/integrations/

I use the package of clientv3 here. When the package is installed in the source code above, there is one in the package downloaded. The package is available for both server and client code.

Get operation

package main

import (
	"context"
	"fmt"
	"go.etcd.io/etcd/clientv3"
	"time"
)

func main(){
	var (
		config clientv3.Config
		client *clientv3.Client
		kv clientv3.KV
		resp *clientv3.GetResponse
		err error
	)
	config = clientv3.Config{
		Endpoints:[]string{"localhost:2379"},
		DialTimeout:5 * time.Second,
	}

	if client,err = clientv3.New(config); err != nil {
		fmt.Println(err)
		return
	}

	kv = clientv3.NewKV(client)

	if resp,  err = kv.Get(context.TODO(),"cron/jobs/",clientv3.WithPrefix()); err != nil {
		fmt.Println(err)
		return
	}
	for k,v := range resp.Kvs {
		fmt.Println(k,v,string(v.Key), string(v.Value))
	}
}

Renewal operation

package main

import (
	"context"
	"fmt"
	"go.etcd.io/etcd/clientv3"
	"time"
)

func main(){
	var (
		config clientv3.Config
		client *clientv3.Client
		Lease clientv3.Lease
		leaseGrantResp *clientv3.LeaseGrantResponse
		leaseId clientv3.LeaseID
		leaseKeepResp *clientv3.LeaseKeepAliveResponse
		leaseKeepRespChan

OP operation

package main

import (
	"context"
	"fmt"
	"go.etcd.io/etcd/clientv3"
	"time"
)

func main(){
	var (
		config clientv3.Config
		client *clientv3.Client
		opPut clientv3.Op
		opGet clientv3.Op
		opResp  clientv3.OpResponse
		kv clientv3.KV

		err error
	)
	config = clientv3.Config{
		Endpoints:[]string{"localhost:2379"},
		DialTimeout:5 * time.Second,
	}

	if client,err = clientv3.New(config); err != nil {
		fmt.Println(err)
		return
	}

	kv = clientv3.NewKV(client)

	opPut = clientv3.OpPut("cron/jobs/job3","job3333")

	if opResp, err  = kv.Do(context.TODO(),opPut); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println ("get revision", opResp.Put (). Header.Revision )


	opGet = clientv3.OpGet("cron/jobs/job3")
	if opResp ,err = kv.Do(context.TODO(),opGet); err != nil {
		fmt.Println(err)
		return
	}

	if len(opResp.Get().Kvs) != 0 {
		fmt.Println ("get value:", string( opResp.Get ().Kvs[0].Value))
	}

}

Simple distributed lock

package main

import (
	"context"
	"fmt"
	"go.etcd.io/etcd/clientv3"
	"time"
)

//Simple distributed lock

func main(){
	var (
		config clientv3.Config
		client *clientv3.Client
		/*opPut clientv3.Op
		opGet clientv3.Op
		opResp  clientv3.OpResponse*/
		kv clientv3.KV
		txn clientv3.Txn
		txnResp *clientv3.TxnResponse

		Lease clientv3.Lease
		leaseGrantResp *clientv3.LeaseGrantResponse
		leaseId clientv3.LeaseID
		leaseKeepResp *clientv3.LeaseKeepAliveResponse
		leaseKeepRespChan

The above is to record the basic introduction and some common operations of etcd. I hope you will like it and look forward to your progress.