物聯網架構成長之路(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.1cbe1991CFEveahtml

 

1 apt-get update
2 apt-get -y install apt-transport-https ca-certificates curl software-properties-common
3 curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
4 add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
5 apt-get -y update
6 apt-get -y install docker-ce

 


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


2. 編寫Dockerfile
  既然學了Docker,那麼就本身寫個Dockerfile來build一個Docker Image,如今etcd最新版是3.3.10linux

 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構建git

docker build -t etcd:3.3 .

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

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. 啓動docker

  我這裏模擬啓動3個Etcd服務,以前須要建立虛擬網卡ubuntu

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

  建立etcd服務curl

 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 管理界面 安裝參考 https://github.com/weaveworks/scope ui

1 curl -L git.io/scope -o /usr/local/bin/scope
2 chmod a+x /usr/local/bin/scope
3 scope launch

 

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

相關文章
相關標籤/搜索