Extra Credit: Kubernetes Extensions

  • Access the web-terminal

  • Login

  • 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

When running the lab for Kubernetes Extensions, you can make use of a web-terminal. The Dockerfile to use is located in https://github.com/IBMAppModernization/web-terminalarrow-up-right, and named Dockerfile-s2i-oc-tekton-operatorarrow-up-right.

To run on localhost as a Docker container,

git clone https://github.com/IBMAppModernization/web-terminal.git
cd web-terminal
docker build --no-cache -t web-terminal:latest -f Dockerfile-s2i-oc-tekton-operator .
docker run -d --restart always --name terminal -p 7681:7681 -v $HOME/dev/tmp:/root/dev web-terminal
docker ps -a
CONTAINER ID    IMAGE    COMMAND    CREATED    STATUS    PORTS    NAMES
85edc0b0ec27    web-terminal    "ttyd -p 7681 bash"    17 minutes ago    Up 17 minutes    0.0.0.0:7681->7681/tcp   terminal

The volume mapping will write all files under the working directory to the host directory $HOME/dev/tmp. So suppose my host's user home directory is /Users/remkohdev@us.ibm.com/. If I open the terminal in the browser, the working directory for the user is /root. Any file that is created under /root is created on the host's directory $HOME/dev/tmp. Similarly if I create a file in $HOME/dev/tmp it is available in the container's /root directory.

Open the web-terminal in a browser and go to http://0.0.0.0:7681arrow-up-right.

web-terminal in browser

If Go, Operator SDK

Login

Go to the OpenShift web console Copy Login command

Create a Custom Resource (CR)

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

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 APIarrow-up-right 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 OpenShift web console, you can browse to Administration > Custom Resource Definitions and find the Guestbook CRD at /k8s/cluster/customresourcedefinitions/guestbooks.apps.ibm.com.

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.

Operators

https://kubernetes.io/docs/concepts/extend-kubernetes/operator/arrow-up-right

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 librariesarrow-up-right.

Ready made operators

At the OperatorHub.ioarrow-up-right, you find ready to use operators written by the community.

OperatorHub.io

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

For detailed installation instructions go herearrow-up-right.

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

Create the Operator

1. Create a New Project

Create a new Operatorarrow-up-right 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.

5. Deploy the Operator

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

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

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

Cleanup

Application CRD

The Application CRD (Custom Resource Definition)arrow-up-right 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?