Implementation steps for docker-compose to deploy etcd cluster

Implementation steps for docker-compose to deploy etcd cluster

Write docker-compose.yml

version: "3.0"

networks:
  etcd-net: # Network driver: bridge # Bridge mode volumes:
  etcd1_data: # Mounted to the local data volume name driver: local
  etcd2_data:
    driver: local
  etcd3_data:
    driver: local
###
### etcd For other environment configurations, see: https://doczhcn.gitbook.io/etcd/index/index-1/configuration
###
services:
  etcd1:
    image: bitnami/etcd:latest # Image container_name: etcd1 # Container name --name
    restart: always # Always restart networks:
      - etcd-net # Network used --network
    ports: #Port mapping -p
      - "20000:2379"
      - "20001:2380"
    environment: # Environment variables --env
      : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
      - etcd1_data:/bitnami/etcd # Mounted data volume etcd2:
    image: bitnami/etcd:latest
    container_name: etcd2
    restart: always
    networks:
      - etcd-net
    ports:
      - "20002:2379"
      - "20003:2380"
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      -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:
      - etcd2_data:/bitnami/etcd

  etcd3:
    image: bitnami/etcd:latest
    container_name: etcd3
    restart: always
    networks:
      - etcd-net
    ports:
      - "20004:2379"
      - "20005:2380"
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      -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:
      - etcd3_data:/bitnami/etcd

Run docker-compose

[root@centos8 etcdtest]# tree
.
└── docker-compose.yml

0 directories, 1 file
[root@centos8 etcdtest]# docker-compose up -d
[+] Running 4/4
 ⠿ Network etcdtest_etcd-net Created 0.1s
 ⠿ Container etcd3 Started 0.6s
 ⠿ Container etcd1 Started 0.7s
 ⠿ Container etcd2 Started 0.7s
[root@centos8 etcdtest]# 

Check build status

Check the node startup status

[root@centos8 etcdtest]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89469f98491f bitnami/etcd:latest "/opt/bitnami/script..." 54 seconds ago Up 53 seconds 0.0.0.0:20004->2379/tcp, :::20004->2379/tcp, 0.0.0.0:20005->2380/tcp, :::20005->2380/tcp etcd3
5454f5a719a2 bitnami/etcd:latest "/opt/bitnami/script..." 54 seconds ago Up 53 seconds 0.0.0.0:20000->2379/tcp, :::20000->2379/tcp, 0.0.0.0:20001->2380/tcp, :::20001->2380/tcp etcd1
bf989f9512b5 bitnami/etcd:latest "/opt/bitnami/script…" 54 seconds ago Up 53 seconds 0.0.0.0:20002->2379/tcp, :::20002->2379/tcp, 0.0.0.0:20003->2380/tcp, :::20003->2380/tcp etcd2

View the mounted data volumes

[root@centos8 etcdtest]# docker volume ls
DRIVER VOLUME NAME
local etcdtest_etcd1_data
local etcdtest_etcd2_data
local etcdtest_etcd3_data
[root@centos8 etcdtest]# docker inspect etcd1 

"Mounts": [
    {
        "Type": "volume",
        "Name": "etcdtest_etcd1_data",
        "Source": "/var/lib/docker/volumes/etcdtest_etcd1_data/_data",
        "Destination": "/bitnami/etcd",
        "Driver": "local",
        "Mode": "z",
        "RW": true,
        "Propagation": ""
    }
]

Test Node

Write a key from etcd1

[root@centos8 etcdtest]# docker exec -it etcd1 bash
I have no name!@5454f5a719a2:/opt/bitnami/etcd$ etcdctl put name "i am wxf"
OK

Read a value from etcd2

[wxf@centos8 ~]$ docker exec -it etcd2 bash
I have no name!@bf989f9512b5:/opt/bitnami/etcd$ etcdctl get name
name
i am wxf

Build successful!

Simple interaction between Golang and etcd

package main

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

func main() {
 cli, err := clientv3.New(clientv3.Config{
  Endpoints: []string{"http://192.168.135.10:20000", "http://192.168.135.10:20002", "http://192.168.135.10:20004"},
  DialTimeout: 5 * time.Second,
 })
 if err != nil {
  fmt.Printf("connect to etcd failed, err:%v\n", err)
  return
 }
 defer cli.Close()
 fmt.Println("connect to etcd success")
 defer cli.Close()

 go Watch(cli)

 Create(cli)
 Read(cli)
 Delete(cli)
 Update(cli)
 select {}
}

func Watch(cli *clientv3.Client) {
 rch := cli.Watch(context.Background(), "name") // type WatchChan <-chan WatchResponse
 for wresp := range rch {
  for _, ev := range wresp.Events {
   fmt.Printf("Type: %s Key: %s Value: %s\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  }
 }
 fmt.Println("out")
}

func Create(cli *clientv3.Client) {
 // put
 ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
 _, err := cli.Put(ctx, "name", "wxf")
 cancel()
 if err != nil {
  fmt.Printf("put to etcd failed, err:%v\n", err)
  return
 }
}

func Read(cli *clientv3.Client) {
 //get
 ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
 resp, err := cli.Get(ctx, "name")
 cancel()
 if err != nil {
  fmt.Printf("get from etcd failed, err:%v\n", err)
  return
 }
 for _, ev := range resp.Kvs {
  fmt.Printf("Type: %s Key: %s Value: %s\n", "READ", ev.Key, ev.Value)
 }
}

func Update(cli *clientv3.Client) {
 // put
 ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
 _, err := cli.Put(ctx, "name", "xyy")
 cancel()
 if err != nil {
  fmt.Printf("put to etcd failed, err:%v\n", err)
  return
 }
}

func Delete(cli *clientv3.Client) {
 //del
 ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
 _, err := cli.Delete(ctx, "name")
 cancel()
 if err != nil {
  fmt.Printf("delete from etcd failed, err:%v\n", err)
  return
 }
}

go run main.go

connect to etcd success
Type: PUT Key:name Value:wxf
Type: READ Key:name Value:wxf
Type: DELETE Key:name Value:
Type: PUT Key:name Value:xyy
Type: PUT Key:name Value:test for terminal
Type: PUT Key:name Value:test for terminal aabbccdd

I have no name!@5454f5a719a2:/opt/bitnami/etcd$ etcdctl put name "test for terminal"
OK
I have no name!@5454f5a719a2:/opt/bitnami/etcd$ etcdctl put name "test for terminal aabbccdd"
OK

This is the end of this article about the implementation steps of docker-compose deployment of etcd cluster. For more relevant content about docker-compose deployment of etcd cluster, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of Docker Compose multi-container deployment
  • Tutorial on how to quickly deploy clickhouse using docker-compose
  • Detailed example of Docker Compose's rapid deployment of multi-container services

<<:  Our thoughts on the UI engineer career

>>:  Detailed explanation of the use of JavaScript functions

Recommend

Comprehensive website assessment solution

<br />Sometimes you may be asked questions l...

Tips for using DIV container fixed height in IE6 and IE7

There are many differences between IE6 and IE7 in ...

Vue implements an Input component that gets the key display shortcut key effect

I encountered a requirement to customize shortcut...

CSS3 timeline animation

Achieve results html <h2>CSS3 Timeline</...

How to view Docker container application logs

docker attach command docker attach [options] 容器w...

Teach you how to make cool barcode effects

statement : This article teaches you how to imple...

Learn the black technology of union all usage in MySQL 5.7 in 5 minutes

Performance of union all in MySQL 5.6 Part 1:MySQ...

JavaScript Document Object Model DOM

Table of contents 1. JavaScript can change all HT...

Vue implements the method example of tab routing switching component

Preface This article introduces the use of vue-ro...

How to implement nested if method in nginx

Nginx does not support nested if statements, nor ...

Vue implements internationalization of web page language switching

1. Basic steps 1: Install yarn add vue-i18n Creat...

MySQL Series 13 MySQL Replication

Table of contents 1. MySQL replication related co...

Detailed explanation of MySQL backup process using Xtrabackup

Table of contents 01 Background 02 Introduction 0...