This operator is used to set up this application. The application includes a Nodejs server and a MongoDB database. Docker image of the server can be found here.
When a new SocialBook
custom resource is created the custom controller will create a MongoDB
deployment, a corresponding service for it, Persistent Volume and Persistent Volume Claim. It will also create a SocialBook
deployment and an external service so that it can be accessed outside the cluster.
Apart from this it will also create a Config Map and Network policies for both MongoDB and SocialBook pods. The number of replicas and other information can be passed in the spec of the custom resource.
Note: For the network policies to work, a network plugin should already be installed on the cluster.
- Clone the repo -
git clone https://github.com/Ashwin901/K8s-Operator-SocialBook.git
- Run
kubectl apply -f manifests/operators_socialbooks.yaml
to install the CRD. - Run
go build
from the parent directory. - Run
./social-book-operator --config <kube config file path>
(In linux systems usually kube config file path is /home/username/.kube/config).
- To install the operator inside the cluster we can use the docker image of the operator.
- Install the CRD by using the following file.
- Copy the files from this directory.
- rbac.yml file conists of a service account, cluster role and cluster role binding. This basically gives the operator permissions to access Kubernetes resources. Run
kubectl apply -f rbac.yml
. - deployment.yml creates a deployment for the operator docker image. Run
kubectl apply -f deployment.yml
Now you can test the operator by creating a new SocialBook custom resource. You can use this example. Run kubectl apply -f example1.yml
.
Once the custom resource is created check the dev
namespace(in the above example dev
namespace is used but you can use any namespace) if all the resources are created.
If you are using minikube use the following command: minikube service -n dev socialbook1
(socialbook1
- name used in the above example)
- Custom Resource
- Controller
- CRD
- Group: ashwin901.operators Version: v1alpha1 Kind: SocialBook
- Example for the Custom Resource can be found here
-
Controller code can be found here.
-
When a SocialBook CR is created the custom controller sets up the following resources:
1. Config Map
2. Persistent Volume
3. Persistent Volume Claim
4. Deployment - MongoDB and SocialBook
5. Services
6. Network Policy - Ensures that theMongoDB
pod only accepts requests fromSocialBook
pods(ingress) andSocialBook
pods can only make requests toMongoDB
pods(egress). -
If any of the above mentioned resource is updated/deleted then the custom controller will detect the change and try to get it back to the desired state.
-
If a particular SocialBook resource is deleted then all the resources setup for it will also be deleted. This is done with the help of owner reference.
- Code Generator - To generate code for clientset, informers and lister
- Controller Gen - To generate manifests for CRD