docker 制作etcd 集群

#####Dockerfile制作

wget https://storage.googleapis.com/etcd/v3.3.2/etcd-v3.3.2-linux-amd64.tar.gz  #下载数据包

cat <<eof >run.sh                                                             #启动脚本,后期写入镜像

#!/bin/bash

set -e 

/etcd/work/etcd-v3.3.2-linux-amd64/etcd --name $name --data-dir=/etcd/data  --auto-compaction-retention=${time} --snapshot-count=$count --quota-backend-bytes=$quota  --initial-advertise-peer-urls $initial  --listen-peer-urls $peer  --listen-client-urls $client --advertise-client-urls $advertise --initial-cluster-token $token --initial-cluster  $cluster --initial-cluster-state $state

eof


cat <<eof >Dockerfile

FROM alpine

MAINTAINER MR wang "[email protected]"

RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main/" > /etc/apk/repositories

RUN apk update \

        && apk upgrade \

        && apk add --no-cache bash \

        bash-completion \

        && rm -rf /var/cache/apk/* 

RUN mkdir /etcd/{work,data,wal} -p 

WORKDIR /etcd/work

VOLUME /etcd/data

ENV name infra0

ENV time 1m

ENV count 5000

ENV quota 6442450944

ENV initial http://0.0.0.0:2380

ENV peer http://0.0.0.0:2380

ENV client http://0.0.0.0:2379

ENV advertise http://0.0.0.0:2379

ENV token etcd-cluster

ENV cluster "infra0=http://0.0.0.0:2380"

ENV state new

ADD etcd-v3.3.2-linux-amd64.tar.gz /etcd/work

ADD run.sh  /

EXPOSE 2379 2380

CMD ["/run.sh"]

eof


#####etcd集群搭建

变量定义

etcd_image=wang718049/etcd:3.3.2

etcd_dir=/data/etcd

etcd_data1=/data/etcd/data1

etcd_data2=/data/etcd/data2

etcd_data3=/data/etcd/data3

etcd_network=ccpass

etcd_time=1m

etcd_count=5000

etcd_quota=6442450944

etcd_name1=etcd1

etcd_name2=etcd2

etcd_name3=etcd3

etcd_initial1=http://$etcd_name1:2380

etcd_initial2=http://$etcd_name2:2380

etcd_initial3=http://$etcd_name3:2380

etcd_peer=http://0.0.0.0:2380

etcd_client=http://0.0.0.0:2379

etcd_advertise=http://0.0.0.0:2379

etcd_token=etcd-cluster

etcd_cluster="$etcd_name1=http://$etcd_name1:2380,$etcd_name2=http://$etcd_name2:2380,$etcd_name3=http://$etcd_name3:2380"

etcd_state=new



compose制作

mkdir  -p $etcd_dir/data{1,2,3}

cat <<eof >$etcd_dir/etcd.yml

version: '2'

services:

  $etcd_name1:

    image: $etcd_image

    restart: always

    container_name: $etcd_name1

    network_mode: "$etcd_network"

    logging:

      options:

        max-size: "200m"

        max-file: "10"

    ports:

      - 2379:2379

      - 2380:2380

    volumes:

      - $etcd_data1:/etcd/data

      - /etc/localtime:/etc/localtime

    environment:

      - name=$etcd_name1

      - time=$etcd_time

      - count=$etcd_count

      - quota=$etcd_quota

      - initial=$etcd_initial1

      - peer=$etcd_peer

      - client=$etcd_client

      - advertise=$etcd_advertise

      - token=$etcd_token

      - cluster=$etcd_cluster

      - state=$etcd_state

  $etcd_name2:

    image: $etcd_image

    restart: always

    container_name: $etcd_name2

    network_mode: "$etcd_network"

    logging:

      options:

        max-size: "200m"

        max-file: "10"

    ports:

      - 2369:2379

      - 2360:2380

    volumes:

      - $etcd_data2:/etcd/data

      - /etc/localtime:/etc/localtime

    environment:

      - name=$etcd_name2

      - time=$etcd_time

      - count=$etcd_count

      - quota=$etcd_quota

      - initial=$etcd_initial2

      - peer=$etcd_peer

      - client=$etcd_client

      - advertise=$etcd_advertise

      - token=$etcd_token

      - cluster=$etcd_cluster

      - state=$etcd_state

  $etcd_name3:

    image: $etcd_image

    restart: always

    container_name: $etcd_name3

    network_mode: "$etcd_network"

    logging:

      options:

        max-size: "200m"

        max-file: "10"

    ports:

      - 2359:2379

      - 2350:2380

    volumes:

      - $etcd_data3:/etcd/data

      - /etc/localtime:/etc/localtime

    environment:

      - name=$etcd_name3

      - time=$etcd_time

      - count=$etcd_count

      - quota=$etcd_quota

      - initial=$etcd_initial3

      - peer=$etcd_peer

      - client=$etcd_client

      - advertise=$etcd_advertise

      - token=$etcd_token

      - cluster=$etcd_cluster

      - state=$etcd_state

eof


服务拉起

docker-compose -f $etcd_dir/etcd.yml up -d


环境测试

docker exec -it etcd1 /etcd/work/etcd-v3.3.2-linux-amd64/etcdctl member list

docker exec -it etcd1 /etcd/work/etcd-v3.3.2-linux-amd64/etcdctl cluster-health


猜你喜欢

转载自blog.51cto.com/13272050/2464982