Kubernetes 部署,其中一个 pod 获得不同的环境 [英] Kubernetes deployment where one of the pods gets different env

查看:20
本文介绍了Kubernetes 部署,其中一个 pod 获得不同的环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以使用 kubernetes 进行 AWS 部署,其中我的应用程序具有可变且可扩展的 Pod 数量,但单个主机的 VM 环境不同?

Is there any way I can have an AWS deployment using kubernetes where I have a variable and scalable number of pods for my app BUT the environment on the VM is different for a single host?

基本上,我希望我的一个 Pod 执行其他任何 Pod 都不会一直执行的任务.我打算使用环境变量来控制它.

Basically I want that one of my pods doing a task that no of the other pods will do all the time. I am planning to control this using an environment variable.

推荐答案

你可以有多个部署,但是如果你需要 Pod 之间的唯一性,你应该考虑 statefulsets 部署.但是,在statefulsetdeployment 之间做出决定主要取决于应用程序的类型,这是一个重大决定.所以,使用statefulset 可能不适合作为部署的 100% 替代品.

You can have multiple deployments, however if you are in need of uniqueness between the pods, you should consider statefulsets over deployments. However, deciding between statefulset and deployment is mostly decided by the type of the application and it is a major decision. so, using statefulset may not fit in as a 100% replacement for a deployment.

statefulsets 中,每个 Pod 通过其主机名获得其唯一但非随机的身份.

In statefulsets each pod get its unique but non-random identity via its hostname.

例如,您可以使用以下方法创建 mysql 状态集:

For example, you can create a mysql statefulset using following:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  creationTimestamp: null
  labels:
    app: foo
  name: foo
spec:
  serviceName: mysql
  replicas: 3
  selector:
    matchLabels:
      app: foo
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: foo
    spec:
      containers:
      - image: mysql
        name: mysql
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        image: mysql
        name: mysql

这将创建 pod 的三个副本:

This would create three replicas of the pod:

k get pod -l app=foo
NAME    READY   STATUS    RESTARTS   AGE
foo-0   1/1     Running   0          5m17s
foo-1   1/1     Running   0          5m14s
foo-2   1/1     Running   0          5m10s

现在您可以获取 pod 的主机名,第一个 pod 将始终以 0 为后缀,第二个为 1 后缀,依此类推.

Now you can fetch the hostname of the pods, the first pod will be always suffixed with 0 and 2nd with 1 and so on.

exec -it foo-0 --  hostname
foo-0
k exec -it foo-1 --  hostname
foo-1
k exec -it foo-2 --  hostname
foo-2

你可以使用 hostname 作为选择器来决定哪个 pod 需要什么操作.这里是一本很好的读物.

you can use hostname as selector do decide what action is required on which pod. Here is one very good read on this.

这篇关于Kubernetes 部署,其中一个 pod 获得不同的环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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