Create a Couchbase cluster using Kubernetes

Editor’s note: today’s guest post is by Arun Gupta, Vice President Developer Relations at Couchbase, showing how to setup a Couchbase cluster with Kubernetes.  Couchbase Server is an open source, distributed NoSQL document-oriented database. It exposes a fast key-value store with managed cache for submillisecond data operations, purpose-built indexers for fast queries and a query engine for executing SQL queries. For mobile and Internet of Things (IoT) environments, Couchbase Lite runs native on-device and manages sync to Couchbase Server.Couchbase Server 4.5 was recently announced, bringing many new features, including production certified support for Docker. Couchbase is supported on a wide variety of orchestration frameworks for Docker containers, such as Kubernetes, Docker Swarm and Mesos, for full details visit this page.  This blog post will explain how to create a Couchbase cluster using Kubernetes. This setup is tested using Kubernetes 1.3.3, Amazon Web Services, and Couchbase 4.5 Enterprise Edition.Like all good things, this post is standing on the shoulder of giants. The design pattern used in this blog was defined in a Friday afternoon hack with @saturnism. A working version of the configuration files was contributed by @r_schmiddy.Couchbase ClusterA cluster of Couchbase Servers is typically deployed on commodity servers. Couchbase Server has a peer-to-peer topology where all the nodes are equal and communicate to each other on demand. There is no concept of master nodes, slave nodes, config nodes, name nodes, head nodes, etc, and all the software loaded on each node is identical. It allows the nodes to be added or removed without considering their “type”. This model works particularly well with cloud infrastructure in general. For Kubernetes, this means that we can use the exact same container image for all Couchbase nodes.A typical Couchbase cluster creation process looks like:Start Couchbase: Start n Couchbase serversCreate cluster: Pick any server, and add all other servers to it to create the clusterRebalance cluster: Rebalance the cluster so that data is distributed across the clusterIn order to automate using Kubernetes, the cluster creation is split into a “master” and “worker” Replication Controller (RC). The master RC has only one replica and is also published as a Service. This provides a single reference point to start the cluster creation. By default services are visible only from inside the cluster. This service is also exposed as a load balancer. This allows the Couchbase Web Console to be accessible from outside the cluster.The worker RC use the exact same image as master RC. This keeps the cluster homogenous which allows to scale the cluster easily.Configuration files used in this blog are available here. Let’s create the Kubernetes resources to create the Couchbase cluster.Create Couchbase “master” Replication ControllerCouchbase master RC can be created using the following configuration file:apiVersion: v1kind: ReplicationControllermetadata:  name: couchbase-master-rcspec:  replicas: 1  selector:    app: couchbase-master-pod  template:    metadata:      labels:        app: couchbase-master-pod    spec:      containers:      – name: couchbase-master        image: arungupta/couchbase:k8s        env:          – name: TYPE            value: MASTER        ports:        – containerPort: 8091—-apiVersion: v1kind: Servicemetadata:  name: couchbase-master-service  labels:    app: couchbase-master-servicespec:  ports:    – port: 8091  selector:    app: couchbase-master-pod  type: LoadBalancerThis configuration file creates a couchbase-master-rc Replication Controller. This RC has one replica of the pod created using the arungupta/couchbase:k8s image. This image is created using the Dockerfile here. This Dockerfile uses a configuration script to configure the base Couchbase Docker image. First, it uses Couchbase REST API to setup memory quota, setup index, data and query services, security credentials, and loads a sample data bucket. Then, it invokes the appropriate Couchbase CLI commands to add the Couchbase node to the cluster or add the node and rebalance the cluster. This is based upon three environment variables:TYPE: Defines whether the joining pod is worker or masterAUTO_REBALANCE: Defines whether the cluster needs to be rebalancedCOUCHBASE_MASTER: Name of the master serviceFor this first configuration file, the TYPE environment variable is set to MASTER and so no additional configuration is done on the Couchbase image.Let’s create and verify the artifacts.Create Couchbase master RC:kubectl create -f cluster-master.yml replicationcontroller “couchbase-master-rc” createdservice “couchbase-master-service” createdList all the services:kubectl get svcNAME                       CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGEcouchbase-master-service   10.0.57.201                 8091/TCP   30skubernetes                 10.0.0.1      <none>        443/TCP    5hOutput shows that couchbase-master-service is created.Get all the pods:kubectl get poNAME                        READY     STATUS    RESTARTS   AGEcouchbase-master-rc-97mu5   1/1       Running   0          1mA pod is created using the Docker image specified in the configuration file.Check the RC:kubectl get rcNAME                  DESIRED   CURRENT   AGEcouchbase-master-rc   1         1         1mIt shows that the desired and current number of pods in the RC are matching.Describe the service:kubectl describe svc couchbase-master-serviceName: couchbase-master-serviceNamespace: defaultLabels: app=couchbase-master-serviceSelector: app=couchbase-master-podType: LoadBalancerIP: 10.0.57.201LoadBalancer Ingress: a94f1f286590c11e68e100283628cd6c-1110696566.us-west-2.elb.amazonaws.comPort: <unset> 8091/TCPNodePort: <unset> 30019/TCPEndpoints: 10.244.2.3:8091Session Affinity: NoneEvents:  FirstSeen LastSeen Count From SubobjectPath Type Reason Message  ——— ——– —– —- ————- ——– —— ——-  2m 2m 1 {service-controller } Normal CreatingLoadBalancer Creating load balancer  2m 2m 1 {service-controller } Normal CreatedLoadBalancer Created load balancerAmong other details, the address shown next to LoadBalancer Ingress is relevant for us. This address is used to access the Couchbase Web Console.Wait for ~3 mins for the load balancer to be ready to receive requests. Couchbase Web Console is accessible at <ip>:8091 and looks like:The image used in the configuration file is configured with the Administrator username and password password. Enter the credentials to see the console:Click on Server Nodes to see how many Couchbase nodes are part of the cluster. As expected, it shows only one node:Click on Data Buckets to see a sample bucket that was created as part of the image:This shows the travel-sample bucket is created and has 31,591 JSON documents.Create Couchbase “worker” Replication ControllerNow, let’s create a worker replication controller. It can be created using the configuration file:apiVersion: v1kind: ReplicationControllermetadata:  name: couchbase-worker-rcspec:  replicas: 1  selector:    app: couchbase-worker-pod  template:    metadata:      labels:        app: couchbase-worker-pod    spec:      containers:      – name: couchbase-worker        image: arungupta/couchbase:k8s        env:          – name: TYPE            value: “WORKER”          – name: COUCHBASE_MASTER            value: “couchbase-master-service”          – name: AUTO_REBALANCE            value: “false”        ports:        – containerPort: 8091This RC also creates a single replica of Couchbase using the same arungupta/couchbase:k8s image. The key differences here are:TYPE environment variable is set to WORKER. This adds a worker Couchbase node to be added to the cluster.COUCHBASE_MASTER environment variable is passed the value of couchbase-master-service. This uses the service discovery mechanism built into Kubernetes for pods in the worker and the master to communicate.AUTO_REBALANCE environment variable is set to false. This ensures that the node is only added to the cluster but the cluster itself is not rebalanced. Rebalancing is required to to re-distribute data across multiple nodes of the cluster. This is the recommended way as multiple nodes can be added first, and then cluster can be manually rebalanced using the Web Console.Let’s create a worker:kubectl create -f cluster-worker.yml replicationcontroller “couchbase-worker-rc” createdCheck the RC:kubectl get rcNAME                  DESIRED   CURRENT   AGEcouchbase-master-rc   1         1         6mcouchbase-worker-rc   1         1         22sA new couchbase-worker-rc is created where the desired and the current number of instances are matching.Get all pods:kubectl get poNAME                        READY     STATUS    RESTARTS   AGEcouchbase-master-rc-97mu5   1/1       Running   0          6mcouchbase-worker-rc-4ik02   1/1       Running   0          46sAn additional pod is now created. Each pod’s name is prefixed with the corresponding RC’s name. For example, a worker pod is prefixed with couchbase-worker-rc.Couchbase Web Console gets updated to show that a new Couchbase node is added. This is evident by red circle with the number 1 on the Pending Rebalance tab.Clicking on the tab shows the IP address of the node that needs to be rebalanced:Scale Couchbase cluster Now, let’s scale the Couchbase cluster by scaling the replicas for worker RC:kubectl scale rc couchbase-worker-rc –replicas=3replicationcontroller “couchbase-worker-rc” scaledUpdated state of RC shows that 3 worker pods have been created:kubectl get rcNAME                  DESIRED   CURRENT   AGEcouchbase-master-rc   1         1         8mcouchbase-worker-rc   3         3         2mThis can be verified again by getting the list of pods:kubectl get poNAME                        READY     STATUS    RESTARTS   AGEcouchbase-master-rc-97mu5   1/1       Running   0          8mcouchbase-worker-rc-4ik02   1/1       Running   0          2mcouchbase-worker-rc-jfykx   1/1       Running   0          53scouchbase-worker-rc-v8vdw   1/1       Running   0          53sPending Rebalance tab of Couchbase Web Console shows that 3 servers have now been added to the cluster and needs to be rebalanced.Rebalance Couchbase ClusterFinally, click on Rebalance button to rebalance the cluster. A message window showing the current state of rebalance is displayed:Once all the nodes are rebalanced, Couchbase cluster is ready to serve your requests:In addition to creating a cluster, Couchbase Server supports a range of high availability and disaster recovery (HA/DR) strategies. Most HA/DR strategies rely on a multi-pronged approach of maximizing availability, increasing redundancy within and across data centers, and performing regular backups.Now that your Couchbase cluster is ready, you can run your first sample application.For further information check out the Couchbase Developer Portal and Forums, or see questions on Stack Overflow.  –Arun Gupta, Vice President Developer Relations at CouchbaseDownload KubernetesGet involved with the Kubernetes project on GitHub Post questions (or answer questions) on Stack Overflow Connect with the community on SlackFollow @Kubernetesio on Twitter for latest updates
Quelle: kubernetes

Published by