Deploying couchbase server and spring boot microservice on Kubernetes
Couchbase is one of the most popular document database around and couchbase server supports full-text search with a nice dashboard UI, where you can set up a cluster, add users, and configure buckets. When deploying couchbase server, as a dockerized container, you’d have to login to the dashboard and manually provision the server with memory, bucket name and index, which can be a tedious process. So, how do we work around this?.
The official couchbase blogs document an example on using the couchbase REST API to pre-configure the server with memory, set up user with admin privileges, configure bucket name, when the couchbase container spins up. We’d have to override the entrypoint.sh file in the couchbase image, with few curl commands to pre-configure the server.
Kubernetes, is the ever popular container orchestrator framework. Even, docker supports k8s officially!. The spring boot microservice that I’m going to deploy on k8s, uses the spring data couchbase module, which requires a couchbase cluster and bucket information (credentials), so that it can open the bucket and establish connectivity to the node. Let’s take a look at the kubernetes deployment file for the spring boot app (spring-boot.yaml)
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: couchbase-rc-pod
tier: backend
ports:
- protocol: "TCP"
port: 8080
targetPort: 8080
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-service
spec:
selector:
matchLabels:
app: couchbase-rc-pod
tier: backend
track: stable
replicas: 1
template:
metadata:
labels:
app: couchbase-rc-pod
tier: backend
track: stable
spec:
containers:
- name: backend-service-app
image: backend-service:2.0
imagePullPolicy: Never
ports:
- containerPort: 8080
env:
- name: COUCHBASE_HOSTNAME
value: couchbase-service
- name: COUCHBASE_BUCKET_NAME
value: beerInfo
- name: COUCHBASE_BUCKET_PASSWORD
value: password
- name: COUCHBASE_CLUSTER_USERNAME
value: Administrator
- name: COUCHBASE_CLUSTER_PASSWORD
value: password
- name: COUCHBASE_CONNECT_TIMEOUT
value: "45000"I’m using the Docker for Mac v18, which has Kubernetes support enabled. The YAML file shown here creates the service and deployment in k8s for the spring app. Using kubectl, we can create the deployment in k8s cluster. kubectl create -f spring-boot.yaml. The credentials required for couchbase server are injected as environment properties. Now, let’s take a look at the deployment file for couchbase server.
apiVersion: v1
kind: Service
metadata:
name: couchbase-service
spec:
selector:
app: couchbase-rc-pod
ports:
- name: admin
port: 8091
- name: views
port: 8092
- name: query
port: 8093
- name: more
port: 8094
- name: memcached
port: 11210
---
apiVersion: v1
kind: ReplicationController
metadata:
name: couchbase-rc
spec:
replicas: 1
template:
metadata:
labels:
app: couchbase-rc-pod
spec:
containers:
- name: couchbase
image: couchbase-custom:4.0
imagePullPolicy: Never
ports:
- containerPort: 8091
- containerPort: 8092
- containerPort: 8093
- containerPort: 8094
- containerPort: 11210The YAML file creates a service and a replication controller for couchbase. The replication controller can be exposed to be accessible outside the cluster. kubectl expose rc couchbase-rc --port=8091 --target-port=8091 --type=NodePort. All HTTP traffic is handled via port 8091 and I can access the couchbase dashboard UI via the exposed NodePort.
kube DNS service is running in my k8s cluster and it should be able to resolve the couchbase-service name to establish communication internally. Docker for Mac with Kubernetes, does not provide a visual dashboard unlike minikube. The kubernetes-dashboard yaml file can be deployed manually using kubectl and you should be able to access it after port forwarding on your localhost.
docker ps | grep dashboard
7a8be2bd8b19 0c60bcf89900 "/dashboard --insecu…" 7 days ago Up 7 days k8s_kubernetes-dashboard_kubernetes-dashboard-7d5dcdb6d9-4g5jt_kube-system_150901d6-95fe-11e8-bcd9-025000000001_1

With the above settings, I can also use docker stack deploy to deploy on k8s cluster. A big thanks to Docker for officially supporting Kubernetes on their app.