对于后台任务一般是不建议在容器中运行的,但是如果我们为了简化应用的部署,可能会使用后台任务进行服务的管理,类似的
工具很多,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. Python批量更改文件名

    一.问题在处理文件或者一些其他信息的时候我们需要更改文件名,那么我们可以写一个程序来修改这些文件名,以减少我们重复的做一件事. 二.解决本次使用的Python,利用的是Python中的OS模块,具体操 ...

  2. CSP2019-S宝典

    目录 CSP2019-S宝典 模板 博客 快读 vim配置 对拍 CSP2019-S注意事项 考前 考时 考后 游记 Day -2(UPDATE:2019-11-14) Day -1(UPDATE:2 ...

  3. golang --for语句

    一条for 语句可以携带一条for子句. for子句可以包含初始化子句.条件子句.后置子句. package main import ( "fmt" ) func main() { ...

  4. 整理:WPF中Binding的几种写法

    原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...

  5. 一,python编程100例

    1.有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? number = (1 ,2,3,4) ...

  6. Python面向对象继承案例

    面向对象三大特性 封装 根据 职责 将 属性 和 方法 封装 到一个抽象的 类 中 继承 实现代码的重用,相同的代码不需要重复的编写 多态 不同的对象调用相同的方法,产生不同的执行结果,增加代码的灵活 ...

  7. Java自学-数字与字符串 装箱和拆箱

    Java中基本类型的装箱和拆箱 步骤 1 : 封装类 所有的基本类型,都有对应的类类型 比如int对应的类是Integer 这种类就叫做封装类 package digit; public class ...

  8. 编写可维护的JavaScript-随笔(六)

    避免空比较 If(item !== null){ item.sort(); Item.forEach(function(item){ //执行代码 } } } 以上判断中item期待的是数组类型的,但 ...

  9. CSS 用法和特性

    一.CSS 基本用法 1.CSS 样式语法 样式是 CSS 最小的语法单元,每个样式包含两部分内容:选择器和声明(规则). 语法: p {font-size:12px; color:#333} 注意: ...

  10. 面试题:什么叫2B树

    在B树的基础上,每个节点有两个域,且他们是有顺序的,在层次上的又满足二叉排序树的性质