gpt4 book ai didi

kubernetes - 为什么要在部署之前在单个 Kubernetes 配置文件中指定服务?

转载 作者:行者123 更新时间:2023-12-04 12:27:27 25 4
gpt4 key购买 nike

我试图理解为什么 kubernetes 文档 recommend在一个配置文件中部署之前指定服务:

The resources will be created in the order they appear in the file. Therefore, it’s best to specify the service first, since that will ensure the scheduler can spread the pods associated with the service as they are created by the controller(s), such as Deployment.



这是否意味着在 kubernetes 集群节点之间传播 pod?

我使用以下配置进行了测试,其中部署位于服务之前,并且 pod 在节点之间分布,没有任何问题。
apiVersion: apps/v1
kind: Deployment
metadata:
name: incorrect-order
namespace: test
spec:
selector:
matchLabels:
app: incorrect-order
replicas: 2
template:
metadata:
labels:
app: incorrect-order
spec:
containers:
- name: incorrect-order
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: incorrect-order
namespace: test
labels:
app: incorrect-order
spec:
type: NodePort
ports:
- port: 80
selector:
app: incorrect-order

另一个 explanation是在这种情况下不会为 Pod 设置一些带有服务 URL 的环境变量。但是,如果配置在一个文件中,如上面的示例,它也可以正常工作。

您能否解释一下为什么在一个配置文件的情况下最好在部署之前指定服务?或者可能是一些过时的建议。

最佳答案

如果您使用 DNS作为服务发现,创建顺序无关紧要。

如果是 Environment Vars (K8S 提供服务发现的第二种方式)顺序很重要,因为一旦将变量传递给 开始 pod,如果服务定义发生变化,它们以后不能修改。

因此,如果您的服务已部署 之前 你启动你的 pod,服务环境变量被注入(inject)到链接的 pod 中。

如果您创建一个带有标签的 Pod/Deployment 资源,则该资源将在最后一个创建后通过服务公开(使用适当的选择器来指示要公开的资源)。

关于kubernetes - 为什么要在部署之前在单个 Kubernetes 配置文件中指定服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50409392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com