A controller that manages an application and its supporting service as a single K8s custom resource
This project was proposed as an assignment to me during my 6-week internship at Delivion GmbH. The source code is published under agreement with Delivion.
The project was developed after studying the article from Martin Helmich. The project skeleton was originally adapted from his code templates.
The overall goal is to create an additional abstraction layer on top of native Kubernetes resources to describe an “Application”.
As a user, I want to be able to create a resource looking more or less like follows:
apiVersion: delivion.io/alpha1
kind: Application
metadata:
name: nginx-whatever
spec:
database: true
externalsecret: http://XXXX/xxx.json
replicas: 3
template:
spec:
containers:
-
name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
If I create such a resource and submit it to Kubernetes, the controller should ensure that the appropriate Kubernetes resources get created.
Now that we have the overall goal, let’s go through in more details:
At this step, just consider the Application definition above, but without the database and externalsecret.
You will create a controller that adds the additional Application
layer, but doesn’t add any additional functionality. What it will do, however, is:
Application
to kubernetes, the corresponding Deployment
gets created automaticallyApplication
, the corresponding Deployment
gets deletedApplication
, the corresponding changes are made to the Deployment
Deployment
(but not the Application
), it gets recreatedDeployment
(but not the Application
), the changes are rolled back (i.e, it is kept in sync with the Application
)kubectl describe
the Application, it shows the status of the DeploymentApplication
use one Postgres cluster, so database name is equal to the Postgres cluster name)When I ...
in the Step 1, but this time you should define it yourself)Application
CR) to connect to the Postgres clusterDeploy a (web) application to Kubernetes. It should:
Add external Secret/Value support in the Application
CRD:
Again, define the logic(s) for these requirements
Implement Controller logics
Unit Testing
$ git clone https://github.com/hoangphanthai/Kubernetes_Custom_Resource_Controller.git
$ kubectl apply -f https://raw.githubusercontent.com/hoangphanthai/Kubernetes_Custom_Resource_Controller/main/crd/crd.yaml
$ cd Kubernetes_Custom_Resource_Controller
$ go run .
$ watch -n 1 kubectl get all -o wide
Start new terminal window (tab)
$ kubectl apply -f https://raw.githubusercontent.com/hoangphanthai/Kubernetes_Custom_Resource_Controller/main/crd/app1.yaml
Enable PostgreSQL cluster in app1$ kubectl apply -f https://raw.githubusercontent.com/hoangphanthai/Kubernetes_Custom_Resource_Controller/main/crd/app1DbEnabled.yaml
Play around with “Applications”
$ kubectl apply -f https://raw.githubusercontent.com/hoangphanthai/Kubernetes_Custom_Resource_Controller/main/crd/app2.yaml
$ kubectl get application
$ kubectl delete application app1
….. more logics defined in the above step 4. Implementation