Toasobi
ELFK---sh脚本部署篇
本文最后更新于2023年08月29日,已超过494天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
elasticsearch.sh
#!/bin/bash
docker run -d \
--restart=always \
--name elasticsearch \
-p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
docker.elastic.co/elasticsearch/elasticsearch:7.10.1
elasticsearch-head.sh
docker run --restart=always --name dev_elasticsearch_head -di -p 9100:9100 mobz/elasticsearch-head:5
elasticsearch-ports.sh
#!/bin/bash
echo "begin to init docker cotainer"
CONTAINER_NAME="dev_elasticsearch"
ELASTICSEARCH_PORT1=19200
ELASTICSEARCH_PORT2=19300
# 1. 判断容器是否已经存在
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
echo "containerExist result is $containerExist"
# 2. 存在则停止容器并删除容器
if [ $containerExist -gt 0 ]
then
echo "$CONTAINER_NAME exist."
containerId=`docker ps -a | grep -i $CONTAINER_NAME | awk '{print $1}'`
echo "$CONTAINER_NAME container id is $containerId."
pid=`docker stats --format "{{.PIDs}}" --no-stream $CONTAINER_NAME`
# 判断溶质是否运行中,运行中的容器停止
if [ "${pid}" != "0" ]
then
echo "$CONTAINER_NAME is running."
echo "$CONTAINER_NAME will stop."
docker stop $CONTAINER_NAME
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
while (( $containerRunning > 0 ))
do
echo "$CONTAINER_NAME stopping. containerRunning=$containerRunning"
sleep 0.5s
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
done
echo "$CONTAINER_NAME stoped."
else
echo "$CONTAINER_NAME is not running."
fi
# 删除容器
echo "$CONTAINER_NAME will remove."
docker rm $CONTAINER_NAME
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
while (( $containerExist > 0 ))
do
echo "$CONTAINER_NAME removing. containerExist=$containerExist"
sleep 0.5s
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
done
echo "$CONTAINER_NAME remove successfully."
else
echo "$CONTAINER_NAME is not exist."
fi
# 3. 新建容器
docker run -d --name $CONTAINER_NAME -p $ELASTICSEARCH_PORT1:9200 -p $ELASTICSEARCH_PORT2:9300 -e "discovery.type=single-node" -v /home/sunmi/config/elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml docker.elastic.co/elasticsearch/elasticsearch:7.10.1
# 4. 判断容器是否正常创建并且启动
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
containerId=`docker ps -a | grep -i $CONTAINER_NAME | awk '{print $1}'`
if [ $containerExist -gt 0 ]
then
echo "$CONTAINER_NAME was created successfully. new container id is $containerId"
if [ $containerRunning -gt 0 ]
then
echo "$CONTAINER_NAME run successfully"
else
echo "$CONTAINER_NAME run failed"
fi
fi
elk_network.sh
#!/bin/bash
# docker自定义网络初始化
CUSTOM_NETWORK=elk_net
isCustomNetworkExist=`docker network ls | grep ${CUSTOM_NETWORK} | wc -l`
if [[ ${isCustomNetworkExist} -eq 0 ]]
then
docker network create ${CUSTOM_NETWORK}
fi
filebeat.sh
#!/bin/bash
echo "Start initializing the filebeat environment..."
CONTAINER_NAME="dev_filebeat"
YRML_FILE=/home/sunmi/config/filebeat/filebeat.yml
imageReposity=elastic/filebeat:7.10.1
imageVersion=7.10.1
imageRegx="^elastic/filebeat\s*7.10.1\s*"
filePath=/home/sunmi/config/filebeat
logPath=/home/sunmi/config/filebeat/log
removeContainer(){
echo "$CONTAINER_NAME removing ...!"
docker rm $CONTAINER_NAME
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
while (( $containerExist > 0 ))
do
echo "wait $CONTAINER_NAME removing. containerExist=$containerExist"
sleep 0.5s
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
done
echo "$CONTAINER_NAME remove successfully."
}
stopContainer(){
echo "$CONTAINER_NAME stop...!"
docker stop $CONTAINER_NAME
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
while (( $containerRunning > 0 ))
do
echo "$CONTAINER_NAME stopping. containerRunning=$containerRunning"
sleep 0.5s
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
done
echo "$CONTAINER_NAME stoped."
}
pullImage(){
docker pull $imageReposity:$imageVersion
logstashImageExist=`docker images | egrep $imageRegx | wc -l`
while(($logstashImageExist == 0))
do
echo "docker pull $imageReposity:$imageVersion not complete, wait it pull!"
sleep 2s
logstashImageExist=`docker images | egrep $imageRegx | wc -l`
done
}
runAndCheckContainer(){
# 创建并启动容器
#docker run docker.elastic.co/beats/filebeat:7.10.1 setup -E setup.kibana.host=192.168.128.18:15601 -E output.elasticsearch.hosts=["192.168.128.18:19200"]
docker run -d --name=$CONTAINER_NAME --restart=always -v $filePath:/usr/share/filebeat -v $logPath:/var/log/messages $imageReposity:$imageVersion
# 判断容器是否正常创建并且启动
checkContainerCreateAndRun
}
checkContainerCreateAndRun(){
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
containerId=`docker ps -a | grep -i $CONTAINER_NAME | awk '{print $1}'`
if [ $containerExist -gt 0 ]
then
echo "$CONTAINER_NAME was created successfully. new container id is $containerId"
if [ $containerRunning -gt 0 ]
then
echo "$CONTAINER_NAME run successfully"
else
echo "$CONTAINER_NAME run failed"
fi
fi
}
# 1. 查看镜像是否存在,egrep代表正则表达式匹配
logstashImageExist=`docker images | egrep $imageRegx | wc -l`
# 2. 镜像不存在则拉取镜像
if [ $logstashImageExist -gt 0 ]
then
# 镜像已存在
echo "docker image $imageReposity:$imageVersion is exist!"
# 判断容器是否已存在
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
if [ $containerExist -gt 0 ]
then
echo "$CONTAINER_NAME exist!"
# 容器已存在
# 容器是否运行中
if [ $containerRunning -gt 0 ]
then
echo "$CONTAINER_NAME is running!"
# 容器运行中,停止容器
stopContainer
else
echo "$CONTAINER_NAME is not running!"
# 容器没有运行
fi
# 删除容器
removeContainer
else
echo "$CONTAINER_NAME not exist!"
# 容器不存在
fi
# 运行容器并检查运行结果
runAndCheckContainer
else
# 拉取镜像
echo "docker image $imageReposity:$imageVersion is not exist!"
echo "docker pull $imageReposity:$imageVersion ... "
pullImage
# 运行容器并检查运行结果
runAndCheckContainer
fi
kibana.sh
#!/bin/bash
echo "Start initializing the docker.elastic.co/kibana/kibana environment..."
CONTAINER_NAME="dev_kibana"
YRML_FILE=/home/sunmi/config/kibana/kibana.yml
SERVER_PORT=15601
imageReposity=docker.elastic.co/kibana/kibana
imageVersion=7.10.1
imageRegx="^docker.elastic.co/kibana/kibana\s*7.10.1\s*"
removeContainer(){
echo "$CONTAINER_NAME removing ...!"
docker rm $CONTAINER_NAME
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
while (( $containerExist > 0 ))
do
echo "wait $CONTAINER_NAME removing. containerExist=$containerExist"
sleep 0.5s
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
done
echo "$CONTAINER_NAME remove successfully."
}
stopContainer(){
echo "$CONTAINER_NAME stop...!"
docker stop $CONTAINER_NAME
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
while (( $containerRunning > 0 ))
do
echo "$CONTAINER_NAME stopping. containerRunning=$containerRunning"
sleep 0.5s
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
done
echo "$CONTAINER_NAME stoped."
}
pullImage(){
docker pull $imageReposity:$imageVersion
logstashImageExist=`docker images | egrep $imageRegx | wc -l`
while(($logstashImageExist == 0))
do
echo "docker pull $imageReposity:$imageVersion not complete, wait it pull!"
sleep 2s
logstashImageExist=`docker images | egrep $imageRegx | wc -l`
done
}
runAndCheckContainer(){
# 创建并启动容器
docker run -d --name $CONTAINER_NAME -p $SERVER_PORT:5601 -v $YRML_FILE:/usr/share/kibana/config/kibana.yml $imageReposity:$imageVersion
# 判断容器是否正常创建并且启动
checkContainerCreateAndRun
}
checkContainerCreateAndRun(){
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
containerId=`docker ps -a | grep -i $CONTAINER_NAME | awk '{print $1}'`
if [ $containerExist -gt 0 ]
then
echo "$CONTAINER_NAME was created successfully. new container id is $containerId"
if [ $containerRunning -gt 0 ]
then
echo "$CONTAINER_NAME run successfully"
else
echo "$CONTAINER_NAME run failed"
fi
fi
}
# 1. 查看镜像是否存在,egrep代表正则表达式匹配
logstashImageExist=`docker images | egrep $imageRegx | wc -l`
# 2. 镜像不存在则拉取镜像
if [ $logstashImageExist -gt 0 ]
then
# 镜像已存在
echo "docker image $imageReposity:$imageVersion is exist!"
# 判断容器是否已存在
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
if [ $containerExist -gt 0 ]
then
echo "$CONTAINER_NAME exist!"
# 容器已存在
# 容器是否运行中
if [ $containerRunning -gt 0 ]
then
echo "$CONTAINER_NAME is running!"
# 容器运行中,停止容器
stopContainer
else
echo "$CONTAINER_NAME is not running!"
# 容器没有运行
fi
# 删除容器
removeContainer
else
echo "$CONTAINER_NAME not exist!"
# 容器不存在
fi
# 运行容器并检查运行结果
runAndCheckContainer
else
# 拉取镜像
echo "docker image $imageReposity:$imageVersion is not exist!"
echo "docker pull $imageReposity:$imageVersion ... "
pullImage
# 运行容器并检查运行结果
runAndCheckContainer
fi
logstash.sh
#!/bin/bash
echo "Start initializing the logstash environment..."
CONTAINER_NAME="dev_logstash"
YRML_FILE=/home/sunmi/config/logstash/logstash.yml
CONF_DIR=/home/sunmi/config/logstash/conf.d/
PORT_5044=15044
PORT_9600=19600
# 1. 查看镜像是否存在,egrep代表正则表达式匹配
logstashImageExist=`docker images | egrep '^logstash\s*7.10.1\s*' | wc -l`
# 2. 镜像不存在则拉取镜像
if[ $logstashImageExist -gt 0]
then
# logstash:7.10.1 已存在
echo "docker image logstash:7.10.1 is exist!"
# 判断容器是否已存在
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
if[ containerExist -gt 0 ]
then
echo "$CONTAINER_NAME exist!"
# 容器已存在
# 容器是否运行中
if[ containerRunning -gt 0 ]
then
echo "$CONTAINER_NAME is running!"
# 容器运行中,停止容器
stopContainer
else
echo "$CONTAINER_NAME is not running!"
# 容器没有运行
fi
# 删除容器
removeContainer
else
echo "$CONTAINER_NAME not exist!"
# 容器不存在
fi
# 运行容器并检查运行结果
runAndCheckContainer
else
# 拉取镜像
echo "docker image logstash:7.10.1 is not exist!"
echo "docker pull logstash:7.10.1 ... "
pullImage logstash 7.10.1
# 运行容器并检查运行结果
runAndCheckContainer
fi
removeContainer(){
echo "$CONTAINER_NAME removing ...!"
docker rm $CONTAINER_NAME
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
while (( $containerExist > 0 ))
do
echo "wait $CONTAINER_NAME removing. containerExist=$containerExist"
sleep 0.5s
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
done
echo "$CONTAINER_NAME remove successfully."
}
stopContainer(){
echo "$CONTAINER_NAME stop...!"
docker stop $CONTAINER_NAME
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
while (( $containerRunning > 0 ))
do
echo "$CONTAINER_NAME stopping. containerRunning=$containerRunning"
sleep 0.5s
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
done
echo "$CONTAINER_NAME stoped."
}
pullImage(){
imageReposity=$1
imageVersion=$2
docker pull $imageReposity:$imageVersion
logstashImageExist=`docker images | egrep '^$imageReposity\s*$imageVersion\s*' | wc -l`
while(($logstashImageExist == 0))
do
echo "docker pull $imageReposity:$imageVersion not complete, wait it pull!"
sleep 2s
logstashImageExist=`docker images | egrep '^$imageReposity\s*$imageVersion\s*' | wc -l`
done
}
runAndCheckContainer(){
# 创建并启动容器
docker run -d --log-driver json-file --log-opt max-size=100m --log-opt max-file=2 -p $PORT_5044:5044 -p $PORT_9600:9600 --name $CONTAINER_NAME -v $YRML_FILE:/usr/share/logstash/config/logstash.yml -v $CONF_DIR:/usr/share/logstash/conf.d/ logstash:7.10.1
# 判断容器是否正常创建并且启动
checkContainerCreateAndRun
}
checkContainerCreateAndRun(){
containerExist=`docker ps -a | grep -i $CONTAINER_NAME | wc -l`
containerRunning=`docker ps | grep -i $CONTAINER_NAME | wc -l`
containerId=`docker ps -a | grep -i $CONTAINER_NAME | awk '{print $1}'`
if [ $containerExist -gt 0 ]
then
echo "$CONTAINER_NAME was created successfully. new container id is $containerId"
if [ $containerRunning -gt 0 ]
then
echo "$CONTAINER_NAME run successfully"
else
echo "$CONTAINER_NAME run failed"
fi
fi
}