Не удается запустить сценарий bash как службу | Танос - Прометей
Я чувствую, что пробовал все, но все равно терпел неудачу. Я пытаюсь создать кластер из Таноса, Прометея и Графаны. Я написал сценарий, который отлично работает в терминале, но не запускается как служба. Моя среда:
-
Ubuntu 20.04.2
-
Docker 20.10.5
И мои файловые зависимости таковы:
Это содержание моего сценария:
#!/bin/bash
# Script to build monitoring cluster with Thanos support.
# Actions: deploy | destroy
#
#
# Usage: ./setup deploy [ prom|sidecar|querier|grafana|all ] | destroy [prom|sidecar|querier|grafana|volume|network|all]
#
#set -x
# Params
#*******************************************************************************************************#
action="$1"
component="$2"
#*******************************************************************************************************#
#---------------------------------Creating volumes for persistant data storage--------------------------#
create_volume() {
echo "--> Creating persistent volumes for prometheus servers"
for item in 1 2 3
do
mkdir -p $(pwd)/prometheusStorage$item
done
}
#---------------------------------------End of creating-------------------------------------------------#
#*******************************************************************************************************#
#----------------------------------------Creating docker network----------------------------------------#
create_docker_network() {
docker network create thanos &> /dev/null
}
#----------------------------------------End of creating network----------------------------------------#
#*******************************************************************************************************#
#----------------------------------------Deploying prometheus instances---------------------------------#
deploy_prom() {
echo "--> Deploying prometheus instances"
for item in 1 2 3
do
echo "--> Deploying prometheus instance #$item"
docker run -d --net=thanos --rm -v $(pwd)/prometheus0$item.yml:/etc/prometheus/prometheus.yml -p 909$item:909$item -v $(pwd)/prometheusStorage$item:/prometheus -u root --name prometheus0$item quay.io/prometheus/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus \
--web.listen-address=:909$item \
--web.enable-lifecycle \
--storage.tsdb.retention.time=5d \
--storage.tsdb.min-block-duration=5m \
--storage.tsdb.max-block-duration=5m \
--web.enable-admin-api &> /dev/null
sleep 3
done
#-------------------------------->Checking for launched Prometheus containers
curl http://localhost:9091 &> /dev/null
prom01=$?
sleep 1
curl http://localhost:9092 &> /dev/null
prom02=$?
sleep 1
curl http://localhost:9093 &> /dev/null
prom03=$?
sleep 1
if [[ prom01 -eq 0 && prom02 -eq 0 && prom03 -eq 0 ]] ; then
echo "--> Prometheus 01, 02 ,03 got deployed on 9091,9092,9093 port respectively"
else
echo "--> Error occurred while deploying prometheus server"
exit 1
fi
echo "*"
#------------------------------->End of checking
}
#----------------------------------------End of deploying instances-------------------------------------#
#*******************************************************************************************************#
#----------------------------------------Deploying sidecar instances------------------------------------#
deploy_sidecar() {
if [ ! -d "$(pwd)/object-storage" ]; then
mkdir object-storage
fi
echo "--> Deploying thanos sidecar for each prometheus instance in the cluster"
for item in 1 2 3
do
echo "--> Deploying sidecar for prometheus instance #$item"
docker run -d --rm --net=thanos -v $(pwd)/prometheus0$item.yml:/etc/prometheus/prometheus.yml -v $(pwd)/bucket_config.yaml:/tmp/bucket_config.yaml --name prometheus0$item-sidecar -u root \
-v $(pwd)/prometheusStorage$item:/tmp/prometheusStorage \
-v $(pwd)/object-storage:/object-storage \
quay.io/thanos/thanos:main-2021-03-22-d173bcb9 sidecar \
--http-address 0.0.0.0:1909$item \
--grpc-address 0.0.0.0:1919$item \
--reloader.config-file /etc/prometheus/prometheus.yml \
--prometheus.url http://prometheus0$item:909$item \
--tsdb.path /tmp/prometheusStorage \
--objstore.config-file /tmp/bucket_config.yaml &> /dev/null
SCRC=$?
sleep 3
done
echo "--> Using Local volume as a object storage"
if [ $SCRC -eq 0 ]; then
echo "--> All sidecars got deployed successfully for all prometheus instances"
else
echo "--> Error while deploying sidecars"
exit 1
fi
echo "*"
}
#----------------------------------------End of deploying instances-------------------------------------#
#*******************************************************************************************************#
#----------------------------------------Deploying querier instances------------------------------------#
deploy_querier(){
docker run -d --rm --net=thanos --name thanos-querier -p 29090:29090 quay.io/thanos/thanos:main-2021-03-22-d173bcb9 query \
--http-address 0.0.0.0:29090 \
--query.replica-label replica \
--store prometheus01-sidecar:19191 \
--store prometheus02-sidecar:19192 \
--store prometheus03-sidecar:19193 &> /dev/null
if [ $? -eq 0 ]; then
echo "--> Deployed thanos querier component"
else
echo "--> Error while deploying thanos querier component"
exit 1
fi
echo "*"
}
#----------------------------------------End of deploying instance--------------------------------------#
#*******************************************************************************************************#
#--------------------------------------------Deploying grafana------------------------------------------#
deploy_grafana() {
echo "--> Deploying single instance of grafana"
docker run -d --name grafana --net=thanos -p 3000:3000 quay.io/bitnami/grafana:latest &> /dev/null
if [ $? -eq 0 ]; then
echo "--> Grafana is up and running on 3000 port"
else
echo "--> Error while deploying grafana"
exit 1
fi
echo "*"
echo "--> Cluster is up and running"
}
#--------------------------------------------End of deploying grafana-----------------------------------#
#*******************************************************************************************************#
case "$action" in
deploy)
case "$component" in
prom)
# preparing persistance volumes.
create_volume
# creating a namespace for docker
create_docker_network
# deploying prometheuse server:
echo "*"
deploy_prom
;;
sidecar)
deploy_sidecar
;;
querier)
# Deploying thanos querier which queries the thanos side car endpoint over gRPC
deploy_querier
;;
grafana)
# deploy grafana instance
deploy_grafana
;;
all)
create_volume
create_docker_network
deploy_prom
deploy_sidecar
deploy_querier
deploy_grafana
;;
*)
echo "Choose an component to deploy from prom|sidecar|querier|grafana|all"
exit 1
;;
esac
;;
destroy)
case "$component" in
prom)
for container in prometheus01 prometheus02 prometheus03
do
echo "--> Removing $container residue."
docker container stop $container &> /dev/null
done
sleep 3
docker network ls | grep thanos &> /dev/null
if [ $? -eq 0 ]; then
echo "--> Removing docker network [thanos]"
docker network rm thanos &> /dev/null
fi
;;
sidecar)
for container in prometheus01-sidecar prometheus02-sidecar prometheus03-sidecar
do
echo "--> Removing $container residue."
docker container stop $container &> /dev/null
done
;;
querier)
echo "--> Removing querier residue."
docker container stop thanos-querier &> /dev/null
;;
grafana)
echo "--> Removing grafana residue."
docker container stop grafana &> /dev/null
docker ps -a | grep grafana &>/dev/null
if [ $? -eq 0 ]; then
docker rm -f grafana &> /dev/null
fi
;;
volume)
for item in prometheusStorage1 prometheusStorage2 prometheusStorage3 object-storage
do
if [ -d "$item" ]; then
echo "--> Deleting mounted storage volume: $item"
rm -rf $item
fi
done
;;
network)
docker network ls | grep thanos &> /dev/null
if [ $? -eq 0 ]; then
echo "--> Removing docker network [thanos]"
docker network rm thanos &> /dev/null
fi
clear
;;
all)
for container in prometheus01 prometheus02 prometheus03 prometheus01-sidecar prometheus02-sidecar prometheus03-sidecar thanos-querier grafana
do
docker ps | grep $container &> /dev/null
UPRC=$?
if [ "$UPRC" -eq 0 ]; then
for container in prometheus01 prometheus02 prometheus03 prometheus01-sidecar prometheus02-sidecar prometheus03-sidecar thanos-querier grafana
do
echo "--> Removing $container residue"
docker container stop $container &> /dev/null
done
CONTAINERRC=$?
if [ "$CONTAINERRC" -eq 0 ]; then
docker ps -a | grep grafana | awk '{print $1}' | xargs docker rm -f &>/dev/null
if [ $? -eq 0 ]; then
docker rm -f grafana &> /dev/null
fi
echo "*"
echo "--> All containers drained out"
else
echo "--> Error while stopping grafana container"
exit 1
fi
else
CLUSTERDOWN=yes
fi
done
docker network ls | grep thanos &> /dev/null
if [ $? -eq 0 ]; then
echo "--> Removing docker network [thanos]"
docker network rm thanos &> /dev/null
fi
if [ ! -z "$CLUSTERDOWN" ]; then
echo "--> Cluster is already down, nothing to tear off"
exit 0
fi
;;
*)
echo "Choose an component to destroy from prom|sidecar|querier|grafana|all"
exit 1
;;
esac
;;
*)
echo "Usage: $0 deploy [ prom|sidecar|querier|grafana|all ] | destroy [ prom|sidecar|querier|grafana|all ]"
exit 1
;;
esac
А это мой
systemd
служба
[Unit]
Description=ethtool script
[Service]
ExecStart=/usr/local/setup.sh deploy all
[Install]
WantedBy=multi-user.target
Проблема в том, что если скрипт запускается как служба, он не может запустить образ докера prometheus, но если я запускаю его с терминала, он работает как шарм. Остальные образы работают нормально. Не знаю, в чем проблема. Любая помощь приветствуется. заранее спасибо
1 ответ
Единственная проблема с этим была о . Я не знаю, почему именно это произошло, но когда я изменился
$(pwd)
с
absolute path
файла