ubuntu+docker搭建etcd集群

本文基于compose管理鏡像,對此不熟悉的,可以先了解下如何使用。

安裝compose

下載compose,使用下面的指令下載compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

將可執(zhí)行權(quán)限應(yīng)用于二進(jìn)制文件:

sudo chmod +x /usr/local/bin/docker-compose

創(chuàng)建軟鏈:

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

測試是否安裝成功:

docker-compose --version
cker-compose version 1.24.1, build 4667896b

拉取etcd官方鏡像

docker pull quay.io/coreos/etcd

在一個(gè)文件夾下創(chuàng)建 etcd-compose.yml文件,用于管理etcd容器
在文件中貼如如下內(nèi)容:

version: '3'
services:
  etcd-node1:
    image: "quay.io/coreos/etcd"
    container_name: "etcd-node1"
    ports:
      - "12379:2379"
      - "12380:2380"
    command: 'etcd -name etcd-node1 -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 -initial-cluster-token etcd-cluster -initial-cluster "etcd-node1=http://etcd-node1:2380,etcd-node2=http://etcd-node2:2380,etcd-node3=http://etcd-node3:2380" -initial-cluster-state new'
    networks:
      - "etcd"

  etcd-node2:
    image: "quay.io/coreos/etcd"
    container_name: "etcd-node2"
    ports:
      - "22379:2379"
      - "22380:2380"
    command: 'etcd -name etcd-node2 -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 -initial-cluster-token etcd-cluster -initial-cluster "etcd-node1=http://etcd-node1:2380,etcd-node2=http://etcd-node2:2380,etcd-node3=http://etcd-node3:2380" -initial-cluster-state new'
    networks:
      - "etcd"

  etcd-node3:
    image: "quay.io/coreos/etcd"
    container_name: "etcd-node3"
    ports:
      - "32379:2379"
      - "32380:2380"
    command: 'etcd -name etcd-node3 -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 -initial-cluster-token etcd-cluster -initial-cluster "etcd-node1=http://etcd-node1:2380,etcd-node2=http://etcd-node2:2380,etcd-node3=http://etcd-node3:2380" -initial-cluster-state new'
    networks:
      - "etcd"

networks:
  etcd:

啟動(dòng)yml文件

docker-compose -f etcd-compose.yml up -d

使用指令,查看容器啟動(dòng)情況

docker ps -a 

可以看到如下內(nèi)容


圖片.png

至此,etcd容器集群搭建完畢.

查看注冊到etcd的服務(wù)有兩種方法,一種是進(jìn)入etcd容器,使用etcdctl指令查看,這里不再介紹。

介紹一種使用go-micro工具查看注冊到etcd服務(wù)的方法

在goland中執(zhí)行如下指令

set MICRO_REGISTRY=etcd
set MICRO_REGISTRY_ADDRESS=192.168.109.131:12379
set MICRO_API_NAMESPACE=api.tutor.com
micro web

192.168.109.131是我的虛擬機(jī)ip
12379這是上面創(chuàng)建的etcd容器代理端口

然后在瀏覽器輸入:http://localhost:8082/registry

即可看到如下效果:


圖片.png

紅框內(nèi)的 是我注冊到etcd的服務(wù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容