Dockerfile文件结构之etcd

本篇文章手把手教你如何使用Dockerfile构建自己etcd镜像,并且已经提供基于etcd3.0.10 的image供读者使用,用户可以快速使用docker image构建自己的etcd集群环境

一、etcd镜像的Dockerfile文件结构

注意:优秀的docker镜像源文件都是会尝试去除所有依赖的,也即是该文件无论被谁拿走使用,都可以快速构建属于自己的image

etcd镜像的Dockerfile项目文件结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
$ tree .
.
├──Dockerfile
├──docker-entrypoint.sh
# Dockerfile 文件
$ cat Dockerfile
FROM centos-biaoge
MAINTAINER 371990778@qq.com
ENV LANG=zh_CN.UTF-8;\
LC_ALL=zh_CN.UTF-8;\
TZ=”Asia/Shanghai”;\
TERM=xterm;\
DOWNLOAD=https://github.com/coreos/etcd/releases/download/v3.0.10/ \
ETCDVERSION=etcd-v3.0.10-linux-amd64 \
USER=admin
RUN yum install mkdir curl wget tar chown unzip -y;\
useradd ${USER} ;\
mkdir -p /export/{servers,Logs,packages,Apps,Shell};\
wget ${DOWNLOAD}${ETCDVERSION}.tar.gz && tar -zxf ${ETCDVERSION}.tar.gz -C /export/servers/ && \
/bin/rm -rf ${ETCDVERSION}.tar.gz;\
chown -R ${USER}.${USER} /export ;\
ln -s /export/servers/${ETCDVERSION}/etcd* /usr/bin/;
EXPOSE 2379 2380
COPY docker-entrypoint.sh /
ENTRYPOINT [“/docker-entrypoint.sh”]
# 镜像启动脚本
$ cat docker-entrypoint.sh
#!/bin/bash
#Filename:docker-entrypoint.sh
#Author_by:Andy_xu
#Contact:[mail:371990778@qq.com,QQ:371990778]
#Date:2017-07-25 16:42
#Description:
if [ -z $NAME ];then
NAME=my-etcd-1
fi
if [ -z $DATADIR ];then
DATADIR=/export/etcd_data
fi
if [ -z $MYHOST ];then
MYHOST=http://localhost
fi
if [ -z $PORT ];then
PORT=2379
fi
if [ -z $CLUSTER_PORT ];then
CLUSTER_PORT=2380
fi
if [ -z $CLUSTER ];then
CLUSTER=my-etcd-1=http://localhost:2380
fi
if [ -z $CLUSTER_TOKEN ];then
CLUSTER_TOKEN=my-etcd-token
fi
if [ -z $CLUSTER_STATE ];then
CLUSTER_STATE=new
fi
ETCD_CMD=”etcd –name ${NAME} –data-dir ${DATADIR} \
–listen-client-urls http://0.0.0.0:${PORT} \
–advertise-client-urls ${MYHOST}:${PORT} \
–listen-peer-urls ${MYHOST}:${CLUSTER_PORT} \
–initial-advertise-peer-urls ${MYHOST}:${CLUSTER_PORT} \
–initial-cluster $CLUSTER \
–initial-cluster-token $CLUSTER_TOKEN \
–initial-cluster-state ${CLUSTER_STATE} \
$*”
echo -e “Running ‘$ETCD_CMD’\nBEGIN ETCD OUTPUT\n”
exec $ETCD_CMD

二、构建并使用image

1.构建etcd的image

进入项目路径下,执行以下命令:

1
$ docker build -t xxbandy123/etcd:3.0.10

2.使用默认参数创建etcd单实例

1
$ docker run -itd –name etcd-1 xxbandy123/etcd:3.0.10 –auto-compaction-retention 1

注意:后面的 --auto-compaction-retention 1 为额外增加的参数表示1小时自动压缩保留 默认的容器启动后面都可以增加额外的参数

3.使用自定义的参数进行创建etcd单实例

image内部定义的默认参数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NAME=my-etcd-1
DATADIR=/export/etcd_data
MYHOST=http://localhost
PORT=2379
CLUSTER_PORT=2380
CLUSTER=my-etcd-1=http://localhost:2380
CLUSTER_TOKEN=my-etcd-token
CLUSTER_STATE=new
etcd –name ${NAME} –data-dir ${DATADIR} \
–listen-client-urls http://0.0.0.0:${PORT} \
–advertise-client-urls ${MYHOST}:${PORT} \
–listen-peer-urls ${MYHOST}:${CLUSTER_PORT} \
–initial-advertise-peer-urls ${MYHOST}:${CLUSTER_PORT} \
–initial-cluster $CLUSTER \
–initial-cluster-token $CLUSTER_TOKEN \
–initial-cluster-state ${CLUSTER_STATE}

可以通过环境变量的方式启动单实例的etcd,也可以将该image传入不通变量去构造集群

1
$ docker run -itd -e DATADIR=/root/etcd_data -e CLUSTER_TOKEN=biaoge xxbandy123/etcd:3.0.10 –auto-compaction-retention 1

4.如何使用etcd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
docker pull xxbandy123/etcd:3.0.10
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e17c9479b424 xxbandy123/etcd:3.0.10 “/docker-entrypoint.s” 19 seconds ago Up 17 seconds 0.0.0.0:1025->2379/tcp, 0.0.0.0:1024->2380/tcp sharp_keller
# docker exec -it sharp_keller etcdctl set test biaoge
biaoge
# curl -s localhost:1025/v2/keys/test | jq .
{
“node”: {
“createdIndex”: 4,
“modifiedIndex”: 4,
“value”: “biaoge”,
“key”: “/test”
},
“action”: “get”
}
在任何一个客户端去访问:
# curl -s 10.241.131.109:1025/v2/keys/test | jq .
{
“node”: {
“createdIndex”: 4,
“modifiedIndex”: 4,
“value”: “biaoge”,
“key”: “/test”
},
“action”: “get”
}

三、自由发挥时间

由于该etcd实例可以通过环境变量传入参数,因此虽然是一个image,但是却非常灵活,用户可以在很快的时间内创建出来一个etcd集群。So,学会如何构建一个etcd镜像后,可以尝试下使用该image来快速构建一个etcd集群。

为方便大家的使用,该image已经上传到docker hub中,读者可以直接在本地pull后进行使用:

1
docker pull xxbandy123/etcd:3.0.10

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注