Getting Started with Couchbase using Kubernetes, Vagrant and VirtualBox
By: Kishore Allwynraj
Tags: Vagrant, Tutorial, Getting Started, Kubernetes, Couchbase

Setting up Kubernetes cluster

For this guide, we will be using a vagrant script provided by Oracle in Github to provision a three node kubernetes cluster in your local machine. The readme file in the Github provides a detailed information for running the scripts which are straight forward to follow. There is also a short video tutorial here which you can go through prior to your installation.

The vagrant scripts uses kubeadm internally to provision the master and worker nodes and join them. Once you complete the setup following the Github link, you can run below kubectl command see the node and cluster information.

D:\Workspaces> kubectl get nodes

NAME STATUS ROLES AGE VERSION

master.vagrant.vm Ready master 32m v1.12.7+1.2.3.el7

worker1.vagrant.vm Ready <none> 16m v1.12.7+1.2.3.el7

worker2.vagrant.vm Ready <none> 3m32s v1.12.7+1.2.3.el7 kubectl cluster-info

Kubernetes master is running at

KubeDNS is running at D:\Workspaces>Kubernetes master is running at https://192.168.99.100:6443 KubeDNS is running at https://192.168.99.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To view all the pods currently running in your kubernetes cluster you can run the below kubectl command. As you see, the kubernetes dashboard pod is already running and by default it is exposed under port 8001.

D:\Workspaces> kubectl get pods — all-namespaces

NAMESPACE NAME READY STATUS RESTARTS AGE

kube-system coredns-b7df4d4c4-mbrxm 1/1 Running 0 39m

kube-system coredns-b7df4d4c4-s86tv 1/1 Running 0 39m

kube-system etcd-master.vagrant.vm 1/1 Running 0 38m

kube-system kube-apiserver-master.vagrant.vm 1/1 Running 0 39m

kube-system kube-controller-manager-master.vagrant.vm 1/1 Running 0 38m

kube-system kube-flannel-ds-d8tbn 1/1 Running 0 10m

kube-system kube-flannel-ds-g2ssq 1/1 Running 0 39m

kube-system kube-flannel-ds-mc67z 1/1 Running 0 23m

kube-system kube-proxy-gmvm5 1/1 Running 0 23m

kube-system kube-proxy-m7npg 1/1 Running 0 39m

kube-system kube-proxy-vrjff 1/1 Running 0 10m

kube-system kube-scheduler-master.vagrant.vm 1/1 Running 0 38m

kube-system kubernetes-dashboard-669df9cb5d-4zhl9 1/1 Running 0 39m

To access the kubernetes dashboard, you need to create a user with cluster administrator role. Lets create a file named “dashboard-adminuser.yaml” and paste the below content into it.

apiVersion: v1

kind: ServiceAccount

metadata:

name: admin-user

namespace: kube-system

Now create another file named “admin-role-binding.yml” and paste the below content into it. This is required to bind the previously created admin-user into cluster admin role.

apiVersion: rbac.authorization.k8s.io/v1

kind: ClusterRoleBinding

metadata:

name: admin-user

roleRef:

apiGroup: rbac.authorization.k8s.io

kind: ClusterRole

name: cluster-admin

subjects:

- kind: ServiceAccount

name: admin-user

namespace: kube-system

Now execute below kubectl commands to create this admin user and bind it to cluster admin role in your local kubernetes cluster.

D:\Workspaces> kubectl apply -f dashboard-adminuser.yaml

serviceaccount/admin-user created D:\Workspaces> kubectl apply -f admin-role-binding.yml

clusterrolebinding.rbac.authorization.k8s.io/admin-user created

When the admin user is created, it also creates a service account token in your cluster. You will be needing this token to login to your kubernetes cluster. Run the below kubectl command to get the token details

kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk ‘{print $1}’)

The kubernetes nodes provisioned using vagrant script already has port forwarding enabled for port 8001, so you can access the kubernetes dashboard using this localhost url. Below is the snapshot of the dashboard.