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

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

问题描述

我有 2 个服务.一个包含用于 Web 应用程序的 2 个 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:

首先,将 init 容器附加到 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-container 结束,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天全站免登陆