Skip to content

Hello Minikube

This tutorial shows you how to run a sample app on Kubernetes using minikube. The tutorial provides a container image that uses NGINX to echo back all the requests.

Objectives

  • Deploy a sample application to minikube.
  • Run the app.
  • View application logs.

Before you begin

This tutorial assumes that you have already set up minikube. See Step 1 in minikube start for installation instructions.

You also need to install kubectl. See Install tools for installation instructions.

Create a minikube cluster

Terminal window
minikube start

Open the Dashboard

Open the Kubernetes dashboard. You can do this two different ways:

  • Launch a browser

Open a new terminal, and run:

Terminal window
# Start a new terminal, and leave this running.
minikube dashboard

Now, switch back to the terminal where you ran minikube start.

  • URL copy and paste

If you don’t want minikube to open a web browser for you, run the dashboard subcommand with the —url flag. minikube outputs a URL that you can open in the browser you prefer.

Open a new terminal, and run:

Terminal window
# Start a new terminal, and leave this running.
minikube dashboard --url

Now, you can use this URL and switch back to the terminal where you ran minikube start.

Create a Deployment

A Kubernetes Pod is a group of one or more Containers, tied together for the purposes of administration and networking. The Pod in this tutorial has only one Container. A Kubernetes Deployment checks on the health of your Pod and restarts the Pod’s Container if it terminates. Deployments are the recommended way to manage the creation and scaling of Pods.

  1. Use the kubectl create command to create a Deployment that manages a Pod. The Pod runs a Container based on the provided Docker image.
Terminal window
# Run a test container image that includes a webserver
kubectl create deployment hello-node --image=registry.k8s.io/e2e-test-images/agnhost:2.39 -- /agnhost netexec --http-port=8080
  1. View the Deployment:
Terminal window
kubectl get deployments
  1. View the Pod:
Terminal window
kubectl get pods
  1. View cluster events:
Terminal window
kubectl get events
  1. View the kubectl configuration:
Terminal window
kubectl config view
  1. View application logs for a container in a pod (replace pod name with the one you got from kubectl get pods).
Terminal window
kubectl logs hello-node-5f76cf6ccf-br9b5

The output is similar to:

Terminal window
I0911 09:19:26.677397 1 log.go:195] Started HTTP server on port 8080
I0911 09:19:26.677586 1 log.go:195] Started UDP server on port 8081

Create a Service

By default, the Pod is only accessible by its internal IP address within the Kubernetes cluster. To make the hello-node Container accessible from outside the Kubernetes virtual network, you have to expose the Pod as a Kubernetes Service.

  1. Expose the Pod to the public internet using the kubectl expose command:
Terminal window
kubectl expose deployment hello-node --type=LoadBalancer --port=8080

The --type=LoadBalancer flag indicates that you want to expose your Service outside of the cluster.

The application code inside the test image only listens on TCP port 8080. If you used kubectl expose to expose a different port, clients could not connect to that other port.

  1. View the Service you created:
Terminal window
kubectl get services

On cloud providers that support load balancers, an external IP address would be provisioned to access the Service. On minikube, the LoadBalancer type makes the Service accessible through the minikube service command.

  1. Run the following command:
Terminal window
minikube service hello-node

This opens up a browser window that serves your app and shows the app’s response.

Enable addons

The minikube tool includes a set of built-in addons that can be enabled, disabled and opened in the local Kubernetes environment.

  1. List the currently supported addons:
Terminal window
minikube addons list
  1. Enable an addon, for example, metrics-server:
Terminal window
minikube addons enable metrics-server
  1. View the Pod and Service you created by installing that addon:
Terminal window
kubectl get pod,svc -n kube-system
  1. Check the output from metrics-server:
Terminal window
kubectl top pods
  1. Disable metrics-server:
Terminal window
minikube addons disable metrics-server

Clean up

Now you can clean up the resources you created in your cluster:

Terminal window
kubectl delete service hello-node
kubectl delete deployment hello-node

Stop the Minikube cluster

Terminal window
minikube stop

Optionally, delete the Minikube VM:

Terminal window
# Optional
minikube delete

If you want to use minikube again to learn more about Kubernetes, you don’t need to delete it.

Conclusion

This page covered the basic aspects to get a minikube cluster up and running. You are now ready to deploy applications.