31z4 / zookeeper-docker

Docker image packaging for Apache Zookeeper
MIT License
285 stars 244 forks source link

Add support for custom init scripts #167

Open bianjp opened 3 months ago

bianjp commented 3 months ago

See #145

Add support for custom init scripts which can be mounted to /docker-entrypoint.d/ directory.

This will allow more easier, flexible and elegant customization. For example, to deploy a ZooKeeper cluster in Kubernetes, user can mount a custom init script to calculate ZOO_MY_ID for each instance.

The idea and code is borrowed from official Nginx image.

There are two kinds of init scripts:

Note:

Kubernetes example to mount a init script from ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: zookeeper-config
data:
  set-my-id.envsh: |
    #!/bin/bash
    HOST=`hostname -s`
    DOMAIN=`hostname -d`
    if [[ $HOST =~ (.*)-([0-9]+)$ ]]; then
        NAME=${BASH_REMATCH[1]}
        ORD=${BASH_REMATCH[2]}
    else
        echo "Failed to parse name and ordinal of Pod"
        exit 1
    fi
    servers=
    for (( i=1; i<=$ZOO_SERVERS_COUNT; i++ )); do
        [[ -n "$servers" ]] && servers+=" "
        servers+="server.$i=$NAME-$((i-1)).$DOMAIN:2888:3888;2181"
    done
    export ZOO_MY_ID="$((ORD+1))"
    export ZOO_SERVERS="$servers"
    echo "Setting ZOO_MY_ID=$ZOO_MY_ID"
    echo "Setting ZOO_SERVERS=$ZOO_SERVERS"
---
apiVersion: apps/v1
kind: StatefulSet
spec:
  template:
    spec:
      volumes:
        - name: zookeeper-config
          configMap:
            name: zookeeper-config
            items:
              - key: set-my-id.envsh
                path: set-my-id.envsh
                mode: 0555

      containers:
        - name: kubernetes-zookeeper
          image: zookeeper:3.9.2
          volumeMounts:
            - name: zookeeper-config
              mountPath: /docker-entrypoint.d/set-my-id.envsh
              subPath: set-my-id.envsh
bianjp commented 1 month ago

@31z4 Can you review this PR?

31z4 commented 3 weeks ago

Hi @bianjp.

As mentioned in https://github.com/31z4/zookeeper-docker/pull/145#issuecomment-2220158270 it's already possible to mount a custom zookeeper-env.sh and therefore adjust Zookeeper configuration via a shell script. So I don't think it's worth it to increase the complexity of the current image entrypoint.

Maybe extend the "Advanced configuration" section of the docs with an example of using zookeeper-env.sh to customize the config?

bianjp commented 3 weeks ago

Hi @bianjp.

As mentioned in #145 (comment) it's already possible to mount a custom zookeeper-env.sh and therefore adjust Zookeeper configuration via a shell script. So I don't think it's worth it to increase the complexity of the current image entrypoint.

Maybe extend the "Advanced configuration" section of the docs with an example of using zookeeper-env.sh to customize the config?

Yes, zookeeper-env.sh works, but personally I think it's not elegant, and I double whether it's the intended use case of ZooKeeper maintainer.

Okay if you insist.