martes, 18 de agosto de 2015

Kubernetes a how to with PHP

Recently i have been reading a lot about containers and using Docker extensively as my default development environment and for a few projects as the deployment delivery method. About a year a go i also heard from this amazing project from google called kubernetes that aim to be a pilot to help us on the common DevOps tasks that bring pain to us every day. If you are a developer or DevOps engineer that perform this tasks every day you will love kubernetes (k8s).


PHP has been my development prefer language for almost 10 years now, i have used just a few of the growing collection of frameworks, been Symfony the one i prefer and use in all the projects i have the opportunity. So i will use this tools for the example i plan to explain and share in this post.


The aim of this post it to share and example i made to test k8s locally so we can see it on action and plan strategies on how to include it on our current projects and deployment strategies. The code and configuration that will be explained on this post can be found on the k8s_php_test github repository.

What to expect:

  • A working k8s cluster
  • A Symfony2 application that will display all the nodes on the k8s cluster running the same app
  • Scale up and down the app
  • Roll and update

Requirements:

First we need to prepare our machine to run the k8s cluster, for that we will use docker, depending on what OS are you using, been Linux/Ubuntu the prefer one; you should:

Then we will follow the  instructions on k8s official page to set up our k8s cluster. Also ensure you have in your $PATH the kubectl binary.

Lastly get the code from the github repository and move into the directory:
clone git@github.com:bitgandtter/k8s_php_test.git && cd k8s_php_test

The fun begins:

Now we can start by building our image
bash development_tools/cluster_config/app/build-image.sh 
And start our replica controller and service. As the k8s official web site says about Replication Controller and Service:
"A replication controller ensures that a specified number of pod "replicas" are running at any one time. If there are too many, it will kill some. If there are too few, it will start more. Unlike in the case where a user directly created pods, a replication controller replaces pods that are deleted or terminated for any reason, such as in the case of node failure or disruptive node maintenance, such as a kernel upgrade. "
"A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service."
We will start our own using this command:
bash development_tools/cluster_config/app/create.sh
After this we should have one Replication Controller and one Service both with the label k8s-php-test. The RC will bring up one container with our example application development on Symfony and the Service will create a Load Balancer for us in front of our App. We can see all this info by running this command:
bash development_tools/cluster_config/get_pods_and_services.sh
This should output something like this:


Now you can go to your browser and navigate to the ip of the service and see a page with our friend Tux on only one instance running also displaying the ip of the container.

What next:

Great we have successfully created a k8s cluster and populated it with our app using Replication Controllers and Services, but we can do that using just docker with out so much pain, so: What makes k8s a better solution for our common and daily DevOps tasks?

Scale

This is one of the tasks that can be really hard to accomplish and certainly if you don't have a tool that provide this kind of strategies out of the box you will end writing a lot of code (bash, puppet, ansible, python) scripts that help you on the provisioning task and deployment. So here comes k8s to rescue the day. Lets scale up our app to 6 instances.
bash development_tools/cluster_config/app/scale-up.sh
If you go to the browser you can see how the new instances are been started.

Rolling Update

Another common task its how you deliver updates to your app, certain measures need to be take into consideration, 0 downtime, do not compromise the user experience, gradual and incremental update of the service. Again k8s make this task easy for us, lets test it:
  • First lets make a change on our app, for example the display picture; lets put another custom on our little friend Tux, for that open the twig template and replace the image.png by the image2.png:
before: framework/app/Resources/views/default/display.twig.html
{% image '@AppBundle/Resources/public/images/image.png' %}
after: framework/app/Resources/views/default/display.twig.html
{% image '@AppBundle/Resources/public/images/image2.png' %}
  • Build a new image
bash development_tools/cluster_config/app/build-image.sh
  • Roll the update
bash development_tools/cluster_config/rolling_release.sh
Again go to your browser and you can see how the instances are been updated, a process really nice to see.

Conclusion

K8S can be really helpful for both full job DevOps an for developers, i also plan to make a post and example on how to use k8s as a development environment. In the repository can be found also the examples on how to scale down and delete the k8s cluster. 

I really hope this can be helpful and start the spark of curiosity about this new piece of technology if you are a beginner.

Starting from 0


With this post i pretend to start a new stage on my professional carer, i will try to expose my ideas and knowledge to the world; knowing that the human being its so complex not just on the body but in mind and hopefully i reach other colleagues that share the same ideas as i, and if its not and i reach also other colleagues that do not share it, a sane discussion can be opened on each of my posts and i'm pretty sure i will learn from both experiences.

No matter what i expect this can be fun and helpful for all the readers.