diff --git a/Dockerfile b/Dockerfile index 620bcc5..353cf2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -49,8 +49,10 @@ RUN bash ${SALT_BUILD_DIR}/install.sh COPY assets/runtime ${SALT_RUNTIME_DIR} RUN chmod -R +x ${SALT_RUNTIME_DIR} +COPY assets/bin/* /usr/local/bin + # Cleaning tasks -RUN rm -rf ${SALT_BUILD_DIR}/* +RUN rm -rf "${SALT_BUILD_DIR:?}"/* # Entrypoint COPY entrypoint.sh /sbin/entrypoint.sh diff --git a/README.md b/README.md index b8e78ec..35313ad 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ For other methods to install SaltStack please refer to the [Official SaltStack I - [GitPython](#gitpython) - [PyGit2](#pygit2) - [Logs](#logs) + - [Healthcheck](#healthcheck) - [Available Configuration Parameters](#available-configuration-parameters) - [Usage](#usage) - [Shell Access](#shell-access) @@ -292,6 +293,65 @@ docker run --name salt_master --detach \ Check [Available Configuration Parameters](#available-configuration-parameters) section for configuring logrotate. +### Healthcheck + +This image includes a [health check](https://docs.docker.com/engine/reference/builder/#healthcheck) script: `/usr/local/bin/healthcheck` (although it is disable by default). It is useful to check if the `salt-master` service is alive and responding. + +If you are running this image under k8s, you can define a _liveness command_ as explained [here](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-liveness-command). + +If you use `docker-compose` as your container orchestrator, you can add the following entries to your compose file: + +```yml +version: '3' + +services: + master: + container_name: salt_master + image: cdalvaro/saltstack-master:3000.3_2 + healthcheck: + test: ["CMD", "/usr/local/bin/healthcheck"] + start_period: 30s +``` + +(More info available at [compose file](https://docs.docker.com/compose/compose-file/#healthcheck) official documentation) + +Or, if you launch your container [with docker](https://docs.docker.com/engine/reference/run/#healthcheck): + +```sh +docker run --name salt_master --detach \ + --publish 4505:4505 --publish 4506:4506 \ + --health-cmd='/usr/local/bin/healthcheck' \ + --health-start-period=30s \ + --env 'SALT_LOG_LEVEL=info' \ + --volume $(pwd)/roots/:/home/salt/data/srv/ \ + --volume $(pwd)/keys/:/home/salt/data/keys/ \ + --volume $(pwd)/logs/:/home/salt/data/logs/ \ + cdalvaro/saltstack-master:3000.3_1 +``` + +Then you can manually check this info by running the following command: + +```sh +docker inspect --format "{{json .State.Health }}" salt_master | jq +``` + +Then, the output will be something similar to this: + +```json +{ + "Status": "healthy", + "FailingStreak": 0, + "Log": [ + { + "Start": "2020-05-23T16:47:55.1046568Z", + "End": "2020-05-23T16:48:02.3381442Z", + "ExitCode": 0, + "Output": "local:\n True\n" + } + ] +} +``` + ### Available Configuration Parameters Please refer the docker run command options for the `--env-file` flag where you can specify all required environment variables in a single file. This will save you from writing a potentially long docker run command. Alternatively you can use docker-compose. diff --git a/assets/bin/healthcheck b/assets/bin/healthcheck new file mode 100755 index 0000000..241c719 --- /dev/null +++ b/assets/bin/healthcheck @@ -0,0 +1,3 @@ +#!/bin/bash + +salt-call --local status.ping_master 127.0.0.1 diff --git a/assets/build/install.sh b/assets/build/install.sh index 94f6951..0b3b45d 100755 --- a/assets/build/install.sh +++ b/assets/build/install.sh @@ -60,7 +60,7 @@ pip3 install "pygit2==v${PYGIT2_VERSION}" \ ## -P: Allow pip based installations ## -p: Extra-package to install ## -x: Changes the python version used to install a git version of salt -SALT_BOOTSTRAP_OPTS=( -M -N -X -d -P -p salt-api -x "python${PYTHON_VERSION}" ) +SALT_BOOTSTRAP_OPTS=( -M -N -X -d -P -p salt-api -p salt-call -x "python${PYTHON_VERSION}" ) echo "Installing saltstack ..." echo "Option: ${SALT_BOOTSTRAP_OPTS[@]}"