Kubernetes CRD开发汇总

1. Kubernetes CRD开发

1.1 kubernetes 自定义资源(CRD)

在研究 Service Mesh 的过程中,发现 Istio 很多参数都通过 kubernetes CRD 来管理,例如 VirtualService 和 DestinationRule,这种方式使部署在 k8s 集群上的服务的管理方式更趋向一致。

kubernetes 的资源管理方式和声明式 API 的良好设计使得在这个平台上的功能扩展变得异常容易。例如 CoreOS 推出的 Operator 框架就是一个很好的例子。

这篇文章通过一个简短的示例来演示如何创建自定义资源。

1.1.1 创建 CRD(CustomResourceDefinition)

这里以创建一个简单的弹性伸缩配置的 CRD 为例。将下面的内容保存在 scaling_crd.yaml 文件中。

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  name: scalings.control.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: control.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: scalings
    # singular name to be used as an alias on the CLI and for display
    singular: scaling
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: Scaling
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:
    - sc

通过 kubectl 创建这个 CRD:

kubectl apply -f scaling_crd.yaml

1.1.2 创建自定义资源的对象

我们编写一个 test.yaml 文件来创建一个自定义的 Scaling 对象。

apiVersion: "control.example.io/v1"
kind: Scaling
metadata:
  name: test
spec:
  targetDeployment: test
  minReplicas: 1
  maxReplicas: 5
  metricType: CPU
  step: 1
  scaleUp: 80
  scaleDown: 40

通过 kubectl 创建:

kubectl apply -f test.yaml

提示:

scaling.control.example.io/test created

你可以通过 kubectl 查看已经创建的名为 test 的 Scaling 对象。

kubectl get scalings.control.example.io test -o yaml

会输出类似如下的结果:

<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值