<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shashank Sinha on Medium]]></title>
        <description><![CDATA[Stories by Shashank Sinha on Medium]]></description>
        <link>https://medium.com/@sinha.shashank.1989?source=rss-dad5d8d3c3dd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*B3NzQyy_RIDZ-ROHwH5elg.jpeg</url>
            <title>Stories by Shashank Sinha on Medium</title>
            <link>https://medium.com/@sinha.shashank.1989?source=rss-dad5d8d3c3dd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:47:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sinha.shashank.1989/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[K8s upgrade KOPS way !!!!]]></title>
            <link>https://medium.com/@sinha.shashank.1989/k8s-upgrade-kops-way-bdb3dfaa9144?source=rss-dad5d8d3c3dd------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdb3dfaa9144</guid>
            <category><![CDATA[kops]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Shashank Sinha]]></dc:creator>
            <pubDate>Tue, 19 Nov 2019 19:31:23 GMT</pubDate>
            <atom:updated>2019-12-12T18:59:24.645Z</atom:updated>
            <content:encoded><![CDATA[<h3>K8s upgrade KOPS way !!!!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XaswJS7WmP08xKE_t7vIOw.png" /></figure><h4>Prerequisites</h4><ul><li>Existing kubernetes cluster is created using kops tool.</li><li>kops &amp; kubernetes <a href="https://github.com/kubernetes/kops/blob/master/channels/stable">version</a> are compatible.</li></ul><h4>Process</h4><p>In my current set up below are the versions of kops and kubernetes version</p><pre>kops version -  &quot;1.12.2&quot; </pre><pre>kubernetes server version - &quot;v1.12.6&quot;</pre><ul><li>Run below command to start the cluster upgrade process</li></ul><p>kops upgrade cluster : Currently patch version upgrade is available and we will proceed with it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1umL6uiS2UaAhJn9lyynEw.png" /><figcaption>kubernetes upgrade version</figcaption></figure><ul><li>kops will only proceed if --yes flag is passed.</li></ul><p>kops upgrade cluster --yes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ctrqT0dgG45_2SC_2-IfuQ.png" /><figcaption>upgrade confirmation</figcaption></figure><p>Above screenshot highlights that kubernetes configuration is updated now. Now we need to apply same configuration to running cluster.</p><p>kops update cluster &lt;cluster name&gt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sXgZkTSsenLTYL7y8J9NjA.png" /></figure><p>kops update cluster &lt;cluster name &gt; --yes</p><ul><li>With below step , updated configuration gets stored into remote config store, in this case it is AWS S3 bucket</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*83FvprQFTmO47RQk0fTvew.png" /></figure><pre>MAC252:$ kops update cluster dev.*****.net --yes<br>I1119 17:56:59.564424   54538 dns.go:92] Private DNS: skipping DNS validation<br>W1119 17:56:59.614857   54538 firewall.go:250] Opening etcd port on masters for access from the nodes, for calico.  This is unsafe in untrusted environments.<br>I1119 17:57:04.541398   54538 executor.go:103] Tasks: 0 done / 141 total; 49 can run<br>I1119 17:57:06.095622   54538 executor.go:103] Tasks: 49 done / 141 total; 34 can run<br>I1119 17:57:07.288987   54538 executor.go:103] Tasks: 83 done / 141 total; 40 can run<br>I1119 17:57:11.746376   54538 executor.go:103] Tasks: 123 done / 141 total; 11 can run<br>I1119 17:57:11.992771   54538 dnsname.go:111] AliasTarget for &quot;bastion.dev.****.net.&quot; is &quot;bastion-dev-****--k723pp-****.eu-west-1.elb.amazonaws.com.&quot;<br>I1119 17:57:12.235309   54538 dnsname.go:111] AliasTarget for &quot;api.dev.***.net.&quot; is &quot;internal-api-dev-****-net-lojosr-***.eu-west-1.elb.amazonaws.com.&quot;<br>I1119 17:57:13.159065   54538 executor.go:103] Tasks: 134 done / 141 total; 7 can run<br>I1119 17:57:13.584201   54538 executor.go:103] Tasks: 141 done / 141 total; 0 can run<br>I1119 17:57:13.584575   54538 dns.go:153] Pre-creating DNS records<br>I1119 17:57:14.921874   54538 update_cluster.go:291] Exporting kubecfg for cluster<br>kops has set your kubectl context to dev.****.net</pre><pre>Cluster changes have been applied to the cloud.</pre><pre>Changes may require instances to restart: kops rolling-update cluster</pre><ul><li>Now we need to rollout changes to cluster. Run below command to start the rollout process.</li></ul><p>kops rolling-update cluster : To roll out the update to running cluster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*loQ_BTmtbEAZvk_0sU8OwA.png" /></figure><p>Now we need to roll out changes to running kubernetes cluster we should apply changes on instances group in order to have better control during upgrade process.</p><p>kops rolling-update cluster --instance-group master-eu-west-1a --yes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YfVL4NlQl8Je_m26L5oGkA.png" /></figure><p>Likewise upgrade the other 2 master instance group .</p><pre>kops rolling-update cluster --instance-group master-eu-west-1b --yes<br>kops rolling-update cluster --instance-group master-eu-west-1c --yes<br>kops rolling-update cluster --instance-group bastions --yes</pre><p>Once done, run this command to check the current status</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E9FV19SVRXrFA3lj1klxcA.png" /><figcaption>cluster status</figcaption></figure><p>In order to upgrade slave nodes, follow below steps</p><ul><li>kubectl get nodes | egrep -v &quot;master|STATUS&quot; | awk {&#39;print $1&#39;}| xargs kubectl cordon : This will ensure pods don’t get re-scheduled on nodes with old version.</li><li>Now it’s time to update last instance group nodes . Since old nodes are now cordon , pods will be scheduled on new nodes by default. Run below command</li></ul><p>kops rolling-update cluster --instance-group nodes --yes</p><p>This step will take time based on number of nodes present in instance group. Once done cluster upgrade is complete.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3rUbeDiLFMtAOIBErf%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3rUbeDiLFMtAOIBErf%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3rUbeDiLFMtAOIBErf%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/7502e2d9d0722f14400ff73af4e034c4/href">https://medium.com/media/7502e2d9d0722f14400ff73af4e034c4/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdb3dfaa9144" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gitlab CI/CD + Helm to deploy PHP Application on Kubernetes]]></title>
            <link>https://medium.com/@sinha.shashank.1989/gitlab-ci-cd-helm-to-deploy-php-application-on-kubernetes-d96963b548ba?source=rss-dad5d8d3c3dd------2</link>
            <guid isPermaLink="false">https://medium.com/p/d96963b548ba</guid>
            <category><![CDATA[helm]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Shashank Sinha]]></dc:creator>
            <pubDate>Sun, 03 Jun 2018 22:23:12 GMT</pubDate>
            <atom:updated>2019-01-27T19:53:16.210Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ErEpyZmmyaMC9cWNmBiXQ.png" /></figure><h4>Prerequisites</h4><ul><li><a href="https://medium.com/@sinha.shashank.1989/automate-rds-aws-kubernetes-cluster-with-kops-and-terraform-84ee72916c43">Kubernetes Cluster</a></li><li><a href="https://github.com/kubernetes/helm">Helm</a></li></ul><p>Now we have running cluster based on this <a href="https://github.com/shashanksinha89/terraform-kops-gitlab">repository</a>. To install gitlab-ce and gitlab-runner check the helm values for both under helm-data repository.</p><pre>.<br>├── README.md<br>├── helm-data<br>│   ├── gitlab-ce.yaml<br>│   └── gitlab-runner.yaml<br>├── main.tf<br>├── terraform.tfvars<br>└── variables.tf</pre><h4>ClusterRole Binding</h4><p>file : bot-rbac.yaml</p><pre>kind: ClusterRoleBinding<br>metadata:<br>  name: bot-rbac<br>subjects:<br>  - kind: ServiceAccount<br>    # Reference to upper&#39;s `metadata.name`<br>    name: default<br>    # Reference to upper&#39;s `metadata.namespace`<br>    namespace: default<br>roleRef:<br>  kind: ClusterRole<br>  name: cluster-admin<br>  apiGroup: rbac.authorization.k8s.io</pre><p>Create the role using kubectl .</p><pre>$ kubectl create -f bot-rbac.yaml</pre><h4>Service Accounts</h4><ul><li>Tiller</li></ul><p>This account is required in order to deploy helm charts. Below steps will enable tiller account.</p><pre>$ helm init<br>$HELM_HOME has been configured at /Users/shashank/.helm.</pre><pre>Tiller (the helm server side component) has been installed into your Kubernetes Cluster.<br>Happy Helming!</pre><pre>$ kubectl create serviceaccount --namespace kube-system tiller</pre><pre>serviceaccount &quot;tiller&quot; created</pre><pre>$ kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller</pre><pre>clusterrolebinding &quot;tiller-cluster-rule&quot; created</pre><pre>$ kubectl patch deploy --namespace kube-system tiller-deploy -p &#39;{&quot;spec&quot;:{&quot;template&quot;:{&quot;spec&quot;:{&quot;serviceAccount&quot;:&quot;tiller&quot;}}}}&#39;</pre><pre>deployment &quot;tiller-deploy&quot; patched</pre><h4>Deploy Gitlab-CE</h4><p>Since we are using gossip based cluster , external url needs to be updated once once it is deployed on kubernetes. These default values are present in helm-data/gitlab-ce.yaml file.</p><pre>externalUrl: <a href="http://gitlab.io/">http://gitlab.io/</a></pre><pre><br>gitlabRootPassword: &quot;demo@123&quot;</pre><p>To deploy gitlab-ce helm chart with customized values</p><pre>$ helm install --namespace default --name gitlab -f gitlab-ce.yaml --wait stable/gitlab-ce</pre><pre>$</pre><p>After deployment helm status can seen</p><pre>$ helm list</pre><pre>NAME   REVISION UPDATED                  STATUS   CHART           NAMESPACE<br>gitlab 1        Sun Jun  3 23:55:48 2018 DEPLOYED gitlab-ce-0.2.1 default</pre><pre>$ helm status gitlab</pre><p>To fetch the gitlab URL use below command</p><pre>$ kubectl get svc --namespace default gitlab-gitlab-ce -o jsonpath=&#39;{.status.loadBalancer.ingress[0].hostname}&#39;</pre><pre>&lt;URL&gt;</pre><p>Once gitlab-ce is deployed on kubernetes externalUrl variable inside gitlab-ce pod needs to be updated.</p><pre>$ kubectl get pods<br>NAME                                 READY     STATUS    RESTARTS   AGE<br>gitlab-gitlab-ce-67b548b6fc-78t9s    1/1       Running   0          7m<br>gitlab-postgresql-7b9554ccd6-rdjwf   1/1       Running   0          7m<br>gitlab-redis-7f68475b85-x2pdf        1/1       Running   0          7m</pre><p>Connect to gitlab pod</p><pre># kubectl exec -it gitlab-gitlab-ce-67b548b6fc-78t9s -- /bin/bash</pre><pre>#root@gitlab-gitlab-ce-67b548b6fc-78t9s:/# echo $EXTERNAL_URL<br><a href="http://gitlab.io/">http://gitlab.io/</a></pre><p>Now update the EXTERNAL_URL to updated gitlab &lt;URL&gt; and reconfigure gitlab.</p><pre>root@gitlab-gitlab-ce-67b548b6fc-78t9s:/# EXTERNAL_URL=&lt;URL&gt;</pre><pre>root@gitlab-gitlab-ce-67b548b6fc-78t9s:/# gitlab-ctl reconfigure</pre><h4>Import Sample Project</h4><p>Sample php-application is available <a href="https://github.com/shashanksinha89/sample-php-app">here</a>.</p><h4>Deploy Gitlab-Runner</h4><p>To configure runner we will require two paramters</p><ul><li>gitlabUrl</li><li>runnerRegistrationToken</li></ul><p>This information is available under Project &gt; Settings &gt; Pipelines</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BHAqg690RFO8-IxmoksVBw.png" /><figcaption>Runner Settings</figcaption></figure><p>Now deploy gitlab-runner using Helm chart</p><pre>$ helm install --namespace default --name gitlab-runner -f gitlab-runner.yaml --wait gitlab/gitlab-runner</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a_B2Vkfv8PO0z5A4jjEjKA.png" /><figcaption>Runner attached</figcaption></figure><h4>Gitlab-Kubernetes Integration</h4><p>Project &gt; Settings &gt; Integrations &gt; Kubernetes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kfp3U5kAZpNlv88EIXBhjQ.png" /><figcaption>Kubernetes Integration</figcaption></figure><p>Get the gitlab-runner token , ca.crt , gitlab &lt;URL&gt; and set namespace to default.</p><h4>CI/CD in action</h4><p>Since we have already imported the php application. This is the structure of php application code repository.</p><pre>.<br>├── Dockerfile<br>├── README.md<br>├── app-code<br>│   ├── CSS<br>│   │   └── style.css<br>│   ├── config.php<br>│   ├── create.php<br>│   ├── delete.php<br>│   ├── error.php<br>│   ├── images<br>│   │   └── File\ download.png<br>│   ├── index.php<br>│   ├── info.php<br>│   ├── read.php<br>│   ├── update.php<br>│   └── upload.php<br>├── bot-rbac.yaml<br>└── helm-data<br>    ├── Chart.yaml<br>    ├── templates<br>    │   ├── NOTES.txt<br>    │   ├── _helpers.tpl<br>    │   ├── deployment.yaml<br>    │   ├── ingress.yaml<br>    │   └── service.yaml<br>    └── values.yaml</pre><pre>5 directories, 21 files</pre><p>Certain environment variables needs to be populated accordingly</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oo3bLNNLzQ-t_Y6yV6E-tA.png" /></figure><ul><li>CI_REGISTRY_PASSWORD = Docker registry password</li><li>CI_REGISTRY_USERNAME = Docker registry username</li><li>DB_NAME = dummy_data</li><li>DB_SERVER = RDS endpoint (Check from AWS Console)</li><li>DB_USERNAME = demo</li><li>DB_PASSWORD = defaultpassword</li></ul><p>CI Configuration file: .gitlab-ci.yaml</p><pre>stages:<br>  - build<br>  - validate<br>  - deploy</pre><pre>variables:<br>  CONTAINER_IMAGE: shashanksinha/php<br>  CONTAINER_IMAGE_LATEST: ${CONTAINER_IMAGE}:${CI_PIPELINE_ID}<br>  CI_REGISTRY: index.docker.io  # container registry URL<br>  DOCKER_DRIVER: overlay2<br>  DOCKER_HOST: tcp://localhost:2375  # required since we use dind</pre><pre>build:<br>  stage: build<br>  image: docker:latest<br>  services:<br>  - docker:dind<br>  before_script:<br>    - echo &quot;FQDN is ${DB_SERVER}&quot;<br>    - echo &quot;dbname is ${DB_NAME}&quot;<br>    - echo &quot;username is ${DB_USERNAME}&quot;<br>    - echo &quot;password is ${DB_PASSWORD}&quot;<br>    - sed -i -e &quot;s/fqdn/${DB_SERVER}/g&quot; app-code/config.php<br>    - sed -i -e &quot;s/dbname/${DB_NAME}/g&quot; app-code/config.php<br>    - sed -i -e &quot;s/username/${DB_USERNAME}/g&quot; app-code/config.php<br>    - sed -i -e &quot;s/password/${DB_PASSWORD}/g&quot; app-code/config.php</pre><pre>script:<br>    - echo &quot;Building Dockerfile-based application...&quot;<br>    - docker build -t ${CONTAINER_IMAGE_LATEST} .<br>    - echo &quot;username is $CI_REGISTRY_USER and password is $CI_REGISTRY_PASSWORD&quot;<br>    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD<br>    - echo &quot;Pushing to the Container Registry...&quot;<br>    - docker push ${CONTAINER_IMAGE_LATEST}</pre><pre>validate:<br>  stage: validate<br>  image: docker:latest<br>  script:<br>    - echo &quot;Run test cases&quot;</pre><pre>deploy:<br>  stage: deploy<br>  image: linkyard/docker-helm<br>  script:<br>    - helm init --upgrade<br>    - helm repo add stable <a href="https://kubernetes-charts.storage.googleapis.com/">https://kubernetes-charts.storage.googleapis.com</a><br>    - helm upgrade --install  php-app --namespace default --wait --set image.tag=${CI_PIPELINE_ID} --set replicaCount=2 helm-data/</pre><p>There are three stages mentioned</p><ul><li>build : This stage takes all the environment variables required to build the docker image and copy latest app code inside the docker image.It is using CI_PIPEPINE_ID to tag the image</li><li>validate : This stage is currently empty. It can be used running test cases.</li><li>deploy : This stage deploys the 2 instances of php application into default namespace with same tag used during build stage.</li></ul><p>Lets trigger the pipeline, these are the stages from the pipeline</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7m94SzTwB76mdRjh-lvJNA.png" /></figure><p>Once deployed to kubernetes cluster, wait for pods to come up for the first time. It approximately takes 2 mins to get registered with EXTERNAL_IP .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tt1ovnBg0D7RH_jYb-MCLQ.png" /></figure><p>Its working !!!!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FddHhhUBn25cuQ%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FddHhhUBn25cuQ%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FddHhhUBn25cuQ%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/84f62051bc1753b21d24018a99bf5b72/href">https://medium.com/media/84f62051bc1753b21d24018a99bf5b72/href</a></iframe><p>Lets check current deployed php helm chart</p><pre>$ helm list php-app<br>NAME    REVISION UPDATED                  STATUS   CHART         NAMESPACE<br>php-app 1        Mon Jun  4 01:06:09 2018 DEPLOYED php-app-1.0.0 default</pre><p>In order to test rolling updates using helm, let’s update banner on front page to UPLOADED DATA</p><pre>$ helm list php-app<br>NAME    REVISION UPDATED                  STATUS   CHART         NAMESPACE<br><strong>php-app 2</strong>        Mon Jun  4 01:33:42 2018 DEPLOYED php-app-1.0.0 default<br>shashank:sample-php-app shashank$</pre><p>revision status has been updated to 2 and front page is also updated as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eb510hIP7IsqOLJZC8-s1A.png" /></figure><p>SUCCESS !!!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F11sBLVxNs7v6WA%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F11sBLVxNs7v6WA%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F11sBLVxNs7v6WA%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="196" frameborder="0" scrolling="no"><a href="https://medium.com/media/17e9f2dd491c9e6ce502bc06094b2fb1/href">https://medium.com/media/17e9f2dd491c9e6ce502bc06094b2fb1/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d96963b548ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automate RDS+AWS-Kubernetes Cluster with Kops and Terraform]]></title>
            <link>https://medium.com/@sinha.shashank.1989/automate-rds-aws-kubernetes-cluster-with-kops-and-terraform-84ee72916c43?source=rss-dad5d8d3c3dd------2</link>
            <guid isPermaLink="false">https://medium.com/p/84ee72916c43</guid>
            <category><![CDATA[kops]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Shashank Sinha]]></dc:creator>
            <pubDate>Sun, 03 Jun 2018 18:23:40 GMT</pubDate>
            <atom:updated>2018-06-04T06:57:30.691Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><a href="https://github.com/kubernetes/kops">Kops</a> (Kubernetes Operation) is a life savior when it comes to creating production grade kubernetes cluster. It is very helpful in managing the lifecycle of cluster.</blockquote><blockquote><a href="https://www.terraform.io/">Terraform</a> is an open source tool which supports multiple providers and helps managing infrastructure as code .</blockquote><p>In this post we are focusing on AWS, Kops supports generating terraform and Cloud Formation model as well. It can create cluster based various modes.</p><p>In <a href="https://medium.com/@sinha.shashank.1989/gitlab-ci-cd-helm-to-deploy-php-application-on-kubernetes-d96963b548ba">upcoming article</a>, we will use this kubernetes-cluser and RDS instance to perform CI/CD using gitlab and deploy sample PHP application.</p><p>To start with I’ll be creating a HA kubernetes cluster using kops to generate terraform configuration. Once the configuration is created it will be used with RDS module.</p><p>This will result in creating terraform based HA kubernetes cluster with RDS access from kubernetes nodes.</p><h4>Prerequisites</h4><ul><li>Go through the <a href="https://github.com/kubernetes/kops/blob/master/docs/aws.md#setup-your-environment">setup</a> steps for creating required user and group with mentioned IAM Roles.</li><li>Add AmazonRDSFullAccess to kops group as shown below</li></ul><pre>$ aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonRDSFullAccess --group-name kops</pre><p>We are ready to create a gossip based cluster i.e cluster name end with .k8s.local</p><p>Clone this <a href="https://github.com/shashanksinha89/terraform-kops-gitlab.git">repository</a> which contain configs to integrate it RDS.</p><pre>.<br>├── README.md<br>├── helm-data<br>│   ├── gitlab-ce.yaml<br>│   └── gitlab-runner.yaml<br>├── main.tf<br>├── terraform.tfvars<br>└── variables.tf</pre><p>Lets begin with creating kubernetes cluster terraform config.</p><pre># kops create cluster --zones eu-west-1a,eu-west-1b \<br>--node-size m4.large --node-count 2 \<br>--name demo.k8s.local \<br>--target=terraform --out=modules/kubernetes</pre><p>This will generate the necessary configurations under modules/kubernetes .</p><pre>.<br>├── README.md<br>├── helm-data<br>│   ├── gitlab-ce.yaml<br>│   └── gitlab-runner.yaml<br>├── main.tf<br>├── modules<br>│   └── kubernetes<br>│       ├── data<br>│       │   ├── aws_iam_role_masters.demo.k8s.local_policy<br>│       │   ├── aws_iam_role_nodes.demo.k8s.local_policy<br>│       │   ├── aws_iam_role_policy_masters.demo.k8s.local_policy<br>│       │   ├── aws_iam_role_policy_nodes.demo.k8s.local_policy<br>│       │   ├── aws_key_pair_kubernetes.demo.k8s.local-15e90ba726254b5f4f0f5c1e166053d4_public_key<br>│       │   ├── aws_launch_configuration_master-eu-west-1a.masters.demo.k8s.local_user_data<br>│       │   └── aws_launch_configuration_nodes.demo.k8s.local_user_data<br>│       └── kubernetes.tf<br>├── terraform.tfvars<br>└── variables.tf</pre><p>Now we need enable RDS access for kubenetes worker nodes. Although it can be enabled for master node as well but hosted applications will run only on worker nodes.</p><h4>Terraform Configs</h4><p>For this to work in terraform way, we need to do the following:</p><p>Config file : main.tf</p><pre>provider &quot;aws&quot; {<br>  region = &quot;eu-west-1&quot;<br>}</pre><pre>module &quot;kubernetes&quot; {<br>  source = &quot;./modules/kubernetes&quot;<br>  <strong>rds-security-group = &quot;${module.rds.db_access_sg_id}&quot;</strong><br>}</pre><pre>module &quot;rds&quot; {<br>  source = &quot;github.com/shashanksinha89/terraform-ecs-efs-rds/rds&quot;<br>  environment       = &quot;production&quot;<br>  allocated_storage = &quot;20&quot;<br>  database_name     = &quot;${var.production_database_name}&quot;<br>  database_username = &quot;${var.production_database_username}&quot;<br>  database_password = &quot;${var.production_database_password}&quot;<br>  subnet_ids        = &quot;${module.kubernetes.node_subnet_ids}&quot;<br>  vpc_id            = &quot;${module.kubernetes.vpc_id}&quot;<br>  instance_class    = &quot;db.t2.micro&quot;<br>}</pre><ul><li>Declare rds-security-group variable in modules/kubernetes/kubernetes.tf</li><li>Enable rds-security-group variable in aws_launch_configuration resource for worker nodes.</li></ul><p>Below I have highlighted the changes for modules/kubernetes/kubernetes.tf config file.</p><pre><strong>variable rds-security-group {}</strong></pre><pre>resource &quot;aws_launch_configuration&quot; &quot;nodes-demo-k8s-local&quot; {<br>  name_prefix                 = &quot;nodes.demo.k8s.local-&quot;<br>  image_id                    = &quot;ami-9084cbe9&quot;<br>  instance_type               = &quot;m4.large&quot;<br>  key_name                    = &quot;${aws_key_pair.kubernetes-demo-k8s-local-15e90ba726254b5f4f0f5c1e166053d4.id}&quot;<br>  iam_instance_profile        = &quot;${aws_iam_instance_profile.nodes-demo-k8s-local.id}&quot;<br>  <strong>security_groups             = [&quot;${aws_security_group.nodes-demo-k8s-local.id}&quot;, &quot;${var.rds-security-group}&quot;]</strong><br>  associate_public_ip_address = true<br>  user_data                   = &quot;${file(&quot;${path.module}/data/aws_launch_configuration_nodes.demo.k8s.local_user_data&quot;)}&quot;</pre><pre>root_block_device = {<br>    volume_type           = &quot;gp2&quot;<br>    volume_size           = 128<br>    delete_on_termination = true<br>  }</pre><pre>lifecycle = {<br>    create_before_destroy = true<br>  }</pre><pre>enable_monitoring = false<br>}</pre><p>Now we have everything in place lets bring terraform in play. Since we are using rds as module mentioned in main.tf . We need to import it as well.</p><pre>$ terraform init</pre><pre>Downloading modules...<br>Get: file:///Users/shashank/demo/terraform-kops-gitlab/modules/kubernetes<br>Get: git::<a href="https://github.com/shashanksinha89/terraform-ecs-efs-rds.git">https://github.com/shashanksinha89/terraform-ecs-efs-rds.git</a></pre><pre>Initializing provider plugins...<br>- Checking for available provider plugins on <a href="https://releases.hashicorp.com/">https://releases.hashicorp.com</a>...<br>- Downloading plugin for provider &quot;aws&quot; (1.21.0)...</pre><pre>The following providers do not have any version constraints in configuration,<br>so the latest version was installed.</pre><pre>To prevent automatic upgrades to new major versions that may contain breaking<br>changes, it is recommended to add version = &quot;...&quot; constraints to the<br>corresponding provider blocks in configuration, with the constraint strings<br>suggested below.</pre><pre>* provider.aws: version = &quot;~&gt; 1.21&quot;</pre><pre>Terraform has been successfully initialized!</pre><p>Now lets generate the terraform plan and apply it.</p><pre>$ terraform plan -out config</pre><pre>**OUTPUT OMITTED**</pre><pre>Plan: 46 to add, 0 to change, 0 to destroy.</pre><pre>------------------------------------------------------------------------</pre><pre>This plan was saved to: config</pre><pre>To perform exactly these actions, run the following command to apply:<br>    terraform apply &quot;config&quot;</pre><p>Now that our configuration is ready . Lets apply the config, it should approx take 4 mins to create everything.</p><pre>$ terraform apply config</pre><pre>***OUTPUT OMITTED***</pre><pre>Apply complete! Resources: 46 added, 0 changed, 0 destroyed.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A9aJEqrRVnWCm9nskcKngw.png" /><figcaption>Kubernetes Cluster</figcaption></figure><p>It shows HA kubernetes cluster hosted in eu-west-1a and eu-west-1b zone.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0FjIKsoiINQVZkcPf-V0jg.png" /><figcaption>RDS Instance</figcaption></figure><p>MySQL RDS instance is created in same subnet. Security group is only enabled to have access from worker nodes to RDS.</p><p>Since we are using gossip-based cluster, currently kops is unable to validate the cluster. This is a <a href="https://github.com/kubernetes/kops/issues/2990">known issue</a> and below is the workaround mentioned to fix this issue</p><pre>$ kops update cluster demo.k8s.local --target=terraform --out=modules/kubernetes/</pre><pre>I0603 19:54:05.790127   13413 apply_cluster.go:456] Gossip DNS: skipping DNS validation<br>I0603 19:54:07.877006   13413 executor.go:91] Tasks: 0 done / 79 total; 30 can run</pre><pre>***OUTPUT OMITTED***</pre><pre>Terraform output has been placed into modules/kubernetes/</pre><pre>Changes may require instances to restart: kops rolling-update cluster</pre><p>We have to force the rolling update. This may take upto 15 mins.</p><pre>$ kops rolling-update cluster --cloudonly --force --yes<br>Using cluster from kubectl context: demo.k8s.local</pre><pre>NAME   STATUS NEEDUPDATE READY MIN MAX<br>master-eu-west-1a Ready 0  1 1 1<br>nodes   Ready 0  2 2 2</pre><pre>W0603 19:58:35.015793   13455 instancegroups.go:152] Not draining cluster nodes as &#39;cloudonly&#39; flag is set.<br>I0603 19:58:35.015815   13455 instancegroups.go:275] Stopping instance &quot;i-0754763295b6ec57e&quot;, in group &quot;master-eu-west-1a.masters.demo.k8s.local&quot;.</pre><pre>***OUTPUT OMITTED***</pre><pre>completed for cluster &quot;demo.k8s.local&quot;!</pre><p>Lets validate and check the kubernetes cluster endpoint.</p><pre>$ kops validate cluster<br>Using cluster from kubectl context: demo.k8s.local</pre><pre>Validating cluster demo.k8s.local</pre><pre>INSTANCE GROUPS<br>NAME   ROLE MACHINETYPE MIN MAX SUBNETS<br>master-eu-west-1a Master m3.medium 1 1 eu-west-1a<br>nodes   Node m4.large 2 2 eu-west-1a,eu-west-1b</pre><pre>NODE STATUS<br>NAME      ROLE READY<br>ip-172-20-48-80.eu-west-1.compute.internal master True<br>ip-172-20-56-107.eu-west-1.compute.internal node True<br>ip-172-20-88-248.eu-west-1.compute.internal node True</pre><pre>Your cluster demo.k8s.local is ready</pre><pre>$ kubectl cluster-info</pre><pre>Kubernetes master is running at &lt;URL&gt;</pre><pre>KubeDNS is running at &lt;URL&gt;/api/v1/proxy/namespaces/kube-system/services/kube-dns </pre><pre>To further debug and diagnose cluster problems, use &#39;kubectl cluster-info dump&#39;.</pre><p>Before hitting the endpoint, kubernetes dashboard needs to be installed.</p><pre>$ kubectl apply -f \<br>  <a href="https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml">https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml</a></pre><pre>secret &quot;kubernetes-dashboard-certs&quot; created<br>serviceaccount &quot;kubernetes-dashboard&quot; created<br>role &quot;kubernetes-dashboard-minimal&quot; created<br>rolebinding &quot;kubernetes-dashboard-minimal&quot; created<br>deployment &quot;kubernetes-dashboard&quot; created<br>service &quot;kubernetes-dashboard&quot; created</pre><ul><li>secret token will be required to login to dashboard</li></ul><pre>$ kops get secrets kube --type secret -oplaintext<br>Using cluster from kubectl context: demo.k8s.local</pre><pre>&lt;TOKEN&gt;</pre><p>Dashboard : &lt;URL&gt;/ui</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pcyX9XKqgIHE2vSvOolFYg.png" /></figure><p>Once authenticated, provide secret token for admin user or upload the kubectl config to login.</p><pre>$ kops get secrets admin --type secret -oplaintext<br>Using cluster from kubectl context: demo.k8s.local</pre><pre>&lt;TOKEN&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h51ByVZLiCMl_DsFbz3IkQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fz23yN3XEUvjvrQHqG_Lvg.png" /><figcaption>Kubernetes Dashboard</figcaption></figure><p>Before tearing down the complete infrastructue, comment out rds-security-group in main.tf as kops update cluster removes it from kubernetes.tf .</p><pre>$ terraform destroy</pre><pre>$ kops delete cluster demo.k8s.local --yes</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84ee72916c43" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>