Lab: Kubernetes Extensions

  • Access the client terminal

  • Login to ibmcloud

  • Connect to your cluster

  • Create a Custom Resource

  • Operators

    • Ready Made Operators

  • Create a Custom Resource and Operator using the Operator SDK

    • Install sdk-operator

    • Create the Operator

    • Cleanup

  • Application CRD

Access the web-terminal

Go to the URL for your web-terminal, given to you by the instructor team.

Login

export CLUSTER_NAME=<your cluster name>
ibmcloud login
oc config current-context

Connect to your cluster by downloading the cluster config,

Check you can access the cluster,

Create a Custom Resource (CR)

https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/

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.

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 Declarative API you can consider adding a CR.

  • 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,

When run in the terminal,

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,

And to create the my-guestbook resource, run the command

When run in the terminal,

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.

To read the details for the my-guestbook of type Guestbook, describe the instance,

Or retrieve the resource information by specifying the type,

In the Kubernetes Dashboard web console, you can browse to Custom Resource Definitions and find the Guestbook CRD.

Administration > Custom Resource Definitions

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.

Cleanup

Operators

https://kubernetes.io/docs/concepts/extend-kubernetes/operator/

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:

In addition, there are many community-maintained client libraries.

Ready made operators

At the Kubernetes OperatorHub.io, you find ready to use operators written by the community.

OperatorHub.io

For a Red Hat curated list of Kubernetes Operators you can go to the OPenShift console > Operators > OperatorHub.

Red Hat OperatorHub

For a list of Red Hat certified partner software go to the Red Hat Marketplace at http://ibm.biz/clientrhm

Red Hat Marketplace

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:

  1. Create a new operator project using the SDK Command Line Interface(CLI)

  2. Define new resource APIs by adding Custom Resource Definitions(CRD)

  3. Define Controllers to watch and reconcile resources

  4. Write the reconciling logic for your Controller using the SDK and controller-runtime APIs

  5. 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,

If you see a command not found error, install both now.

For detailed installation instructions go here.

To install the Operator SDK in Ubuntu, you need to install the Go tools and the Operator SDK.

Install the Operator SDK,

Check again,

Create the Operator

1. Create a New Project

Create a new Operator project,

and

Create a new Operator project,

The scaffolding of a new project will create an operator, an api and a controller.

new project directory structure

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.

The command will create a new API, a Custom Resource (CR), a Custom Resource Definition (CRD).

new project directory structure

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,

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,

3. Create a new Controller

Add a new controller under pkg/controller/<kind>.

This command creates two files in pkg/controller:

  • 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,

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.

Login in to the container registry,

And push the build image to your image repository,

5. Deploy the Operator

First replace the image attribute in the operator resource with the built image,

Inspect the deploy/operator.yaml file,

And replace the REPLACE_IMAGE value,

Make sure you are connected to the OpenShift cluster (see above how to connect), and deploy the operator with the following template code.

Verify the deployment,

For our example Guestbook project the above templates should resolve as follows,

Cleanup

Application CRD

The Application CRD (Custom Resource Definition) and Controller provide the following:

  • Describe an applications metadata,

  • A point to connect the infrastructure, such as Deployments, to as a root object.

  • Application level health checks.

This could be used by:

  • Application operators,

  • Tools, such as Helm, and

  • Dashboards.

Last updated

Was this helpful?