<?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 Alican Uzun on Medium]]></title>
        <description><![CDATA[Stories by Alican Uzun on Medium]]></description>
        <link>https://medium.com/@alican-uzun?source=rss-648a5ff91fc3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tVZvsCfutY40I2tUb7JzGA@2x.jpeg</url>
            <title>Stories by Alican Uzun on Medium</title>
            <link>https://medium.com/@alican-uzun?source=rss-648a5ff91fc3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:04:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alican-uzun/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[Automating Log Rotation on AWS]]></title>
            <link>https://medium.com/bitaksi-tech/automating-log-rotation-on-aws-9902fda5a63b?source=rss-648a5ff91fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/9902fda5a63b</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[system-administration]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[automation]]></category>
            <dc:creator><![CDATA[Alican Uzun]]></dc:creator>
            <pubDate>Fri, 10 Jun 2022 22:37:42 GMT</pubDate>
            <atom:updated>2023-05-31T20:41:54.281Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_keAvgsEoppeWdnCgt5cQg.png" /></figure><h4>What will you find in this article?</h4><ul><li>What is logrotate? How to configure it?</li><li>How to set up logrotate with Ansible?</li><li>Automation suggestion for constantly occurring large amounts of logs.</li></ul><h4>Case</h4><p>MongoDB logs can cause the disk to fill up over time in production environments. Taking manual actions does not solve this problem permanently.</p><h4>What is the suggestion?</h4><p>Configure a logrotate. Send the rotated, timestamp-added, and Gzip-compressed logs to the bucket in Amazon S3. Ansible can come in handy as implementations will be necessary for more than one node.</p><p>logrotate is one of the core functions of Linux. Its binary file is at <strong>/bin/logrotate</strong>.</p><p>If you did not install it as part of the default OS installation, you can install it on Debian/Ubuntu with:</p><pre><strong>apt install logrotate</strong></pre><p>on RHEL/CentOS:</p><pre><strong>yum install logrotate</strong></pre><p>on RPM/Fedora:</p><pre><strong>dnf install logrotate</strong></pre><h4>What does logrotate do?</h4><p>logrotate simplifies the management of systems that generate large amounts of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file can be processed daily, weekly, monthly, or when it reaches a specified size. Usually, logrotate runs as a daily cron job.</p><h4>What do logrotate files look like?</h4><pre>/var/log/mongodb/mongod.log {<br>  daily<br>  delaycompress<br>  compress<br>  dateext<br>  missingok<br>  copytruncate<br>  notifempty<br>  postrotate<br>    reload rsyslog &gt;/dev/null 2&gt;&amp;1 || true<br>  endscript<br>}</pre><h4>What are these parameters?</h4><ul><li><strong>daily:</strong> It performs the rotation operation daily.</li><li><strong>delaycompress: </strong>It delays the compression till the next rotation of the log file.</li><li><strong>compress:</strong> After each rotation, it compresses the previously rotated log file with Gzip format by default.</li><li><strong>dateext:</strong> Adds timestamp in YYYYMMDD format to the rotated log files.</li><li><strong>missingok:</strong> If there is no log file, it will not give an error.</li><li><strong>copytruncate:</strong> After copying the content of the log file, it resets the content of the main file.</li><li><strong>notifempty: </strong>If the log file is empty, the rotation will not occur.</li><li><strong>postrotate: </strong>If we are going to run a script after rotation, we can add shell commands between these two parameters as in the example above. <strong>prerotate</strong> is the alternative to the postrotate to run a script before rotation.</li></ul><h4>What are the other parameters?</h4><ul><li><strong>weekly / monthly: </strong>They are alternatives to the daily parameter.</li><li><strong>rotate count:</strong> It rotates log files the specified number of times before being removed somehow. If the count is 0, old versions will be deleted instead of being rotated.</li><li><strong>size: </strong>The rotation will not occur until it reaches the preferred size. If the size parameter has <strong>k </strong>at the end, the size is in kilobytes. <strong>M is </strong>for megabytes, and <strong>G </strong>is for gigabytes. No character means the size is in bytes. For example, <strong>size 100</strong>, <strong>size 100k</strong>, <strong>size 100M</strong>, and <strong>size 100G</strong> are valid.</li></ul><p>In my opinion, these were the most important ones. You can have a look at logrotate’s man page for all parameters:</p><pre><strong>man logrotate</strong></pre><h3>AWS Management Console Implementations</h3><h4>Creating <a href="https://s3.console.aws.amazon.com/">S3</a> bucket to store log files</h4><p>Let’s start with creating an<strong> </strong>S3 bucket:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Q0fPzPM4sm8Ag8WNnIUkg.png" /><figcaption><strong>Do not allow any public access.</strong></figcaption></figure><p>We should create some directories in this bucket according to EC2 hostnames.</p><p>In the script part of the rotation, we will use the sed commands in the CLI screenshot below. We will first create a directory named hostname without the instance number. Later, we will create a directory named by instance number inside the first one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*nSEFKdVdDRjgP61ADgpPMg.png" /></figure><p>We should do this directory creation process for all instances. When we do it this way, a single script will run correctly on every instance.</p><p>An example of S3 directory hierarchy:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*1n9NrjJ41ApNohEPrC0gBw.png" /></figure><p>Let’s create one time to show:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AZCYP3KI0gVfe1hF_ODdAQ.png" /></figure><p>Now, a subdirectory with the EC2 number:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NODzErR1iCV36szg_eoHpQ.png" /></figure><blockquote>In this way, we should create as many directories as necessary for all instances. Because the “aws mv” command gives an error when there is no folder named you are trying to send!</blockquote><p>After creating the directories for all instances, we can move on to the next step.</p><h4>Creating an <a href="https://us-east-1.console.aws.amazon.com/iam/home#/roles">IAM role</a> and inline policy</h4><p>Now, we need an IAM role. You can skip this step if you have an IAM role for MongoDB instances.</p><p>We should select <strong>EC2</strong> under the <strong>Use case:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kO_-25I7-iTL7wXsV0Ia_g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_ECAfwwuWEVxaEvTnC4Pbw.png" /></figure><p>We should create an <strong>inline policy</strong> for the IAM role:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j_AL9SePsMOqI01xkaobDA.png" /></figure><p>We give only the necessary permission without forgetting the <strong>Least Privilege</strong> principle.</p><p>JSON Version of the inline policy:</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Sid&quot;: &quot;PutMongodbLogs&quot;,<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: &quot;s3:PutObject&quot;,<br>            &quot;Resource&quot;: &quot;arn:aws:s3:::mongodb-logs/*&quot;<br>        }<br>    ]<br>}</pre><p>Now, we need to attach this role to all MongoDB instances:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*1c4C7CzXejtIsHI2haxPWA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*27oeqwQm_3B1E8kHTfcK5Q.png" /></figure><p>So far, we have created an IAM role and an inline policy that allows only putting objects to the specific S3 bucket. Then we attached this role to MongoDB instances. As a result, MongoDB Instances can put objects into the S3 bucket we created.</p><h3>Setting up logrotate with Ansible</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*HDr-qJuduaN2h91B7r2W0g.png" /></figure><h4>hosts:</h4><p>We set on which instances we will run the playbook. You should change hosts here according to your own SSH config.</p><pre>[prod-mongo]<br>bitaksi-prod-mongo-01<br>bitaksi-prod-mongo-02<br>bitaksi-prod-mongo-03<br>bitaksi-prod-inmemory-01<br>bitaksi-prod-inmemory-02<br>bitaksi-prod-inmemory-03</pre><h4>mongodb.j2:</h4><p>The logrotate file we want to copy to the instances.</p><p>In the script part, it parses the hostname in two ways relative to the last dash and assigns them to two variables. Then, it sends the zipped and timestamp-added log files to the S3 bucket using these two variables without leaving any evidence behind.</p><ul><li>Since we’re moving some things under a directory, it is important to exclude every file first and include the file type we want to send.</li><li>We must use the region parameter. If we do not use it, the probability of getting a timeout is very high! (Proven by experience)</li></ul><pre>/var/log/mongodb/mongod.log {<br>  daily<br>  delaycompress<br>  compress<br>  dateext<br>  missingok<br>  copytruncate<br>  notifempty<br>  postrotate<br>    EC2NUM=`hostname | sed &#39;s/.*-//&#39;`<br>    EC2NAME=`hostname | sed -e &#39;s/\-[^\-]*$//&#39;`<br>    aws s3 mv /var/log/mongodb/ s3://bitaksi-prod-mongodb-logs/$EC2NAME/$EC2NUM/ \<br>    --recursive --exclude &quot;*&quot; --include &quot;*.gz&quot; --region eu-central-1 &gt;&gt; /var/log/MongoS3Sender.log 2&gt;&amp;1<br>  endscript<br>}</pre><h4>logrotate.yml:</h4><ul><li><strong>Installing AWS-CLI</strong>: Installs AWS CLI tool.</li><li><strong>Copying the logrotate configuration file:</strong> It copies the logrotate file to logrotate.d directory.</li></ul><pre>- hosts: prod-mongo<br>  remote_user: ubuntu<br>  become: true<br><br>  tasks:<br>    - name: Installing AWS-CLI<br>      apt:<br>        update_cache: yes<br>        name: awscli<br>        state: present<br>      <br>    - name: Copying the logrotate configuration file<br>      template:<br>        src: &quot;~/logrotate/mongodb.j2&quot;<br>        dest: &quot;/etc/logrotate.d/mongodb&quot;</pre><p>Now we can run the playbook:</p><pre><strong>~/logrotate$ </strong>ansible-playbook logrotate.yml -i hosts --verbose</pre><p>If we want to dry-run the logrotate file, we can use this command:</p><pre>logrotate -f /etc/logrotate.d/<strong>&lt;logrotatefile&gt;</strong></pre><blockquote>Change the <strong>&lt;logrotatefile&gt;</strong> with your own logrotate file.</blockquote><h3>Conclusion</h3><p>We have examined detailly one of the core functions of Linux, logrotate. We have seen a permanent solution to the problem of filling disks with logs, which can occur in production environments and cause us to take manual actions. You can use this solution directly, or you can use it by making custom developments. It’s up to you.</p><p>That’s all from me for now. May the force be with you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9902fda5a63b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bitaksi-tech/automating-log-rotation-on-aws-9902fda5a63b">Automating Log Rotation on AWS</a> was originally published in <a href="https://medium.com/bitaksi-tech">BiTaksi Product &amp; Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating CI/CD Pipeline with Jenkins]]></title>
            <link>https://medium.com/bitaksi-tech/creating-ci-cd-pipeline-with-jenkins-46ca03b4f84b?source=rss-648a5ff91fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/46ca03b4f84b</guid>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[continous-integration]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[continous-deployment]]></category>
            <category><![CDATA[minikube]]></category>
            <dc:creator><![CDATA[Alican Uzun]]></dc:creator>
            <pubDate>Wed, 13 Apr 2022 18:09:11 GMT</pubDate>
            <atom:updated>2023-06-01T13:45:00.870Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*7UomaEgGSFlZ2Uxw.jpg" /></figure><h4>What will you find in this article?</h4><ul><li>What is CI/CD?</li><li>How to install <strong>Jenkins</strong>, <strong>Docker</strong>, <strong>Minikube</strong>, <strong>kubectl</strong>, <strong>Helm</strong>, and <strong>Nginx</strong>.</li><li>How to set SSH and account credentials in Jenkins.</li><li>How to create a Pipeline that deploys to Minikube with Jenkins.</li><li>How to export an application running on Minikube with Nginx.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*WkC_aHFckfZxI_H5Ii3i_g.png" /></figure><h4>What is CI/CD?</h4><p>Continuous Integration is the automation of the code integration process in projects contributed by multiple developers. If there are tests to be done, these can also be automated. It makes the development of the code more comfortable and faster.</p><p>Continuous Deployment or Continuous Delivery is the automation of the software distribution process. In modern software businesses, Continuous Deployment is a must to enable reacting quickly to the changing demands of the market and customer feedback.</p><p>The two are referred to as CI/CD as if they were one thing because they are closely connected.</p><h4>Installations</h4><p>Jenkins is an open-source CI/CD tool developed in Java. It is pretty popular because it is free and open-source. We will start with its installation:</p><pre><strong>sudo apt update</strong></pre><pre><strong>sudo apt install openjdk-11-jdk</strong></pre><pre><strong>sudo apt install git</strong></pre><pre><strong>wget -q -O - </strong><a href="https://pkg.jenkins.io/debian-stable/jenkins.io.key"><strong>https://pkg.jenkins.io/debian-stable/jenkins.io.key</strong></a><strong> | sudo apt-key add -</strong></pre><pre><strong>sudo sh -c &#39;echo deb </strong><a href="https://pkg.jenkins.io/debian-stable"><strong>https://pkg.jenkins.io/debian-stable</strong></a><strong> binary/ &gt; /etc/apt/sources.list.d/jenkins.list&#39;</strong></pre><pre><strong>sudo apt update</strong></pre><pre><strong>sudo apt install jenkins</strong></pre><p>Go to the port 8080 of the machine&#39;s reachable IP address:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*r9emhpvkU64_Tt15y_Mkww.png" /></figure><p>Since the rights of the initial admin password file belong to the Jenkins user, we can run the cat command with sudo to copy our initial password:</p><pre><strong>sudo cat /var/lib/jenkins/secrets/initialAdminPassword</strong></pre><p>Log in and click<strong> Install Default Plugins</strong>. After the installations are complete, create a user and set the Jenkins URL. Now we have access to the Jenkins control panel. Let us put Jenkins aside. We will get back to it once we have provided the other installations.</p><p><strong>Minikube</strong> is a tool that allows us to run Kubernetes locally. It launches a Single-Node Kubernetes Cluster. It gives us a chance to develop and test applications on Kubernetes. Let us install it:</p><pre><strong>wget </strong><a href="https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64"><strong>https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64</strong></a></pre><pre><strong>sudo cp minikube-linux-amd64 /usr/local/bin/minikube</strong></pre><pre><strong>sudo chmod 755 /usr/local/bin/minikube</strong></pre><p>Check the Minikube installation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*DaNfv399UsSdo2icejWeBg.png" /></figure><p><strong>kubectl</strong> is the CLI tool that allows us to communicate with the Kubernetes API. Let us install it:</p><pre><strong>curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s </strong><a href="https://storage.googleapis.com/kubernetes-"><strong>https://storage.googleapis.com/kubernetes-</strong></a><strong><br>release/release/stable.txt)/bin/linux/amd64/kubectl</strong></pre><pre><strong>chmod +x ./kubectl</strong></pre><pre><strong>sudo mv ./kubectl /usr/local/bin/kubectl</strong></pre><p>Now we will install <strong>Helm</strong>. Helm is a tool developed in Golang. It allows us to package and manage Kubernetes applications easily. It is very effective in applications that have many resources and dependencies. DEIS originally developed it but soon became an open-source project after being incorporated into CNCF.</p><pre><strong>wget </strong><a href="https://get.helm.sh/helm-v3.4.1-linux-amd64.tar.gz"><strong>https://get.helm.sh/helm-v3.8.1-linux-amd64.tar.gz</strong></a></pre><pre><strong>tar xvf helm-v3.8.1-linux-amd64.tar.gz</strong></pre><pre><strong>sudo mv linux-amd64/helm /usr/local/bin</strong></pre><pre><strong>rm helm-v3.8.1-linux-amd64.tar.gz</strong></pre><pre><strong>rm -rf linux-amd64</strong></pre><p>Check the Helm installation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*J7A5ujqb2bP3aBxPg2VWQA.png" /></figure><p>Continue with the Docker installation:</p><pre><strong>sudo apt update</strong></pre><pre><strong>sudo apt install \<br>    ca-certificates \<br>    curl \<br>    gnupg \<br>    lsb-release</strong></pre><pre><strong>curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg</strong></pre><pre><strong>echo \<br>  &quot;deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \<br>  $(lsb_release -cs) stable&quot; | sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null</strong></pre><pre><strong>sudo apt update</strong></pre><pre><strong>sudo apt install docker-ce docker-ce-cli containerd.io</strong></pre><p>Check the Docker installation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*xYtIKogPrYmblessdYEBNw.png" /></figure><p>Finally, Nginx:</p><pre><strong>sudo apt update</strong></pre><pre><strong>sudo apt install nginx</strong></pre><pre><strong>sudo ufw allow &#39;Nginx HTTP&#39;</strong></pre><pre><strong>sudo rm</strong> <strong>/etc/nginx/sites-available/default</strong> <strong>/etc/nginx/sites-enabled/default /var/www/html/index.nginx-debian.html</strong></pre><p>Check the Nginx installation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*N1aiJiOldufiWMASQD6Afg.png" /></figure><h4>Implementations</h4><p>Add the Jenkins user to the root and docker groups so that it does not have to worry about permission issues:</p><pre><strong>sudo usermod -a -G root,docker jenkins</strong></pre><p>Get our Minikube cluster up in the Jenkins user and enable the <strong>ingress</strong> add-on:</p><pre><strong>sudo su jenkins</strong></pre><pre><strong>minikube start</strong></pre><pre><strong>minikube addons enable ingress</strong></pre><p>We will set the credentials for the integration stages in the Pipeline. Generate an SSH Key in Jenkins user. We can perform the naming as we wish:</p><pre><strong>ssh-keygen</strong></pre><pre><strong>/var/lib/jenkins/.ssh/springBoot_rsa</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*KgdBDaT_6cGldeqQAmJyfg.png" /></figure><p>Add the Public Key to the project repo as a Deploy Key:</p><pre><strong>cat /var/lib/jenkins/.ssh/springBoot_rsa.pub</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x66MRUx_gEXqm-WKnk1Etg.png" /><figcaption><strong>GitHub -&gt; Repository -&gt; Settings -&gt; Deploy keys</strong></figcaption></figure><p>Add the Private Key as a Credential in Jenkins:</p><pre><strong>cat /var/lib/jenkins/.ssh/springBoot_rsa</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*g8ZcX1QNNtaE1jNjdHMuLg.png" /><figcaption><strong>Manage Jenkins -&gt; Manage Credentials -&gt; Global -&gt; Add Credentials -&gt; Kind: SSH Username with private key</strong></figcaption></figure><p>As a result, we enabled Jenkins and the GitHub repo that stores the project to be able to shake hands with SSH. Now we can code the stage that checks if a new commit has come to the code in the Pipeline.</p><p>We need to do the same operations for HelmChart and Jenkinsfile repos. In a nutshell, we should generate separate keys for each repo. Then add Public Keys to GitHub and add Private Keys to Jenkins <strong>(We cannot use the same key for two different repos!).</strong></p><blockquote><strong>This way may not be the best solution for projects with many repos. Instead, we can open a DevOps account on GitHub and generate an SSH Key with Jenkins User. Add the Public Key to the DevOps account on GitHub, add the Private Key as SSH Credential to Jenkins and give read permission to GitHub DevOps account in all repos. Thus, Jenkins can check out all repos with a single credential.</strong></blockquote><p>Let us add the username and password of the Docker Registry we use to Jenkins as a credential. Thus, the Username and Password will not appear in the Pipeline script. It will remain a secret between Jenkins and us:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*IJhIhk7841N4eZseTGpQWw.png" /><figcaption><strong>Manage Jenkins -&gt; Manage Credentials -&gt; Global -&gt; Add Credentials -&gt; Kind: Username with password</strong></figcaption></figure><p>Now we can build our Pipeline. Click <strong>New Item</strong> from Jenkins Control Center, mark <strong>Pipeline</strong> as the type and give it a name. Later, click on the Pipeline we created and enter the <strong>Edit Configuration</strong> section. Mark the Definition part as <strong>Pipeline script from SCM</strong> and select <strong>Git</strong> under SCM. Then, enter the <strong>SSH URL</strong> of the Jenkinsfiles repo and select the credential which is proper for the Jenkinsfile repo:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*8iqDvPPv7693yP2u203nWw.png" /></figure><p>Since there is no development related to the branch in the Jenkinsfile repo, we will give the master branch. In the Script Path section, we need to provide the directory of the Jenkinsfile to be used in the Pipeline:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*FgYjCaNusxwLmfjzT1ae5Q.png" /></figure><p>Jobs created in Jenkins create its directory under the <strong>/var/lib/jenkins/workspace</strong> directory. Jobs keep repos that it checks out under these directories.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*aFpinGO5LLn9ypIZha_tmA.png" /></figure><p>Here is an example Pipeline:</p><pre>String branch = &quot;master&quot;<br>String Registry = &quot;alicanuzun/springboot&quot;<br>String currentContext = &quot;minikube&quot;<br>String serviceName = &quot;springboot&quot;<br>String environment = &quot;default&quot;<br>String chartPath = &quot;apps/springboot&quot;<br><br>timestamps() {<br>    node(label: &#39;master&#39;) {<br>        stage(&#39;Git Checkout&#39;) {<br>            git branch: &quot;${branch}&quot;,<br>                credentialsId: &#39;SpringBoot-GitHub&#39;,<br>                url: &#39;git@github.com:alican-uzun/springboot&#39;<br>        }<br>        stage(&quot;Docker Build&quot;) {<br>            sh &quot;docker build -t alicanuzun/springboot .&quot;<br>        }<br>        stage(&quot;Login to DockerHub&quot;){<br>            withCredentials([usernamePassword(credentialsId: &#39;dockerhub-creds&#39;, usernameVariable: &#39;DOCKERHUB_USERNAME&#39;, passwordVariable: &#39;DOCKERHUB_PASSWORD&#39;)]){<br>                sh(&#39;docker login --username $DOCKERHUB_USERNAME --password $DOCKERHUB_PASSWORD&#39;)<br>            }<br>        }<br>        stage(&quot;Push Image to DockerHub&quot;){<br>                sh &quot;&quot;&quot;<br>                    docker tag ${Registry}:latest ${Registry}:prod-${currentBuild.number}<br>                    docker push ${Registry}:prod-${currentBuild.number}<br>                &quot;&quot;&quot;<br>        }<br>        stage(&#39;Helm Chart Git Checkout&#39;){<br>            git branch: &quot;${branch}&quot;,<br>                credentialsId: &#39;Helm-Charts-GitHub&#39;,<br>                url: &#39;git@github.com:alican-uzun/helm-charts&#39;<br>        }<br>        stage(&quot;Deploy on Minikube Cluster&quot;){<br>            sh  &quot;helm upgrade --install &quot; +<br>                &quot;${serviceName} ${chartPath} -f ${chartPath}/values.yaml -n ${environment} &quot; + <br>                &quot;--set image.tag=prod-${currentBuild.number} &quot; +<br>                &quot;--set image.repository=${Registry} &quot; +<br>                &quot;--namespace=${environment} &quot;<br>                &quot;--kube-context=${currentContext}&quot;<br>        }<br>    }<br>}</pre><p>Firstly, the Pipeline checks out the project’s repo with the credential we set in Jenkins. In the Docker Build stage, it takes an image build with a shell command. In the next stage, it logs in to the Docker Registry with the credential which we set in Jenkins. Later, it tags the image with Jenkins built-in environment variable <strong>${currentBuild.number}</strong> and pushes it to the registry. It does this to set the image in Helm the same as in the Docker Push Stage. In addition, in this way, images are stored in the registry in some order. The next stage is to check out the Helm Charts repo. In the final stage, it deploys the application to Minikube with Helm.</p><p>After creating the Pipeline, we can now run our job by clicking <strong>Deploy Now</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ATSPV5oyN_6vA3DVtA-giA.png" /></figure><p>To access the application from outside after deployed in Jenkins, we must have exposed the service as NodePort or LoadBalancer in Helm Chart. (Minikube supports only these two types of services)</p><p>If we have not, let us see how we can do it manually for NodePort via kubectl. LoadBalancer is a little different than NodePort. <strong>(We should execute the Minikube and kubectl commands in the Jenkins user.)</strong>:</p><pre><strong>kubectl expose deployment springboot --type=NodePort --port=8080</strong></pre><p>Check the service:</p><pre><strong>kubectl get service springboot</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*-6DC5Kb3qOyHvULJ4AyCjw.png" /></figure><p>Request the URL of the service from Minikube:</p><pre><strong>minikube service springboot --url</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*amhJOyvsWZhVkg6D_JMYDg.png" /></figure><p>Now we can make reverse proxy to the application with Nginx. Create a configuration file for reverse proxy under the <strong>/etc/nginx/conf.d/</strong> directory. <strong>(We should execute the commands for Nginx in the default user.)</strong>:</p><pre><strong>sudo vim /etc/nginx/conf.d/springboot.conf:</strong></pre><p><strong>springboot.conf:</strong></p><pre>server {<br>    listen 80;<br>    server_name _;<br>location / {<br>       proxy_pass http://192.168.49.2:30537;<br>    }<br>}</pre><p>Check the syntax of the configuration file and reload the Nginx service:</p><pre><strong>sudo nginx -t</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FZVnJgw8IXsnhwXAzYOL6g.png" /></figure><pre><strong>sudo systemctl reload nginx</strong></pre><p>Now we can access our application from outside the machine with HTTP. Test it both internally and externally:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*T8H3Xl9jC6dzbPK6fcpixw.png" /></figure><p>In Helm Chart, we can also access the application by using ingress. Let us see how to do it manually with ingress so that you have an idea of how you can do it in your Helm Chart:</p><p><strong>springboot-ingresss.yaml:</strong></p><pre>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: springboot-ingress<br>  annotations:<br>    ingress.kubernetes.io/rewrite-target: /<br>spec:<br>  rules:<br>  - http:<br>      paths:<br>      - path: /<br>        pathType: Prefix<br>        backend:<br>          service:<br>            name: springboot<br>            port:<br>              number: 8080</pre><p>Get ingress up:</p><pre><strong>kubectl apply -f springboot-ingress.yaml</strong></pre><p>It may take some time for ingress to obtain the IP address. Check:</p><pre><strong>kubectl get ingress</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*8KeE_Qj-iwtzyPsB3IfpIA.png" /></figure><p>Now we can make a reverse proxy to ingress just like we did with the service:</p><pre><strong>sudo vim /etc/nginx/conf.d/springboot.conf:</strong></pre><p><strong>springboot.conf:</strong></p><pre>server {<br>    listen 80;<br>    server_name _;<br>location / {<br>       proxy_pass http://192.168.49.2;<br>    }<br>}</pre><p>Reload Nginx and test it both internally and externally:</p><pre><strong>sudo systemctl reload nginx</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*Jostk7mVm4CJHohcuqq8ow.png" /></figure><h4>Conclusion</h4><p>So far, we have seen how Jenkins communicates with GitHub repos and add credentials in Jenkins. We’ve seen what we need to install for a Pipeline and what the Pipeline structure looks like. We’ve also seen two different ways to export an application running on Minikube.</p><p>That’s all from me for now, may the force be with you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46ca03b4f84b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bitaksi-tech/creating-ci-cd-pipeline-with-jenkins-46ca03b4f84b">Creating CI/CD Pipeline with Jenkins</a> was originally published in <a href="https://medium.com/bitaksi-tech">BiTaksi Product &amp; Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Load Balancing with Nginx via Docker]]></title>
            <link>https://medium.com/bitaksi-tech/round-robin-load-balancing-with-nginx-via-docker-eng-b183a11bcc8b?source=rss-648a5ff91fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b183a11bcc8b</guid>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[load-balancing]]></category>
            <dc:creator><![CDATA[Alican Uzun]]></dc:creator>
            <pubDate>Tue, 25 Jan 2022 23:52:09 GMT</pubDate>
            <atom:updated>2023-05-31T20:13:52.696Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MCEksMc59kMFIZek.jpeg" /></figure><h4>What will you find in this article?</h4><ul><li>What is load balancing?</li><li>Implementation of Round Robin Load Balancing between two Docker applications. (Without Docker Compose)</li></ul><h4>Introduction</h4><p>The need for extra resources arises as the HTTP requests on the application server get denser. There are several ways to meet this need.</p><p><strong>Scale-Up:</strong> Upgrading the resource (Processor, RAM) on the server.</p><p><strong>Scale-Out:</strong> Upgrading the resource by creating a copy of the server.</p><p><em>Scale-Out</em><strong> </strong>is where Load Balancing comes into play. It is a tool positioned in front of the application servers and distributes the incoming requests equally or according to the determined rule. In addition, if any server is down, it does not forward the incoming request to that side, thus preventing HTTP request loss.</p><h4><strong>Load Balancing Types</strong></h4><p><strong>Round Robin:</strong> Distributes incoming requests equally.</p><p><strong>Ratio:</strong> Distributes incoming requests according to the limits. (e.g. 3 to 1, 2 to 1, 5 to 2.)</p><p><strong>Least Connection:</strong> It redirects to the server with the minimum load.</p><p><strong>IP Hash:</strong> Every request from the client is directed to the same server where the client session information is located.</p><h4>Implementations</h4><p>We will do Round Robin Load Balancing between two simple applications. One is written in NodeJS, and the other is written in Flask.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*aFFtdDJ510FhARFdgzXmEQ.png" /></figure><p><strong>Dockerize the NodeJS application:</strong></p><p><strong>server.js:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*hDY7-jwvlbWATNbwK7HGKg.png" /></figure><p><strong>package.json:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*1nBlSEpEDBUIpvBFbuK2bg.png" /></figure><p><strong>Dockerfile:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*AMxbVD0wYIt2aZm3u91cQw.png" /></figure><p>Use the Alpine tagged version of the Node image as the base image. Alpine is one of the light and useful Linux distributions. After specifying the directory where the application will run, copy all the JSON files whose names start with the package to this directory (If there is no such directory, it will be created automatically). Installing NPM packages. After copying the code, start the application.</p><p><strong>Dockerize the Flask application:</strong></p><p><strong>index.py:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*753TBLx6qkvEiF4t-Gby8Q.png" /></figure><p><strong>Dockerfile:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*cwP6wiWqQxRuXAaE0JWHQw.png" /></figure><p>As in the NodeJS application, still use the Alpine distro. After specifying the directory where the application will run, copy the application to this directory (If there is no such directory, it will be created automatically.). Install the necessary framework Flask with pip. Finally, running the application.</p><p><strong>Create an Nginx image:</strong></p><p>First, configure an Nginx.conf file for Round Robin Load Balancing.</p><p><strong>nginx.conf:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*kIWojjee9aYuwEolol8qtQ.png" /></figure><p>In the config file, add the name of the application containers and the port it uses to the upstream block. Then, tell to Nginx distribute the HTTP requests on Port 80 to these two containers. Have to add an empty events block because Nginx gets angry when the events block does not exist.</p><p><strong>Dockerfile:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*GIAx6nUwu-EkTygDr1p-Dg.png" /></figure><p>Again, use the Alpine distro <strong>(Always pay attention to using the original base image to be safe.)</strong> Copy the nginx.conf file and expose Port 80. Tell Nginx to run in the background with the daemon off command. This part is significant because a container should only handle one process according to the holy book of Docker.</p><p>Build the images:</p><pre>cd ~/docker-images/nodejs</pre><pre>docker image build -t alicanuzun/nodejs .</pre><pre>cd ~/docker-images/flask</pre><pre>docker image build -t alicanuzun/flask .</pre><pre>cd ~/docker-images/nginx</pre><pre>docker image build -t alicanuzun/nginx .</pre><p>As we have specified in the nginx.conf file, our Nginx container must be on the same network as the application containers to access them by their names. To achieve this, create a bridge network.</p><pre>docker network create –d bridge bitaksi</pre><p>First, we raise our application containers and then our Load Balancer. While doing this, we give the correct names to the containers that our applications will run on and connect all of them to the bridge network we created. We only do port publishing (connecting the container’s port and the host machine’s port) with Nginx because application containers do not have such a need.</p><pre>docker container run -d -net bitaksi -name nodejs alicanuzun/nodejs</pre><pre>docker container run -d -net bitaksi -name flask alicanuzun/flask</pre><pre>docker container run -d -net bitaksi -name nginx -p 80:80 alicanuzun/nginx</pre><p>Containers are running, take a look at how they are:</p><pre>docker container ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qhbo18WOtl7bEokp4vBFrA.png" /></figure><p>Examine the bridge network that is created:</p><pre>docker network ls</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/0*QjYoa4V4LxrmE_06.png" /></figure><pre>docker network inspect bitaksi</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KMxefrDbsgW8d5k2Pzmjuw.png" /></figure><p>As can be seen in the Containers section, it is observable that all three of our containers are in this Bridge Network.</p><p>Finally, check if our Load Balancer is working by making repeated requests to Localhost with curl:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*wq_mfS22b1s9xjh6jwQfWA.png" /></figure><h3>Conclusion</h3><p>We have seen what a load balancer is and why we need it. After that, we implemented a load balancer in front of two Docker applications.</p><p>That’s all from me for now, may the force be with you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b183a11bcc8b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bitaksi-tech/round-robin-load-balancing-with-nginx-via-docker-eng-b183a11bcc8b">Load Balancing with Nginx via Docker</a> was originally published in <a href="https://medium.com/bitaksi-tech">BiTaksi Product &amp; Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>