kubectl apply_了解Kubectl Apply命令

2023-05-16

kubectl apply

In this post, we’ll explore how the kubectl apply command works internally. Which will give you a better understanding of how kubernetes works under the hood and make you stand out as a DevOps Engineer.

在本文中,我们将探讨kubectl apply命令在内部如何工作。 这将使您更好地了解kubernetes的工作原理,并使您脱颖而出成为DevOps工程师。

Usually, the kubectl apply command is used to create and update objects in a declarative way.

通常,kubectl apply命令用于以声明方式创建和更新对象。

For instance, to update/create an object defined in nginx.yaml:

例如,要更新/创建在nginx.yaml中定义的对象:

kubectl apply -f nginx.yaml

As a reminder, the previous command shows how the apply command is used to create and update kubernetes objects in a declarative fashion.

提醒一下,上一个命令显示了apply命令如何以声明性方式用于创建和更新kubernetes对象。

In order to understand the internals of the apply command, let’s first take a step back and understand how objects are created within kubernetes:

为了了解apply命令的内部原理,让我们先退后一步,了解如何在kubernetes中创建对象:

Take as an example the following POD object defined in a YAML file:

以在YAML文件中定义的以下POD对象为例:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end-service
spec:
  containers:
    - name: nginx-container
      image: nginx:1.18

Now create the object:

现在创建对象:

kubectl create -f nginx.yaml

Important: In the background, a live configuration file is additionally generated within kubernetes. This new file is similar to the one we created locally. However, it contains further information regarding the status of the object:

重要提示 :在后台,将在kubernetes中另外生成一个实时配置文件。 这个新文件类似于我们在本地创建的文件。 但是,它包含有关对象状态的更多信息:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end-service
spec:
  containers:
    - name: nginx-container
      image: nginx:1.18
# New
status:
  conditions:
    - lastProbeTime: null
      status: "True"
      type: Initialized

The above file is the live configuration of the object on the kubernetes cluster.

上面的文件是kubernetes集群上对象的实时配置。

As depicted above, kubernetes appends the status field to the configuration file in order to track information about the object.

如上所述,kubernetes将状态字段附加到配置文件中,以便跟踪有关对象的信息。

This is how kubernetes internally stores information about an object, regardless of the way it was created (declarative or imperative).

这是kubernetes在内部存储有关对象的信息的方式而不管其创建方式(声明性或命令性)如何

返回Apply命令 (Back to the Apply command)

The apply command, when executed, takes into consideration the following 3 files in for book-keeping.

apply命令在执行时会考虑以下3个文件以进行簿记。

  • A local configuration file (defined by the developer)

    本地配置文件 (由开发人员定义)

  • A live object definition on kubernetes (The Additional YAML file created by kubernetes)

    kubernetes上的活动对象定义 (由kubernetes创建的附加YAML文件)

  • The last applied configuration. (New)

    最后应用的配置 。 (新)

As you may have noticed, the first two listed files are the ones we have previously discussed, the third is unique to the apply command.

您可能已经注意到,列出的前两个文件是我们之前讨论的文件,第三个是apply命令所独有的文件。

Whenever we run the apply command All of the three files are compared to identify what changes are to be made on the live object

每当我们运行apply命令时,都会比较所有三个文件,以确定将对活动对象进行哪些更改

有关已应用的配置文件的更多信息 (More on the applied configuration file)

The YAML version of the local object configuration file is converted to a JSON format, and it is then stored as the last applied configuration.

本地对象配置文件的YAML版本将转换为JSON格式,然后将其存储为最后应用的配置。

Following our previous examples:

遵循我们之前的示例:

{
  "apiVersion": "v1",
  "kind": "Pod",
  "metadata": {
    "annotations": {},
    "labels": {
      "run": "myapp-pod",
      "type": "front-end-service"
    },
    "name": "myapp-pod"
  },
  "spec": {
    "containers": [
      {
        "image": "nginx:1.18",
        "name": "nginx-container"
      }
    ]
  }
}

kubernetes如何利用这些文件? (How does kubernetes utilize these files?)

For example: say we update the image field from 1.18 to 1.19 in our local configuration file, and we then run the apply command as before.

例如:假设我们将本地配置文件中的image字段从1.18更新为1.19,然后像以前一样运行apply命令。

This changed image field is compared with the value in the live configuration and if there is a difference, the live configuration is updated with the new value.

将此更改的图像字段与实时配置中的值进行比较,如果存在差异,则使用新值更新实时配置。

After any change, the last applied JSON file is always updated.

进行任何更改后,上一次应用的 JSON文件始终会更新。

At this point you will probably be asking yourself: What is the purpose of the JSON applied configuration file?

此时,您可能会问自己:应用JSON的配置文件的目的是什么?

This is best illustrated with an example:

最好用一个例子说明:

Let’s imagine that a field was deleted, say, the type field in label was deleted in our local configuration file. When we run the kubectl apply command, we see that the last applied configuration had a label, but it’s not present in the local configuration.

假设有一个字段被删除,例如,label的type字段在本地配置文件中被删除。 当运行kubectl apply命令时,我们看到最后一个应用的配置带有标签,但是在本地配置中不存在。

This means that the field needs to be removed from the live configuration (The YAML file generated by kubernetes).

这意味着需要从实时配置 (由kubernetes生成的YAML文件)中删除该字段。

So if a field was present in the live configuration and not present in the local or the last applied configuration, then it will be left as is.

因此,如果某个字段存在于实时配置中 ,而不存在于本地配置或上次应用的配置中,则该字段将保持不变。

But if a field is missing from the local file and it is present in the last applied configuration, that means that in the previous step the field was removed.

但是,如果本地文件中缺少某个字段,并且该字段存在于上一次应用的配置中 ,则意味着在上一步中将该字段删除了。

So in summary: the newly added applied configuration file helps kubernetes figure out the fields that have been removed from the local file.

因此,总而言之:新添加的应用配置文件可帮助kubernetes找出已从本地文件中删除的字段。

What we just discussed is available for your reference in detail in the kubernetes is documentation pages.

我们刚刚讨论的内容可以在kubernetes文档页面中找到,以供您详细参考。

结语 (Wrapping Up)

  • We learned about the three sets of files used by the kubectl apply command

    我们了解了kubectl apply命令使用的三组文件
  • The local file is what’s stored on our local system.

    本地文件是存储在我们本地系统上的文件

  • The live object configuration is in the kubernetes memory.

    活动对象配置位于kubernetes内存中。

但是,此JSON文件中最后一次应用的配置存储在哪里? (But where is this JSON file that has the last applied configuration stored?)

It’s stored on the live object configuration on the kubernetes cluster itself as an annotation named: last applied configuration.

它以注释的形式存储在kubernetes集群本身的活动对象配置上,该注释名为: last Applied configuration

So remember that this is only done when you use the apply command, the kubectl create or replace command do not store the last applied configuration like explained.

因此请记住,仅当您使用apply命令时,此操作才完成,kubectl create或replace命令不存储上次应用的配置,如所述。

Once you use the apply command going forward, whenever a change is made, the apply command compares all three sections.

一旦使用了apply命令,则只要进行更改,apply命令就会比较所有三个部分。

  1. The local definition file.

    本地定义文件。

  2. The live object configuration file.

    活动对象配置文件。

  3. The last applied configuration stored

    上次应用的配置存储

Originally published at https://luispreciado.blog.

最初发布在 https://luispreciado.blog

翻译自: https://medium.com/dev-genius/understanding-the-kubectl-apply-command-f1d56527be01

kubectl apply

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

kubectl apply_了解Kubectl Apply命令 的相关文章

随机推荐