myENA / consul-backinator

Command line Consul backup and restore utility supporting KVs, ACLs and Queries

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Discution] How to schedulle this a Docker orchestration manager like Swarm or K8s?

Sispheor opened this issue · comments

Hi,

Thanks for your job. I just tried it from a standalone container and it work perfectly.

I would like to integrate it to my docker compose. As cron jobs are not yet implemented in Swarm, I would like to know if there is a best practice for this?

Should I:

  • Wrap your image with a crontab based container?
  • Wrap your image with a scheduler script ?
  • PR the project by implementing a daemon mode with a cron lib?
  • stop trying to backup stuff and start praying god for keeping my data alive
  • other ideas?

Many thanks by advance for your time.

This is what I finally use

# BUILD:
# docker build --force-rm=true -f consul-backinator-cron.dockerfile -t consul-backinator-cron .

# RUN:
# docker run -it consul-backinator-cron

FROM golang:stretch as builder

RUN mkdir /app
RUN apt-get install git
RUN cd /go/src/ && git clone https://github.com/myENA/consul-backinator.git

# ADD . /go/src/consul-backinator
WORKDIR /go/src/consul-backinator

# Go dep
RUN go get -d ./...

# Build a standalone binary
RUN ls && export GOPATH=$GOPATH:/go && set -ex && \
  CGO_ENABLED=0 go build \
        -tags netgo \
        -o /app/consul-backinator \
        -v -a \
        -ldflags '-extldflags "-static"' && \
  ls

# Create the second stage with a basic image.
# this will drop any previous
# stages (defined as `FROM <some_image> as <some_name>`)
# allowing us to start with a fat build image and end up with
# a very small runtime image.

FROM ubuntu:latest

# add compiled binary
COPY --from=builder /app/consul-backinator /consul-backinator

# Create the log file to be able to run tail forever
RUN touch /var/log/cron.log

# Install Cron
RUN apt-get update && apt-get -y install cron

# Add crontab file in the cron directory
ADD consul_backup_crontab /consul_backup_crontab

# Give the job to the crontab
RUN cat /consul_backup_crontab | crontab

# Run the command on container startup
CMD cron -f

#  To check if the job is scheduled
# docker exec -ti <your-container-id> bash -c "crontab -l"
# To check if the cron service is running
# docker exec -ti <your-container-id> bash -c "pgrep cron"

Example content of consul_backup_crontab file.

*/10 * * * * /consul-backinator backup --addr ktm35.gre.hpecorp.net:8500 --scheme http --dc dc1 --file backup.consul.$(date -d"-0 days" +\%Y-\%m-\%d).bak > /proc/1/fd/1 2>/proc/1/fd/2
# Don't remove the empty line at the end of this file. It is required to run the cron job

That should work. You could get a much smaller image by using the alpine image for the last stage. The busybox cron works well and we've used it in a few containers.

To the points above I have considered adding a daemon mode but have waffled on how exactly to structure that function. I can't really see any reason to demonize for anything other than backup.

I have had questions though as to weather it should just be scheduled (cron like) and/or setup a watcher to backup on change.

Thoughts?

You are right, the final image size is now 37 MB.

Here is the docker file

# BUILD:
# docker build --force-rm=true -f consul-backinator-cron.dockerfile -t consul-backinator-cron .

# RUN:
# docker run -it consul-backinator-cron

FROM golang:stretch as builder

RUN mkdir /app
RUN apt-get install git
RUN cd /go/src/ && git clone https://github.com/myENA/consul-backinator.git

# ADD . /go/src/consul-backinator
WORKDIR /go/src/consul-backinator

# Go dep
RUN go get -d ./...

# Build a standalone binary
RUN ls && export GOPATH=$GOPATH:/go && set -ex && \
  CGO_ENABLED=0 go build \
        -tags netgo \
        -o /app/consul-backinator \
        -v -a \
        -ldflags '-extldflags "-static"' && \
  ls

# Create the second stage with a basic image.
# this will drop any previous
# stages (defined as `FROM <some_image> as <some_name>`)
# allowing us to start with a fat build image and end up with
# a very small runtime image.

FROM busybox

# add compiled binary
COPY --from=builder /app/consul-backinator /consul-backinator

# Add crontab file in the cron directory
ADD consul_backup_crontab /var/spool/cron/crontabs/consul_backup_crontab

# Give the job to the crontab
RUN crontab /var/spool/cron/crontabs/consul_backup_crontab

# Run the command on container startup
CMD crond -f

#  To check if the job is scheduled
# docker exec -ti <your-container-id> bash -c "crontab -l"
# To check if the cron service is running
# docker exec -ti <your-container-id> bash -c "pgrep cron"

and cronfile

* * * * * /consul-backinator backup --addr ktm35.gre.hpecorp.net:8500 --scheme http --dc dc1 --file backup.consul.$(date +\%Y-\%m-\%d).bak > /proc/1/fd/1 2>/proc/1/fd/2
# Don't remove the empty line at the end of this file. It is required to run the cron job