docker-compose部署etcd集群的实现步骤

目录
  • 编写docker-compose.yml
  • 运行docker-compose
  • 检查搭建状态
  • 测试节点
  • golang 与 etcd 简单交互

编写docker-compose.yml

version: "3.0"

networks:
  etcd-net:           # 网络
    driver: bridge    # 桥接模式

volumes:
  etcd1_data:         # 挂载到本地的数据卷名
    driver: local
  etcd2_data:
    driver: local
  etcd3_data:
    driver: local
###
### etcd 其他环境配置见:https://doczhcn.gitbook.io/etcd/index/index-1/configuration
###
services:
  etcd1:
    image: bitnami/etcd:latest  # 镜像
    container_name: etcd1       # 容器名 --name
    restart: always             # 总是重启
    networks:
      - etcd-net                # 使用的网络 --network
    ports:                      # 端口映射 -p
      - "20000:2379"
      - "20001:2380"
    environment:                # 环境变量 --env
      - allow_none_authentication=yes                       # 允许不用密码登录
      - etcd_name=etcd1                                     # etcd 的名字
      - etcd_initial_advertise_peer_urls=http://etcd1:2380  # 列出这个成员的伙伴 url 以便通告给集群的其他成员
      - etcd_listen_peer_urls=http://0.0.0.0:2380           # 用于监听伙伴通讯的url列表
      - etcd_listen_client_urls=http://0.0.0.0:2379         # 用于监听客户端通讯的url列表
      - etcd_advertise_client_urls=http://etcd1:2379        # 列出这个成员的客户端url,通告给集群中的其他成员
      - etcd_initial_cluster_token=etcd-cluster             # 在启动期间用于 etcd 集群的初始化集群记号
      - etcd_initial_cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 # 为启动初始化集群配置
      - etcd_initial_cluster_state=new                      # 初始化集群状态
    volumes:
      - etcd1_data:/bitnami/etcd                            # 挂载的数据卷

  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

运行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]# 

检查搭建状态

查看节点启动情况

[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

查看挂载的数据卷

[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": ""
    }
]

测试节点

从etcd1写一个key

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

从etcd2读一个value

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

搭建成功!

golang 与 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

到此这篇关于docker-compose部署etcd集群的实现步骤的文章就介绍到这了,更多相关docker-compose部署etcd集群内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐