为什么要在单个Kubernetes配置文件中部署之前指定服务? [英] Why should I specify service before deployment in a single Kubernetes configuration file?

查看:66
本文介绍了为什么要在单个Kubernetes配置文件中部署之前指定服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解为什么kubernetes文档建议在部署之前在一个配置文件中指定服务:

I'm trying to understand why kubernetes docs recommend to specify service before deployment in one configuration file:

将按照它们在文件中出现的顺序创建资源.因此,最好先指定服务,因为这将确保调度程序可以在由控制器(如部署)创建的Pod上散布与该服务相关联的Pod.

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集群节点之间散布吊舱?

Does it mean spread pods between kubernetes cluster nodes?

我使用以下配置进行了测试,其中在将服务和Pod分配到点头之间之前,没有任何问题的情况下放置了部署.

I tested with the following configuration where a deployment is located before a service and pods are distributed between nods without any issues.

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

另一个解释是,某些带有服务URL的环境变量不会被使用.在这种情况下为豆荚设置.但是,如果配置像上面的示例一样位于一个文件中,它也可以正常工作.

Another explanation is that some environment variables with service URL will not be set for pods in this case. However it also works ok in case a configuration is inside one file like the example above.

请解释一下为什么只有一个配置文件的情况下最好在部署之前指定服务?也许这是一些过时的建议.

Could you please explain why it is better to specify service before the deployment in case of one configuration file? Or may be it is some outdated recommendation.

推荐答案

如果将DNS用作服务发现,则创建的顺序无关紧要.

If you use DNS as service discovery, the order of creation doesn't matter.

对于Environment Vars(K8S提供服务发现的第二种方式),顺序很重要,因为一旦将var传递到 starting 窗格后,如果服务定义发生更改,便无法在以后修改它们

In case of Environment Vars (the second way K8S offers service discovery) the order matters, because once that vars are passed to the starting pod, they cannot be modified later if the service definition changes.

因此,如果之前部署了您的服务,则启动您的Pod,服务环境将注入到链接的Pod中.

So if your service is deployed before you start your pod, the service envvars are injected inside the linked pod.

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

If you create a Pod/Deployment resource with labels, this resource will be exposed through a service once this last is created (with proper selector to indicate what resource to expose).

这篇关于为什么要在单个Kubernetes配置文件中部署之前指定服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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