深入Kubernetes之一声明式API(api资源,源码,运维层面等分析)

 

https://kubernetes.io/docs/reference/kubernetes-api/

我们简单一句话来说明 https://kubernetes.io/docs/reference/kubernetes-api/ 这个文档的作用,这里有明确的解释:
https://kubernetes.io/zh/docs/concepts/overview/working-with-objects/kubernetes-objects/#required-fields
 对象 spec 的精确格式对每个 Kubernetes 对象来说是不同的,包含了特定于该对象的嵌套字段。 

Kubernetes API 参考 能够帮助我们找到任何我们想创建的 kubernetes objects的spec格式。

Kubernetes API 参考 即地址 https://kubernetes.io/docs/reference/kubernetes-api/


具体该文档内容翻译和解释如下: 
 一、Kubernetes API

Kubernetes' API is the application that serves Kubernetes functionality through a RESTful interface and stores the state of the cluster.

Kubernetes的API是通过 RESTful 接口提供Kubernetes功能并存储集群状态的应用程序。

Kubernetes resources and "records of intent" are all stored as API objects, and modified via RESTful calls to the API. The API allows configuration to be managed in a declarative way. Users can interact with the Kubernetes API directly, or via tools like kubectl. The core Kubernetes API is flexible and can also be extended to support custom resources.

Kubernetes资源和“意图记录”都存储为API对象,并通过对API的RESTful调用进行修改。API允许以声明方式来管理配置。用户可以直接与Kubernetes API(即与kube-apiserver)交互,
也可以通过kubectl等工具进行交互。核心Kubernetes API非常灵活,还可以扩展以支持自定义资源。

下面是REST API资源相关内容(各种对象的spec的详细格式都可以在下面对应的资源中找到)
Workload Resources
https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/
Service Resources
https://kubernetes.io/docs/reference/kubernetes-api/service-resources/
Config and Storage Resources
https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/
Authentication Resources
https://kubernetes.io/docs/reference/kubernetes-api/authentication-resources/
Authorization Resources
https://kubernetes.io/docs/reference/kubernetes-api/authorization-resources/
Policy Resources
https://kubernetes.io/docs/reference/kubernetes-api/policy-resources/
Extend Resources
https://kubernetes.io/docs/reference/kubernetes-api/extend-resources/
Cluster Resources
https://kubernetes.io/docs/reference/kubernetes-api/cluster-resources/

从官网的这页可以看出,kubernetes-api把交互的内容统一成资源来与集群进行沟通.
英文中是Declarative API 解释为 声明式API,  编写这样的交互方式需要下面几点认识:

 
1.首先需要了解术语Manifest和yaml文件

   Manifest(针对的是对象spec,注意,一个yaml文件中可以有多个Manifest,每个manifest对应一个object spec)

   Specification of a Kubernetes API object in JSON or YAML format.

   A manifest specifies the desired state of an object that Kubernetes will maintain when you apply the manifest. Each configuration file can contain multiple manifests.

   Kubernetes API对象规范采用的JSON或YAML格式
   当你应用了manifest时候,一个manifest指定了Kubernetes维护的object的所需状态。每个配置文件可以包含多个mainfests。
   一个yaml文件的组成部分样例如下(一个yaml文件包括2个Api对象(manifest),yaml文件内的字段和缩进需要符合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
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginxnamespace: ccy
  annotations: {}
spec:
  selector:
    app: madp-auth
  ports:
    - name: tcp-3000-3000
      targetPort: 3000
      nodePort: 0
      port: 3000
      protocol: TCP
  type: NodePort


如果你对Manifest有疑惑,可以参考 https://stackoverflow.com/questions/55130795/what-is-a-kubernetes-manifest
因为
这很重要.


2.了解 Kubernetes objects
   https://kubernetes.io/zh/docs/concepts/overview/working-with-objects/kubernetes-objects/#required-fields

 在 Kubernetes 系统中,Kubernetes 对象 是持久化的实体, 如果你用 Kubernetes API 操作 kubernetes object 
 比如,当使用 kubectl 命令行接口时,CLI 会执行必要的 Kubernetes API 调用, 也可以在程序中使用 客户端库直接调用 Kubernetes API。

  在想要创建的 Kubernetes 对象对应的 .yaml 文件( 一个yaml文件中可以包括多个Manifest)中,需要配置如下的字段(必需字段 ):

  • apiVersion - 创建该对象所使用的 Kubernetes API 的版本
  • kind - 想要创建的对象的类别
  • metadata - 帮助唯一性标识对象的一些数据,包括一个 name 字符串、UID 和可选的 namespace
  • spec - 你所期望的该对象的状态
    注意: 这里的yaml文件创建对象方式只是最重要,最常用和推荐的创建方式,也可以通过命令行等方式来创建,所以重点就写了.yaml方式

  3.  了解管理Kubernetes 对象的其中几种方式
   https://kubernetes.io/zh/docs/concepts/overview/working-with-objects/object-management/#what-s-next

 二、代码层面

   kubenetes 官方单独维护api部分介绍:https://www.cnblogs.com/aozhejin/p/16285259.html
   有介绍官方已经把api部分独立出来进行维护,维护地址为: https://github.com/kubernetes/api#recommended-use 
   对应本地源码目录(以1.10.13版本为例)
   E:\k8s源码\kubernetes-1.10.13\kubernetes-1.10.13\pkg\apis
  同时请参考kubenetes github子项目: https://www.cnblogs.com/aozhejin/p/16286669.html

  三、运维层面


posted @ 2023-02-15 11:08  jinzi  阅读(88)  评论(0编辑  收藏  举报