{"componentChunkName":"component---src-templates-blog-post-js","path":"/introduction-to-containers-and-docker/","result":{"data":{"site":{"siteMetadata":{"title":"tech-blog","author":"Alamgir Qazi"}},"markdownRemark":{"id":"6e665fd5-49be-5212-aa34-e7e916f6dd1d","excerpt":"Containers, Docker and Kubernetes have become the new buzzwords in tech (in this post, I won’t talk about Kubernetes). However, they’re…","html":"<p>\n  <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/fdaf8/docker.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n  \n  <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 29.72972972972973%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsSAAALEgHS3X78AAABgElEQVQY002RyytEURzHf3fGeaSUJBZSSmHyB9hZMQskQpKNLDxqLoosRxMzKCOaMTWaWLgeIYVsPRKZ8SzPLKSGLGyEhQUd3+saOfXr+/udc3+f+z2/QzJwT2LoxMbdWySDyN3bDtG/bxd92yS8MY21RjTetUjMZWjcZRDXZzVmKXHXrGaGecYsJZIjFzY5eEQAFMuBg4jwRmNJNe5k7t5MF8NnBCDxzoUEgJhuwdhv/ZMnFGG6A2ynXHpjb8x3rITv8EN4dqel//pcePaqWHMwg+tztWhyAJCCpkw4KeO6kYM6FfvZgGXhR/nYg8OZDxKjNxv20KNKC8ffZfhJyVBcSf/lOO9aKgCgDQ1+aDeaugFrR+1CBADsgU7ibAxaaLokm++UqP+oumH94dO5+qy0icev5LHbqTSlyFz4qBEQJ7QObiqgcGtUQksQ9chNeDHTrXGQM6qoaOWFXu/Oc5uWr0qpdzOP1hQltUQsYMc8JR6B/c7r36P8zRHX1UzgN1A6q+31HsT/AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n    >\n      <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\"\n        alt=\"Docker\"\n        title=\"\"\n        src=\"/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/fcda8/docker.png\"\n        srcset=\"/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/12f09/docker.png 148w,\n/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/e4a3f/docker.png 295w,\n/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/fcda8/docker.png 590w,\n/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/fdaf8/docker.png 674w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n      />\n    </span>\n  </span>\n  \n  </a>\n    </p>\n<p>Containers, Docker and Kubernetes have become the new buzzwords in tech (in this post, I won’t talk about Kubernetes). However, they’re correctly being hyped because they are indeed the future of how applications will be built. Infact, using Docker now is becoming extremely common.</p>\n<h2>so what is Docker?</h2>\n<p>Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker is a technology that allows you to incorporate and store your code and its dependencies into a neat little package - an image.</p>\n<p>A docker image holds your source code and the dependencies it needs. Lets say you have a Node.js app, A docker image of your app will contain the code + the node_modules and everything that’s needed to run your Node.js app.</p>\n<p>So now we know what a Docker Image is, let’s talk about Containers.</p>\n<h2>Containers</h2>\n<p>Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.</p>\n<p>Containers are encapsulated environments in which applications are run. A container is defined by the Image and configuration options. Containers basically are where the (Docker) Image runs.</p>\n<p>Now you can have many containers running the same Docker Image since a Docker Image it has all it needs (code + dependencies/configuration). Now you can increase and decrease the number of containers to scale your application. How easy is that.</p>\n<!-- Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications built by Google. Kubernetes has now become the leader in container orchestration.\n\nThere are different ways we can expose our Kubernetes Deployments.\n\n### 1. Load Balancer\n\nKubernetes provides a simple HTTPS L7 Load Balancer. In order to expose a deployment, run this command in Kubectl. Here, myapp is our deployment which is available via ClusterIP but isn't exposed to the outside world.\n\n```javascript{numberLines: true}\nkubectl expose deployment myapp - type=\"LoadBalancer\"\n```\n\n### 2. Ingress\n\nIngress can be considered as a fancier Load Balancer with more configurations. In Kubernetes Engine, Ingress is implemented using Cloud Load Balancing. When you create an Ingress in your cluster, Kubernetes Engine creates an HTTP(S) load balancer and configures it to route traffic to your application. We can set up several rules for our load balancer. Ingress can also help in exposing multiple services via a single host endpoint with different URLs.\n\nIn order to set up Ingress, we need to do 2 things.\n\n1. Reserve a static IP on Google Cloud.\n\n2. set up SSL of our domain. (needed if you want to expose to a custom domain e.g myapp.com instead of a reserved IP address).\n\nStep 1:\n\nRun the following command and make sure you have Google Cloud SDK (https://cloud.google.com/sdk/) installed and configured.\n\n```javascript{numberLines: true}\ngcloud compute addresses create kubernetes-ingress - region us-central1\n```\n\nThis will reserve a static IP with the name kubernetes-ingress.\n\nStep 2:\n\nDownload the private key for your custom domain.\n\nRun the following command to generate the RSA Private key\n\n```javascript{numberLines: true}\nopenssl rsa -in private.key -out private_rsa.key\n```\n\nif you get passphrase error, run this\n\n```javascript{numberLines: true}\nopenssl rsa -in privateKey.pem -out newPrivateKey.pem\n```\n\nNow run:\n\n```javascript{numberLines: true}\nkubectl create secret tls ingress-ssl - key private_rsa.key - cert mycert.crt\n```\n\nThis will create an SSL configuration named ingress-ssl. We will use this configuration for our Ingress.\n\nHere's a yaml configuration file for Kubernetes Ingress. This yaml file can be deployed.\n\n```javascript{numberLines: true}\napiVersion: extensions/v1beta1\nkind: Ingress\nmetadata:\nname: kubernetes-ingress\n# kubernetes-ingress is the name of static ip we've reserved in #Google Cloud\nannotations:\nkubernetes.io/ingress.global-static-ip-name: \"kubernetes-ingress\"\nspec:\ntls:\n- hosts:\n- myapp.com\nsecretName: ingress-ssl\nbackend:\nserviceName: myapp\nservicePort: 80\n```\n\n### 3. Port-Forward\n\nPort-forward basically exposes a service running in Kubernetes cluster and maps it to a local port on your computer. The below command is an example. It exposes my service prometheus from namespace 'monitoring' and it maps it to localhost:8082. This can be ideal for testing purposes and for services that you do not want to expose to the outside world but might need for testing for your own.\n\n```javascript{numberLines: true}\nkubectl port-forward svc/prometheus - namespace monitoring 8082:9090\n```\n\nWhich one should you use?\n\nLoad Balancer is ideal if you are testing and only have a single service that you want to expose. Mostly this is not the case and thus this can become expensive.\n\nPort-Forward is mostly for testing locally and not an option to expose for production.\n\nIngress is the most likely solution as it provides more customization and offers much more.\n\nThere are also other ways to expose your Kubernetes service. Have a look at Istio (a service mesh) or an API Gateway like (Express-gateway for Node.js). -->","frontmatter":{"title":"Introduction to Containers and Docker","date":"November 22, 2018"}}},"pageContext":{"slug":"/introduction-to-containers-and-docker/","previous":{"fields":{"slug":"/exposing-your-app-in-kubernetes/"},"frontmatter":{"title":"Exposing your app in Kubernetes"}},"next":{"fields":{"slug":"/intro-to-nodejs-part-1/"},"frontmatter":{"title":"A very basic introduction to Node.js - Part 1"}}}},"staticQueryHashes":[]}