Kubernetes进阶实战读书笔记:资源管理基础(一)

一、资源对象及API群组

1、表征状态转移

基本元素为资源:resource

资源即对象、一个资源通常意味着一个附带类型和关联数据、支持的操作方法以及与其他对象的关系的对象、他们是有状态的事物、即rest中的S(state)

表征:representation

REST组件通过使用表征来捕获资源的当前逾期状态并在组件之间传输改表征从而对资源执行操作、表征是一个字节序列、由数据、描述数据的元数据以及偶尔描述元数据组成、表征的数据格式为媒体类型
常用的有JSON或XML.API客户端不能直接访问资源、他们需要执行动作来改变资源的状态

行为:action

2、资源分类

kubernetes将一切事物都抽象为API资源、资源可以分组为集合,每个集合只包含单一类型的资源、集合、资源、子集及资源间的关系如下图所示

二、Kubernetes资源对象

1、常用资源对象

2、工作负载型资源

 daemonset

3、发现和负责均衡

4、配置存储:volume

5、集群级资源

6、元数据型资源

三、资源在API中的组织形式

1、资源在API中的组织形式

2、资源类型

3、种类(kind)

4、集合(collecton)

5、资源或对象

四、访问Kubernetes REST API

接助命令在本地主机上为API Server启动一个代理网关、由它支持使用HTTP进行通讯、其工作逻辑如下图所示

例如、本地127.0.0.1的8080端口上启动API Server的一个代理网关

1
2
[root@master ~]# kubectl proxy --port=8080
Starting to serve on 127.0.0.1:8080

1、列出集群上所有的Namespaces对象

1
2
3
4
5
6
7
8
[root@master ~]# curl localhost:8080/api/v1/namespaces
{
  "kind": "NamespaceList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/namespaces",
    "resourceVersion": "329417"
    ......

2、安装JSON的命令行处理器jq命令

1
2
3
4
5
安装EPEL源:
yum install epel-release -y
 
安装jq:
yum install jq -y

3、仅显示相关的NamespacesList对象中的各成员对象

1
2
3
4
5
6
7
[root@master ~]# curl -s localhost:8080/api/v1/namespaces/ | jq .items[].metadata.name
"default"
"ingress-nginx"
"kube-node-lease"
"kube-public"
"kube-system"
"weave"

4、给出特定的Namespaces资源对象的名称则能够直接获取相应的资源信息以kube-system名称空间为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@master ~]# curl -s localhost:8080/api/v1/namespaces/kube-system
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "kube-system",
    "selfLink": "/api/v1/namespaces/kube-system",
    "uid": "1e69045d-bfea-4292-b4e8-1fbaaefaae22",
    "resourceVersion": "14",
    "creationTimestamp": "2020-08-03T15:20:46Z",
    "managedFields": [
      {
        "manager": "kube-apiserver",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2020-08-03T15:20:46Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {"f:status":{"f:phase":{}}}
      }
    ]
  },
  "spec": {
    "finalizers": [
      "kubernetes"
    ]
  },
  "status": {
    "phase": "Active"
  }

五、资源配置清单

 1、资源配置清单:namespaces kube-system

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@master ~]# kubectl get namespaces kube-system -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2020-08-03T15:20:46Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:phase: {}
    manager: kube-apiserver
    operation: Update
    time: "2020-08-03T15:20:46Z"
  name: kube-system
  resourceVersion: "14"
  selfLink: /api/v1/namespaces/kube-system
  uid: 1e69045d-bfea-4292-b4e8-1fbaaefaae22
spec:
  finalizers:
  - kubernetes
status:
  phase: Active

除了极少数资源之外、Kubernetes系统上的绝大多数资源都是由其使用者所创建的、创建时、需要以上述输出结果中类似的方式以YAML或JSON序列化方案定义资源的相关配置数据

即用户期望的目标状态、而后再由Kubernetes的底层组件确保活动对象的运行时状态与用户提供的配置清单中定义的状态无限接近

 2、资源配置清单:deployments.apps myapp-deploy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@master chapter5]# kubectl get deployments.apps myapp-deploy -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
  ......
   name: myapp-deploy
  namespace: default
  resourceVersion: "361135"
  selfLink: /apis/apps/v1/namespaces/default/deployments/myapp-deploy
  uid: 74ac89a8-0f06-43a4-81bc-c39fddbde74d
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: myapp
.......
status:
  availableReplicas: 3
  conditions:
.......

事实上、对几乎所有的资源来说apiVersion、kind、metadata字段的功能基本上都是相同的、但spec则用于资源的期望状态、而status字段则用于记录活动对象的当前状态

六、对象资源格式

1、所有一级字段

2、metadata嵌套字段

必选字段

可选字段

3、spec字段

七、资源配置清单格式文档

1、了解一级字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@master ~]# kubectl explain pods
KIND:     Pod
VERSION:  v1
 
DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.
 
FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
 
   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
 
   metadata <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
 
   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
 
   status   <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

2、了解二级字段

kubectl explain pods.spec

3、了解三级以上字段

kubectl explain pods.spec.containers

4、优势

1
kubectl get deployments.apps myapp-deploy  -o yamal --export > deploy-demo.yaml

命令行只支持部分资源对象的部分属性、而资源清单支持配置资源的所有属性字段而且使用配置清单文件还能够进行版本追踪、复审等高级功能的操作

八、资源对象管理方式

1、声明式编程

2、陈述式编程

3、命令分类

posted @   活的潇洒80  阅读(710)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示