1. 程式人生 > >物聯網架構成長之路(22)-Docker練習之Etcd服務搭建

物聯網架構成長之路(22)-Docker練習之Etcd服務搭建

0. 前言
  時隔多日,前段時間忙完一個可有可無的專案後,又進入摸魚時間,沒有辦法,非網際網路公司,就是閒得蛋疼。又開始了自學之路。以前入門過Docker,然後又很久沒有看了,最近重新看了一下,推薦一下這個人的部落格: https://www.cnblogs.com/CloudMan6 寫得不錯,深入淺出。然後學著測試練習一下,部署Etcd服務當作練手。下面是利用Docker部署一個Etcd單機叢集測試環境。順便回顧一下Docker 基本操作。
  由於Docker更新特別快,需要較新的Linux核心版本,加上牆的原因,有時候安裝Docker不是那麼順心,建議保持好心態。IRNG加油!
  可以參考
  https://www.cnblogs.com/wunaozai/p/6936787.html


  https://yq.aliyun.com/articles/110806?spm=5176.8351553.0.0.1cbe1991CFEvea


1. 下載Etcd
  在這裡 https://github.com/etcd-io/etcd/releases 下載最新二進位制包,用Go寫的,最近遇到的一些開源軟體,Go語言的出鏡率有點高啊,是不是有必要入坑了呀!


2. 編寫Dockerfile
  既然學了Docker,那麼就自己寫個Dockerfile來build一個Docker Image,現在etcd最新版是3.3.10

 1 FROM alpine:3.2
 2 RUN apk add --update ca-certificates openssl tar && \
3 wget https://github.com/etcd-io/etcd/releases/download/v3.3.10/etcd-v3.3.10-linux-amd64.tar.gz && \ 4 tar -zxvf etcd-v3.3.10-linux-amd64.tar.gz && \ 5 mv etcd-v3.3.10-linux-amd64/etcd* /bin/ && \ 6 apk del --purge tar openssl && \ 7 rm -Rf etcd-v3.3.10-linux-amd64* /var/cache/apk/* 8
VOLUME /data 9 EXPOSE 2379 2380 10 CMD ["/bin/etcd"]

  Docker構建

docker build -t etcd:3.3 .

  這裡順便介紹一下如何把Image推送到私有倉庫(阿里雲)

1 docker login registry.cn-shenzhen.aliyuncs.com
2 docker tag etcd:3.3 registry.cn-shenzhen.aliyuncs.com/wunaozai/etcd:3.3
3 docker push registry.cn-shenzhen.aliyuncs.com/wunaozai/etcd:3.3
4 
5 docker pull registry.cn-shenzhen.aliyuncs.com/wunaozai/etcd:3.3

 

3. 啟動

  我這裡模擬啟動3個Etcd服務,之前需要建立虛擬網絡卡

docker network create --driver bridge --subnet 172.22.16.0/24 --gateway 172.22.16.254 my_net2

  建立etcd服務

 1 docker run -d --name etcd-client-1 -p 2001:2379 -p 3001:2380 -v /root/workspace/docker/k8s/etcd/data1/:/data --network my_net2 --ip 172.22.16.1 etcd:3.3 \
 2       /bin/etcd \
 3       --data-dir=/data --name node1 \
 4       --initial-advertise-peer-urls http://172.22.16.1:2380 --listen-peer-urls http://0.0.0.0:2380 \
 5       --advertise-client-urls http://172.22.16.1:2379 --listen-client-urls http://172.22.16.1:2379,http://127.0.0.1:2379 \
 6       --initial-cluster-state new --initial-cluster-token docker-etcd \
 7       --initial-cluster node1=http://172.22.16.1:2380,node2=http://172.22.16.2:2380,node3=http://172.22.16.3:2380
 8 
 9 docker run -d --name etcd-client-2 -p 2002:2379 -p 3002:2380 -v /root/workspace/docker/k8s/etcd/data2/:/data --network my_net2 --ip 172.22.16.2 etcd:3.3 \
10       /bin/etcd \
11       --data-dir=/data --name node2 \
12       --initial-advertise-peer-urls http://172.22.16.2:2380 --listen-peer-urls http://0.0.0.0:2380 \
13       --advertise-client-urls http://172.22.16.2:2379 --listen-client-urls http://172.22.16.2:2379,http://127.0.0.1:2379 \
14       --initial-cluster-state new --initial-cluster-token docker-etcd \
15       --initial-cluster node1=http://172.22.16.1:2380,node2=http://172.22.16.2:2380,node3=http://172.22.16.3:2380
16 
17 docker run -d --name etcd-client-3 -p 2003:2379 -p 3003:2380 -v /root/workspace/docker/k8s/etcd/data3/:/data --network my_net2 --ip 172.22.16.3 etcd:3.3 \
18       /bin/etcd \
19       --data-dir=/data --name node3 \
20       --initial-advertise-peer-urls http://172.22.16.3:2380 --listen-peer-urls http://0.0.0.0:2380 \
21       --advertise-client-urls http://172.22.16.3:2379 --listen-client-urls http://172.22.16.3:2379,http://127.0.0.1:2379 \
22       --initial-cluster-state new --initial-cluster-token docker-etcd \
23       --initial-cluster node1=http://172.22.16.1:2380,node2=http://172.22.16.2:2380,node3=http://172.22.16.3:2380

  docker ps -a 命令介面


  weaveworks/scope 管理介面

 

4. 進入到etcd容器

docker exec -it etcd-client-1 /bin/sh
etcdctl member list

 

5. API操作
  我們可以對任意一個node節點進行讀寫,即使容器關閉,這些KV資料都儲存在Host主機上的。

curl http://127.0.0.1:2001/v2/keys/hello -XPUT -d value="hello world"
curl http://127.0.0.1:2003/v2/keys/hello

 

 

參考資料
  https://github.com/etcd-io/etcd
  https://www.cnblogs.com/CloudMan6
  https://www.cnblogs.com/xishuai/p/docker-etcd.html
  https://www.cnblogs.com/wunaozai/p/6936787.html
  https://yq.aliyun.com/articles/110806?spm=5176.8351553.0.0.1cbe1991CFEvea

本文地址: https://www.cnblogs.com/wunaozai/p/9917488.html