This is a very simple hello world Kubernetes operator that used to demonstrate the basic functionalities and implementation details of an operator. The implementation of this operator based on the Operator SDK framework, a framework that generates all the boilerplate code of a Kubernetes operator and provides functionalities to build operators.
- kubectl v1.11.3+
- Kubernetes v1.11.3+ cluster
- operator-sdk
- dep version v0.5.0+
- git
- go version v1.12+
- docker version 17.03+
- kubectl version v1.11.3+
- Kubernetes v1.11.3+ cluster
As described in the prerequisites section, to run or build the operator we need to configure a Kubernetes cluster. There are multiple ways to configure a K8s cluster, therefore we can configure one of the following clusters.
Install the prerequisites.
$ kubectl apply -f https://raw.githubusercontent.com/BuddhiWathsala/helloworld-k8s-operator/v0.4.0/deploy/crds/helloworld.io_helloworlds_crd.yaml
$ kubectl apply -f https://raw.githubusercontent.com/BuddhiWathsala/helloworld-k8s-operator/v0.4.0/deploy/service_account.yaml
$ kubectl apply -f https://raw.githubusercontent.com/BuddhiWathsala/helloworld-k8s-operator/v0.4.0/deploy/role.yaml
$ kubectl apply -f https://raw.githubusercontent.com/BuddhiWathsala/helloworld-k8s-operator/v0.4.0/deploy/role_binding.yaml
Install the operator deployment.
$ kubectl apply -f https://raw.githubusercontent.com/BuddhiWathsala/helloworld-k8s-operator/v0.4.0/deploy/operator.yaml
Here, we are going to deploy a HelloWorld example app that created
apiVersion: helloworld.io/v1alpha1
kind: HelloWorld
metadata:
name: example-helloworld
spec:
size: 1
Apply the HelloWorld example using the following command.
$ kubectl apply -f https://raw.githubusercontent.com/BuddhiWathsala/helloworld-k8s-operator/v0.4.0/deploy/crds/helloworld.io_v1alpha1_helloworld_cr.yaml
These commands will create the following Kubernetes artifacts in your cluster.
➜ ~ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-helloworld-849865f49c-5cr2n 1/1 Running 0 84s
helloworld-k8s-operator-b46f79fb5-6kvq5 1/1 Running 0 112s
➜ ~ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-helloworld ClusterIP 10.106.176.32 <none> 8080/TCP 97s
helloworld-k8s-operator-metrics ClusterIP 10.108.109.142 <none> 8383/TCP,8686/TCP 108s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d19h
The Go application that we just deployed using this helloworld-k8s-operator
, received HTTP GET requests and send the received payload back to the caller. Here we are using NGINX ingress controller to pass the requests to the service, so that first we need to setup the NGINX ingress controller in our Kubernetes cluster. To setup it please refer to this documentation.
Here, the helloworld operator, setup an ingress for the host called helloworld
, therefore we need to set the external IP of the ingress with this hostname. To do that we can obtain the external IP of the ingress using the following command.
$ kubectl get ing
Note that, for minikube, the external IP is the minikube IP and we can get the minikube IP using the following command.
$ minikube ip
Now we can add the above host (helloworld
) as an entry in /etc/hosts
file.
Now, we can send requests to the deployed HelloWorld service from our local machine and the request URL is as follows.
$ curl http://helloworld/<CUSTOM_RESOURCE_NAME>/<MESSAGE>
$ curl http://helloworld/example-helloworld/HelloWorld
The deployed HelloWorld pod will log as follows.
➜ ~ kubectl logs -f `kubectl get pods | grep ^example-helloworld | awk '{print $1}'`
2020/05/07 11:07:37 START THE HELLO WORLD SERVER..!!
2020/05/07 11:09:26 Received message: HelloWorld