物联网架构成长之路(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

猜你喜欢

转载自www.cnblogs.com/wunaozai/p/9917488.html