对于后台任务一般是不建议在容器中运行的,但是如果我们为了简化应用的部署,可能会使用后台任务进行服务的管理,类似的
工具很多,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. java异常的基本概念和处理流程

    一.异常的基本概念 在java中把导致程序中断运行的情况分为两种,一种就是异常,而另外一种叫做错误.所有异常的基类是Exception,错误的基类是Error.Exception是在java程序中可以 ...

  2. Stopwatch 类用于计算程序运行时间

    Stopwatch 类 命名空间:System.Diagnostics.Stopwatch 实例化:Stopwatch getTime=new Stopwatch(); 开始计时:getTime.St ...

  3. swiper4基础

    这段时间在公司实习做前端,感觉前端没学习到很多前端框架,接口那些都是写好的,只需要调用就好,感觉没什么好记的,唯一觉得有必要记的就是swiper轮播了,在前端做网站的时候经常用到swiper做公告,图 ...

  4. Ubuntu中的两套网络连接管理方式

     版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/haifeng_gu/article/details/78286895 Linux里面有两套管理网络 ...

  5. Spring怎么管理事务?

    我们一般通过aop管理事务,就是把代码看成一个纵向有序的,然后通过aop管理事务,就好比增删改的时候需要开启一个事务,我们给他配置一个required,required就是有事务就执行事务,没有就给他 ...

  6. Django---ORM的常用字段和自定义字段,DjangoORM字段与数据库类型对应,字段参数和Meta的参数,Django的admin操作,13中orm操作方法,单标的双下方法

    Django---ORM的常用字段和自定义字段,DjangoORM字段与数据库类型对应,字段参数和Meta的参数,Django的admin操作,13中orm操作方法,单标的双下方法 一丶ORM常用字段 ...

  7. Node.js 连接 MySQL数据库

    安装指令:npm install mysql var mysql = require("mysql");console.log(mysql); // 创建链接对象 var conn ...

  8. selenium 开启开发者工具(F12)

    selenium 开启开发者工具(F12) options = webdriver.ChromeOptions(); options.add_argument("--auto-open-de ...

  9. Swaks绕过SPF验证进行邮件伪造

    0x00 swaks简介 Swaks是一个功能强大,灵活,可编写脚本,面向事务的SMTP测试工具,由John Jetmore编写和维护. 目前Swaks托管在私有svn存储库中.官方项目页面是http ...

  10. Httpd服务入门知识-Httpd服务常见配置案例之DSO( Dynamic Shared Object)加载动态模块配置

    Httpd服务入门知识-Httpd服务常见配置案例之DSO( Dynamic Shared Object)加载动态模块配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.加载动 ...