Try Kubernetes locally with minikube
Tags: kubernetes, docker, beginners
Published: 2020-03-14 08:18:26

### Prerequisite
- OS: ubuntu 18.04.2 LTS

### What is Kubernetes?
Kubernetes is a container orchestration tool developed by Google. Kubernetes is a platform tool to manage containerized workloads and services.
> Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.  
> -- Kubernetes [https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/)

### Official Interactive tutorial
If you don't want to install minikube locally, you can also try the [official kubernetes interactive tutorial](https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-interactive/).

### Minikube
But we don't use the tutorial. We will use Minikube to run Kubernetes locally. But what is minikube? The website of Kubernetes explains what it is.
> Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine (VM) on your laptop for users looking to try out Kubernetes or develop with it day-to-day.   
> -- Kubernetes [https://kubernetes.io/docs/setup/learning-environment/minikube/](https://kubernetes.io/docs/setup/learning-environment/minikube/)

### Kubernetes vs Docker-compose vs Docker Swarm
Kubernetes is for running and connecting containers on multiple hosts (= cluster). Each node is a VM or a physical computer that serves as a worker machine in a Kubernetes cluster according to their [explanation](https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-intro/).

See the following stackoverflow for more detail:  
[What's the difference between docker compose and kubernetes? - stackoverflow](https://stackoverflow.com/questions/47536536/whats-the-difference-between-docker-compose-and-kubernetes)

For docker/docker-compose, see "[how to use docker/docker-compose to create Laravel environment](https://noteoneverything.blogspot.com/2020/01/or-use-vagrant-and-virtual-box-what-is.html)".

### Use minikube

#### Install minikube
See this [page of Kubernetes](https://kubernetes.io/docs/tasks/tools/install-minikube/). This tutorial is well written.


#### Use minikube and kubectl
Simply run this command to start minikube.
```
$ minikube start
```
Then you will see these messages:
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-fKU08_as6to/Xmx3Sp6ZOHI/AAAAAAAAIGY/rUudjcLhLAQIYoMfKqH3zjQzFq0z-jg2gCLcBGAsYHQ/s1600/Screenshot%2Bfrom%2B2020-03-14%2B15-15-39.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="261" data-original-width="958" height="174" src="https://1.bp.blogspot.com/-fKU08_as6to/Xmx3Sp6ZOHI/AAAAAAAAIGY/rUudjcLhLAQIYoMfKqH3zjQzFq0z-jg2gCLcBGAsYHQ/s640/Screenshot%2Bfrom%2B2020-03-14%2B15-15-39.png" width="640" /></a></div>

Now we are ready to use "kubectl" locally. Kubectl is a command line tool for controlling Kubernetes clusters.

#### Create nginx service
With `kubectl`, we will use a container image nginx:1.7.9 to deploy nginx in our kubernetes cluster.

At first, make sure the maser node is working:
```
$ kubectl get all 
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   3d7h
```

Then create yaml files somewhere as follows:  
nginx_deploy.yml
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    name: nginx
spec:
  selector:
    matchLabels:
      name: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
```
nginx_service.yml  
```yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30080
      name: http
  selector:
    name: nginx
```
Save the files and run this command:
```
$ cd (the directory where you saved the two yaml files)
$ kubectl apply -f ./nginx_deploy.yml  -f ./nginx_service.yml
```

#### Check the nginx from browser
Check your minikube IP:
```
$ minikube ip
192.168.99.100
```
Check port number:
```
$ kubectl describe service/nginx
Name:                     nginx
Namespace:                default
Labels:                   name=nginx
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"name":"nginx"},"name":"nginx","namespace":"default"},"spec":{"...
Selector:                 name=nginx
Type:                     NodePort
IP:                       10.111.250.254
Port:                     http  80/TCP
TargetPort:               80/TCP
NodePort:                 http  30080/TCP ####Port number is here!!!!!!!####
Endpoints:                172.17.0.4:80,172.17.0.5:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
```
Check from the browser with the IP and port. Confirm nginx is working in the cluster.
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-LRx_QwXRd7o/XmyRQY9086I/AAAAAAAAIGk/GHpoUxBLPVcA5cZeOgeG7BrXdv0uGQ3ugCLcBGAsYHQ/s1600/Screenshot%2Bfrom%2B2020-03-14%2B17-09-15.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="325" data-original-width="817" height="254" src="https://1.bp.blogspot.com/-LRx_QwXRd7o/XmyRQY9086I/AAAAAAAAIGk/GHpoUxBLPVcA5cZeOgeG7BrXdv0uGQ3ugCLcBGAsYHQ/s640/Screenshot%2Bfrom%2B2020-03-14%2B17-09-15.png" width="640" /></a></div>

To delete the service and deployment after confirming from the browser:
```
$ kubectl delete service/nginx deployment.apps/nginx-deployment
$ kubectl get all 
```