对于后台任务一般是不建议在容器中运行的,但是如果我们为了简化应用的部署,可能会使用后台任务进行服务的管理,类似的
工具很多,supervisor,systemd , init.d 同时对于docker 的alpine 容器镜像我们可以使用openrc,以下是一个简单的demo,也是
借鉴子haproxy 的ingress 组件

目的

通过使用openrc 管理容器中haproxy 服务的启动(可以在修改配置之后,重启服务,而不用重新启动容器)

环境准备

包含了haproxy 的监控,以及dataplaneapi 还有dashboard

  • docker-compose 文件
 
version: "3"
services:
    grafana:
     image: grafana/grafana
     ports:
     - "3000:3000"
    prometheus:
     image: prom/prometheus
     volumes:
     - "./prometheus.yml:/etc/prometheus/prometheus.yml"
     ports:
     - "9090:9090"
    haproxy:
     image: dalongrong/dumb-init-haproxy-dataplan:2.0.5
     build: ./
     volumes:
     - "./haproxy.cfg:/etc/haproxy/haproxy.cfg"
     ports:
     - "80:80"
     - "6379:6379"
     - "5555:5555"
     - "8404:8404"
     - "8080:8080"
     - "9000:9000"
     - "9001:9001"
     - "9002:9002"
     - "1000-1005:1000-1005"
     - "10080:10080"
    nginx1:
     image: nginx
     ports:
     - "8090:80"
    nginx2:
     image: nginx
     ports:
     - "8091:80"      
  • haproxy dockerfile
FROM haproxy:2.0.5-alpine
# 添加依赖的服务openrc
RUN apk update && apk add --no-cache openrc htop
## 使用dumb-init 接管进程1
RUN wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.2/dumb-init_1.2.2_amd64
RUN chmod +x /usr/local/bin/dumb-init
COPY start.sh /start.sh
RUN chmod +x /start.sh
COPY haproxy /etc/init.d/haproxy
COPY dataplaneapi /usr/local/sbin/dataplaneapi
RUN chmod +x /usr/local/sbin/dataplaneapi
RUN chmod +x /etc/init.d/haproxy
ENTRYPOINT [ "/usr/local/bin/dumb-init","--","/start.sh" ]
  • start.sh
    很简单的一个脚本,规避容器停止的
 
#!/bin/sh
set -e
while true 
do
    sleep 10
    echo "demo"
done
 
  • prometheus 监控配置
scrape_configs:
  - job_name: haproxy
    metrics_path: /metrics
    scrape_interval: 10s
    scrape_timeout: 10s
    static_configs:
      - targets: ['haproxy:8404']
  • openrc haproxy 启动脚本
#!/bin/sh
case "$1" in 
start)
   if [ -e /var/run/haproxy.pid ]; then
      echo haproxy is running, pid=`cat /var/run/haproxy.pid`
      exit 1
   else
      haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
   fi   
   ;;
stop)
   kill -USR1 `cat /var/run/haproxy.pid`
   rm /var/run/haproxy.pid
   ;;
restart)
   $0 apply
   ;;
reload)
   $0 apply
   ;;
status)
   if [ -e /var/run/haproxy.pid ]; then
      echo haproxy is running, pid=`cat /var/run/haproxy.pid`
   else
      echo haproxy is NOT running
      exit 1
   fi
   ;;
apply)   
   if [ -e /var/run/haproxy.pid ]; then
      haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)
   else
      $0 start
   fi
   ;;
push_apply)   
   $0 apply
   ;;
validate)   
   haproxy -c -f /etc/haproxy/haproxy.cfg
   ;;
*)
   echo "Usage: $0 {start|stop|status|reload}"
esac
exit 0

启动&&效果

  • 启动
docker-compose build
docker-compose up -d
  • 进入容器启动haproxy
docker-compose exec haproxy sh
service haproxy start
  • 效果

容器服务

grafana(导入了haproxy2.0 的配置)

haproxy 代理服务

promethesu 服务

haproxy stats

说明

以上是一个简单的操作,实际上类似的需求在实际中还是有的(尽管推荐大家容器只跑一个服务),比如kubernetes 的ingress 组件好多就用了
这种方式解决运行的问题,比如以下nginx-ingress 的

参考资料

https://wiki.gentoo.org/wiki/OpenRC
https://github.com/OpenRC/openrc
https://wiki.alpinelinux.org/wiki/Writing_Init_Scripts
https://github.com/rongfengliang/alpine-docker-openrc-haproxy

使用openrc 管理容器中的服务的更多相关文章

  1. [docker] 管理docker容器中的数据

    之前我们介绍了Docker的基本概念(前面的没翻译...),了解了如何使用Docker镜像进行工作,并且学习了网 络和容器之间的链接.这一节我们将讨论如何管理容器中及容器之间的数据. 我们将查看下面两 ...

  2. Chris Richardson微服务翻译:微服务架构中的服务发现

    Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关 构建微服务之微服务架构的进程通讯 微服务架构中的服务发现(本文) 微服务之事件驱动的数据管理 微服 ...

  3. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  4. 在win10 docker启动的centos容器中安装nginx

    我是在win10机器上搭建了一个docker,在docker启动了centos容器,在centos中安装nginx. 安装配置docker 直接在官网下载docker for windows:http ...

  5. 一个docker容器中运行多个服务还是弄一堆docker容器运行?

    不建议直接在单个 Docker 容器中运行多个程序. 以 2017年 10 月18 日 Docker 官方支持 Kubernetes 为分水岭计算,Kubernetes 赢得容器编排之战的最终胜利已经 ...

  6. 无需安装 vsftpd , 直接使用 FTP 来管理 docker 容器中的文件

    无图无真相,先放个效果图:     背景 使用 docker 来跑一些服务很方便,但是有的时候想管理容器里面的文件却很麻烦 -- 一般常规做法有3种: 通过数据卷或数据卷容器的方式 启动容器的时候时候 ...

  7. laravel学习:php写一个简单的ioc服务管理容器

    php写一个简单的ioc服务管理容器 原创: 陈晨 CoderStory 2018-01-14 最近学习laravel框架,了解到laravel核心是一个大容器,这个容器负责几乎所有服务组件的实例化以 ...

  8. Docker windows nano server容器中安装ssh实现远程登录管理

    [问题] 使用ServiceMonitor.exe作为前台进程运行起来的容器无法attach. 无法远程连接到运行中的容器中进行管理. [解决方法] 在container中新建管理员用户,通过SSH实 ...

  9. 我们都可以把它放 Sidecar 容器中,这样微服务具备了 Super power,一种超能力

    云原生时代,微服务如何演进? 原创 李响 阿里技术 2020-08-28   https://mp.weixin.qq.com/s/KQG2U8_aotDL4YFB8ee6Zw 一  微服务架构与云原 ...

随机推荐

  1. linux下僵尸进程的发现与处理

    一.概述 僵尸进程是怎么产生的 当子进程退出时,父进程没有调用wait函数或者waitpid()函数等待子进程结束,又没有显式忽略SIGCHLD信号,那么它将一直保持在僵尸状态,如果这时父进程结束了, ...

  2. - Git常用命令 基础 总结 MD

    目录 目录 Git常用命令 帮助 help 常用操作 初始化 clone init 提交 push 暂存 更新 撤销修改 分支 branch 查看分支 创建分支 切换分支 checkout 删除分支 ...

  3. win10下apache superset的使用

    官方文档:http://superset.apache.org/ 一.环境准备 安装python3即3.4以上版本 二.python创建一个虚拟环境用来作为superset的容器 -pip3 inst ...

  4. 简单实现python调用c#dll动态链接库

    在python调用c#dll库时要先安装库clr,即安装pythonnet,参考文章:https://www.cnblogs.com/kevin-Y/p/10235125.html(为在python中 ...

  5. Java静态变量初始化的坑

    class SingleTon { private static SingleTon singleTon = new SingleTon(); public static int count1; pu ...

  6. intel AVX指令集

    听说这 AVX 很牛,,支持Win7,大幅提高游戏浮点运算性能warning C4752: 发现 Intel(R) 高级矢量扩展:请考虑使用 /arch:AVX

  7. python基础09--闭包,装饰器

    1.1 闭包 1.函数作为第一类对象,支持赋值给变量,作为参数传递给其它函数,作为其它函数的返回值,支持函数的嵌套,实现了__call__方法的类实例对象也可以当做函数被调用 2.s= func  - ...

  8. python数据分析三剑客之: Numpy

    数据分析三剑客之: Numpy 一丶Numpy的使用 ​ numpy 是Python语言的一个扩展程序库,支持大维度的数组和矩阵运算.也支持针对数组运算提供大量的数学函数库 创建ndarray # 1 ...

  9. C++ 中的静态成员函数与静态成员变量

    于CSDN 2014-01-17 与静态数据成员一样,静态成员函数是类的一部分,而不是对象的一部分.如果要在类外调用公用的静态成员函数,要用类名和域运算符"∷".如Box∷volu ...

  10. Django:RestFramework之-------解析器

    7.解析器 依靠Content-Type对用户的请求体中的数据进行解析. 7.1django中的解析器 前戏:django中的request.POST/request.body request.POS ...