{"id":42706,"date":"2016-12-13T16:17:38","date_gmt":"2016-12-13T10:47:38","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=42706"},"modified":"2016-12-14T09:21:08","modified_gmt":"2016-12-14T03:51:08","slug":"kubernetes-namespaces-one-cluster-for-different-environments","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/kubernetes-namespaces-one-cluster-for-different-environments\/","title":{"rendered":"Kubernetes Namespaces &#8211; One Cluster for Different Environments"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-43150\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/Kube.png\" alt=\"Kube\" width=\"574\" height=\"159\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/Kube.png 660w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/Kube-300x83.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/Kube-624x173.png 624w\" sizes=\"(max-width: 574px) 100vw, 574px\" \/><\/p>\n<p>In one of the project that I was working, I had to set up multiple environments in the same <a title=\"Getting started with Kubernetes\" href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-kubernetes\/\">Kubernetes cluster<\/a>. The environments are namely <strong>QA<\/strong> and <b>UAT<\/b>.\u00a0The blog will guide the reader on how to segregate different environments in same Kubernetes\u00a0cluster and have control over each environment in such a way that the other environments are not impacted.<\/p>\n<p><strong>About Kubernetes<br \/>\n<\/strong>Kubernetes is an open-source tool that manages deployment, scaling, and orchestration of containerized applications.\u00a0It groups containers that make up an application into logical units for easy management and discovery.<\/p>\n<p>Read my previous blog to understand\u00a0<a title=\"Setup Kubernetes Cluster on AWS EC2\" href=\"http:\/\/www.tothenew.com\/blog\/setup-kubernetes-cluster-on-aws-ec2\/\">how to set up Kubernetes\u00a0cluster<\/a>, so I assuming that the reader has a running Kubernetes\u00a0cluster and plans to\u00a0implement multiple\u00a0environments in it.<\/p>\n<p>Before we get started, let&#8217;s first understand about a functionality which is provided by Kubernetes\u00a0called\u00a0<strong>namespaces.\u00a0<\/strong><\/p>\n<p><strong>What namespace does?<br \/>\n<\/strong>Namespace sets up virtual clusters over the same physical cluster. Rather than the need to set up an\u00a0entire physical cluster for a new environment. We can set up several different virtual cluster over the same physical cluster using namespaces.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-43151\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/kube.png\" alt=\"kube\" width=\"839\" height=\"334\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/kube.png 839w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/kube-300x119.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/kube-624x248.png 624w\" sizes=\"(max-width: 839px) 100vw, 839px\" \/><\/p>\n<p>&#8211; \u00a0Helps in easy management of applications of each environment<br \/>\n&#8211; \u00a0Control multiple apps with same names in a single cluster<br \/>\n&#8211; \u00a0Cost-effective<\/p>\n<p><strong>How to create namespaces?<br \/>\n<\/strong><strong>cmd<\/strong>: kubectl create namespace qa<br \/>\n<strong style=\"font-size: 1rem;\">cms:<\/strong> kubectl<span style=\"font-size: 1rem;\"> create namespace <\/span>uat<\/p>\n<p>The above two commands create 2 separate namespaces <strong>QA\u00a0and UAT<\/strong>\u00a0on the existing\u00a0physical cluster. Once the creation is done, let&#8217;s understand how to use it<\/p>\n<p><strong>How do we deploy our applications on namespace?<\/strong><\/p>\n<p>To <a title=\"Docker DevOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">deploy applications<\/a> into particular namespaces, we only need to add a namespace option in the existing application YAML. The configuration looks as follows:<\/p>\n<pre>apiVersion: extensions\/v1beta1\r\nkind: Deployment\r\nmetadata:\r\n name: nginx\r\n <strong>namespace:<\/strong> <strong>qa<\/strong>\r\nspec:\r\n replicas: 1\r\n strategy:\r\n rollingUpdate:\r\n maxSurge: 1\r\n maxUnavailable: 0\r\n type: RollingUpdate\r\n template:\r\n metadata:\r\n labels:\r\n run: sample\r\n spec:\r\n containers:\r\n - name: nginx\r\n image: sample\/sample:custom_nginx\r\n imagePullPolicy: Always\r\n ports:\r\n - containerPort: 80<\/pre>\n<p>The above YAML will create a Nginx pod in <strong>QA namespace<\/strong>.<\/p>\n<p>The following configuration\u00a0will create a Nginx pod in <strong>UAT namespace.<\/strong><\/p>\n<pre>apiVersion: extensions\/v1beta1\r\nkind: Deployment\r\nmetadata:\r\n name: nginx\r\n <strong>namespace: uat<\/strong>\r\nspec:\r\n replicas: 1\r\n strategy:\r\n rollingUpdate:\r\n maxSurge: 1\r\n maxUnavailable: 0\r\n type: RollingUpdate\r\n template:\r\n metadata:\r\n labels:\r\n run: sample\r\n spec:\r\n containers:\r\n - name: nginx\r\n image: sample\/sample:custom_nginx\r\n imagePullPolicy: Always\r\n ports:\r\n - containerPort: 80<\/pre>\n<p>The pods of each namespace can be listed by following commands<\/p>\n<p>kubectl get pods<strong> &#8211;namespace=qa<\/strong> or<br \/>\nkubectl\u00a0get pods<strong> &#8211;namespace=uat<\/strong><\/p>\n<p>All resources created in a particular namespace can be listed using the parameter <strong>&#8220;&#8211;namespace=&#8221;\u00a0<\/strong>after any Kubernetes command.<\/p>\n<p>The usage and implementation of a namespace is very simple but it solves an essential use case of segregating multiple environments without the need to launch multiple clusters.<\/p>\n<p>This helps in reducing the <strong>infrastructure cost and efforts to manage applications<\/strong>\u00a0on different environments.<\/p>\n<p>The best example for <strong>namespaces<\/strong> lies in Kubernetes base setup itself. The <a title=\"Working with Kubernetes deployment, pods and service\" href=\"http:\/\/www.tothenew.com\/blog\/working-with-kubernetes-deployment-pods-and-service-part1\/\">pods and services<\/a> that are essential for Kubernetes functioning, run in a separate \u00a0namespace named &#8220;kube<strong>-system<\/strong>&#8220;. This is how Kubernetes segregates its own system environment from the one that is used by the users. This ensures that no user activity can impact the resources running in Kubernetes system environment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of the project that I was working, I had to set up multiple environments in the same Kubernetes cluster. The environments are namely QA and UAT.\u00a0The blog will guide the reader on how to segregate different environments in same Kubernetes\u00a0cluster and have control over each environment in such a way that the other [&hellip;]<\/p>\n","protected":false},"author":916,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[1174,2348],"tags":[248,1892,1883,3965,3984,1724],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42706"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/916"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=42706"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42706\/revisions"}],"predecessor-version":[{"id":53732,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42706\/revisions\/53732"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=42706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=42706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=42706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}