πŸ“œ ⬆️ ⬇️

Getting started in Kubernetes with Minikube



Translator's Preface : Minikube is a handy tool that we use in the company for local experiments with Kubernetes (in particular, to perform laboratory work on this system when training employees). This article seemed useful to me during my acquaintance with Kubernetes. It was written a year ago by the author using Mac OS X, and I did all the operations on Ubuntu 16.04 recently and with the current versions of the main software: Minikube 0.20, Docker 17.06.0-ce, kubectl 1.7.0. Therefore, all the conclusions of the teams were converted to new versions and slightly different from those in the original article.
Kubernetes is an open source container orchestration system, ready for production and designed to automate the placement, scaling, and management of containers.

This is a simplified version of the Hello Minikube manual , in which Minikube is used to launch the local Kubernetes cluster instead of the Google Container Engine, eliminating the need for a cloud platform.
')
The manual is written under Mac OS X, but the commands below will work for any other OS. (As noted above, everything was reproduced and adapted for Linux - approx. Transl. )

Prerequisites



Test your Minikube installation


 $ minikube start Starting local Kubernetes v1.6.4 cluster... Starting VM... Moving files into cluster... Setting up certs... Starting cluster components... Connecting to cluster... Setting up kubeconfig... Kubectl is now configured to use the cluster. 

We can see the list of running in the hearth cluster:
 $ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system kube-addon-manager-minikube 1/1 Running 6 1d kube-system kube-dns-1301475494-p5x6k 3/3 Running 18 1d kube-system kubernetes-dashboard-llc98 1/1 Running 6 1d 

... and node:
 $ kubectl get nodes NAME STATUS AGE VERSION minikube Ready 1d v1.6.4 

Installation of hello-minikube


The Minikube project on GitHub offers a demo version for quick start, using the collected Docker image hello-minikube . Since we have already started the cluster, we can skip the first step ( minikube start ).

Now let's run the built-in hello-minikube . To do this, a pre-configured deployment will be created:

 $ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080 deployment "hello-minikube" created 

With the help of the following commands, we can look at the current lists of pods and deployments to make sure of the changes:
 $ kubectl get pods NAME READY STATUS RESTARTS AGE hello-minikube-938614450-nng53 1/1 Running 0 2m $ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE hello-minikube 1 1 1 1 2m 

To access the hello-minikube you need to open an external IP with the command:
 $ kubectl expose deployment hello-minikube --type=NodePort service "hello-minikube" exposed 

Note: it is necessary to use the NodePort type, since Minikube does not support the LoadBalancer service. So you can make sure that the service is open:
 $ kubectl get services NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-minikube 10.0.0.70 <nodes> 8080:30531/TCP 2m kubernetes 10.0.0.1 <none> 443/TCP 1d 

Now we can either use curl from the command line, or the browser to open a link to the service. To find out its external IP and port, first use the command:
 $ minikube service hello-minikube --url http://192.168.99.100:30531 $ curl $(minikube service hello-minikube --url) CLIENT VALUES: client_address=172.17.0.1 command=GET real path=/ query=nil request_version=1.1 request_uri=http://192.168.99.100:8080/ SERVER VALUES: server_version=nginx: 1.10.0 - lua: 10001 HEADERS RECEIVED: accept=*/* host=192.168.99.100:30531 user-agent=curl/7.47.0 BODY: -no body in request- 

Note: The IP address that is managed by VirtualBox may change. Find it out using the minikube ip command or in the output of ifconfig :
 … vboxnet0 Link encap:Ethernet HWaddr 0a:00:27:00:00:00 inet addr:192.168.99.1 Bcast:192.168.99.255 Mask:255.255.255.0 … 

After we are done with hello-minikube , we can delete its deployment and service, freeing up resources and checking that everything is really deleted:
 $ kubectl delete service,deployment hello-minikube service "hello-minikube" deleted deployment "hello-minikube" deleted $ kubectl get pods No resources found. $ kubectl get services NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.0.0.1 <none> 443/TCP 1d 

Building and installing a service with Docker


This example is somewhat more complicated and describes creating a small Node.js server, building it into a Docker image using a Dockerfile and launching it in Kubernetes.

Create a simple Node.js project hello-node :
 $ mkdir hello-node && cd hello-node && touch Dockerfile server.js $ tree . β”œβ”€β”€ Dockerfile └── server.js 0 directories, 2 files 

Create a simple HTTP server that returns β€œHello World!”:
 $ vi server.js var http = require('http'); var handleRequest = function(request, response) { response.writeHead(200); response.end('Hello World!'); }; var helloServer = http.createServer(handleRequest); helloServer.listen(8080); 

Let's fix the Dockerfile by declaring that the image uses node 4.4 and the container starts the service using the server.js file:
 $ vi Dockerfile FROM node:4.4 EXPOSE 8080 COPY server.js . CMD node server.js 

Before running any Docker commands, you need to set the environment. Similar to running eval $(docker-machine env) , we create Docker environment variables for Minikube using the minikube docker-env :
 $ eval $(minikube docker-env) 

Now we will collect an image. It will take some time, because To resolve dependencies, images from Docker Hub, such as node 4.4, will be loaded. Upon completion, you will receive a new Docker image ready for deployment (the final point . At the end of the command, Docker says to collect the image from the current directory - approx. Transl. ) :
 $ docker build -t hello-node:v1 . Sending build context to Docker daemon 3.072kB Step 1 : FROM node:4.4 ---> 93b396996a16 Step 2 : EXPOSE 8080 ---> Using cache ---> 989ed85905c2 Step 3 : COPY server.js . ---> a5dc90f3df9d Removing intermediate container 8fbc055e7016 Step 4 : CMD node server.js ---> Running in c58bc5e3daff ---> 8521ce4accb3 Removing intermediate container c58bc5e3daff Successfully built 8521ce4accb3 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-node v1 8521ce4accb3 30 seconds ago 647MB <none> <none> dc5a4cc0b371 3 days ago 647MB gcr.io/google_containers/kubernetes-dashboard-amd64 v1.6.1 71dfe833ce74 2 months ago 134MB gcr.io/google_containers/k8s-dns-sidecar-amd64 1.14.2 7c4034e4ffa4 2 months ago 44.5MB gcr.io/google_containers/k8s-dns-kube-dns-amd64 1.14.2 ca8759c215c9 2 months ago 52.4MB gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64 1.14.2 e5c335701995 2 months ago 44.8MB gcr.io/google-containers/kube-addon-manager v6.4-beta.1 85809f318123 4 months ago 127MB node 4.4 93b396996a16 11 months ago 647MB gcr.io/google_containers/echoserver 1.4 a90209bb39e3 13 months ago 140MB gcr.io/google_containers/pause-amd64 3.0 99e59f495ffa 14 months ago 747kB 

Now we can place under the hello-node in the local cluster Kubernetes using kubectl :
 $ kubectl run hello-node --image=hello-node:v1 --port=8080 deployment "hello-node" created $ kubectl get pods NAME READY STATUS RESTARTS AGE hello-node-1644695913-h7qwh 1/1 Running 0 6s $ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE hello-node 1 1 1 1 18s 

As before, you need to assign an external IP and port to the service in order to access it using curl :
 $ kubectl expose deployment hello-node --type=NodePort service "hello-node" exposed $ kubectl get services NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-node 10.0.0.219 <nodes> 8080:30987/TCP 8s kubernetes 10.0.0.1 <none> 443/TCP 4d $ curl $(minikube service hello-node --url) Hello World! 

Victory! We successfully wrote, compiled and placed a simple Node.js service using a Docker image in the local Kubernetes cluster.

This toy project is designed for beginners who want to try Kubernetes locally before you start publishing videos in paid cloud platforms. It allows you to take the first steps without the extra cost of these experiments.

Cleaning


Do not forget to delete the service and deployment for hello-node and turn off the minikube service when done:
 $ kubectl delete service,deployment hello-node service "hello-node" deleted deployment "hello-node" deleted $ minikube stop Stopping local Kubernetes cluster... Machine stopped. 

PS From the translator


More complete documentation on the local launch of Kubernetes from Minikube can be found on the project website . You may also be interested in the following articles from our blog:

Source: https://habr.com/ru/post/333470/


All Articles