{"id":8708,"date":"2021-01-31T19:48:51","date_gmt":"2021-01-31T19:48:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8708"},"modified":"2021-06-01T15:39:54","modified_gmt":"2021-06-01T15:39:54","slug":"deploying-and-exposing-an-nginx-server-in-kubernetes","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/deploying-and-exposing-an-nginx-server-in-kubernetes\/","title":{"rendered":"Deploying and exposing an nginx server in kubernetes"},"content":{"rendered":"<p>In this post we&#8217;re going to create the configuration for a deployment, service and ingress controller to access an nginx instance to our network. I&#8217;ve picked nginx simply to demonstrate the process of these steps and obviously, out of the box, we get a webpage to view to know whether everything work &#8211; feel free to can change the image used to something else for your own uses.<\/p>\n<p><strong>Deployment<\/strong><\/p>\n<p>Below is the deployment configuration for k8s, creating a deployment named <em>my-nginx<\/em>. This will generate 2 replicas and expose the image on port 80 of the container.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\napiVersion: apps\/v1\r\nkind: Deployment\r\nmetadata:\r\n  name: my-nginx\r\nspec:\r\n  selector:\r\n    matchLabels:\r\n      run: my-nginx\r\n  replicas: 2\r\n  template:\r\n    metadata:\r\n      labels:\r\n        run: my-nginx\r\n    spec:\r\n      containers:\r\n      - name: my-nginx\r\n        image: nginx\r\n        ports:\r\n        - containerPort: 80\r\n<\/pre>\n<p><strong>Service<\/strong><\/p>\n<p>Now we&#8217;ll create the configuration for the <em>my-nginx<\/em> service<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\napiVersion: v1\r\nkind: Service\r\nmetadata:\r\n  name: my-nginx\r\n  labels:\r\n    run: my-nginx\r\nspec:\r\n  ports:\r\n  - port: 80\r\n    protocol: TCP\r\n  selector:\r\n    run: my-nginx\r\n<\/pre>\n<p><strong>Ingress<\/strong><\/p>\n<p>We&#8217;re going to use Ingress to expose our service to the network, alternatively we could use a load balancer, but this options requires an external load balancer, so more like used on the cloud, or we could use NodePort which allows us to assigna port to a service so that any request to the port is forwarded to the node &#8211; the main problem with this is the port may change, instead  Ingress acts like a load balancer within our cluster and will allow us to configure things to route port 80 calls through to our <em>my-nginx<\/em> service as if it was running outside of k8s. <\/p>\n<p>We&#8217;re going to need to enable ingress within k8s. As we&#8217;re using Ubuntu and microk8s, run the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nmicrok8s enable ingress\r\n<\/pre>\n<p>The following is the configuration for this ingress.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\napiVersion: networking.k8s.io\/v1\r\nkind: Ingress\r\nmetadata:\r\n  name: http-ingress\r\nspec:\r\n  rules:\r\n  - http:\r\n      paths:\r\n      - path: \/\r\n        pathType: Prefix\r\n        backend:\r\n          service:\r\n            name: my-nginx\r\n            port: \r\n              number: 80\r\n<\/pre>\n<p><strong>Running and testing<\/strong><\/p>\n<p>Now we need to apply the configuration to our kubernetes instance, so I&#8217;ve saved all the previously defined configuration sections into a single file named my-nginx.yaml. Now just run the following to run the configuration in k8s<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nkubectl apply -f my-nginx.yaml\r\n<\/pre>\n<p>If you like, you can check the endpoint(s) assigned within k8s using<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nkubectl get ep my-nginx\r\n<\/pre>\n<p>and then curl the one or more endpoints (in our example 2 endpoints). Or we can jump straight to the interesting bit and access your k8s host&#8217;s ip and if all worked, you&#8217;ll be directed to one of the replicates nginx instances and we should see the &#8220;Welcome to nginx!&#8221; page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we&#8217;re going to create the configuration for a deployment, service and ingress controller to access an nginx instance to our network. I&#8217;ve picked nginx simply to demonstrate the process of these steps and obviously, out of the box, we get a webpage to view to know whether everything work &#8211; feel free [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[314],"tags":[],"class_list":["post-8708","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8708"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8708\/revisions"}],"predecessor-version":[{"id":8774,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8708\/revisions\/8774"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}