Kubernetes Getting Started

Let’s setup Kubectl and Minikube and use Minikube to setup a one node cluster.

Prerequisite

Download and install VirtualBox. see Download page https://www.virtualbox.org/wiki/Linux_Downloads

Install Kubectl

The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. Installation instruction is in https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-linux.

1
2
3
4
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version

To check kubectl installation

1
kubectl version

The config file is in the $HOME/.kube directory. This file defines clusters, context and users. You can view the file using command kubectl config view.

If the configuration file is not working as expected, use rm -rf ~/.kube command to remove the existing configuration and cache.

Useful links for kubectl:

Install Minikube

minikube is a tool that runs a single-node Kubernetes cluster in a virtual machine on your personal computer. see Install Instructions https://kubernetes.io/docs/tasks/tools/install-minikube/

Download and install minikube

1
2
3
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
&& chmod +x minikube
sudo install minikube /usr/local/bin

You can check minikube version using minikube version command.

Note hat Minikube can only set up a one node cluster.

Set Up a Cluster with Minikube

A brief guide to start use, and delete Minikube locally. See https://kubernetes.io/docs/tasks/tools/install-minikube/

1 - Start Minikube and create a cluster. You can optionally specify cpu and memory.

1
2
3
4
5
6
7
8
9
10
$ minikube start --memory=10000 --cpus=4
😄 minikube v1.3.1 on Ubuntu 19.04
🔥 Creating virtualbox VM (CPUs=4, Memory=10000MB, Disk=20000MB) ...
🐳 Preparing Kubernetes v1.15.2 on Docker 18.09.8 ...
💾 Downloading kubeadm v1.15.2
💾 Downloading kubelet v1.15.2
🚜 Pulling images ...
🚀 Launching Kubernetes ...
⌛ Waiting for: apiserver proxy etcd scheduler controller dns
🏄 Done! kubectl is now configured to use "minikube"

You can use minikube status to check its status. also use kubectl get pods --all-namespaces command to ensure all pods are running without error.

Now minikube will start a Virtual Machine. The cluster runs on that Virtual Machine. If you open VirtualBox Manager, you will see a virtual machine named “minikube”.

check cluster info using kubectl cluster-info command

1
2
3
4
5
$ kubectl cluster-info
Kubernetes master is running at https://192.168.99.101:8443
KubeDNS is running at https://192.168.99.101:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

To view the nodes, use kubectl get nodes command

2 - create a deployment

1
kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.10 --port=8080

You can use kubectl get deployments to list deployments and kubectl describe deployments/hello-minikube to get details of a particular deployment.

3 - Expose deployment as a service

1
kubectl expose deployment hello-minikube --type=NodePort

4 - If you check the pods, it should return a pod

1
2
3
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-856979d68c-rndtw 1/1 Running 0 82s

You can use kubectl scale deployments/hello-minikube --replicas=3 to scale the deployment to 3 replicas

5 - Get the url of the exposed service

1
2
$ minikube service hello-minikube --url
http://192.168.99.101:31777

Note that the port number is randomly assigned

6 - View the web page using the URL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ curl http://192.168.99.101:31777

Hostname: hello-minikube-856979d68c-rndtw

Pod Information:
-no pod information available-

Server values:
server_version=nginx: 1.13.3 - lua: 10008

Request Information:
client_address=172.17.0.1
method=GET
real path=/
query=
request_version=1.1
request_scheme=http
request_uri=http://192.168.99.101:8080/

Request Headers:
accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
accept-encoding=gzip, deflate
accept-language=en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6
cache-control=max-age=0
connection=keep-alive
host=192.168.99.101:31777
upgrade-insecure-requests=1
user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36

Request Body:
-no body in request-

7 - Open dashboard

Dashboard is a web-based Kubernetes user interface. You can use Dashboard to deploy containerized applications to a Kubernetes cluster, troubleshoot your containerized application, and manage the cluster resources.

run this command to open dashboard in the browser

1
$ minikube dashboard

Dashboard is a great tool to monitor minikube cluster.

8 - Delete the service

1
2
$ kubectl delete services hello-minikube
service "hello-minikube" deleted

9 - Delete the deployment

1
2
$ kubectl delete deployment hello-minikube
deployment.extensions "hello-minikube" deleted

10 - Stop the local Minikube cluster

1
2
3
$ minikube stop
✋ Stopping "minikube" in virtualbox ...
🛑 "minikube" stopped.

10 - Delete minikube cluster

If you don’t need the cluster any more, delete the cluster.

1
2
3
$ minikube delete
🔥 Deleting "minikube" in virtualbox ...
💔 The "minikube" cluster has been deleted.

More on Minikube

By Default. Minikube creates node on Virtualbox VM. the default settings is CPUs=2, Memory=2000MB, Disk=20000MB. You can override the default settings:

1
minikube start --memory=6000 --cpus=4 --disk-size=10000

For more options, see start command

Other minikube commands

1
2
3
4
5
6
7
8
9
10
11
12
# check minikube status
minikube states

# retrieves the IP address of the running cluster
minikube ip

# get a list of service and URLs
minikube service list

# Log into or run a command on a machine with SSH
# You can then use `free -h` to check memory usage and `df -h` to check disk usage
minikube ssh

You can configure docker CLI to use minikube’s Docker Daemon.

1
2
3
4
5
6
7
8
$ minikube docker-env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.109:2376"
export DOCKER_CERT_PATH="/home/xing/.minikube/certs"
# Run this command to configure your shell:
# eval $(minikube docker-env)

$ eval $(minikube docker-env)

For more information on Minikube, see [Minikube documentation](https://minikube.sigs.k8s.io/docs/

Other Ways to setup a Cluster

Minikube creates a single node cluster. If you want to create a multi-node cluster, checkout
kubeadm.

A more convenient way to run a multi-node cluster is kind. With kindl you can easily setup local Kubernetes clusters using Docker container

There is also online option to play with Kubernetes. Katacoda Playground sets up a two node cluster cluster for you to play with. Another online kubernetes playgound is play-with-k8s

Resources

These are the resources for learning Kubernetes

References