Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom init scripts #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bianjp
Copy link
Contributor

@bianjp bianjp commented Aug 15, 2024

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:

  • *.envsh: environment script, it will be executed by source command, and can be used to export environment variables which will be visible to docker-entrypoint.sh
  • *.sh: normal script, normally it should have a shebang to declare how to execute it

Note:

  • Files that not ends with .envsh or .sh will be ignored
  • Each init script must have execute permission, otherwise it will be ignored to reduce security risks
  • If the container is started by root user (the default case), the init scripts will be executed after switching to zookeeper user

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
Copy link
Contributor Author

bianjp commented Oct 18, 2024

@31z4 Can you review this PR?

@bianjp bianjp force-pushed the feature-custom-init-scripts branch from 1342460 to 6fea8be Compare October 30, 2024 03:17
@31z4
Copy link
Owner

31z4 commented Nov 7, 2024

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?

@bianjp
Copy link
Contributor Author

bianjp commented Nov 7, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants