Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Minikube: Rapid Dev and Testing for Kubernetes

DZone's Guide to

Minikube: Rapid Dev and Testing for Kubernetes

Take a look at a tool that can help quickly help set up virtual dev and test environments for your applications using Kubernetes.

· Cloud Zone
Free Resource

Deploy and scale data-rich applications in minutes and with ease. Mesosphere DC/OS includes everything you need to elastically run containerized apps and data services in production.

One of the attendees from Kubernetes for Java Developers training suggested to try minikube for simplified Kubernetes dev and testing. This blog will show how to get started with minikube using a simple Java application.

minikube-logo

Minikube starts a single node Kubernetes cluster on your local machine for rapid development and testing. Requirements list the exact set of requirements for different operating systems.

This blog will show:

  • Start one node Kubernetes cluster.
  • Run Couchbase service.
  • Run Java application.
  • View Kubernetes Dashboard.

All Kubernetes resource description files used in this blog are at github.com/arun-gupta/kubernetes-java-sample/tree/master/maven.

Start Kubernetes Cluster Using Minikube

Create a new directory with the name minikube.

In that directory, download kubectl CLI:

curl -Lo kubectl http://storage.googleapis.com/kubernetes-release/release/v1.4.0/bin/darwin/amd64/kubectl && chmod +x kubectl


Download minikube CLI:

curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.10.0/minikube-darwin-amd64 && chmod +x minikube


Start the cluster:

minikube start
Starting local Kubernetes cluster...
Kubectl is now configured to use the cluster.


The list of nodes can be seen:

kubectl get nodes
NAME       STATUS    AGE
minikube   Ready     2h


More details about the cluster can be obtained using the kubectl cluster-info command:

kubectl cluster-info
Kubernetes master is running at https://192.168.99.100:8443
kubernetes-dashboard is running at https://192.168.99.100:8443/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.


Behind the scenes, a Virtual Box VM is started.

Complete set of commands supported can be seen by using --help:

minikube --help
Minikube is a CLI tool that provisions and manages single-node Kubernetes clusters optimized for development workflows.

Usage:
  minikube [command]

Available Commands:
  dashboard        Opens/displays the kubernetes dashboard URL for your local cluster
  delete           Deletes a local kubernetes cluster.
  docker-env       sets up docker env variables; similar to '$(docker-machine env)'
  get-k8s-versions Gets the list of available kubernetes versions available for minikube.
  ip               Retrieve the IP address of the running cluster.
  logs             Gets the logs of the running localkube instance, used for debugging minikube, not user code.
  config           Modify minikube config
  service          Gets the kubernetes URL for the specified service in your local cluster
  ssh              Log into or run a command on a machine with SSH; similar to 'docker-machine ssh'
  start            Starts a local kubernetes cluster.
  status           Gets the status of a local kubernetes cluster.
  stop             Stops a running local kubernetes cluster.
  version          Print the version of minikube.

Flags:
      --alsologtostderr[=false]: log to standard error as well as files
      --log-flush-frequency=5s: Maximum number of seconds between log flushes
      --log_backtrace_at=:0: when logging hits line file:N, emit a stack trace
      --log_dir="": If non-empty, write log files in this directory
      --logtostderr[=false]: log to standard error instead of files
      --show-libmachine-logs[=false]: Whether or not to show logs from libmachine.
      --stderrthreshold=2: logs at or above this threshold go to stderr
      --v=0: log level for V logs
      --vmodule=: comma-separated list of pattern=N settings for file-filtered logging

Use "minikube [command] --help" for more information about a command.


Run Couchbase Service

Create a Couchbase service:

kubectl create -f couchbase-service.yml 
service "couchbase-service" created
replicationcontroller "couchbase-rc" created


This will start a Couchbase service. The service is using the pods created by the replication controller. The replication controller creates a single node Couchbase server.

The configuration file is at github.com/arun-gupta/kubernetes-java-sample/blob/master/maven/couchbase-service.yml and looks like:

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: 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: arungupta/oreilly-couchbase
        ports:
        - containerPort: 8091
        - containerPort: 8092
        - containerPort: 8093
        - containerPort: 11210


Run the Java Application

Run the application:

kubectl create -f bootiful-couchbase.yml 
job "bootiful-couchbase" created


The configuration file is at github.com/arun-gupta/kubernetes-java-sample/blob/master/maven/bootiful-couchbase.yml and looks like:

apiVersion: batch/v1
kind: Job
metadata:
  name: bootiful-couchbase
  labels:
    name: bootiful-couchbase-pod
spec:
  template:
    metadata:
      name: bootiful-couchbase-pod
    spec:
      containers:
      - name: bootiful-couchbase
        image: arungupta/bootiful-couchbase
        env:
        - name: COUCHBASE_URI
          value: couchbase-service
      restartPolicy: Never


This is run-once job which runs a Java (Spring Boot) application and upserts (insert or update) a JSON document in Couchbase.

In this job, COUCHBASE_URI environment variable value is set to couchbase-service. This is the service name created earlier. Docker image used for this service is arungupta/bootiful-couchbase and is created using fabric8-maven-plugin as shown at github.com/arun-gupta/kubernetes-java-sample/blob/master/maven/webapp/pom.xml#L57-L68. Specifically, the command for the Docker image is:

java -Dspring.couchbase.bootstrap-hosts=$COUCHBASE_URI -jar /maven/${project.artifactId}.jar


This ensures that COUCHBASE_URI environment variable is overriding spring.couchbase.bootstrap-hosts property as defined inapplication.properties of the Spring Boot application.

Kubernetes Dashboard

Kubernetes 1.4 included an updated dashboard. For minikube, this can be opened using the following command:

minikube dashboard
Waiting, endpoint for service is not ready yet...Opening kubernetes dashboard in default browser...

 

The default view is shown below:
minikube-dashboard-1-4

But in our case, a few resources have already been created and so this will look like as shown:

minikube-dashboard-couchbase

Notice, our Jobs, Replication Controllers, and Pods are shown here.

Shutdown Kubernetes Cluster

The cluster can be easily shutdown:

minikube stop
Stopping local Kubernetes cluster...
Machine stopped.


Couchbase.com/containers provide more details about running Couchbase using different orchestration frameworks. Further references:

Related Refcard:

Discover new technologies simplifying running containers and data services in production with this free eBook by O'Reilly. Courtesy of Mesosphere.

Topics:
cloud ,devops ,testing ,kubernetes

Published at DZone with permission of Arun Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}