You need to install in your machine :
- Docker (https://docs.docker.com/engine/installation/)
- Minikube (https://github.com/kubernetes/minikube/releases) And the requirement of minikube (https://github.com/kubernetes/minikube#installation)
- Kubernetes command-line tool (http://kubernetes.io/docs/user-guide/prereqs/)
First start minikube that will run in local kubernetes (instead have to use google cloud or AWS )
minikube startCheck that Kubernetes are running well
kubectl cluster-infoFor your first example you can follow the one given by minikube itself : https://github.com/kubernetes/minikube#quickstart
We will do more fun things that this first example. Once minikube is started you can do everything kubernetes allow (there are a lot of tutorial in internet) with some differences that we will see throught an example :
Let's begin by creating 2 replication of an nginx docker load balanced on Kubernetes.
kubectl run test-nginx --image=nginx --replicas=2 --port=80 --expose --service-overrides='{ "spec": { "type": "LoadBalancer" } }'You can see your 2 replicas some seconds later "running" by typing
kubectl get podOnce it's running you can see their internal IP
kubectl get servicesYou can't see their external IP, because it's running in a minikube To Test you can find the ip by typing :
minikube service test-nginx --url or if you are using linux :
curl $(minikube service test-nginx --url)Let's delete one of our pod, and see how kubernetes behave : So get the name of one of your pods typing :
kubectl get podOnce you have the name :
kubectl delete pod [name]And right after you will see kubernetes creating an other container to continue having 2 replicas :
kubectl get podcheck the name of your pods
kubectl get podExecute the following command with the name of your first pod.
kubectl exec -ti test-nginx [name] --bashYou will have the prompt commnd inside of the nginx container. Type :
cd /usr/share/nginx.html
rm index.html & echo "<html><body><h1>Hello World</h1></body></html>" > index.html
exitreload the page
Don't forget to clean your room :
kubectl delete deployment test-nginx
kubectl delete service test-nginx
minikube stopWe will try to dockerize our own code. This comes from the excellent tutorial http://kubernetes.io/docs/hellonode/, but applied to minikube
Create in a directory 2 files :
- server.js
- dockerfile
Edit server.js with :
const http = require('http');
const handleRequest = (request, response) => {
console.log('Received request for URL: ' + request.url);
response.writeHead(200);
response.end('Hello World!');
};
const www = http.createServer(handleRequest);
www.listen(8080);Edit the dockerfile with :
FROM node:4.5
EXPOSE 8080
COPY server.js .
CMD node server.jsNow we will build your docker file inside minikube for that on your console type :
eval $(minikube docker-env)if you do a docker images you will see that in this console you are not anymore inside your own docker, but inside the minikube one.
So build your dockerfile :
docker build -t hellonode:v1 .Now we will mount this image :
kubectl run test-node --image=hellonode:v1 --port=8080you can see it deployed :
kubectl get deploymentsyou can see it running :
kubectl get podsOnce it is running, we will allow external traffic :
kubectl expose deployment test-node --type="LoadBalancer"check :
kubectl get serviceswe can now test :
curl -i $(minikube service test-node --url)
HTTP/1.1 200 OK
Date: Fri, 9 Dec 2016 14:57:19 GMT
Connection: keep-alive
Transfer-Encoding: chunked
Hello World!With Kubernetes it's easy to scale, we only need one command line :
kubectl scale deployment test-node --replicas=4And in order to check :
kubectl get deploymentskubectl get pods