DEV Community

lechat
lechat

Posted on

Try Kubernetes locally with minikube

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/

Official Interactive tutorial

If you don't want to install minikube locally, you can also try the official kubernetes interactive tutorial.

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/

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.

See the following stackoverflow for more detail:

What's the difference between docker compose and kubernetes? - stackoverflow

For docker/docker-compose, see "how to use docker/docker-compose to create Laravel environment".

Use minikube

Install minikube

See this page of Kubernetes. This tutorial is well written.

Use minikube and kubectl

Simply run this command to start minikube.

$ minikube start
Enter fullscreen mode Exit fullscreen mode

Then you will see these messages:

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
Enter fullscreen mode Exit fullscreen mode

Then create yaml files somewhere as follows:

nginx_deploy.yml

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
Enter fullscreen mode Exit fullscreen mode

nginx_service.yml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30080
      name: http
  selector:
    name: nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Check the nginx from browser

Check your minikube IP:

$ minikube ip
192.168.99.100
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Check from the browser with the IP and port. Confirm nginx is working in the cluster.

To delete the service and deployment after confirming from the browser:

$ kubectl delete service/nginx deployment.apps/nginx-deployment
$ kubectl get all 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)