Sunday, November 18, 2018

JTD-DesignerSeries-13-KubernetesOperatorForMongoDB-101


A Brief on Containerization
Generally transporting goods involves packaging & shipping containers that move between different modes of transportation by different shipping companies to deliver your goods at the doorstep. critical business functions. Modern software techniques like microservices divide software applications into smaller independent functions that are build, packaged, scaled & managed independently as isolated containers. Containers can mentioned as operating system level virtualization method for running multiple isolated linux systems on a host with a single linux kernel.


Docker has become the de-facto standard for managing container images defined using Dockerfile, and allows that to run on any machine with Docker Install. However complex application require container orchestration solutions, like Kubernetes, that can manage the lifecycle of containers & how these containers communicate can each other.

A Brief on Kubernetes
Kubernetes is an open source platform for managing containerized workloads & services. Kubernetes, with support from major cloud vendors, has emerged as the de-facto standard for container orchestration governed by Cloud Native Computing Foundation a.k.a CNCF. Kubernetes, born at Google, has a backing from large open source community has quite an advantage compared to other products like Docker Swarm, Apache Mesos.
Master node in a kubernetes cluster contains services to support the Rest API, scheduler & controller manager. Each cluster contain one or more worker node, which contain the components to communicate with master node & also manage the containers running on the node. Worker node run containers managed as a logical layer represented by Pod.


A Brief on MongoDB Ops Manager & Operator
Ops Manager 4.0 contains a specialized component called MongoDB Ops Manager Kubernetes operator, simply referred as Operator. Operator Implementation, now part of Kubernetes framework, is a continuously running lightweight process deployed as a Pod with single container.


Operator defines & registers the custom types within the Kubernetes cluster, which allows operator to receive notification about the events occurring on the registered types. Notifications such as object creation or object deletion allow Operator to trigger custom logic on Kubernetes tasks, such as add mongod replica set to the Ops Manager project. Operator essentially acts as a proxy between the Kubernetes & Ops Manager to perform the needed tasks against each system. Helm, which is a tool for managing packaging & deployment in Kubernetes, can be used to deploy an operator Pod with a helm chart.


Lab - Setup Kubernetes Cluster with MongoDB


Pre-requisite Step: Virtual Box, Docker, Kubectl, Minikube, Helm



Virtual Box: virtualbox --help

Docker: docker version










Kubectl: kubectl version



Minikube: minikube version

Helm: helm version



Step: MiniKube

minikube start
minikube status

eval $(minikube docker-env) - This sets the shell environment variables so that docker points to the registry running inside the kubernetes cluster. Running docker images will list the kubernetes images deployed in the minikube cluster.


Lab - Kubernetes Operator

a) Create a MongoDB namespace.

Jeetans-MacBook-Pro:dirKubernetes home$ kubectl create namespace mongodb
namespace/mongodb created

b) Configure Kubectl to mongodb namespace
Jeetans-MacBook-Pro:dirKubernetes home$ kubectl config set-context $(kubectl config current-context) --namespace=mongodb

Context "minikube" modified.

c) Check for deployed resources in mongodb namespace.
Jeetans-MacBook-Pro:dirKubernetes home$ kubectl get all

No resources found.

d) Clone MongoDB-Enterprise-Kubernetes repository
Jeetans-MacBook-Pro:dirKubernetes home$ git clone https://github.com/mongodb/mongodb-enterprise-kubernetes
Cloning into 'mongodb-enterprise-kubernetes'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 132 (delta 8), reused 14 (delta 6), pack-reused 111
Receiving objects: 100% (132/132), 29.71 KiB | 2.29 MiB/s, done.
Resolving deltas: 100% (52/52), done.
Jeetans-MacBook-Pro:dirKubernetes home$ ls
mongodb-enterprise-kubernetes

d) Create a service account for helm
Jeetans-MacBook-Pro:dirKubernetes home$ kubectl create serviceaccount --namespace kube-system tiller
serviceaccount/tiller created

e) Create a cluster role binding for the account
Jeetans-MacBook-Pro:dirKubernetes home$ kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
clusterrolebinding.rbac.authorization.k8s.io/tiller-cluster-rule created

f) Initialize the helm system
Jeetans-MacBook-Pro:dirKubernetes home$ helm init --service-account tiller
Creating /Users/home/.helm 
Creating /Users/home/.helm/repository 
Creating /Users/home/.helm/repository/cache 
Creating /Users/home/.helm/repository/local 
Creating /Users/home/.helm/plugins 
Creating /Users/home/.helm/starters 
Creating /Users/home/.helm/cache/archive 
Creating /Users/home/.helm/repository/repositories.yaml 
Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com 
Adding local repo with URL: http://127.0.0.1:8879/charts 
$HELM_HOME has been configured at /Users/home/.helm.

Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.

Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
To prevent this, run `helm init` with the --tiller-tls-verify flag.
For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation
Happy Helming!

g) Create a secret & verify it with describe command.
$ kubectl -n mongodb create secret generic my-credentials --from-literal="user=some@example.com" --from-literal="publicApiKey=my-public-api-key"

secret/madajeeblog-credentials created
Jeetans-MacBook-Pro:dirKubernetes home$ kubectl describe secrets/madajeeblog-credentials -n mongodb
Name:         madajeeblog-credentials
Namespace:    mongodb
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
publicApiKey:  36 bytes
user:          21 bytes

i) Install the operator with helm chart.
Jeetans-MacBook-Pro:mongodb-enterprise-kubernetes home$ helm install helm_chart/ --name mongodb-enterprise
NAME:   mongodb-enterprise
LAST DEPLOYED: Sun Nov 18 09:33:07 2018
NAMESPACE: mongodb
STATUS: DEPLOYED

RESOURCES:
==> v1/ServiceAccount
NAME                         AGE
mongodb-enterprise-operator  1s

==> v1beta1/CustomResourceDefinition
mongodbstandalones.mongodb.com      1s
mongodbreplicasets.mongodb.com      1s
mongodbshardedclusters.mongodb.com  1s

==> v1/Role
mongodb-enterprise-operator  1s

==> v1/RoleBinding
mongodb-enterprise-operator  1s

==> v1/Deployment
mongodb-enterprise-operator  1s

==> v1/Pod(related)

NAME                                          READY  STATUS             RESTARTS  AGE
mongodb-enterprise-operator-74fbcbd9b7-p944v  0/1    ContainerCreating  0         1s

i) Operator is up & running.

Jeetans-MacBook-Pro:mongodb-enterprise-kubernetes home$ kubectl get all
NAME                                               READY   STATUS    RESTARTS   AGE
pod/mongodb-enterprise-operator-74fbcbd9b7-p944v   1/1     Running   0          9m

NAME                                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mongodb-enterprise-operator   1         1         1            1           9m

NAME                                                     DESIRED   CURRENT   READY   AGE
replicaset.apps/mongodb-enterprise-operator-74fbcbd9b7   1         1         1       9m
Jeetans-MacBook-Pro:mongodb-enterprise-kubernetes home$ 


Lab - MongoDB Ops Manager
a) Simple Test Ops Manager - Deployment with one pod with container running a mongoDB instance for Ops Manager application DB, another container running an instance of Ops Manager.
Jeetans-MacBook-Pro:dirKubernetes home$ curl -OL https://raw.githubusercontent.com/jasonmimick/mongodb-openshift-dev-preview/master/simple-test-opsmanager-k8s/simple-test-opsmgr.yaml

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100  3228  100  3228    0     0  14934      0 --:--:-- --:--:-- --:--:-- 14944

b) Use kubectl & downloaded yaml configuration to create an instance of Ops Manager.
Jeetans-MacBook-Pro:dirKubernetes home$ kubectl create -f simple-test-opsmgr.yaml
persistentvolume/mongodb-opsmgr-appdb-pv-volume created
persistentvolumeclaim/mongodb-opsmgr-appdb-pv-claim created
persistentvolume/mongodb-opsmgr-config-pv-volume created
persistentvolumeclaim/mongodb-opsmgr-config-pv-claim created
secret/mongodb-opsmgr-global-admin created
service/mongodb-opsmgr created
deployment.apps/mongodb-opsmgr created

c) Ops Manager is up & running.
Jeetans-MacBook-Pro:dirKubernetes home$ kubectl get all
NAME                                               READY   STATUS             RESTARTS   AGE
pod/mongodb-enterprise-operator-74fbcbd9b7-p944v   1/1     Running            0          5h
pod/mongodb-opsmgr-8c44d98f8-97jvs                 0/2     Running            0          1m

NAME                     TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/mongodb-opsmgr   NodePort   10.100.253.9   <none>        8080:30080/TCP   1m

NAME                                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mongodb-enterprise-operator   1         1         1            1           5h
deployment.apps/mongodb-opsmgr                1         1         1            0           1m

NAME                                                     DESIRED   CURRENT   READY   AGE
replicaset.apps/mongodb-enterprise-operator-74fbcbd9b7   1         1         1       5h
replicaset.apps/mongodb-opsmgr-8c44d98f8                 1         1         0       1m
Jeetans-MacBook-Pro:dirKubernetes home$ 

c) Ops Manager runtime contains the configuration for creation of config map, projects & secrets objects.
d) Create a mongoDB replica set config file with appropriate project, credentials & namespace.
e) Run the config file to create a replica set which have mongod container pod as its members. Container Pods will be associated with the Stateful Sets.
f) You can then connect to the mongoDB replica set with the minikube IP & exposed external port from a kubernetes replica set service.





Thanks

No comments:

Post a Comment