我们如何使用kubernetes创建服务依赖关系 [英] How can we create service dependencies using kubernetes

查看:117
本文介绍了我们如何使用kubernetes创建服务依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个服务.一个包含2个用于Web应用程序的pod副本,该副本依赖于另一个后端服务,该后端服务具有用于MySQL容器的pod(2个副本).

I have 2 services. One containing 2 pod replicas for a web application which is dependent on another backend service having pod (2 replicas) for MySQL containers.

Web应用程序使用后端数据库服务设置的环境变量.我把所有的json都放在同一个目录中.

The web application uses environment variables set by backend DB service. I've all the json inside the same directory.

是否有任何方法来表达依赖关系,以便kubectl在启动Web应用程序服务之前始终创建(并运行)后端Pod和服务?我已经使用kubedeam创建了集群.

Is there any way to express the dependencies so that kubectl always create (and run) backend pods and services before it starts the web application service? I have used kubedeam to create the cluster.

推荐答案

我可以提出两种解决方案:

I can suggest two solutions:

首先,将一个初始化容器附加到Web服务器,该容器等待MySQL启动并运行.部署将如下所示:

First, to attach an init container to the web servers that waits until MySQL is up and running. The deployment would be something like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 2
  template:
    metadata:
      labels:
        app: web
    spec:
      initContainers:
      - name: init-wait
        image: alpine
        command: ["sh", "-c", "for i in $(seq 1 300); do nc -zvw1 mysql 3306 && exit 0 || sleep 3; done; exit 1"]
      containers:
      - name: web
        image: web-server
        ports:
        - containerPort: 80
          protocol: TCP

它使用netcat尝试每3秒在端口3306上启动到mysql服务的TCP连接.一旦建立连接,init容器将结束,Web服务器将正常启动.

It uses netcat to try to start a TCP connection to the mysql service on the port 3306 every 3 seconds. Once it achieves to connect, the init-container ends and the web server starts normally.

第二个选项是使用 Mirantis AppController .它允许您在服务器和数据库部署之间根据需要创建依赖项对象.查看他们的回购以获取完整文档.

The second option is to use Mirantis AppController. It allows you to create dependency objects as you need between server and database deployments. Check their repo for a full documentation.

这篇关于我们如何使用kubernetes创建服务依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆