首页 > 其他分享 >Kubernetes架构设计Concepts之理解Kubernetes API之理解Kubernetes对象

Kubernetes架构设计Concepts之理解Kubernetes API之理解Kubernetes对象

时间:2023-02-15 11:25:00浏览次数:46  
标签:架构设计 object kubernetes Kubernetes 理解 API io spec

 

首先我说下为什么去翻译这些文章,当然也有一些不少文章是翻译的,但是没有对照,或者是全中文的,这个时候你就不好判断理解,特别是比如:从代码角度和运维角度,代码角度呢,我们看到很多英文概念和代码里面的文件夹目录是对应的,很多英文的输出结果和Kubenetes里面概念和term(术语)也是吻合的,所以我尽量是为了让大家能够对比,即使我翻译错了,大家也能自己再去对照.

来自官方:
https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/#kubernetes-objects

 

理解Kubernetes对象)

This page explains how Kubernetes objects are represented in the Kubernetes API, and how you can express them in .yaml format.

本页介绍说明Kubernetes对象如何在Kubernetes API中表示,以及如何用.yaml文件格式来表达。
如果想了解对象还是要看下术语manifest https://kubernetes.io/docs/reference/glossary/?fundamental=true#term-manifest

Understanding Kubernetes objects

Kubernetes objects are persistent entities in the Kubernetes system. Kubernetes uses these entities to represent the state of your cluster. Specifically, they can describe:

Kubernetes对象是Kubernetes系统中的持久化的实体。Kubernetes使用这些实体来表示(代表)你的集群的状态。
具体来说,这些实体或者叫api对象可以描述为:
    • 1.What containerized applications are running (and on which nodes) 
    •  正在运行哪些容器化应用程序
    • 2.The resources available to those applications     
    •  对于这些应用程序可用的资源
    • 3.The policies around how those applications behave, such as restart policies, upgrades, and fault-tolerance 
    •  有关这些应用程序行为的策略,例如重启策略、升级和容错

 

A Kubernetes object is a "record of intent"--once you create the object, the Kubernetes system will constantly work to ensure that object exists. By creating an object, you're effectively telling the Kubernetes system what you want your cluster's workload to look like; this is your cluster's desired state.

Kubernetes对象是一个“intent记录”—— 一旦创建了该对象,Kubernetes系统将持续工作以确保该对象存在。
通过创建一个对象,您实际上是在告诉Kubernetes系统您希望集群的workload是什么样子;这是集群所需的状态。
//intent是有歧义的,
//https://aws.amazon.com/cn/blogs/containers/introducing-cdk8s-intent-driven-apis-for-kubernetes-objects/?nc1=b_nrp

 

To work with Kubernetes objects--whether to create, modify, or delete them--you'll need to use the Kubernetes API. When you use the kubectl command-line interface, for example, the CLI makes the necessary Kubernetes API calls for you. You can also use the Kubernetes API directly in your own programs using one of the Client Libraries.

要让Kubernetes对象工作,无论是create、modify还是delete它们,你都需要使用Kubernetes API。
例如,当您使用kubectl命令行界面时,CLI会为您进行必要的Kubernetes API调用。您也可以在自己的程序中直接使用某个client libraries的Kubernetes API.
Kubernetes API: https://kubernetes.io/docs/concepts/overview/kubernetes-api/
Client Libraries: https://kubernetes.io/docs/reference/using-api/client-libraries/

 https://kubernetes.io/docs/reference/kubectl/kubectl/      英文
 https://kubernetes.io/zh/docs/reference/kubectl/overview/ 中文

Object Spec and Status(对象规格和状态)

Almost every Kubernetes object includes two nested object fields that govern the object's configuration: the object spec and the object status. For objects that have a spec, you have to set this when you create the object, providing a description of the characteristics you want the resource to have: its desired state.

几乎每个Kubernetes对象都包含两个嵌套的对象字段,它们控制对象的配置:对象spec和对象status。
对于具有spec的对象,你不得不在创建对象时设置该spec,并提供您希望资源具有的特征的描述:即所需状态。
//status描述kubernetes对象的实时状态

The status describes the current state of the object, supplied and updated by the Kubernetes system and its components.

status 字段描述的是Kubernetes系统及其组件提供和更新的 current state of the object(对象当前的状态)。

 

The Kubernetes control plane continually and actively manages every object's actual state to match the desired state you supplied.

Kubernetes控制平面持续主动地管理每个对象的实际状态,以匹配您提供的所需状态。

 

For example: in Kubernetes, a Deployment is an object that can represent an application running on your cluster.

例如:在Kubernetes中,一个Deployment是可以表示在集群上运行的应用程序的对象。(即Deployment是一个对象)
//意味着一旦用Deployment方式部署之后,就会生成一个对应的Deployment对象

 

 When you create the Deployment, you might set the Deployment spec to specify that you want three replicas of the application to be running.

当你创建Deployment(用yaml)时,你可以在Deployment中设置spec为指定要运行应用程序的三个replicas。

 

deployment部署yaml样例:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "18"
    description: ""
  creationTimestamp: null
  generation: 1
  labels:
    run: auth
    version: v1
  name: auth
  selfLink: /apis/extensions/v1beta1/namespaces/pro/deployments/auth
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
....省略

 

The Kubernetes system reads the Deployment spec and starts three instances of your desired application--updating the status to match your spec.

Kubernetes系统 api就可以读取Deployment spec并启动所需应用程序的三个实例——更新状态以匹配您的spec。

 

If any of those instances should fail (a status change), the Kubernetes system responds to the difference between spec and status by making a correction--in this case, starting a replacement instance.

如果这些实例中的任何一个出现故障(状态更改),Kubernetes系统将通过进行更正来响应规范和状态之间的差异——在本例中,启动一个替换实例。

 

For more information on the object spec, status, and metadata, see the Kubernetes API Conventions.

//对于更多的信息,请看object的(三要素) spec, status 和 metadata ,请参考 Kubernetes API Conventions

 

Describing a Kubernetes object(描述一个Kubernetes对象)

When you create an object in Kubernetes, you must provide the object spec that describes its desired state, as well as some basic information about the object (such as a name). When you use the Kubernetes API to create the object (either directly or via  kubectl), that API request must include that information as JSON in the request body. Most often, you provide the information to kubectl in a .yaml file. kubectl converts the information to JSON when making the API request.

在Kubernetes中创建对象时,你必须提供object spec 其所需状态,以及有关对象的一些基本信息(例如名称)。
当你使用Kubernetes API创建对象时(也可以直接通过kubectl命令创建),该API请求必须将该信息作为JSON格式包含在请求体中。
通常情况下,您会以提供信息给kubelet用.yaml文件方式,当构造api request的时候, kubectl在发出API请求时将信息转换为JSON。

 

Here's an example .yaml file that shows the required fields and object spec for a Kubernetes Deployment:

这里有一个.yaml文件例子。针对Kubernetes Deployment 方式,部署所需字段和对象spec的 .yaml 文件:

application/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

 

One way to create a Deployment using a .yaml file like the one above is to use the kubectl apply command in the kubectl command-line interface, passing the .yaml file as an argument. Here's an example:

一种使用。类似于上面的yaml文件是在kubectl命令行界面中使用kubectl apply命令,传递。yaml文件作为参数。下面是一个执行创建的例子:
[root@k2 k8s]# kubectl apply -f https://k8s.io/examples/application/deployment.yaml
deployment.apps/nginx-deployment created

 
 必需字段(一个对象的组成)

    In the .yaml file for the Kubernetes object you want to create, you'll need to set values for the following fields:

   在想要创建的 Kubernetes 对象对应的 .yaml 文件中,需要配置如下的字段:

必需字段 原文 翻译
apiVersion  Which version of the Kubernetes API you're using to create this object 你创建对象使用,哪个版本的Kubernetes API
kind What kind of object you want to create

你想要创建哪类的对象.

metadata Data that helps uniquely identify the object, including a name string, UID, and optional namespace 帮助唯一标识对象的数据,包括名称字符串、UID和可选的namespace
spec What state you desire for the object

你期望该对象达到的状态.

  注: 一个.yaml中可以有多个manifest,一个manifest对应一个完整的对象spec, 

The precise format of the object spec is different for every Kubernetes object, and contains nested fields specific to that object.

看下示例
对象 spec 的精确格式对于每个Kubernetes对象都不同,并且包含特定于该对象的嵌套字段。
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: open-api-service
  labels:
    app: open-api
  namespace: ccy
  annotations: {}
spec:
  selector:
    app: open-api
  ports:
    - name: tcp-8097-8097
      targetPort: 8097
      nodePort: 0
      port: 8097
      protocol: TCP
  type: NodePort

 

上面的例子中有两个manifest. 组成了一个yaml文件.

The Kubernetes API Reference can help you find the spec format for all of the objects you can create using Kubernetes.

Kubernetes API Reference 可以帮助您找到可以使用Kubernetes创建的所有对象的spec格式。

 

 例如, 参看 the spec field for the Pod API reference.

   关于:   spec field   https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec

For each Pod, the .spec field specifies the pod and its desired state (such as the container image name for each container within that pod).

对于每一个Pod。spec字段指定pod及其所需状态(例如该pod中每个容器的容器映像(镜像)名称)。

 

Another example of an object specification is the spec field for the StatefulSet API. For StatefulSet, the .spec field specifies the StatefulSet and its desired state.

对象spec的另一个例子是StatefulSet API的spec字段。对于StatefulSet,这个。spec字段指定StatefulSet及其所需状态。

 

Within the .spec of a StatefulSet is a template for Pod objects. That template describes Pods that the StatefulSet controller will create in order to satisfy the StatefulSet specification(规范). Different kinds of object can also have different .status; again, the API reference pages detail the structure of that .status field, and its content for each different type of object

在StatefulSet的 .spec内部是Pod对象的template。该模板描述了StatefulSet controller创建的POD,以满足StatefulSet规范(要求)。
不同种类的object也可以有不同的.status。再有,API参考页详细介绍了每个不同类型object的 .status 字段的结构
template 链接地址: https://kubernetes.io/docs/concepts/workloads/pods/#pod-templates

 

参考
https://kubernetes.io/docs/reference/kubernetes-api/
https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec
https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/stateful-set-v1/#StatefulSetSpec
https://kubernetes.io/docs/concepts/workloads/pods/#pod-templates
https://kubernetes.io/docs/concepts/architecture/controller/
https://kubernetes.io/docs/concepts/workloads/pods/
https://kubernetes.io/docs/reference/using-api/
如果想看中文可以
https://kubernetes.io/zh/docs/reference/using-api/

 

标签:架构设计,object,kubernetes,Kubernetes,理解,API,io,spec
From: https://www.cnblogs.com/aozhejin/p/16253480.html

相关文章