Custom Resource Definitions (CRD) were added in Kubernetes v1.7 in June 2017. A CRD defines Custom Resources (CR). A CR is an extension of the Kubernetes API that allows you to store your own API Objects and lets the API Server handle the lifecycle of a CR. On their own, CRs simply let you store and retrieve structured data.
For instance, our Guestbook application consists of an object Guestbook with attributes GuestbookTitle and GuestbookSubtitle, and a Guestbook handles objectes of type GuestbookMessage with attributes Message, Sender.
Your API has a small number of small objects (resources).
The objects define configuration of applications or infrastructure.
The objects are updated relatively infrequently.
Users often need to read and write the objects.
main operations on the objects are CRUD (create, read, update and delete).
Transactions between objects are not required.
It doesn't immediately make sense to store messages by Guestbook users in Kubernetes, but it might make sense to store meta-data about a Guestbook deployment, for instance the title and subtitle of a Guestbook deployment, assigned resources or replicas.
Another benefit of adding a Custom Resource is to view your types in the Kubernetes Dashboard.
If you want to deploy a Guestbook instance as a Kubernetes API object and let the Kubernetes API Server handle the lifecycle events of the Guestbook deployment, you can create a Custom Resource Definition (CRD) for the Guestbook object as follows. That way you can deploy multiple Guestbooks with different titles and let each be managed by Kubernetes.
You can see that the apiVersion is part of the apiextensions.k8s.io/v1 API Group in Kubernetes, which is the API that enables extensions, and the kind is set to CustomResourceDefinition.
The served flag can disable and enable a version.
Only 1 version can be flagged as the storage version.
The spec.names.kind is used by your resource manifests and should be CamelCased.
Create the Custom Resource for the Guestbook witht he command,
oc create -f guestbook-crd.yaml
When run in the terminal,
$ kubectl create -f guestbook-crd.yaml
customresourcedefinition.apiextensions.k8s.io/guestbooks.apps.ibm.com created
You have now added a CR to the Kubernetes API, but you have not yet created a deployment of type Guestbook yet.
Create a resource specification of type Guestbook named my-guestbook,
cat <<EOF >>my-guestbook.yaml
apiVersion: "apps.ibm.com/v1"
kind: Guestbook
metadata:
name: my-guestbook
spec:
guestbookTitle: "The Chemical Wedding of Remko"
guestbookSubtitle: "First Day of Many"
EOF
And to create the my-guestbook resource, run the command
oc create -f my-guestbook.yaml
When run in the terminal,
$ oc create -f my-guestbook.yaml
guestbook.apps.ibm.com/my-guestbook created
If you list all Kubernetes resources, only the default Kubernetes service is listed. To list your Custom Resources, add the extended type to your command.
$ oc get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 172.21.0.1 <none> 443/TCP 5d14h
$ oc get guestbook
NAME AGE
my-guestbook 8m32s
To read the details for the my-guestbook of type Guestbook, describe the instance,
$ oc describe guestbook my-guestbook
Name: my-guestbook
Namespace: default
Labels: <none>
Annotations: <none>
API Version: apps.ibm.com/v1
Kind: Guestbook
Metadata:
Creation Timestamp: 2020-06-30T20:31:36Z
Generation: 1
Resource Version: 1081471
Self Link: /apis/apps.ibm.com/v1/namespaces/default/guestbooks/my-guestbook
UID: dcbdcafc-999d-4051-9244-0315093357e7
Spec:
Guestbook Subtitle: First Day of Many
Guestbook Title: The Chemical Wedding of Remko
Events: <none>
Or retrieve the resource information by specifying the type,
$ oc get Guestbook -o yaml
apiVersion: v1
items:
- apiVersion: apps.ibm.com/v1
kind: Guestbook
metadata:
creationTimestamp: "2020-07-02T04:41:57Z"
generation: 1
name: my-guestbook
namespace: default
resourceVersion: "1903244"
selfLink: /apis/apps.ibm.com/v1/namespaces/default/guestbooks/my-guestbook
uid: 3f774899-3070-4e00-b74c-a6a14654faeb
spec:
guestbookSubtitle: First Day of Many
guestbookTitle: The Chemical Wedding of Remko
kind: List
metadata:
resourceVersion: ""
selfLink: ""
In the Kubernetes Dashboard web console, you can browse to Custom Resource Definitions and find the Guestbook CRD.
You have now created a new type or Custom Resource (CR) and created an instance of your new type. But just having a new type and a new instance of the type, does not add as much control over the instances yet, we can basically only create and delete a static type with some descriptive meta-data. With a custom controller or Operator you can over-write the methods that are triggered at certain lifecycle events.
Operators are clients of the Kubernetes API that act as controllers for a Custom Resource.
To write applications that use the Kubernetes REST API, you can use one of the following supported client libraries:
Ready made operators
For a Red Hat curated list of Kubernetes Operators you can go to the OPenShift console > Operators > OperatorHub.
Create a Custom Resource and Operator using the Operator SDK
To write your own operator you can use existing tools:
The Operator SDK provides the following workflow to develop a new Operator:
The following workflow is for a new Go operator:
Create a new operator project using the SDK Command Line Interface(CLI)
Define new resource APIs by adding Custom Resource Definitions(CRD)
Define Controllers to watch and reconcile resources
Write the reconciling logic for your Controller using the SDK and controller-runtime APIs
Use the SDK CLI to build and generate the operator deployment manifests
Install sdk-operator
The following section uses the sdk-operator cli, which depends on Go to be installed. Check if both tools are installed in your web-terminal,
go version
operator-sdk version
If you see a command not found error, install both now.
To install the Operator SDK in Ubuntu, you need to install the Go tools and the Operator SDK.
curl -LO https://golang.org/dl/go1.14.4.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.14.4.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
go version
operator-sdk new $OPERATOR_PROJECT --type go --repo github.com/$DOCKER_USERNAME/$OPERATOR_NAME
cd $OPERATOR_PROJECT
The scaffolding of a new project will create an operator, an api and a controller.
2. Create a new API
Add a new API definition for a new Custom Resource under pkg/apis and generate the Custom Resource Definition (CRD) and Custom Resource (CR) files under deploy/crds.
operator-sdk add api --api-version=$OPERATOR_GROUP/$OPERATOR_VERSION --kind=$CRD_KIND
The command will create a new API, a Custom Resource (CR), a Custom Resource Definition (CRD).
One file is created in pkg/apis called addtoscheme_guestbook_v1.go that registers the new schema. One new file is created in pkg/apis/guestbook called group.go that defines the package. Four new files are created in pkg/apis/guestbook/v1:
doc.go,
guestbook_types.go,
register.go,
zz_generated.deepcopy.go.
The guestbook_types.go file,
package v1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GuestbookSpec defines the desired state of Guestbook
type GuestbookSpec struct {
}
// GuestbookStatus defines the observed state of Guestbook
type GuestbookStatus struct {
}
type Guestbook struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec GuestbookSpec `json:"spec,omitempty"`
Status GuestbookStatus `json:"status,omitempty"`
}
// GuestbookList contains a list of Guestbook
type GuestbookList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Guestbook `json:"items"`
}
func init() {
SchemeBuilder.Register(&Guestbook{}, &GuestbookList{})
}
The Custom Resource (CR) in file deploy/crds/guestbook.remkoh.dev_v1_guestbook_cr,
The Custom Resource Definition (CRD) in file deploy/crds/guestbook.remkoh.dev_guestbooks_crd.yaml,
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: guestbooks.guestbook.remkoh.dev
spec:
group: guestbook.remkoh.dev
names:
kind: Guestbook
listKind: GuestbookList
plural: guestbooks
singular: guestbook
scope: Namespaced
versions:
- name: v1
schema:
openAPIV3Schema:
description: Guestbook is the Schema for the guestbooks API
properties:
apiVersion:
description: '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'
type: string
kind:
description: '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'
type: string
metadata:
type: object
spec:
description: GuestbookSpec defines the desired state of Guestbook
type: object
status:
description: GuestbookStatus defines the observed state of Guestbook
type: object
type: object
served: true
storage: true
subresources:
status: {}
add_guestbook.go, which registers the new controller, and
guestbook/guestbook_controller.go, which is the actual custom controller logic.
The file guestbook/guestbook_controller.go defines the Reconcile function,
// Reconcile reads state of the cluster for a Guestbook object and makes changes based on the state read and what is in the Guestbook.Spec
// TODO(user): User must modify this Reconcile function to implement their own Controller logic. This example creates a Pod as an example
func (r *ReconcileGuestbook) Reconcile(request reconcile.Request) (reconcile.Result, error) {
...
// Fetch the Guestbook instance
instance := &guestbookv1.Guestbook{}
...
// Define a new Pod object
pod := newPodForCR(instance)
...
}
4. Compile and Build the Code
The operator-sdk build command compiles the code and builds the executables. fter you built the image, push it to your image registry, e.g. Docker hub.
You have to ask yourself if it makes sense if your objects are added as a Custom Resource to Kubernetes or not. If your API is a you can consider adding a CR.
,
,
,
,
,
.
In addition, there are many community-maintained .
At the Kubernetes , you find ready to use operators written by the community.
For a list of Red Hat certified partner software go to the Red Hat Marketplace at