For this experiment I have used Ubuntu 16.04 machine. I believe still using ubuntu machine is much convenient to play around with these kind of technologies. I am not going to go deep with any of these technologies. And I am more focusing on kubernetes commands which I got familiar recently.
First you need to setup docker environment in your machine to develop the docker image. For this post, I’m using NodeJS server which responds a simple text message. First I have created NodeJS application locally and used following Dockerfile to create a docker image of it. You need to put the Dockerfile in the same directory where the NodeJS project resides.
Sample docker file I used is as follow:
COPY package.json .
RUN npm install
COPY . .
CMD [ “npm”, “start” ]
You can refer this article for installation and getting familiar with docker (https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04)
Once you create the docker image, push it to docker hub. This is for kubernetes to pick it up.
You can use ubuntu software center for installing virtual box. In addition to that, you can use commadline to install virtualbox (https://askubuntu.com/questions/367248/how-to-install-virtualbox-from-command-line). I personally like to recommend using virtualbox, compared to other hypervisors.
Install minikube and kubectl
You may wonder why I was asking to install Virtualbox. The reason is minikube. At the moment Kubernetes recommended way of testing is using minikube with virtualbox. To install minikube and kubectl, please follow the instructions given in this document (https://kubernetes.io/docs/tasks/tools/install-minikube/) . While doing that, please make sure that to install minikube first and install kubectl which supports.
Minikube gives you a single node cluster. In that you can create a new kubernetes deployment. Docker is a dependency when using Kubernetes (can be used with rkt too). Once minikube is setup, you need to start it using minikube start command. Then you can interact with kubernetes cluster with kubectl commands. Here I have list down some important kubectl commands
For the docker image I created, I used following command to create a kubernete deployment
kubectl run my-test-app –image=docker.io/buddhima/node-web-app:v1 –port=3000
Other useful commands;
kubectl get <resource_type> – Get listed information about a resource type. Resource type can be nodes/deployments/pods/services etc
kubectl describe <resource_type> – get a descriptive information about a resource type
kubectl describe <resource_type>/ID – get a descriptive information about a single resource given by the ID
kubectl logs – print the logs from a container in a pod
kubectl exec – to execute a command on a container (eg: kubectl exec -it POD_NAME -c CONTAINER_NAME bash – to execute bash shell of a container in a pod)
Deployment – a deployment is a configuration which instruct how Kubernetes can create/update instances of app.
Pod – a pod is a collection of one or more application containers which are tightly coupled. A pod shares a same IP and port space. A pod in kubernetes cluster has a unique IP
Service – A service is a logical set of pods defined by YAML/JSON. Pods are selected by a LabelSelector. Types are ClusterIP, NodePort, LoadBalancer, ExternalName. This abstraction allows pods to die and replicate match set of pods using labels and selectors
kubectl expose deployment/<deployment_name> –type=”NodePort” –port 8080 – create a new service and expose to external traffic
kubectl label pod POD_NAME app=foo – this is use to add a new label to a pod
kubectl delete <resource_type> <id> – deletes a resource given by id
kubectl set image deployments/<deployment_id> <deployment_name> = docker.io/buddhima/node-web-app:v2 – set the image to the given docker hub url
kubectl rollout status deployment/<deployment_name> – confirms the update status
kubectl rollout undo deployment/<deployment_name> – undo rollout update
kubectl scale deployment/<deployment_name> –replicas=4 – scale the deployment to 4 replicas. After scaling use kubectl get pods -o wide to view pods’ status
The objective of this post to give you a summarized set of important docker, kubernetes related commands. Actually I have talked more about kubernetes commands which might help you in future.