Learn

Learn about latest technology

Build

Unleash your talent and coding!

Share

Let more people use it to improve!
 

Kubernetes from scratch for developers

miércoles, 28 de noviembre de 2018

Trying to explain Kubernetes from scratch I bring you here a guide about how make a very fruitful study of it. This thoughts are from a software developer perspective. It's not complicate and is quite well explained in Kubernetes(k8s) documentation.

My FIRST ADVICE is before beginning the study of k8s: you should study in-depth about any application container engine, I suggest docker but there are many others like rkt, runc or any other that follows OCI  specifications.
If you feel comfortable in Docker, keep going, in any other situation you will need to get some skills, it can be done by yourself. As a developer it won't be too complicated:
  • the documentation site can guide you with several examples and practical tasks.  
  • docker free lab is very useful too, easy and intuitive and mainly it encompasses the whole learning process in Docker. 
ONCE that we know at least the most important  aspects related with container engine let’s start.

At this point should be obvious for everyone concepts like Cluster or Node
So first things to know:
  1. What k8s really is? It is an open source system for managing containerized applications across multiple hosts, providing basic mechanisms for deployment, maintenance and scaling of applications. 
  2. The components of k8s 


















The previous pictures show us the basic infrastructure BUT where are the containers?. Our target is to deploy and finally run applications that are living in our containers. Our containers are living into the k8s PODs. These PODs are the basic building block in k8s and the most important aspects in this process are our applications hosted in our containers and living in those PODs at the same time.

Because our target is to deploy and finally run applications this is the whole picture that we have at this point:

A Cluster: that contains NODEs [1 Master - 1.. n Worker]
Worker Nodes: Contain : PODs
PODs: Contain: Containers
Containers: It contains applications with their own environments.

































In the previous picture there are some elements that I haven't mentioned yet (volume), I will talk about it in next posts but you can get the idea that the POD's volume is similar at docker container volume in Docker.
These PODs are living in NODEs, but which NODEs? Who decides which node spin up an specific POD?
If we take a look at the Master Node components we can see kube-scheduler, it is responsible for creating a POD in a specific node. The kube-scheduler watches newly created PODs that have no node assigned, and selects a node for them to run on, this process is not so simple, many factors taken into account for scheduling decisions include individual and collective resource requirements. In next posts we'll see that scheduler behavior can be changed and how some times you can choose in what node your POD can spin up.

There are several way for POD creations BUT all of them involve a reference to  images running on containers.
From the developer’s point of view is too easy understand k8s PODs creations process, it has two main components:
  • controller
  • custom resource definition
and third aspect is how can we talk to the cluster, k8s lets to do that by different ways:
So it is up to you, if your are familiar with RESTful environment you can access through the k8s REST API. The API is documented through Swagger so you will have an interface or apply a curl with the proper command.

In this case I am going to use the command line interface (kubectl) that  will be running commands against Kubernetes cluster.

kubectl syntax: kubectl [command] [TYPE] [NAME] [flags]

kubectl + creations commands + definition document 

Every time that the above statement take place the object defined in our definition document must be validated by kube-apiserver before configure data to any api objects(pods, services, replicationcontrollers, and others)

creations commands(to create api objects):
definition document: a custom resource definition[yaml or json document] that should describe a Controller used during the creation process. In our case our target is the POD creation.
In k8s, a Controller is a control loop that watches the shared state of the cluster through the apiserver and makes changes attempting to move the current state towards the desired state in this case we are talking about our desired state, it will be a POD.

Let's see some definition document examples and its relations with the creation commands

# nginx-deployment.yaml document describe a desired state in a Deployment object, and the Deployment controller changes the actual state to the desired state at a controlled rate. 

create -f https://raw.githubusercontent.com/ldipotetjob/k8straining/develop/yamldefinitions/nginx-deployment.yaml

# nginx-repcontroller.yaml describe a ReplicationController object

create -f https://raw.githubusercontent.com/ldipotetjob/k8straining/develop/yamldefinitions/nginx-repcontroller.yaml

Some yaml definitions documents examples

Every thing done here is from a software developer perspective so in the same way when we are going to implement our project:
  1. Local projects/environments: means create/run everything in local. 
  2. Production projects/environments: means upload to cloud environment created by ourselves or by a certified provider. 
For implement Local projects/environments previously indicated we have several options:
  1. the online training platforms[katacoda], some time run too slow but if fine just for test.
  2. install a local environment, it is very easy with minikube. It is a configurable environment that let you a simulation of a k8s cluster.
  3. install k8s in your bare metal/laptop. 
I want to give you some interesting and important reference links for the work with k8s:
  1. whole references of kubectl command line [you can find here examples about how to use a kubectl command with its flags and arguments]
  2. kubectl Cheat Sheet
Is very important to know that as a developer docker & kubernetes must be familiar for us. Do not try to memorize commands or sintaxis. Try to understand how the whole system works, the idea. Dive in K8s documentation in an ordered way. The following topics are a very good starting point:
  1. Kubernetes architecture 
  2. Containers 
  3. Workloads(Pods and Controllers)

The aforementioned aspects are physical and logical objects, to enable that every thing can work together I will need some abstract aspects for example services. In next posts I am going to review in-depth k8s services and its relationship with the rest of k8s elements.

Making available an Unscanned Cassandra image from the official docker file

miércoles, 21 de noviembre de 2018

I want to make here a constructive criticism, make thing easy please to all companies that work in open source environment. We make to many thing for free but that not mean that it shouldn't be almost(not completely) perfect.

The problem to solve in this post is that when I want to create a Cassandra ver 3.7 Image from the Oficial Docker file, the process fail and when I take a look in internet too many people are facing the same problem. So the docker file is wrong in that version an in others.

I have tested Cassandra Docker Hub repository and its true that we can build our images from his sources docker files BUT quite well just only few of them (see image below).

If you want to pull Cassandra image and spin up containers it can be done with almost all Cassandra versions. You can take a look, there are Scanned and Unscanned images and if you try to build images from source and the tag that you want to build image from is an Unscanned images perhaps you will face too many troubles.

I gonna try with Cassandra 3.7 and the first thing that I am going to do is follow the instructions described in the Cassandra github repository about its docker file template and surprisingly we get an Error or even using the dockerfile Cassandra version  3.7 we face the same error, see image below.


So in a best case when I construct the image changing the Debian's suite  I have to refactor it because the image can be can be built it BUT  the cqlsh tool cannot be accessed, see image below.


You can dive into github post  that explain how other Unscanned  Cassandra versions are facing the same problem when you try to create image from docker file.

Any way is frustrating after to many hours trying, so finally we refactored the image for solve the problems with openjdk-8-jre-headless adding to the official image the following snapshot of code:

RUN { \
  echo 'Package: openjdk-* ca-certificates-java'; \
  echo 'Pin: release n=*-backports'; \
  echo 'Pin-Priority: 990'; \
 } > /etc/apt/preferences.d/java-backports

You can find the solution for several version of cassandra docker files images in case that you want to build images from source in Cassandra version (3.9 - 3.10) that's the code that I have indicated previously.

You can find all the source code of Cassandra's image ver 3.7 in my github repository. We have tested it but if you have any problems please send us a feedback.

For many reason we should need to build an Official image from its sources for example is we need to add a layer to the image for Liveness and Readiness Probes in case of we want orchestrate our container with Kubernetes, it is not an obligation it is just a good practice. You can face the same situation in Docker when you are defining your Docker Compose yaml file. In next posts I am going to explain how deal with it.