<?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 Bruno Fonseca on Medium]]></title>
        <description><![CDATA[Stories by Bruno Fonseca on Medium]]></description>
        <link>https://medium.com/@brunose?source=rss-8496792a2673------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TarIIr6lgDlEL1eKIk6ytg.jpeg</url>
            <title>Stories by Bruno Fonseca on Medium</title>
            <link>https://medium.com/@brunose?source=rss-8496792a2673------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 10:10:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brunose/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[GitLab CI/CD && Kubernetes]]></title>
            <link>https://medium.com/@brunose/gitlab-ci-cd-kubernetes-65eec29d0555?source=rss-8496792a2673------2</link>
            <guid isPermaLink="false">https://medium.com/p/65eec29d0555</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Bruno Fonseca]]></dc:creator>
            <pubDate>Sun, 08 Apr 2018 01:43:00 GMT</pubDate>
            <atom:updated>2018-04-08T02:00:35.978Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DdwLGxImtrIDdedgKU7PLA.jpeg" /></figure><p>At Nosebit we are currently using GitLab to store all our repos and recently we started to use it&#39;s CI to automatically deploy our work to Kubernetes, making every release changes accessible as fast as possible. In this article we going to walk through all steps necessary to integrate GitLab and Kubernetes since we little well documented articles in this matter. Ready to go?</p><h3>Step 1 : Create a Kubernetes Cluster</h3><p>The first step is to create a Kubernetes cluster to which we going to deploy our services. Signin or signup to <a href="https://console.cloud.google.com/">https://console.cloud.google.com/</a> and then create a new project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*vs1JlPFGz7RyY37r_nKVUQ.png" /><figcaption>Click to select a project</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*irt0anUGPEk_YG_aFOTetA.png" /><figcaption>Click the add button to create a new project</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*A7_bCsjX9BYzbgBLgn5Lcg.png" /><figcaption>Select a project name and then click on create button</figcaption></figure><p>The project creation could take a while to be finished and after that you will be able to select the new project in your projects list.</p><p>After selecting the new project, go to google cloud right menu, select &quot;Kubernetes Engine&quot; and then click the button &quot;Create cluster&quot; that shows on &quot;Kubernetes Cluster&quot; tab:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dq4jbqS6hjT-i6-wKLZ_Hw.png" /><figcaption>The created nosebit-project is selected and we going to create the cluster clicking &quot;Create cluster&quot; button</figcaption></figure><p>Choose a name for your cluster (test-1 for our case), the cluster version (we are selecting the most recent one), a cluster size (we are choosing default 3 nodes), the machine type (we are selecting the simplest micro machine type) and then click on &quot;Create&quot; button. Sit back and wait while the cluster finish to be created (it can take some time):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*6hOa46ddk-uYT-DbuuJ6GQ.png" /><figcaption>Cluster creation has finished.</figcaption></figure><h3>Step 2 : Create the project</h3><p>Now let&#39;s create a basic project on GitLab that we going to deploy automatically to our fresh kubernetes cluster. The project is very simples and straightforward: a &quot;hello world&quot; website.</p><p>Create an account accessing <a href="https://gitlab.com/">https://gitlab.com/</a> and then click the &quot;New project&quot; button. Select a &quot;Project name&quot; (we choose <em>kube-test</em>) and &quot;Visibility Level&quot; for the project (we choose private visibility since it&#39;s more challenging in terms of deploy).</p><p>Ok. Now let&#39;s create the sources files. Clone the empty project created above somewhere in your system and place the following files within it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*ByEG4MoVUn35haLxUwtNcw.png" /><figcaption>The files in our demo repo</figcaption></figure><p>Let&#39;s begin with our &quot;stupid&quot; index.html file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/974b3cd5fcd87288ad3b14d8c41013f6/href">https://medium.com/media/974b3cd5fcd87288ad3b14d8c41013f6/href</a></iframe><p>Then let&#39;s inspect our Dockerfile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71c2fa1c3a48a0a7660c4614dcc2f310/href">https://medium.com/media/71c2fa1c3a48a0a7660c4614dcc2f310/href</a></iframe><p>This Dockerfile going to create a very simple image based on mhart/alpine-node:9 image which going to use http-server module to provide our index.html to external world on port 80.</p><p>Now it&#39;s time to digg into gitlab and kubernetes integration!! Take a look at our gitlab-ci.yml file that drives jobs that GitLab CI going to perform:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/502d6c34d08f1dd3b2cc9899457b6285/href">https://medium.com/media/502d6c34d08f1dd3b2cc9899457b6285/href</a></iframe><p>In line 5 we state that our jobs will be grouped in two consecutive stages: first CI going to run all jobs within build stage and upon success CI going to run all jobs within deploy stage. The practical rule is: CI runs in parallel jobs at the same stage and runs sequentially jobs in different stages according to stage order.</p><p>In line 9 we declare one environment variable that stores development image url in our docker registry (we are using GitLab own registry). Remember to replace brunomacf by you own username or group (organization) name and kube-test by your own project (repo) name.</p><p>In line 12 we define a job named build_dev at build stage that going to run only when we merge code to develop branch. This job going to execute a script with a series of actions: (1) first it logins to our GitLab private docker registry; (2) we build docker image using our local Dockerfile; (3) we push the image to registry.</p><p>In line 21 we define a job named deploy_dev at deploy stage that going to run just after all jobs at build stage (only build_dev in our case) has successfully run. This job going to execute the following actions: (1) on lines 27–30 we download and install kubectl so we can communicate with our remote kubernetes cluster; (2) on line 31 we set a new cluster named &quot;nosebit&quot; to kubectl config file and inform the url entrypoint of that cluster using server option; (3) on line 32 we set an user named admin and his credentials to access the cluster; (4) on line 33 we create a new context named default which bonds together cluster nosebit and user admin just created; (5) on line 34 we set default context as our current working context (so, from now on we are using admin user to make request to nosebit cluster); (6) on line 35 we are setting our docker registry credentials to kubernetes so it can pull private images from it; (7) on line 36 we generate our kubernetes deploy file (kubernetes.yml file explained next) from a template file (kubernetes.tpl.yml) using some environment variables; (8) and finally we apply the new deploy file to kubernetes.</p><p>Our kubernetes.tpl.yml file defines the deployment of our service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/346c99de2a71c668401e702640147534/href">https://medium.com/media/346c99de2a71c668401e702640147534/href</a></iframe><p>In this file we define a deployment which dictates the replication schema (desired state) for all instances (pods) of our service. For example, we want to have just 1 instance of this service running and we want the instance to be created from repo image created on build stage. Furthermore, pay attention that we specified the imagePullSecrets to match the docker registry credentials created on deploy stage… without that kubernetes will not be able to clone our private image. Finally, at the end of the file we define a service load balancer in front of all service instances so that it balances the load (duuhhhh) to all available instances, provide a single entrypoint for these instances (dns) and expose the service to outside world (public ip).</p><p>Now that everything is set, let&#39;s make our first commit:</p><pre>cd /path/to/repo<br>git add .<br>git commit -m &quot;Initial commit to create master branch&quot;<br>git push</pre><p>This push will not trigger any CI jobs since all jobs we stated in gitlab-ci.yml going to run only on develop branch.</p><h3>Step 3: Deploy</h3><p>Before we can see the magic happen we need to set KUBE_URL, KUBE_USER and KUBE_PASSWORD secret environment variables in our project to store our cluster url as well as credentials so we can connect with it. To do so, go to your GitLab project page, then Settings &gt; CI &gt; Secret Variables</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-_Y44OT0WERdi7E-vqy8Q.png" /></figure><p>The values for these variables you can find accessing your google cloud console, then clicking on cluster name to see it&#39;s details and then clicking on &quot;Show credentials&quot;:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zBYRnuJ-wIOhIYI5G26tWA.png" /><figcaption>Endpoint going to be KUBE_URL variable</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ij08OPfyg0UCFuehCevaHg.png" /><figcaption>After clicking &quot;Show credentials&quot; — username and password going to be KUBE_USER and KUBE_PASSWORD variables respectively</figcaption></figure><p>Now we are good to make magic happen! First create our target develop branch:</p><pre>cd /path/to/repo<br>git checkout -b develop<br>git push --set-upstream origin develop</pre><p>Now if you go to your GitLab project page, then CI/CD &gt;Pipelines you going to see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kxfhKqKI8LurJvz9F-LjpA.png" /><figcaption>Pipeline is running the first stage (build stage)</figcaption></figure><p>And after a while (with God&#39;s help) everything will become green:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mGQ95zHJUHVAQuAtqosePw.png" /><figcaption>All stages are successful</figcaption></figure><p>To see that everything works go to your google cloud kubernetes console page and then select Workloads tab to see all created service instances (pods):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8fg3NbGmrkz7RenuBSpCrA.png" /><figcaption>Everything is OK!</figcaption></figure><p>Then select &quot;Discovery &amp; load balancing&quot; tab so we can see all service load balancers created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cdgV5Dhd9YLT_FKIsxYGmQ.png" /><figcaption>Load balancer is OK!</figcaption></figure><p>Click on endpoint url and hopefully we will be presented with our silly website:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*ehwV-3HzLeu5kslhIAB8FA.png" /></figure><p>To make a final test try to change &quot;Hello World!&quot; title in our index.html file to something like &quot;Hello Brazil!&quot;. Commit and push the changes to develop branch:</p><pre>cd /path/to/repo<br>git branch develop<br>git add .<br>git commit -m &quot;Change title&quot;<br>git push</pre><p>Wait for GitLab pipeline to finish successfully and then refresh the website:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*72tai60Bnc9aamSdvWAjDA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65eec29d0555" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>