【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

简介:

前言

上文【从入门到放弃-Kubernetes】Kubernetes进阶-pod水平自动伸缩(hpa),我们学习了如何对标准资源Deployment、replication controller、replica set等内置资源进行水平自动伸缩。

实际在生产中(一般较大的公司)不太会用到这些内置标准资源,都会使用CustomResources自定义资源进行灵活的扩展,来满足复杂多变的业务需求。

CustomResources是Kubernetes API的扩展,可以通过动态注册,控制资源在集群中的运行状态。

安装CustomResources后,就可以和内置资源如pod一样,通过kubectl创建和访问对象。

下面我们将CustomResources简称为CR。

定义CRD

添加CR通常有两种方式,通过CRD(CustomResourceDefinitions)的方式和使用聚合API的方式。

CRD的方式使用简单,聚合API的方式更灵活。

选型建议如下:

更详细的对比可以在官方文档中查看详情。

这里我们使用CRD的方式创建,有两种版本的CRD定义方式分别如下

apiextensions.k8s.io/v1

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  name: crontabs.stable.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com
  # list of versions supported by this CustomResourceDefinition
  versions:
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                cronSpec:
                  type: string
                image:
                  type: string
                replicas:
                  type: integer
  # either Namespaced or Cluster
  scope: Namespaced
  names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: crontabs
    # singular name to be used as an alias on the CLI and for display
    singular: crontab
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: CronTab
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:
    - ct

apiextensions.k8s.io/v1beta1

# Deprecated in v1.16 in favor of apiextensions.k8s.io/v1
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  name: crontabs.stable.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com
  # list of versions supported by this CustomResourceDefinition
  versions:
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
  # either Namespaced or Cluster
  scope: Namespaced
  names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: crontabs
    # singular name to be used as an alias on the CLI and for display
    singular: crontab
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: CronTab
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:
    - ct
  preserveUnknownFields: false
  validation:
    openAPIV3Schema:
      type: object
      properties:
        spec:
          type: object
          properties:
            cronSpec:
              type: string
            image:
              type: string
            replicas:
              type: integer

创建并验证

# 使用上面任意一版本的yaml文件
kubectl apply -f crd.yaml

# 开启api server http代理
kubectl proxy --port=8080 &

# 访问/apis/stable.example.com/v1/namespaces/*/crontabs查看
curl http://127.0.0.1:8080/apis/stable.example.com/v1/namespaces/*/crontabs

# 查看crd
kubectl get crd

可以看到类似以下输出

创建CR对象

apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
  name: my-new-cron-object
spec:
  cronSpec: "* * * * */5"
  image: my-awesome-cron-image
# 创建CR对象
kubectl apply -f cr-object.yaml

# 查看crontab资源
kubectl get crontab

# 查看crontab yaml文件
kubectl get crontab -o yaml

删除CRD

# 删除CRD
kubectl delete -f crd.yaml

# 再次查看crontab资源,会看到Error from server (NotFound): Unable to list "stable.example.com/v1, Resource=crontabs": the server could not find the requested resource (get crontabs.stable.example.com)
kubectl get crontabs

总结

本文学习了如何通过CRD来创建CR,实际上通常还需要通过Custom controllers监听资源的变化,在周期内的各个阶段做相应的处理。
接下里会开始学习Custom controllers的开发。

更多文章

见我的博客:https://nc2era.com

written by AloofJr,转载请注明出处

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
5天前
|
存储 Kubernetes 监控
Kubernetes快速进阶与实战:构建可靠的容器化应用平台
Kubernetes快速进阶与实战:构建可靠的容器化应用平台
111 0
|
8月前
|
NoSQL 测试技术 调度
ACK集群应用部署进阶
该实验是“云原生与Kubernates基础课程”的配套实验。 涉及ACK集群上ingress、pod,以及日志服务、hpa的应用部署。 进行该实验的前提:需熟悉上述理论概念,建议完成配套理论课程学习。
195 1
|
7月前
|
Kubernetes 负载均衡 前端开发
【K8S系列】第十二讲:Service进阶
【K8S系列】第十二讲:Service进阶
42 0
|
8月前
|
运维 Kubernetes API
Kubernetes 入门&进阶实战
Kubernetes 入门&进阶实战
Kubernetes 入门&进阶实战
|
9月前
|
监控 NoSQL 调度
ACK集群应用部署进阶
通过 Ingress 服务来暴露 pod,让应用可以通过统一的入口访问;通过 pod 亲和性调度, 让 web 的 pod 和 redis 的 pod 尽量调度到一个节点上,提高 web 和 redis 之间的网络质量。 同时将应用的标准输出日志采集到日志服务,监控日志中的特殊字段内容,如果出现对 应字段的日志,通过日志服务进行告警,通知到邮件。 另外通过创建 hpa 来保证业务高峰期能够自动调整 pod 副本数,来增加架构弹性。
|
存储 Kubernetes 负载均衡
Kubernetes 入门到进阶(二)
三、Kubernetes基础入门 以下的所有都先进行基本理解,我们后来会一一详细讲解 0、基础知识 以上展示了一个master(主节点)和6个worker(工作节点)的k8s集群 docker是每一个
276 0
|
Kubernetes 负载均衡 应用服务中间件
Kubernetes 入门到进阶(一)
一、Kubernetes简介 1、背景 1、部署方式的变迁 传统部署时代: 在物理服务器上运行应用程序 无法为应用程序定义资源边界 导致资源分配问题 例如,如果在物理服务器上运行多个应用程序,则可能会
319 0
|
弹性计算 运维 监控
进阶课程:在 ACK 中如何使用容器优化的操作系统|学习笔记(二)
快速学习进阶课程:在 ACK 中如何使用容器优化的操作系统
121 0
进阶课程:在 ACK 中如何使用容器优化的操作系统|学习笔记(二)
|
Kubernetes Cloud Native 安全
进阶课程:如何在 ACK 中使用 MSEIngress|学习笔记(四)
快速学习进阶课程:如何在 ACK 中使用 MSEIngress
85 1
进阶课程:如何在 ACK 中使用 MSEIngress|学习笔记(四)
|
运维 Kubernetes Cloud Native
进阶课程:如何在 ACK 中使用 MSEIngress|学习笔记(三)
快速学习进阶课程:如何在 ACK 中使用 MSEIngress
131 0
进阶课程:如何在 ACK 中使用 MSEIngress|学习笔记(三)