容器内多进程方法
思路:容器启动时启动脚本,脚本内写好你需要启动的进程
(第一个进程需要放在后台运行,不然脚本后面的命令就无法运行了,最后一个进程需要放在前台运行,结束即容器销毁)
场景:需要将容器内日志通过flume推送至kafka,flume用的华为的MRS组件
实现:
1.修改Dockerfile,增加脚本,wget客户端包,修改启动命令为启动脚本
2.构建镜像
脚本内容:
#!/bin/bash
# Start the first process
ln -s /usr/local/openjdk-8 /usr/local/java-se-8u41-ri
chmod 755 -R /flume/fusioninsight-flume-1.9.0/bin/*
nohup bash /flume/fusioninsight-flume-1.9.0/bin/flume-manage.sh start &
ps aux |grep flume |grep -q -v grep
PROCESS_1_STATUS=$?
echo "flume status..."
echo $PROCESS_1_STATUS
if [ $PROCESS_1_STATUS -ne 0 ]; then
echo "Failed to start my_first_process: $PROCESS_2_STATUS"
exit $PROCESS_1_STATUS
fi
sleep 5
# Start the second process
java -jar /data-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
ps aux |grep data-0.0.1-SNAPSHOT.jar |grep -q -v grep
PROCESS_2_STATUS=$?
echo "data status..."
echo $PROCESS_2_STATUS
if [ $PROCESS_2_STATUS -ne 0 ]; then
echo "Failed to start my_second_process: $PROCESS_2_STATUS"
exit $PROCESS_2_STATUS
fi
# 每隔60秒检查进程是否运行
while sleep 60; do
ps aux |grep thread1 |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep thread2 |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit 1
fi
目前是用的这种方式实现的,自己记录分享一下,脚本也是从网上搞来自己修改的,有更好的建议非常欢迎留言告知!