{"id":40547,"date":"2016-10-24T12:56:09","date_gmt":"2016-10-24T07:26:09","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=40547"},"modified":"2016-10-24T14:27:09","modified_gmt":"2016-10-24T08:57:09","slug":"working-with-kubernetes-deployment-pods-and-service-part1","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/working-with-kubernetes-deployment-pods-and-service-part1\/","title":{"rendered":"Working with Kubernetes deployment, pods and service Part1"},"content":{"rendered":"<p>In my previous blog, <a title=\"Getting started with Kubernetes\" href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-kubernetes\/\">Getting Started with Kubernetes<\/a>,\u00a0I have explained the basics of kubernetes. In this blog, I will be explaining how to create deployment, pods, and service.<\/p>\n<p><strong>Deployment and pods:<\/strong> Below command will create a deployment controller named a &#8220;my-nginx&#8221;, a pod with a single\u00a0 container of an image nginx (base image of nginx in docker hub) which listens on port 80. Replicas will define how many pods do\u00a0 you want to run, in this case, it is &#8220;1&#8221;, so deployment controller will ensure that there should always be one pod running.<\/p>\n<p>[shell]<br \/>\nkubectl run my-nginx &#8211;image=nginx &#8211;replicas=1 &#8211;port=80<br \/>\n[\/shell]<\/p>\n<p>Get the status of the deployment and pod with below commands:<\/p>\n<p>[shell]<br \/>\nkubectl get deployment -o wide<br \/>\n[\/shell]<\/p>\n<p><code><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-40552\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/deployment.png\" alt=\"deployment\" width=\"1147\" height=\"57\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/deployment.png 1147w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/deployment-300x14.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/deployment-1024x50.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/deployment-624x31.png 624w\" sizes=\"(max-width: 1147px) 100vw, 1147px\" \/><br \/>\n<\/code><\/p>\n<p>[shell]<br \/>\nkubectl get pods -o wide<br \/>\n[\/shell]<\/p>\n<p><code><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-40554\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/pods2.png\" alt=\"pods\" width=\"1224\" height=\"60\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/pods2.png 1224w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/pods2-300x14.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/pods2-1024x50.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/pods2-624x30.png 624w\" sizes=\"(max-width: 1224px) 100vw, 1224px\" \/><br \/>\n<\/code><\/p>\n<p><strong>Service<\/strong>: Below command will create a service named &#8220;my-nginx&#8221; with type Loadbalancer, it will create ELB an endpoint. This service listens on the port 80 and redirects it to the target port 80 (the port on which containers listens).<\/p>\n<p>[shell]<br \/>\nkubectl expose deployment my-nginx &#8211;port=80 &#8211;type=LoadBalancer<br \/>\n[\/shell]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-40558\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc.png\" alt=\"svc\" width=\"1164\" height=\"37\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc.png 1164w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc-300x9.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc-1024x32.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc-624x19.png 624w\" sizes=\"(max-width: 1164px) 100vw, 1164px\" \/><\/p>\n<p>Get the status of the service with the below command:<\/p>\n<p>[shell]<br \/>\nkubectl get service -o wide<br \/>\n[\/shell]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-40559\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc1.png\" alt=\"svc1\" width=\"1298\" height=\"69\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc1.png 1298w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc1-300x15.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc1-1024x54.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/svc1-624x33.png 624w\" sizes=\"(max-width: 1298px) 100vw, 1298px\" \/><\/p>\n<p>Curl the ELB endpoint and you should see the <strong>Welcome Nginx Page<\/strong> response back.<\/p>\n<p>Deployments, pods, and services can also be created using yaml\/json file. Please find below a sample yaml file nginx.yaml:<\/p>\n<p>[shell]<\/p>\n<p>apiVersion: extensions\/v1beta1<br \/>\nkind: Deployment<br \/>\nmetadata:<br \/>\nname: my-nginx<br \/>\nspec:<br \/>\nreplicas: 1<br \/>\ntemplate:<br \/>\nmetadata:<br \/>\nlabels:<br \/>\nrun: my-nginx<br \/>\nspec:<br \/>\ncontainers:<br \/>\n&#8211; name: my-nginx<br \/>\nimage: nginx<br \/>\nports:<br \/>\n&#8211; containerPort: 80<br \/>\n&#8212;<br \/>\napiVersion: v1<br \/>\nkind: Service<br \/>\nmetadata:<br \/>\nname: my-nginx<br \/>\nspec:<br \/>\nports:<br \/>\n&#8211; name: &quot;www&quot;<br \/>\nport: 80<br \/>\ntargetPort: 80<br \/>\nselector:<br \/>\nrun: my-nginx<br \/>\ntype: LoadBalancer<\/p>\n<p>[\/shell]<\/p>\n<p>Run the below command which uses the above yaml and creates deployment controller, a pod and service.<\/p>\n<p>[shell]<br \/>\nkubectl apply -f nginx.yaml<br \/>\n[\/shell]<\/p>\n<p><strong>Assign pod to a specific node:<\/strong>\u00a0Suppose there has been an application which requires to run on the same machine i.e on the same IP. Steps to achieve the same:<\/p>\n<ul>\n<li>Get the nodes<\/li>\n<\/ul>\n<p>[shell]<br \/>\nkubectl get nodes<br \/>\n[\/shell]<\/p>\n<ul>\n<li>Assign label to the node. Here I am assigning node=app label to the node:<\/li>\n<\/ul>\n<p>[shell]<br \/>\nkubectl label nodes &lt;node name&gt; node=app<br \/>\n[\/shell]<\/p>\n<ul>\n<li>Update the yaml to add nodeSelector in pod spec:<\/li>\n<\/ul>\n<p>[shell]<br \/>\n&#8212;&#8212;&#8212;<br \/>\n spec:<br \/>\n      containers:<br \/>\n      &#8211; name: my-nginx<br \/>\n        image: nginx<br \/>\n        ports:<br \/>\n        &#8211; containerPort: 80<br \/>\n        nodeSelector:<br \/>\n         node: app<br \/>\n&#8212;-<br \/>\n[\/shell]<\/p>\n<ul>\n<li>\u00a0Run the kubectl apply command and get the pods status to verify it is running on the node which has the lable node=app:<\/li>\n<\/ul>\n<p>[shell]<br \/>\nkuebctl apply -f nginx.yaml<br \/>\nkubectl get pods -o wide<br \/>\n[\/shell]<\/p>\n<p>Hope, now we know how to create deployment pods, services and how to assign pod to a specific node. In the next blog we will covering deployment strategy, multi-container pods, communication between pods.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous blog, Getting Started with Kubernetes,\u00a0I have explained the basics of kubernetes. In this blog, I will be explaining how to create deployment, pods, and service. Deployment and pods: Below command will create a deployment controller named a &#8220;my-nginx&#8221;, a pod with a single\u00a0 container of an image nginx (base image of nginx [&hellip;]<\/p>\n","protected":false},"author":970,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[2348,1],"tags":[1892,3969,3965,3966],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/40547"}],"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\/970"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=40547"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/40547\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=40547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=40547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=40547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}