<?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 Yankee Maharjan on Medium]]></title>
        <description><![CDATA[Stories by Yankee Maharjan on Medium]]></description>
        <link>https://medium.com/@yankeexe?source=rss-348042aca5a2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j2-fGzd3fQuGQezluhI07Q.png</url>
            <title>Stories by Yankee Maharjan on Medium</title>
            <link>https://medium.com/@yankeexe?source=rss-348042aca5a2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:53:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yankeexe/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[Running GLM 4.7-Flash on Nvidia DGX Spark]]></title>
            <link>https://yankeexe.medium.com/running-glm-4-7-flash-on-nvidia-dgx-spark-e8bf29040c7b?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8bf29040c7b</guid>
            <category><![CDATA[vllm]]></category>
            <category><![CDATA[dgx-spark]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[local-llm]]></category>
            <category><![CDATA[inference]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Mon, 26 Jan 2026 05:38:35 GMT</pubDate>
            <atom:updated>2026-01-26T10:26:03.705Z</atom:updated>
            <content:encoded><![CDATA[<p>GLM 4.7-Flash is one of the best coding model you can run on your local machine.</p><p>I tried and failed multiple times running GLM 4.7 Flash using vLLM, but the fastest way to get model up and running was using <a href="https://github.com/ggml-org/llama.cpp">Llama.CPP.</a></p><p>To get started:</p><h4>Download the model</h4><p>You can find all the models on <a href="https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/tree/main">unsloth repo for GLM 4.7-Flash</a>, I went with <a href="https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/blob/main/GLM-4.7-Flash-Q4_1.gguf">the Q4</a> because DGX Spark Blackwell architecture is optimized for Q4 model.</p><pre>wget https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/resolve/main/GLM-4.7-Flash-Q4_1.gguf</pre><h3>Build the latest Llama Server</h3><p>Clone llama.cpp on your machine</p><pre>https://github.com/ggml-org/llama.cpp<br><br>cd llama.cpp</pre><h4>Generating the Build System:</h4><pre>cmake -B build \<br>  -DGGML_CUDA=ON \<br>  -DCMAKE_CUDA_ARCHITECTURES=121 \<br>  -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc \<br>  -DCMAKE_C_COMPILER=gcc \<br>  -DCMAKE_CXX_COMPILER=g++</pre><p><strong>Command Breakdown:</strong></p><ul><li><strong>cmake -B build</strong>: Initializes the build system and generates Makefiles in a dedicated build directory to keep source code folder clean.</li><li><strong>-DGGML_CUDA=ON</strong>: enables the CUDA backend, instructing the compiler to build the GPU-accelerated layers required for NVIDIA hardware.</li><li><strong>-DCMAKE_CUDA_ARCHITECTURES=121</strong>: Sets the target GPU architecture for optimization</li><li><strong>-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc</strong>: Explicitly specifies the path to the NVIDIA nvcc compiler to ensure the correct CUDA toolkit is used rather than the system default. Use which nvcc to determine the installed path of the binary.</li><li><strong>-DCMAKE_C_COMPILER=gcc</strong>: Forces the use of the standard GCC compiler for C files to prevent compatibility issues with other system compilers like Clang.</li><li><strong>-DCMAKE_CXX_COMPILER=g++</strong>: Forces the use of the G++ compiler for C++ files, ensuring the CPU-side code is built with the standard Linux toolchain.</li></ul><h4>Compiling the Binary</h4><pre>cmake --build build --config Release -j$(nproc)</pre><p><strong>Command Breakdown:</strong></p><ul><li><strong>cmake — build build</strong>: Executes the compilation process using the configuration files generated in the previous step.</li><li><strong>— config Release</strong>: Builds the binary with high-level optimizations (speed over debugging), which is mandatory for acceptable inference performance.</li><li><strong>-j$(nproc)</strong>: Accelerates the build by running parallel compilation jobs equal to the number of CPU cores available on the system.</li></ul><h4>Copy the llama-server binary to system PATH</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TigvfEqSLuahUUWJLrQh2Q.png" /></figure><pre>sudo cp dist/bin/llama-server /usr/local/bin</pre><h3>Run llama-server for inference</h3><pre>llama-server -m GLM-4.7-Flash-Q4_1.gguf -c 32000  --no-mmap -ngl 99 --host 0.0.0.0 --jinja</pre><p><strong>Command Breakdown:</strong></p><ul><li><strong>llama-server</strong>: The executable binary (compiled in the previous step) that launches the HTTP API server.</li><li><strong>-m GLM-4.7-Flash-Q4_1.gguf</strong>: Specifies the path to the model file; here it is a 4-bit quantized version of GLM-4 optimized for efficiency.</li><li><strong>-c 32000</strong>: Sets the context window size to 32,000 tokens, allowing the model to “remember” and process large amounts of text in a single conversation.</li><li><strong>— no-mmap</strong>: Forces the model to load directly into RAM/VRAM rather than mapping from the hard drive, preventing I/O lag during generation.</li><li><strong>-ngl 99</strong>: Offloads layers to the GPU; setting this to a high number (99) ensures the <em>entire</em> model runs on the GPU for maximum inference speed.</li><li><strong>— host 0.0.0.0</strong>: Binds the server to all network interfaces, making the API accessible to other computers on your network (not just localhost).</li><li><strong>— jinja</strong>: Enables Jinja2 templating support, which is strictly required for this specific model (GLM-4) to format user and system prompts correctly.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8bf29040c7b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Effortless CI/CD with GitHub Composite Actions]]></title>
            <link>https://yankeexe.medium.com/effortless-ci-cd-with-github-composite-actions-f7907a3827e2?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7907a3827e2</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[cicd]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[github-actions]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Tue, 19 Dec 2023 07:26:27 GMT</pubDate>
            <atom:updated>2023-12-22T15:27:52.540Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FcCExsEC5TVPEAg9rYDaVw.jpeg" /></figure><p>GitHub Actions is a powerful and versatile CI/CD solution that works for different teams and projects. It’s a built-in feature of GitHub, the platform that many developers use daily, so it’s easy to set up and use.</p><p>One of the benefits of GitHub Actions is that it allows you to write DRY (Don’t Repeat Yourself) code for your pipeline, using reusable steps. Composite actions let you combine multiple run steps into a single reusable action that you can share across repos, organizations or publicly.</p><h3>🧠 Mental Model for Composite Actions</h3><h4>🧩 As extensions</h4><p>When you think of composite action you should think of it <strong>as an extension of your existing workflow.</strong> If certain steps are repeated in multiple workflows, then it makes sense to put them as a composite action.</p><p>Note that: composite action is one of the three ways you can create actions that can be shared in public.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vlV8nvuqagRof_3B" /></figure><p><strong>For example,</strong> there is a process of building, tagging and uploading a Docker image to the registry for many of your projects, then you can create a composite action that can be referenced in all of your required projects.</p><h4>🧬 As Functions</h4><p>Now as developers, we are pretty familiar with what functions are and how they work. Just for a refresher, you wrap a piece of code as a function when it is supposed to be used in multiple places. You can pass arguments to it which it processes and produces outputs.</p><p>Similarly, we can think of Composite Actions as functions to which we can pass arguments, which process and produce certain output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*40iOh75nUNntblFr" /></figure><h3>📚 How to structure your actions?</h3><p>There are two ways of structuring Composite Actions:</p><ol><li>Single repo single action (root action)</li><li>Single repo multi-action (path-based)</li></ol><h4>Single repo action</h4><p>This is the most common pattern that you see in publicly shared GitHub Actions. You have a repository that houses action.yaml file on the root which defines a set of steps that can be reused.</p><p>Example of a simple hello-world composite action:</p><pre>name: Greeter<br>description: Composite Action to greet someone<br><br>inputs:<br>  who-to-greet:<br>    required: false<br>    default: World<br>    description: who to greet<br><br>runs:<br>  using: composite<br>  steps:<br>    - name: greet<br>      run: echo &quot;Hello ${{ inputs.who-to-greet }}&quot;<br>      shell: bash</pre><p>You can browse the above sample action here:</p><p><a href="https://github.com/yankeexe/actions-hello-world">GitHub - yankeexe/actions-hello-world: Single Repo Composite Action</a></p><h4>Single repo multiple actions</h4><p>In this pattern, you have a single git repository that houses multiple actions that you can reuse across your organizations. This pattern is mostly popular in organizations where you house multiple composite actions in the same place which is referenced across different projects, teams or domains.</p><blockquote><em>When using this pattern you have to specify exactly where your action is located so the path of the action is a key here.</em></blockquote><p>An example of this pattern is as follows:</p><p><a href="https://github.com/yankeexe/actions-combined">GitHub - yankeexe/actions-combined: actions-combined</a></p><p>You can notice that each composite action has its directory and the workflow file is named action.yaml .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*na7hCncFsuqzIK0y" /></figure><p>When we name the files action.yaml, we don&#39;t have to mention the file name when calling the action. Meaning we can do this:</p><pre>uses: yankeexe/actions-combined/actions/ping@mainv</pre><p>If your filename is something different then you have to mention the filename in the calling workflow as well. For example:</p><pre>uses: yankeexe/actions-combined/actions/ping/ping-action.yaml@main</pre><h3>👻 Actions Gotchas</h3><h4>🔎 Is your Action Accessible?</h4><p>For both single repo actions and single repo multi-actions, we have to note repository visibility. If the composite action is in a public repository then it’s accessible for everyone. If the action is in a private repository then it can be accessed within the organization or users repositories by going to: settings &gt; Actions &gt; General and enabling the following option.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OFbZBkqWC0Sl716E" /></figure><h4>🫵 Referencing your action</h4><p>You can reference composite actions in multiple ways:</p><ol><li>🪵 Branch name</li></ol><p>Takes the steps from the workflow file present in a branch.</p><pre>uses: yankeexe/actions-hello-world@main</pre><p>2. #️⃣ Commit hash</p><p>Takes the steps from the workflow file present in a particular commit.</p><pre>uses: yankeexe/actions-hello-world@a37bf8bbc2b75c505b41a0741ce589bf403ed9f9</pre><p>3. 🏷️ Tag</p><p>Takes the steps from the workflow file present in a particular tag.</p><pre>uses: yankeexe/actions-hello-world@0.0.1</pre><h4>🌌 Action Context</h4><p>The context of the composite action is always set to the repo of the calling workflow if they have checkout action used in them. Otherwise, we can explicitly use the checkout action on the composite action as well.</p><p>To use checkout action in the calling workflow it needs the permissions for reading the content of the repository.</p><pre># calling workflow<br>permissions: <br>    contents: read<br><br>demo_job:<br>    runs-on: ubuntu-latest<br>    steps:<br>      - uses: actions/checkout@v4<br>      - name: call_composite_action<br>        uses: demo_user/demo_actions/.github/actions/do_something@main<br>        ...</pre><p>To use the checkout action in the composite action itself, we need the GITHUB_TOKEN passed from the calling workflow to the composite action.</p><pre># Calling workflow<br>- name: call_composite_action<br>  uses: demo_user/demo_actions/.github/actions/do_something@main<br>  with:<br>      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}</pre><pre># composite action <br>inputs: <br>   GITHUB_TOKEN:<br>       type: string<br>       description: Github token passed from calling workflow<br>       required: true<br><br>runs:<br>  using: composite<br>  steps:<br>    - uses: actions/checkout@v4<br>      shell: bash<br>      with:<br>          token: ${{ inputs.GITHUB_TOKEN }}<br>          repository: demo_user/demo_project<br>          path: main</pre><h4>🐚 Each step requires a shell</h4><p>Whenever we are writing our composite action we’ll likely forget to add the shell key for each of the steps. Shell defines where we want to execute the commands defined in the run field. Possible values for this field are:</p><ul><li>sh</li><li>bash</li><li>pwsh</li><li>python</li><li>nodejs</li><li>cmd</li><li>powershell</li></ul><p>You can read more about the shell <a href="https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell">field here.</a></p><h3>🔐 OIDC in reusable workflow</h3><p>One of the benefits of composite actions over other reusable workflows is its ability to <a href="https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-cloud-providers">use OIDC</a> to authenticate with cloud providers without having to store any credentials for it. You can use OIDC with:</p><ul><li>Amazon Web Services</li><li>Azure</li><li>Google Cloud Platform</li><li>HashiCorp Vault</li></ul><p><strong>Example</strong>: If your reusable action has some step that involves using AWS resources like S3 to upload an object, then using OIDC you can authenticate with AWS, assume a role you want to use and access the services. This is not possible with workflow_run or workflow_call.</p><p>But a gotcha when using OIDC with composite action is that the calling workflow or the main workflow that calls/consumes composite action should have permission set to:</p><pre>permissions: <br>    id_token: write<br>    contents: read<br><br># Calling workflow<br>demo_job:<br>  runs-on: ubuntu-latest<br>  steps:<br>    - uses: actions/checkout@v4<br>    - name: call_composite_action<br>      uses: demo_user/demo_actions/.github/actions/do_something@main<br>      with:<br>          role_to_assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}</pre><p>Then your composite action can have steps as:</p><pre>inputs:<br>  aws_role_to_assume:<br>    required: true<br>    description: AWS Role ARN to use<br><br>runs:<br>  using: composite<br>  steps:<br>    - name: Configure AWS Credentials<br>      uses: aws-actions/configure-aws-credentials@v2<br>      with:<br>        role-to-assume: ${{ inputs.aws_role_to_assume }}<br><br>    - name: List S3 buckets<br>      run: aws s3 ls<br>      shell: bash</pre><h3>👋 Conclusion</h3><p>As we navigate through the ever-evolving landscape of technology, tools like Composite Actions become indispensable and can be a game-changer propelling you toward more streamlined, effective, and enjoyable deployment experiences.</p><p>Thank you for reading!</p><p><em>Originally published at </em><a href="https://yankee.dev/cicd-with-github-composite-actions"><em>https://yankee.dev</em></a><em> on December 19, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7907a3827e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker tips for better Developer Experience]]></title>
            <link>https://yankeexe.medium.com/docker-tips-for-better-developer-experience-6e8797614acd?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e8797614acd</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[sbom]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Mon, 05 Jun 2023 02:23:24 GMT</pubDate>
            <atom:updated>2023-06-05T02:23:24.946Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5x3YQ7m53gSmOKf0r-BwWQ.jpeg" /></figure><p>Docker is an indispensable tool for development. Knowing your way around it makes you more productive — getting things done faster. This blog incorporates some of the Docker know-hows ( <em>updated frequently</em>) that will help you to get the best experience from Docker development.</p><h3>Overwriting Entrypoint</h3><p>Whenever you use the docker run command the first process that runs inside of your container is determined by the ENTRYPOINT command.</p><p>The most common thing you want to do with a container is to attach a pseudo-terminal to it. We do it using:</p><pre>docker run -it &lt;image-name&gt; &lt;shell-name&gt; <br>docker run -it ubuntu:latest bash</pre><p>Now if you are using some images like the official AWS python image, you can’t actually do it by default.</p><pre>docker run -it amazon/aws-lambda-python sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gLEfpHc6k9yjsvyb" /></figure><p>You get some generic info related to lambdas like your current working directory and handler but no sign of a terminal 🤔. If you <a href="https://hub.docker.com/layers/aws-lambda-python/amazon/aws-lambda-python/latest/images/sha256-1f11684c63f4cf4c221ee5a336e55912f9fbe695469354f2afb4211aef448bff?context=explore">dig into the image layers</a> we can see that at the end of the Dockerfile there’s an ENTRYPOINT command that runs a shell script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/0*9EIImXpEYdfc8-pl" /></figure><p>Docker is pretty flexible in how we want to make it work. So we can overwrite the default ENTRYPOINT which can be any valid Linux command and attach a pseudo-terminal.</p><pre>docker run --rm -it --entrypoint sh amazon/aws-lambda-python</pre><h3>One-off containers</h3><p>Sometimes we just want to run the container for a single purpose, and don’t need it in our system. These can be cases where we want to attach a terminal inside an image and check for contents or debug something, run a container for testing purposes, and all the other things that might float your boat.</p><p>For that kind of need, docker ships with the --rm command, when you run your container with it, it immediately removes the container instead of putting it in a <strong>stop</strong> state.</p><p>By default, when you stop a container, it is in a dormant state but occupies space on your hard drive. You can use -a flag to view all your stopped containers.</p><pre>dcocker ps -a</pre><p>To remove them from your system you’d have to use:</p><pre>docker rm &lt;container-id&gt;/&lt;container-name&gt;</pre><p><strong>For example</strong>: I use these one-off containers to test out the new features from the latest release of Python by opening a Python Shell from the container.</p><pre>docker run --rm -it python:3.11.0b4-alpine3.16 python</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bVVj7zK7kQQg5kfp" /></figure><h3>Command Substitution</h3><p>Docker has some filters and flags that you can use to perform an iterative task.</p><p>Common tasks like creating new versions of the image with the same name and tag can result in dangling images. The command below shows us a list of all the dangling images in the system eating our disk space.</p><pre>docker images --filter dangling=true</pre><p>Now we want to remove all of them to clear up some storage! It’s not productive to remove them one by one using</p><p>With -q flag we can list only the ids of the images and pass its output to the remove command using <strong>command substitution</strong> (supported by most SHELL)</p><pre>docker rmi $(docker images -q --filter dangling=true)</pre><p>NOTE: If your SHELL supports it, you can replace $(...) with back-ticks:</p><pre>docker rmi `docker images -q --filter dangling=true`</pre><h3>Software Bill of Materials (SBOM)</h3><p>If you are shipping images that are used by multiple teams or are public for use then it is essential that you publish the Software Bill of Materials (SBOM) along with your images. Software Bill of Materials provides a transparent overview of the packages that make up your final image. These can be your OS system packages, or language-specific packages, along with their version. It makes it easy for teams and end-users to identify packages on our image that are vulnerable with simple search or vulnerability scanners like <a href="https://github.com/anchore/grype">grype</a>.</p><p>There are two ways you can go about generating SBOMs for your Docker image:</p><ol><li>Docker introduced native support for generating SBOM in Docker Desktop 4.7.0. You can also install it as a plugin.</li></ol><pre> curl -sSfL https://raw.githubusercontent.com/docker/sbom-cli-plugin/main/install.sh | sh -s --</pre><p>Note: that the plugin is in the <strong>Experimental</strong> stage and things are subject to change rapidly.</p><p>2. You can use the <a href="https://github.com/anchore/syft#installation">syft command-line tool</a> that Docker uses internally.</p><h4>Generating SBOM</h4><p>You can generate SBOM in two widely used standard formats <a href="https://spdx.dev/">SPDX</a> and <a href="https://cyclonedx.org/">CycloneDX</a> or take the plain JSON route if you aren’t sure of the mentioned formats. Plain JSON has a dump of all the information syft can find in your image.</p><pre>docker sbom &lt;image-name&gt; -o &lt;image-name.sbom.json&gt; <br>docker sbom ubuntu:latest -o ubuntu.sbom.json <br><br>syft &lt;image-name&gt; -o json=&lt;image-name.sbom.json&gt; <br>syft ubuntu:latest -o json=ubuntu.sbom.json</pre><h4>Vulnerability Scanning</h4><p>Once you have the SBOM, the next step is to scan for vulnerabilities. One of the profound tools for this is <a href="https://github.com/anchore/grype">grype</a>. You can pass the SBOM JSON generated into grype to list the vulnerabilities. It will include the installed version, fixed version, and severity of the packages scanned.</p><pre>grype ubuntu.sbom.json</pre><p>If you want to go further you can add an <a href="https://github.com/anchore/syft#adding-an-sbom-to-an-image-as-an-attestation-using-syft">SBOM to an image as an attestation using Syft and Cosign.</a></p><p>There are multiple tools to scan for vulnerabilities in Docker images with/without the need for SBOM, you can find these tools listed <a href="https://geekflare.com/container-security-scanners/">in this article by geekflare</a>.</p><h3>Always be scanning</h3><p>With the introduction of Docker extensions on Docker Desktop scanning for vulnerability in your images has never been easier.</p><p>Open your Docker Desktop and go to extensions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eJSgnV1kxgYudoJk" /></figure><p>From there, you can download <a href="https://www.notion.so/Blogs-db72e383211b4dc499714e6000e36221">Snyk</a> and <a href="https://www.notion.so/Blogs-db72e383211b4dc499714e6000e36221">Trivy</a>. You can download their CLI’s if you are not using Docker Desktop.</p><p>Go to one of its dashboards, select the image to scan and you’ll be provided with a list of vulnerabilities in your image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A_nnMI_76Eb06Dzo" /></figure><h3>Cross-platform Images</h3><p>When you want to distribute images among your team; you have to make sure that it runs on all of the machines. That’s why we are using Docker in the first place, right?</p><p>Although Docker makes it quite easy to package your application and move around in any environment; we do have to make sure that the underlying CPU architecture supports the image we have packaged. Images are tied to the CPU architecture where it is built and it specified on the <a href="https://github.com/opencontainers/image-spec/blob/main/image-index.md#image-index-property-descriptions">OCI Image Index properties</a>. To view this, you can inspect your any image on your machine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Lh17uRpHTmpUrois" /></figure><pre>docker image inspect busybox:latest | less</pre><p>To view all the available CPU architecture for a particular image, you can run the following command:</p><pre>docker buildx imagetools inspect busybox:latest</pre><p>Best practice is to build these multi-platform images on your CI pipelines; for this tutorial, we will be doing it on our local machine. After we build the multi-arch images, we need to push them to the Docker Hub since the Docker engine registry only supports single CPU architecture. For this you need to authenticate your Docker CLI for Docker Hub before you proceed to the next section.</p><p><strong>Create a new builder:</strong></p><pre>docker buildx create --name multi --use</pre><p><strong>Create a new test Dockerfile</strong></p><pre>FROM alpine:latest CMD echo &quot;Running on $(uname -a)&quot;</pre><p><strong>Build and push to Docker Hub</strong></p><p>To create multi-arch images we use the --platforms flag and specify which architecture we want to build against.</p><pre>docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t yankexe/host-info --push .</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*m_W9R2OaUuZSe-WR" /></figure><p><strong>View multi-arch images in Docker Hub</strong></p><p>In DockerHub, we have images that can be used against multiple platforms. Now, if anyone pulls our image from the Hub, Docker CLI will fetch the one specific to their CPU architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*69fAUNjNUHh4ckOz" /></figure><h3>Connecting to the host machine</h3><p>Publishing port using -p or -port flags to make services available outside of Docker (host machine) is common in everyone&#39;s development workflow. But what if you want to connect to a service running on your host machine from inside of the container?</p><p>For this, Docker has a built-in DNS name: host.docker.internal which resolves to the host machine IP address. Note that, this is only available on Docker for Mac and Windows.</p><p>If you have a database instance running on your host machine at port :5432 then you can access it from the application inside your container using host.docker.internal:5432</p><h3>Demo</h3><p>Create a simple flask server: <a href="http://app.py/">app.py</a> on your host machine.</p><pre>from flask import Flask<br><br>app = Flask(__name__)<br><br>@app.route(&quot;/&quot;)<br>def index():<br>    return &quot;Hello world&quot;</pre><p>Run the server:</p><pre>flask --app app --debug run</pre><p>Get inside an one-off container:</p><pre>docker run --rm -it ubuntu bash</pre><p>Make a request from inside of the container to the host machine.</p><pre>apt update; apt install curl -y curl host.docker.internal:5000</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*amEl69Hrqz9r1kAe" /></figure><blockquote><em>Thank you for reading. If you have any suggestions or feedback feel free to reach out to me on </em><a href="https://twitter.com/yankexe"><em>Twitter</em></a><em>.</em></blockquote><p><em>Originally published at </em><a href="https://yankee.dev/docker-tips-for-better-developer-experience"><em>https://yankee.dev</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e8797614acd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[6 Tools to Run Kubernetes Locally]]></title>
            <link>https://yankeexe.medium.com/6-tools-to-run-kubernetes-locally-1655ebe0841e?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/1655ebe0841e</guid>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Thu, 05 Aug 2021 08:14:09 GMT</pubDate>
            <atom:updated>2021-08-05T08:14:09.560Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C_yj5u237RVL7fcAbfjbRQ.png" /></figure><p>Kubernetes is a big and complicated technology and it clearly requires some time and dedication to wrap your head around. There is no vendor lock-in meaning it runs the same no matter which managed cloud platform you use it on. This means using it locally will be no different from using it on the cloud.</p><p>There are multiple tools for running Kubernetes on your local machine, but it basically boils down to two approaches on how it is done:</p><ol><li>running it from a single binary package</li><li>running it as a container using Docker in Docker (DinD)</li></ol><h3>Kubernetes Marketplace</h3><p><a href="https://github.com/alexellis/arkade">GitHub - alexellis/arkade: Open Source Marketplace For Developer Tools</a></p><p>Before we move on to talk about all the tools, it will be beneficial if you installed arkade on your machine. It will help you get these tools with a single command.</p><pre>curl -sLS https://get.arkade.dev | sudo sh</pre><h3>Features?</h3><p>All of the tools listed here more or less offer the same feature, including but not limited to:</p><ol><li>Multi-Node cluster</li><li>Persistent volumes</li><li>Networking</li><li>Certificates</li><li>Bare-metal support</li><li>Dashboard</li><li>Kubernetes Versions</li><li>Add-ons</li><li>Cross-platform</li><li>Tracks upstream Kubernetes</li></ol><h3>Single package binary</h3><ul><li><a href="https://github.com/k3s-io/k3s"><strong>k3s</strong></a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/0*qu5-ei-NStOfHLo-" /></figure><p>k3s is a lightweight Kubernetes distribution from Rancher Labs. It is specifically targeted for running on IoT and Edge devices, meaning it is a perfect candidate for your Raspberry Pi or a virtual machine.</p><p>It comes with a single binary of mere &lt;40 MB and takes as low as 500 MB of RAM.</p><p>You can bootstrap k3s quickly using <a href="https://github.com/alexellis/k3sup">k3sup</a> !</p><pre>arkade get k3sup</pre><ul><li><a href="https://github.com/k0sproject/k0s"><strong>k0s</strong></a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/0*RRor2W_KI9zh74u3" /></figure><p>k0s is the latest entry on the block. By name, you might think it’s a more stripped version of k3s, but it’s an entirely different distribution from an entirely different company, called Mirantis. Contrary to the name, it comes in a larger binary of 150 MB+.</p><p>It can be run as a binary or in DinD mode. k0s takes security seriously and out of the box, it meets the <a href="https://www.sdxcentral.com/security/definitions/what-does-mean-fips-compliant/">FIPS compliance</a>. Although, a new distribution, k0s has reached the production-ready status, so there wouldn’t be an issue for development usage.</p><pre>arkade get k0s</pre><ul><li><a href="https://github.com/ubuntu/microk8s"><strong>Microk8s</strong></a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/0*cty_tNHhidKypw5S" /></figure><p>MicroK8s is a Kubernetes distribution by Canonical, the company behind Ubuntu. You already saw this coming; it can only be installed using snap. It comes with loads of add-ons baked in like Fluentd, Grafana and Prometheus.</p><p>If you are on Ubuntu or its derivatives that uses snap you&#39;ll feel right at home using MicroK8s.</p><pre>sudo snap install microk8s --classic</pre><h3>Docker in Docker (DinD)</h3><p>Running Docker inside of Docker (Inception anyone?) is a popular way of bootstrapping Kubernetes. The isolating nature of Docker makes running a multi-node cluster a breeze on a single machine, and also ensures the running instance does not affect the machine itself.</p><blockquote><em>As the title suggests, you need to have Docker installed on your machine to go this route.</em></blockquote><p><a href="https://github.com/kubernetes/minikube"><strong>minikube</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/0*s_kgXj4fvNaKv-56" /></figure><p>Despite running on top of Docker and similar container technologies, minikube is really flexible in how it operates and supports multiple virtualization drivers making it adaptable to different computing environments. These include KVM2, Virtualbox, Podman, Hyperkit, Hyper-V and many more.</p><pre>arkade get minikube</pre><p><a href="https://github.com/kubernetes-sigs/kind"><strong>KinD</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/0*72C1eOGOt0fYU1jQ" /></figure><p>Kubernetes in Docker (KinD) is similar to minikube but it does not spawn VM’s to run clusters and works only with Docker. KinD for the most part has the least bells and whistles and offers an intuitive developer experience in getting started with Kubernetes in no time.</p><pre>arkade get kind</pre><p><a href="https://github.com/rancher/k3d/"><strong>k3d</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/0*Wh8cRbsRl0vR0mq0" /></figure><p>k3d is basically running k3s inside of Docker. It provides an instant benefit over using k3s on a local machine, that is, multi-node clusters. Running inside Docker, we can easily spawn multiple instances of our k3s Nodes.</p><pre>arkade get k3d</pre><h3>Conclusion</h3><p>Either you choose a single binary package or DinD approach, Kubernetes has made itself pretty accessible. For new learners, the barrier to entry is low, and the feedback loop is instantaneous.</p><p>I hope this article has been helpful in deciding which tool to use for running your local Kubernetes instance.</p><p><em>Originally published at </em><a href="https://yankee.dev/6-tools-to-run-kubernetes-locally"><em>https://yankee.dev</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1655ebe0841e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Incognito Mode for Shell Environments]]></title>
            <link>https://yankeexe.medium.com/incognito-mode-for-shell-environments-ba1627ca569e?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba1627ca569e</guid>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[zsh]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Sun, 16 May 2021 11:26:41 GMT</pubDate>
            <atom:updated>2021-05-16T11:27:50.980Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aTshZQXomKSNR5Hl" /></figure><p>Whatever we write on the Shell, its history is saved on the HISTFILE. If you go and type history on your terminal, you&#39;re going to see a list of all the commands you recently typed.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FWYD6Xkk1U8E&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DWYD6Xkk1U8E&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FWYD6Xkk1U8E%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa97b11a7c5f841b3bf3a7dded9e5808/href">https://medium.com/media/fa97b11a7c5f841b3bf3a7dded9e5808/href</a></iframe><h3>What is Shell’s history?</h3><p>Think of it like your browser history, where all the sites you have visited are stored, but when you don’t want to keep that history, you switch to <strong>Incognito Mode</strong>. Like the browser, we can switch to Incognito Mode when working with secrets and other sensitive data that we don’t want to be leaked on the history.</p><p>How many times have you done this to test out something, and the next thing you know, this is logged in your history.</p><pre>export AWS_PASSWORD=&quot;xyz#31&quot;</pre><p>All of our histories are saved on a file; depending on your shell environment, HISTFILE can be in a certain location with a certain name.</p><pre>echo $HISTFILE</pre><pre># View the contents of HISTFILE<br>cat ${HISTFILE}</pre><p>And how much history is saved is determined by the HISTSIZE environment variable.</p><pre>echo $HISTSIZE</pre><p>You can change the HISTSIZE to let&#39;s say store only the last 100 commands you have written.</p><pre>export HISTSIZE=100</pre><h3>Incognito Mode on Bash shell</h3><p>To use the Bash shell on Incognito mode all we have to do is unset the HISTFILE.</p><pre>unset HISTFILE</pre><p>This will unset the history file for the current terminal session, meaning, if you restart the terminal session or open a new instance of the Bash shell, you won’t find any history.</p><p>To see that in action, write some random unique commands you can identify on the terminal, and restart the terminal.</p><pre># Do random things <br># ...</pre><pre># Refresh bash session<br>exec bash</pre><pre># Check history<br>history</pre><h3>Incognito mode on ZSH</h3><p>ZSH has one of the easiest ways of disabling history, all you have to do is <strong>add a space in front of the command</strong> and that won’t be listed on the HISTFILE.</p><pre># space in front of the command <br> echo &quot;Goodbye History&quot;</pre><p>And just like that, you won’t have any history. If this is not working out for you, go to the zsh configuration file, usually .zshrc, and follow the instructions below.</p><pre># open the config file and write<br>setopt HIST_IGNORE_SPACE<br># close and save the config file</pre><pre># execute the contents of config file <br>source &lt;path-to-zsh_config_file&gt;</pre><h3>Incognito mode on Fish Shell</h3><p>Fish shell has a built-in private mode that can be enabled with ease using:</p><pre>fish --private</pre><p>Once you are done with dealing with sensitive data, you can just exit from the private mode and there will be no single trace of history.</p><pre># exit from private mode<br>exit</pre><pre># check history<br>history</pre><h3>Conclusion</h3><p>Hope this has been a helpful guide on working with sensitive data on your terminal. If you want to read more articles like this make sure to subscribe to my newsletter and <a href="https://www.youtube.com/channel/UCsHbJuyHI7eKje1AH_joWyQ">YouTube channel</a>.</p><p><em>Originally published at </em><a href="https://yankee.dev/incognito-mode-for-shell-environments"><em>https://yankee.dev</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba1627ca569e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Streamline your projects using Makefile]]></title>
            <link>https://yankeexe.medium.com/streamline-your-projects-using-makefile-744ebbc69cc1?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/744ebbc69cc1</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[cli]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[terminal]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Sat, 26 Dec 2020 16:22:36 GMT</pubDate>
            <atom:updated>2021-05-09T14:09:00.590Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*98_dmVMJfzM9JdV9cifG_g.jpeg" /></figure><p>make is one of the tools that we use heavily for streamlining tasks on our projects. It has proven to be helpful specifically for streamlining the development process, repeating mundane tasks with custom CLI like subcommands and mainly <strong>onboarding new team members</strong>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FvybdPTfNLo4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DvybdPTfNLo4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FvybdPTfNLo4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4fb21e24c998f76d3c0fa44005566c8/href">https://medium.com/media/a4fb21e24c998f76d3c0fa44005566c8/href</a></iframe><p>With a set of rules in <strong>Makefile</strong>, you can get up and running in no time, keeping the process sane and saving time and effort for everyone in the team. We’ll be going through the basics to some interesting stuffs we can do with Makefile.</p><p>There are two pieces to this equation, one is the make CLI tool and the next is the Makefile . The basics is make reads the <strong>rules</strong> from the Makefile and executes them. What I will be showing today is just a small part of what make is capable of.</p><h3>Writing Makefile</h3><p>If you have worked with YAML files before then you will feel right at home writing Makefiles.</p><h3>Anatomy of Rules</h3><p>Every Makefile consists of rules with the anatomy of:</p><pre>target: dependencies<br>     recipe</pre><ul><li><strong>target</strong>: target can be an executable, object or just a name for an action that we want to carry out. We will be using targets purely with the placeholder name for the rule. Be mindful about the name, as it should resonate with the action we want to perform with no confusion whatsoever.</li><li><strong>dependencies</strong>: Dependencies are the rules that needs to be executed, in order for the current rule to work.</li><li><strong>recipe</strong>: recipe is the meat of the Makefile, it is the action that we want to perform with our target name. Make sure to put a tab character at the start of every recipe line (just like YAML). You can also replace the tab character with anything you want using the .RECIPEPREFIX variable.</li></ul><p>Next we will be looking into some examples on how to make use of Makefile. These examples will be based on setting up development environments.</p><h3>Basic Rules</h3><p>A basic rule where you just want to put some alias is straight forward.</p><p>Let’s say you have a <strong>python</strong> project and you want to hand it over to a new team member. How do you streamline the setup process. Maybe it can look something like this.</p><blockquote><strong><em>Note</em></strong><em>: </em><em># is for comments.</em></blockquote><blockquote>@ symbol is to disable printing the recipe to stdout.<br><em>Test without the </em><em>@ symbol at the beginning of the recipe.</em></blockquote><blockquote><em>:= is the expansion operator which prevents using subsequent value with the same variable name.</em></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/058747c7617dfb4024dac119b62e02bb/href">https://medium.com/media/058747c7617dfb4024dac119b62e02bb/href</a></iframe><p>You can do something similar with your existing project.</p><p>Now to get up and running, all you have to do is:</p><pre>$ make venv <br> ​<br> $ . venv/bin/activate <br> ​<br> $ make setup<br> ​<br> $ make format <br> ​<br> # and so on</pre><h3>Rules with Dependencies</h3><p>Taking the reference from the example above, suppose we want to print out the output of check target every time we run the format target. So how do we create that dependency? It’s plain simple, we just have to update the format target to look something like this:</p><pre>format: check # run the formatter on files.<br>  @black .</pre><p>We have added the dependency of check to the right of the target, just like showcased on the anatomy <strong>Anatomy of Rules</strong> section.</p><h3>Variables</h3><p>We can also define variables if we have some piece of command for repeated use. For this example we will be taking the reference of the Django management command.</p><p>Variables are normally written with all caps and uses := to assign variable name to a value. Variables can be accessed using either $() or ${} syntax.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1fdaaef253e4c2cd64b316a5dd05d7e/href">https://medium.com/media/b1fdaaef253e4c2cd64b316a5dd05d7e/href</a></iframe><p>Also your <strong>SHELL environment variables</strong> are converted in to Makefile environment variables, so you can directly make use of them while creating your rules.</p><p><strong>Example:</strong></p><p>In our shell we can export an environment variable called INFO</p><pre>$ export INFO=&quot;Run make help to show all the available rules.&quot;</pre><p>And now in the Makefile we can refer to it as any variable.</p><pre>info: # show project info<br>   @echo ${INFO}</pre><h3>Default target</h3><p>If you just run make on your command line nothing is going to happen. But we can change that by using the .DEFAULTGOAL special variable and assigning the target we want to run by default.</p><pre>.DEFAULT_GOAL := run</pre><p>Now, next time you run make it is going to run the Django server by default.</p><h3>Self documenting</h3><p>Now we have bunch of targets on our Makefile and we also called this combo as a custom mini CLI app. Wouldn’t it be great, if we could have a help command similar to a real CLI app? Say no more, thanks to the <a href="https://medium.com/better-programming/how-to-create-a-self-documenting-makefile-533ebf8f82e2">blog</a> from <a href="https://medium.com/u/d838624534b3">Victoria Drake</a> we have the script to do so.</p><p>Just create a help target and assign it as a .DEFAULT_GOAL. With this, all the comments we have been writing on our target gets converted into a nice help message.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ed6acd83f25b2e7922ec66c5852ca54/href">https://medium.com/media/8ed6acd83f25b2e7922ec66c5852ca54/href</a></iframe><h3>Include other Makefiles</h3><p>We can separate out Makefiles based on the tasks they perform and include them into the main Makefile. We usually have separate Makefile managed for <strong>environment variables</strong>, <strong>Docker</strong> and <strong>Kubernetes</strong>. This offloads all the tasks from project set up to Deployment to the Makefile.</p><p>I will show a brief example of each of the file just to give an example:</p><blockquote><em>Note: Since make runs each recipe on a new instance of the shell, we can lazy evaluate the variables using </em><em>?= meaning, they are initialized only when referenced for a single shell instance.</em></blockquote><p><strong>Makefile</strong> Root makefile composed of other Makefiles.</p><pre>SHELL :=/bin/bash<br> APP_ROOT := $(PWD)<br> TMP_PATH := $(APP_ROOT)/.tmp<br> VENV_PATH := $(APP_ROOT)/.venv<br> ​<br> export ENVIRONMENT_OVERRIDE_PATH ?= $(APP_ROOT)/env/Makefile.override<br> ​<br> -include $(ENVIRONMENT_OVERRIDE_PATH)<br> include $(APP_ROOT)/targets/Makefile.docker<br> include $(APP_ROOT)/targets/Makefile.k8s</pre><p><strong>Environment Variables</strong> <em>Makefile.override</em> Makefile containing just the essential environment variables.</p><pre>STAGE ?= &lt;stage&gt;<br> SERVICE_NAME ?= &lt;service-name&gt;<br> AKS_RESOURCE_GROUP ?= &lt;resource-group&gt;<br> AKS_CLUSTER_NAME ?= &lt;cluster-name&gt;<br> REGISTRY_URL ?= &lt;registry-url&gt;<br> AZ_ACR_REPO_NAME ?= &lt;repo-name&gt;</pre><p><strong>Docker</strong> <em>Makefile.docker</em> Makefile containing docker rules.</p><pre>export GIT_COMMIT ?= $(shell cut -c-8 &lt;&lt;&lt; `git rev-parse HEAD`)<br> export BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)<br> ​<br> export DOCKER_BUILD_FLAGS ?= --no-cache<br> export DOCKER_BUILD_PATH ?= $(APP_ROOT)<br> export DOCKER_FILE ?= $(APP_ROOT)/Dockerfile<br> ​<br> export TARGET_IMAGE ?= $(REGISTRY_URL)/$(AZ_ACR_REPO_NAME)/$(SERVICE_NAME)<br> export TARGET_IMAGE_LATEST ?= $(TARGET_IMAGE):$(BRANCH)-$(GIT_COMMIT)<br> ​<br> acr-docker-login:<br>   az acr login --name $(AZ_ACR_REPO_NAME)<br> ​<br> docker-build:<br>   docker build $(DOCKER_BUILD_FLAGS) -t $(SERVICE_NAME) -f $(DOCKER_FILE) $(DOCKER_BUILD_PATH)<br> ​<br> docker-tag:<br>   docker tag $(SERVICE_NAME) $(TARGET_IMAGE_LATEST)<br> ​<br> docker-push: acr-docker-login<br>   docker push $(TARGET_IMAGE_LATEST)</pre><p><strong>Kubernetes</strong></p><p><em>Makefile.k8s</em> Makefile containing rules for Kubernetes.</p><pre>export OVERLAY_PATH ?= $(APP_ROOT)/k8s/overlays/$(STAGE)/<br> ​<br> define kustomize-image-edit<br>   cd $(OVERLAY_PATH) &amp;&amp; kustomize edit set image api=$(1) &amp;&amp; \<br>   cd $(APP_ROOT)<br> endef<br> ​<br> kubectl-apply:<br>   kustomize build $(OVERLAY_PATH)<br>   kustomize build $(OVERLAY_PATH) | kubectl apply -f -<br> ​<br> update-kubeconfig:<br>   az aks get-credentials --resource-group $(AKS_RESOURCE_GROUP) --name $(AKS_CLUSTER_NAME)<br> ​<br> aks-deploy: update-kubeconfig<br>   $(call kustomize-image-edit,$(TARGET_IMAGE_LATEST))<br>   make kubectl-apply<br> ​<br> aks-delete: update-kubeconfig<br>   kubectl delete namespace $(STAGE)-api<br> ​<br> kustomize-edit:<br>   $(call kustomize-image-edit,$(TARGET_IMAGE_LATEST))<br> ​</pre><p>Now we have orchestrated all these Makefiles, it is easier to keep track of all the rules and makes working with Makefiles sane, if you are doing a lot with it.</p><h3>Conclusion</h3><p>So with the use of Makefile we can streamline a lot of redundant tasks in our projects without having to remember overwhelmingly long and varying commands. It increases the productivity of the whole team; with easier project setup and redundant tasks outsourced to the Makefile with intuitive target names, leaving the devs to focus on more serious tasks at hand.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=744ebbc69cc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Rolling and Rollback Deployments work in Kubernetes]]></title>
            <link>https://yankeexe.medium.com/how-rolling-and-rollback-deployments-work-in-kubernetes-8db4c4dce599?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8db4c4dce599</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[cloud-native]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Sun, 25 Oct 2020 08:18:40 GMT</pubDate>
            <atom:updated>2020-10-25T08:36:07.752Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KtnpIx1twobr16FP7hvAUg.png" /></figure><p>Kubernetes has been used heavily on production for the past few years. It offers a plethora of solutions for orchestrating your containers using its declarative API. One of the prominent feature of Kubernetes is its resilience with the ability to perform Rolling and Rollback Deployments.</p><h3>Primer</h3><h4>Deployments</h4><p>Deployment is one of the mechanisms for handling workloads (applications) in Kubernetes. It is managed by <a href="https://kubernetes.io/docs/concepts/workloads/controllers/">Kubernetes Deployment Controller</a>.</p><blockquote>In Kubernetes, controllers are control loops that watch the state of your cluster, then make or request changes where needed. Each controller tries to move the current cluster state closer to the desired state.</blockquote><p>In case of deployment here, the desired state we want to achieve is for the pods. Everything is declarative in K8s, so the desired state is written as a spec in Deployment manifest file.</p><pre># deployment.yaml<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: nginx-deployment<br>  labels:<br>    app: nginx<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      app: nginx<br>  template:<br>    metadata:<br>      labels:<br>        app: nginx<br>    spec:<br>      containers:<br>      - name: nginx<br>        image: nginx:1.14.2<br>        ports:<br>        - containerPort: 80</pre><p>If any of our pod instances should fail or update(a status change), the Kubernetes system responds to the difference between manifest spec and status by making a correction, i.e. matching the state of Deployment as defined on the spec.</p><h4>Deployment under the hood</h4><p>Deployment is an abstraction over ReplicaSet. Under the hood, Deployment creates a ReplicaSet which in turn creates pods on our cluster. As per the name, ReplicaSet is used for managing the replicas of our pods.</p><p>In summary, Controller reads the Deployment spec, forwards the pod configuration to ReplicaSet and then it creates the pods with proper replicas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Nk_o7OgLxap7YeoT2nchw.png" /><figcaption><strong>Deployment &gt; ReplicaSet &gt; Pods</strong></figcaption></figure><h4><strong>Rolling Deployment</strong></h4><p>Kubernetes promises zero down time and one of the reasons behind it is Rolling Deployments. With Rolling Deployments, Kubernetes makes sure that the traffic to the pods are not interrupted when updated pods are being deployed.</p><h4><strong>Rollback Deployment</strong></h4><p>Rollback Deployment means, going back to the previous instance of the deployment if there is some issue with the current deployment.</p><h3>Hands on 🙌</h3><p>Let’s get our hands-on with Kubernetes and see how these deployment strategies are carried out.</p><h4>Setup KinD</h4><p>We will setup a single node Kubernetes cluster on our local machine using KinD (Kubernetes in Docker). Make sure you have Docker running.</p><blockquote><a href="https://kind.sigs.k8s.io/docs/user/quick-start/">Install KinD</a></blockquote><p>We will create a K8s cluster using:</p><pre>$ kind create cluster</pre><p>With our cluster ready we are ready for Deployments.</p><p>For the purpose of this tutorial, we won’t be touching any YAML files and will be fully utilizing the power of kubectl CLI tool.</p><h4>Create Deployment</h4><p>Let’s create our first Deployment using thenginx image.</p><pre>$ kubectl create deployment test-nginx --image=nginx:1.18-alpine</pre><p>Like mentioned earlier, a Deployment creates a ReplicaSet followed by Pods. You can check these newly created resources using:</p><pre>$ kubectl get deploy,rs,po -l app=test-nginx</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*BPNB73wic-lm6ETxKBA_dQ.png" /><figcaption>List newly created Deployment, ReplicaSet and Pod</figcaption></figure><p>You can check to make sure the Replicaset was created by our Deployment using:</p><pre>$ kubectl describe rs &lt;replica-set-name&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1vXq7RDIyCOH50ZmK7VAdA.png" /><figcaption>Check ReplicaSet created by Deployment</figcaption></figure><p>You can do the same with Pods:</p><pre>$ kubectl describe po &lt;pod-name&gt; </pre><h4>Scale Deployment</h4><p>Let’s scale the deployment to have 3 instances of nginx pods.</p><pre>$ kubectl scale deploy test-nginx --replicas=3</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*t1jYzWl1JNy9S2vvG_xuZw.png" /><figcaption>Creating 3 replicas of nginx pods</figcaption></figure><p>Now that we have significant number of pods on our cluster. Let’s try out the Deployment strategies.</p><h4>Hands-on: Rolling Update Deployment 🍥⏩</h4><p>Let’s say you were having some issues with v18 of nginx and the v19 fixes it for you. You need to rollout a new update of nginx image to your pod.</p><p>By updating the image of the current pods (state change), Kubernetes will rollout a new Deployment.</p><pre>$ kubectl set image deploy test-nginx nginx=nginx:1.19-alpine</pre><p>After we set the new image, we can see the old pods getting terminated and new pods getting created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*Kuckqy4F8_SnYitb3U1Zcw.png" /><figcaption>Rolling update, new pod replacing old ones</figcaption></figure><p>We can see Kubernetes at work, making sure the pods are maintained properly. The last of the old pod doesn’t get terminated until the complete replicas for the new pods are created. The old pods also have a <strong><em>grace period</em></strong> which makes sure the traffic it is serving isn’t disconnected for certain time until the requests can be safely routed to the newly created pods.</p><p>We successfully updated all our pods to use nginx v19.</p><pre>$ kubectl describe deploy test-nginx</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*i_itv0eTq5kGpVxXRb8ejw.png" /><figcaption>Pods updated to nginx v19</figcaption></figure><h4>Hands-on: Rollback Deployment 🍥⏪</h4><p>Let’s assume the new nginx update has even more problems than the last one and now you realized how blissful life was with the old version. Time for a rollback to the previous version of nginx.</p><p><strong><em>But how do we do that?</em></strong> You might have noticed that there are now two ReplicaSets. It’s due to the same Deployment pattern we discussed earlier, we update our Deployment, it creates a new ReplicaSet which creates new Pods. Kubernetes holds history of up to 10 ReplicaSet by default, we can update that figure by using revisionHistoryLimit on our Deployment spec.</p><p>These history are tracked as rollouts. Only the latest rollout is active.</p><p>By now, we have made two changes to our Deployment test-nginx so the rollout history should be two.</p><pre>$ kubectl get rs </pre><pre>$ kubectl rollout history deploy test-nginx</pre><pre>$ kubectl rollout history deploy test-nginx --revision=1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*gOsETG1h7JZ_buPrFc_eSg.png" /><figcaption>Rollout history for test-nginx Deployment</figcaption></figure><p>Alright let’s get to rolling back our update to the previous rollout. We want to rollback to the stage where we were using nginx v18 which is rollout Revision 1.</p><pre>$ kubectl rollout undo deploy test-nginx --to-revision=1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*CBfbT_dXYdLw9lhtBGut4Q.png" /><figcaption>Rollout undo pod states</figcaption></figure><p>Like the Rolling Update Deployment, the Rollback Deployment terminates the current pods and replaces them with the pods containing the spec from Revision 1.</p><p>If you check the rollout history once again, you can see that the Revision 1 has been used to create the latest pods tagging it with Revision 3. There is no point in maintaining the same spec repeated for multiple revisions, so Kubernetes removes the Revision 1 since we have the latest Revision 3 of the same spec.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*3v4H7G4c7dVsCvPJUeow5Q.png" /><figcaption>Revision history after rollout undo</figcaption></figure><p>Now we are back on the nginx v18. with the Rollback Deployment. You can check that out by:</p><pre>$ kubectl describe deploy test-nginx</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cj3FfsLEdiB3IZyXxnRtGw.png" /><figcaption>Check rollback nginx version</figcaption></figure><h3>Conclusion 🚀</h3><p>Kubernetes makes it easy to control the Deployment of our applications with these strategies. This was just a rundown of how Rolling and Rollback Update Deployments work from a ground level. In real-life, we rarely do all these steps manually, since we hand it down to our CI/CD pipeline like <a href="https://argoproj.github.io/argo-cd/">ArgoCD</a>.</p><p>Thank you for reading. Hope this guide has been helpful for you to understand how Rolling and Rollback Deployments work in Kubernetes. If you have any corrections, suggestions, or feedback feel free to DM me on <a href="https://twitter.com/yankexe">Twitter</a> or comment down below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8db4c4dce599" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up multi-node Kubernetes cluster with K3s and Multipass]]></title>
            <link>https://yankeexe.medium.com/setting-up-multi-node-kubernetes-cluster-with-k3s-and-multipass-d4efed47fed5?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4efed47fed5</guid>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Sat, 24 Oct 2020 15:01:54 GMT</pubDate>
            <atom:updated>2020-11-14T19:16:56.222Z</atom:updated>
            <content:encoded><![CDATA[<h3>Setting up multi-node Kubernetes cluster locally with K3s and Multipass</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rj3ZXAY85tQ3PpeTkvNguw.png" /></figure><p>There are a lot of tools that allow you to setup a local Kubernetes cluster in no time. But with a full-blown K8s running on your local machine, you will soon hit a wall if you want to play with multi-node cluster.</p><p>For tackling this very issue, we will be looking into how we can setup a lightweight Kubernetes cluster using <a href="https://k3s.io/">K3s</a> and <a href="https://multipass.run/">Multipass</a> for our VMs.</p><blockquote>If you are interested in what makes K3s so light, you can watch the talk on <a href="https://www.youtube.com/watch?v=-HchRyqNtkU">K3s under the hood</a>.</blockquote><h3>Prerequisites</h3><h4>Installing Multipass</h4><p>Multipass is a command line tool to orchestrate virtual Ubuntu instances on your local machine. With the CLI, you can spawn VMs within minutes allocating optimal CPU, memory and disk space.</p><blockquote><a href="https://github.com/canonical/multipass#install-multipass"><strong><em>Download Multipass</em></strong></a></blockquote><h4>Installing K3sup (ketchup)</h4><p>We talked about K3s but it can be daunting to set up if you just want a quick hands on with Kubernetes. To abstract all that we will be installing a handy CLI called <a href="https://github.com/alexellis/k3sup">k3sup</a>. It will bootstrap Kubernetes cluster on our VMs using K3s within a minute! 🚀</p><p>k3sup uses SSH under the hood, so make sure you have SSH service on your machine.</p><blockquote><a href="https://github.com/alexellis/k3sup#download-k3sup-tldr">Install k3sup</a></blockquote><h3>Creating Virtual machines</h3><p>Nodes are just virtual machines on Kubernetes working in sync. For our cluster we will be creating 3 virtual machines, each with 1CPU, 1GB RAM, 2GB Storage.</p><h4>Generating keys</h4><p>If you have SSH installed and already have ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub , you can skip this part and move to <strong>creating config file </strong>section.</p><p>Create Private/Public key:</p><pre>$ ssh-keygen</pre><p>This will create the aforementioned files on your system. Copy the contents of ~/.ssh/id_rsa.pub</p><pre>$ cat ~/.ssh/id_rsa.pub</pre><h4><strong>Creating config file</strong></h4><p>Create a file called multipass.yaml and place your public key in ssh-rsa.</p><pre># multipass.txt<br>ssh_authorized_keys:</pre><pre>  - ssh-rsa &lt;add-your-public-key&gt;</pre><p>This config file makes sure the public key is stored on the virtual machine once it’s created. Let’s create our VMs with proper names based on their roles we will be assigning (master/worker).</p><pre>$ multipass launch --cpus 1 --mem 1G --disk 2G --name master-node --cloud-init multipass.yaml</pre><pre>$ multipass launch --cpus 1 --mem 1G --disk 2G --name agent-master --cloud-init multipass.yaml</pre><pre>$ multipass launch --cpus 1 --mem 1G --disk 2G --name agent-worker --cloud-init multipass.yaml</pre><pre>$ multipass ls</pre><p>In K3s terms, a master node is called the server and the rest of the nodes are called agents. Agents are simply the nodes that gets added to the master node; they can be another master node or a worker node.</p><h3>Adding K8s sauce with k3sup</h3><p>Now that we have our VMs ready, let’s install Kubernetes on them. First we will be creating a master node to setup a control plane.</p><h4>Adding a master node</h4><p>We will need the IP and username of our virtual machine to SSH into and install Kubernetes. Run multipass ls and take note of IP of master-node . All the usernames for VMs are ubuntu by default.</p><pre>$ k3sup install --ip &lt;IP&gt; --user ubuntu --k3s-extra-args &quot;--cluster-init&quot;</pre><p>We are passing the --k3s-extra-args &quot;--cluster-init&quot; to make sure this node is prepared to connect with another master node, else it might cause errors.</p><p>Once installed it downloads the kubeconfig file on the directory where you invoked your command. You can set environment variable KUBECONFIG with path to recently downloaded kubeconfig file.</p><pre># mac / linux<br>$ export KUBECONFIG=&lt;path-to-kubeconfig&gt;</pre><pre># windows<br>$ setx KUBECONFIG &lt;path-to-kubeconfig&gt;</pre><p>Now you can kubectl get nodes to view the nodes on your cluster.</p><h4>Adding a second master node (HA)</h4><p>Multi master setup on local machine is an overkill for most use cases but for the purpose of this tutorial we are going to add it anyway. To join a new master node in the cluster we use the join command.</p><p>Here, all we have to do is introduce the server IP and username (previously created) that this node should connect to . And additionally pass the --server flag specifying this will be a server node (master).</p><pre>$ k3sup install --ip &lt;IP-of-agent-master&gt; --user ubuntu --server-ip &lt;IP-of-master-node&gt; --server-user ubuntu --server</pre><p>And that’s about it. Now you can kubectl get nodes again, and you’ll see two master nodes that is Highly Available (HA).</p><h3>Adding a worker node</h3><p>Finally, we have to setup our worker node where we will be deploying our applications and services.</p><p>For this grab the IP of agent-worker and pass on the same command as before minus the --server flag.</p><pre>$ k3sup join --ip &lt;IP-of-agent-worker&gt; --user ubuntu --server-ip &lt;IP-of-master-node&gt; --server-user ubuntu</pre><p>Now if you kubectl get nodes , you will find two master nodes and a single worker node composing your multi node cluster setup. You can create new VMs and add more master or worker nodes depending on how you plan to utilize your cluster.</p><h3>Conclusion</h3><p>Thank you for reading. Hope this guide has been helpful for you to setup and play with multi-node Kubernetes cluster. If you have any corrections, suggestions, or feedback feel free to DM me on <a href="https://twitter.com/yankexe">Twitter</a> or comment below.</p><p>Happy hacking! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4efed47fed5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Git Stash Workflow]]></title>
            <link>https://yankeexe.medium.com/mastering-git-stash-workflow-a310313ca7b6?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a310313ca7b6</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Sun, 23 Aug 2020 11:49:10 GMT</pubDate>
            <atom:updated>2022-10-19T03:34:01.552Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fb96QiQdGKuCXSHDL7ZCzA.png" /></figure><p>Git is a powerful tool that makes up for a lot of use cases on our development workflow. One such case is to isolate the changes of a certain branch to itself. Let me elaborate.</p><p>Suppose you are working on a branch called admin-dashboard implementing ,well, an administrative dashboard. But you are not done yet, and the project manager wants a quickfix for the login implementation. Now you want to switch to the login branch and fix the issue but don’t want to carry the changes you are doing on the admin-dashboard branch. Well this is where git stash comes in.</p><h4>Git stash what?</h4><p>Git stash allows you to quickly put aside your modified changes to a LIFO stack and re-apply them when feasible. We will be doing a rough walkthrough to see this in action.</p><h3>Git stash walkthrough 🚶‍♂️</h3><p>Initiate git on an empty directory. Add a file named add.py and put the following code.</p><pre># add.py </pre><pre>def add(a, b):</pre><pre>  return a + b</pre><p>Let’s add the file to git and commit it .</p><pre>git add add.py &amp;&amp; git commit -m &quot;Add function&quot;</pre><p>Next let’s create and checkout to a new branch called mul and create a file called mul.py</p><pre>git checkout -b mul</pre><p>Add the following code to the file.</p><pre># mul.py </pre><pre>def mul(a, b): <br>    return a * b</pre><p>Add the file to git and commit it.</p><pre>git add mul &amp;&amp; git commit -m &quot;Mul function&quot;</pre><p>Now suppose, we need to update the mul function to take in a third argument and you just edited the function like so:</p><pre># mul.py</pre><pre>def mul(a, b, c): <br>    return a * b </pre><p>Take note that, we still haven’t update the return value with c. While we were making our changes, the project manager called-in to update the add function immediately with a third argument. Now you can’t waste a second on the mul function which you haven’t completed. <strong>What are you going to do?</strong></p><p>If you try to checkout to master where add function resides, git won’t let you because you have unfinished changes that hasn’t been committed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sm1VcF-XIR9CZKJRrd9V0w.gif" /></figure><h4>Stashing Changes 📤</h4><p>Well this is the situation you should be using the git stash command. We want to put away the changes on our current branch so that we can come back to it later.</p><p>Stash the file with a message on the mul branch.</p><pre>git stash save &quot;Multiply function&quot;</pre><p>Now if you git status, the working directory will be clean and you can jump into the master branch for the changes. We can view the items in our stash using git stash list . We can view the diff of the items on our stack using git stash show</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1w-UUFmluedhcbGROyvi2A.gif" /></figure><p>Now you are in master branch modifying the add function, and comes an even higher priority job to include a subtraction function.</p><blockquote><strong>Note</strong>: <br>Project Managers in real life doesn’t put forward tasks on this manner.</blockquote><p>Your addfunction looks pretty much like the mul function now.</p><pre># add.py </pre><pre>def add(a, b, c): <br>    return a + b   # couldn&#39;t include `c` due to a priority task.</pre><p>Stash it with a message.</p><pre>git stash save &quot;Add function third argument&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JPNqrbe449Konsoxrdg3-g.gif" /></figure><p>Now let’s suppose we have completed our task for subtraction branch and we want to continue working on other branches.</p><p>Let’s move to the master branch first to complete our changes for addition.</p><p>There are two ways we can re-apply those changes:</p><ol><li>git stash pop <br>applies the top most change stored on the stack and removes it from the stack.</li><li>git stash apply <br>applies the stash based on the index provided, keeps the applied item intact on the stack.</li></ol><h4>Popping stashed changes 🍾</h4><p>Like I mentioned earlier, stash follows the LIFO convention. The latest stash we save are always on the top. And when we use pop, always the top most change is applied to the current branch. Run the following command on master.</p><pre>git stash pop</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rL3Sk-m74A8v-GYGbxKkOg.gif" /></figure><p>Complete the add function and commit it.</p><h4>Applying stashed changes</h4><p>Next, checkout mul branch. We can use pop here as well since there is only on item remaining on the stack. But let’s see how git stash apply works.</p><pre>git stash apply </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VI7FJ2kNfy4_KWZCQ5WGVA.gif" /></figure><p>When we apply from the stash, the item still remains on the stack. <br>Complete the changes here and commit it.</p><h4>Create a new branch with stashed changes</h4><p>Let’s do something fun. Let’s say we want to add a divide function on a new branch. Well it is kind of similar to our multiple function so why not utilize the item in stash and create a divide function with it?</p><p>We can do that with the git stash branch command. It takes the &lt;item-id&gt; and a branch name, then <strong><em>applies</em></strong> those changes to that branch.</p><pre>git stash branch &lt;branch-name&gt; &lt;item-id&gt;</pre><pre>git stash branch divide stash@{0}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ROehknP7W7-a8v6Rdtr_sQ.gif" /></figure><p>Now we can rename the file and change the function to perform division.</p><h4>View the stashed changes</h4><p>Sometimes we tend to forget what changes we stashed. What we forget can range from which files were stashed to what changes on the files were stashed.</p><p>To view a list of files that were stashed, we can run:</p><pre># EXAMPLE:<br># git stash show stash@{&lt;stash-id&gt;}</pre><pre>git stash show stash@{2}</pre><p>To get a diff view of changes on the files that were stashed, we can run:</p><pre># EXAMPLE:<br># git stash show -p stash@{&lt;stash-id&gt;}</pre><pre>git stash show -p stash@{2}</pre><h4>Clearing the stack</h4><p>Now that the stash has served its purpose, we can clear it. There is again two ways of doing it:</p><ol><li>git stash clear<br>wipes the whole stack clean.</li><li>git stash drop &lt;item-id&gt; <br>removes the item from stack based on provided id.</li></ol><h4>Conclusion 🚀</h4><p>Git stash is a powerful tool that comes handy in many situations. Hope this article helped you in understanding and implementing the concept in your project. If you have any suggestions or feedback, let’s talk on the comment section or you can <a href="https://twitter.com/yankexe">@ me on Twitter</a>.</p><p>Follow me on GitHub:</p><p><a href="https://github.com/yankeexe">yankeexe - Overview</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a310313ca7b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy your Serverless Python function locally with OpenFaas in Kubernetes]]></title>
            <link>https://yankeexe.medium.com/deploy-your-serverless-python-function-locally-with-openfaas-in-kubernetes-c3b564e6fd18?source=rss-348042aca5a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3b564e6fd18</guid>
            <category><![CDATA[openfaas]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[serverless]]></category>
            <dc:creator><![CDATA[Yankee Maharjan]]></dc:creator>
            <pubDate>Sat, 15 Aug 2020 10:05:11 GMT</pubDate>
            <atom:updated>2021-04-29T11:00:45.545Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gvNE0XXULup6rw6FQlQo1g.jpeg" /></figure><p>We have come a long way in building distributed applications. From monoliths to Microservices we have been able to decouple and scale our applications with optimal use of the underlying hardware resources. But now we are moving towards more lightweight approach to deploy our applications; <strong>Serverless</strong>! With the inception of Function as a Service (FaaS), application modularization has been taking an ascent in the world of cloud computing.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F3JpnyF0agxY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3JpnyF0agxY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F3JpnyF0agxY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/df34bb644a3ef4b0741be4b0ae77bfb9/href">https://medium.com/media/df34bb644a3ef4b0741be4b0ae77bfb9/href</a></iframe><p>In this blog, we will be exploring development to deployment of our Serverless function locally.</p><blockquote>The beauty of OpenFaas and KinD is that you don’t need to deploy your functions to cloud to test them, you can run them locally!</blockquote><h4>Prerequisite:</h4><p>You just need to have <strong>Docker</strong> installed on your machine. The rest we will setup as we go along. We will be installing a few tools so bear with me. 🐻</p><h4><a href="https://www.openfaas.com/">OpenFaas</a>? 🐬</h4><p>OpenFaas is an awesome tool/framework developed by <a href="https://medium.com/u/89b4ba5a32ff">Alex Ellis</a> with the Open Source community that helps to deploy burstable functions and microservices to Kubernetes without repetitive, boiler-plate coding.</p><p>It was built with developer experience in mind creating a low barrier of entry to the Kubernetes ecosystem. It comes with it’s own cli-tool called the faas-cli which makes a clever use of templates and makes pushing docker images and deployment of containers a breeze. And once deployed it takes care of auto-scaling and auto-provisioning based on the network traffic.</p><p>With all the overhead being handled by OpenFaas, all you have to do is focus on writing your function. But in our case we have some setting up to do 👨‍🏭</p><h4>Properties of functions:</h4><p>Here are few things to keep in mind when you are creating a function.</p><ul><li>A rule of thumb is to make your function stateless.</li><li>Function should be atomic (single responsibility).</li><li>It should be idempotent.</li><li>Trigger to the function could be TCP or event, however effect must be an event.</li></ul><h3>Let’s get started! 🚀</h3><h4>Install arkade</h4><p><a href="https://github.com/alexellis/arkade">arkade</a> will be our one stop tool to install and deploy apps and services to Kubernetes. Think of this tool as a package manager like apt or pacman . We will be needing a bunch of CLI utilities to get up and running.</p><p>Make sure you install this if you have never set up a Kubernetes tooling in your system.</p><pre>$ curl -sLS <a href="https://dl.get-arkade.dev/">https://dl.get-arkade.dev</a> | sudo sh</pre><p><strong>Just to make things clear:</strong></p><ul><li>use arkade get to download cli tools and applications related to Kubernetes.</li><li>use arkade install to install Kubernetes applications using <a href="https://helm.sh/docs/topics/charts/">helm charts</a> or vanillayamlfiles.</li></ul><h4><strong>Install kubectl</strong></h4><p><a href="https://kubernetes.io/docs/reference/kubectl/overview/">kubectl</a> is a command line tool that talks to the Kubernetes Master Controller API for performing actions on our cluster.</p><pre>$ arkade get kubectl</pre><pre># follow the instructions on the screen.</pre><h3>Creating a local Kubernetes cluster 🧱</h3><p>We will be deploying our function on a local Kubernetes cluster so let’s set that up. We will be using a tool called <strong>KinD(</strong>Kubernetes in Docker<strong>) </strong>for this.</p><p>Unlike <a href="https://github.com/kubernetes/minikube">Minikube</a> which requires VM; KinD utilizes Docker container to deploy your cluster. It is comparatively faster.</p><h4>Install KinD</h4><p>Our goal is to keep everything local so we will be creating a local Docker registry (<a href="https://hub.docker.com/">Dockerhub</a> for your local machine). KinD provides a <a href="https://kind.sigs.k8s.io/docs/user/local-registry/">shell script</a> to create a Kubernetes cluster along with local Docker registry enabled.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fce978b58c0978b1f056567bae6cb81/href">https://medium.com/media/6fce978b58c0978b1f056567bae6cb81/href</a></iframe><p>Once you have the file on your local machine. Run the following command to make it executable.</p><pre>$ chmod +x kind-with-registry.sh</pre><p>Now you can run the script to create your local Kubernetes cluster with local Docker registry.</p><pre>$ ./kind-with-registry.sh</pre><p>To make sure the kubectlcontext is set to the newly created cluster; run:</p><pre>$ kubectl config current-context</pre><p>If the result is not kind-kind ; then run:</p><pre>$ kubectl config use kind-kind</pre><p>Make sure your cluster is running:</p><pre>$ kubectl cluster-info</pre><p>Make sure Docker registry is running.</p><pre>$ docker logs -f kind-registry<br>OR <br>$ docker ps -l</pre><h4>Deploying OpenFaas to KinD cluster</h4><p>Now that we have our local Kubernetes cluster up and running lets deploy our OpenFaas services to support our functions.</p><pre>$ arkade install openfaas</pre><p>Make sure OpenFaas is deployed. It might take a minute or two.</p><pre>$ kubectl get pods -n openfaas</pre><h4><a href="https://docs.openfaas.com/cli/templates/#templates">Templates</a>📂</h4><p>Before we move to creating our function, it is essential to understand the concept of templates in OpenFaas.</p><p><strong>What are templates?</strong></p><p>Templates are basically a wrapper for your functions. Same template can be used for multiple functions.</p><p>OpenFaas already provides a variety of templates for different programming languages to start with.</p><p>There are two type of templates based on the webserver :</p><ol><li><strong>classic watchdog</strong> which uses template format from <a href="https://github.com/openfaas/templates">here</a>.</li><li><strong>of-watchdog</strong> which uses template format from <a href="https://github.com/openfaas-incubator?q=template&amp;type=&amp;language=">here</a>.</li></ol><p>Watchdogs are basically webservers to proxy request to our functions.</p><p>We will be using the templates that supports the latest of-watchdog . You can read more about <a href="https://docs.openfaas.com/architecture/watchdog/">OpenFaas watchdog</a>.</p><p>The most important piece in the templates directory is the Dockerfile, it drives everything from your watchdog to how and where your function gets placed in the template. You can also create your own template based on the need of your function.</p><p>Pull the template from store:</p><pre>$ faas-cli template store pull python3-flask</pre><p>This will pull all the templates for python from OpenFaas store to your <strong>template</strong> directory of your project folder. We will be using the <a href="https://github.com/openfaas-incubator/python-flask-template/tree/master/template/python3-flask-debian">python3-flask-debian</a> template. You can ignore rest of the templates or delete them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-eT29RL_snrWai59dry6g.png" /><figcaption>Openfaas template structure.</figcaption></figure><p>Shown above is the basic template structure for all languages.</p><p><strong>Template structure explanation:</strong></p><ol><li>index.py is an entrypoint to our Flask application.</li><li>requirements.txt on the root project directory contains dependencies for our Flask application. It contains <a href="https://flask.palletsprojects.com/en/1.1.x/">Flask</a> and <a href="https://docs.pylonsproject.org/projects/waitress/en/latest/">waitress</a> (WSGI).</li><li>template.yml contains the deployment specific configurations.</li><li>requirements.txt inside of the function directory is for the function specific dependencies.</li><li>Dockerfile contains the instructions to build image for our function.</li></ol><h4>Building our function</h4><p>Now it’s time to create our Serverless function.</p><p>We’ll keep the scope really small and build a simple dictionary function that returns the meaning of words you query. For this we will be using the <a href="https://pypi.org/project/PyDictionary/">PyDictionary</a> module.</p><p>Since we have our template in place, we can utilize it to scaffold our function. You may have noticed the <strong><em>function</em></strong> directory and <strong><em>template.yml file </em></strong>inside our python3-flask-debiantemplate folder earlier. We can leverage that to create our new function using the faas-cli.</p><pre>$ faas-cli new &lt;function-name&gt; --lang &lt;template-name&gt;</pre><pre>$ faas-cli new pydict --lang python3-flask-debian</pre><p>This will create a folder with the function name (pydict)along with the contents we saw earlier inside the templates function folder, plus a yaml file with the function name (pydict).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*4kwHYzSifJlsqmqIIHzMCg.png" /></figure><p>Let’s add our dependency to the requirements.txt file of function folder.</p><pre><strong>PyDictionary==2.0.1</strong></pre><p>Update the handler.py with the following code.</p><pre>from PyDictionary import PyDictionary</pre><pre>dictionary = PyDictionary()</pre><pre>def handle(word):     <br>    return dictionary.meaning(word)</pre><p>Our function is complete. ✔</p><h4>Openfaas YAML configuration 📄</h4><p>A Kubernetes guide won’t be complete without talking about YAML files. They are necessary to describe the state of our deployments. faas-cli has already created a yaml file for our function, namely, pydict.yml</p><p>This file will tell the faas-cliabout the functions and images we want to deploy, language templates we want to use, scaling factor, and other configurations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ZDh8rEB2abV6eQqZuJlhQ.png" /><figcaption>contents of pydict.yml</figcaption></figure><h4>YAML file breakdown</h4><ol><li>gateway determines the IP address to connect to the OpenFaas service. Since we are testing it locally the gateway provided by default is fine. <br><strong>Note:</strong> if we were deploying on managed cloud: we will take the IP address of gateway-external</li><li>Under functions we can register different Serverless functions we want to deploy along with their configuration.</li><li>lang name of the language template the function uses.</li><li>handler location of our Serverless function.</li><li>image fully qualified docker registry URL along with image name and tag.</li></ol><p>Since we are using Local registry we have to change the image tag to incorporate it. Change value of image to the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ex99mjBGUJ048DIHpkyBMw.png" /><figcaption>Change image tag to use local container registry</figcaption></figure><h4>Port-forward to Localhost ⏩</h4><p>Now, we need to port-forward the OpenFaas gateway service to our localhost port. Remember the gateway on our yaml file?</p><p>To check the gateway of OpenFaas service; run:</p><pre>$ kubectl get service -n openfaas</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uj9C0YMr_2_dc7a3_mcy-Q.png" /></figure><pre>$ kubectl port-forward -n openfaas svc/gateway 8080:8080</pre><p>Although the the cluster is deployed locally, internally Kubernetes manages it’s own IP addresses. We are port forwarding to access the service inside the Kubernetes cluster from our local machine. Once that is done, open a new terminal window, and prepare for take off!</p><h4>Build ➡Push ➡Deploy 🚀</h4><p>We have most of the setup ready; now we can build our image, push it to the registry and deploy it to Kubernetes.</p><p><strong>Build our image</strong></p><pre>faas-cli build -f pydict.yml</pre><p><strong>Push our image to the local registry</strong></p><pre>faas-cli push -f pydict.yml</pre><p>Now to deploy the function to the cluster; we need to login to faas-cli Let’s generate credentials for authentication.</p><p><strong>Generate password:</strong></p><pre>$ PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath=&quot;{.data.basic-auth-password}&quot; | base64 --decode; echo)</pre><p><strong>Check if password exists:</strong></p><pre>$ env | grep PASSWORD</pre><p>Now, login to faas-cli :</p><pre>$ echo -n $PASSWORD | faas-cli login --username admin --password-stdin</pre><p><strong>Time for deployment:</strong></p><pre>faas-cli deploy -f pydict.yml -g http://127.0.0.1:8080</pre><p>Check if our deployment has been successful.</p><pre>kubectl get pods -n openfaas-fn</pre><h3>Testing our function 👨‍🔬</h3><h4>Test using CLI</h4><p>We can invoke our function from CL I to get the result.</p><pre>$ echo &quot;brevity&quot; | faas-cli invoke pydict</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*T_A_aLiJ65CPOCqd5y0d1A.png" /></figure><h4>Test using Openfaas Portal</h4><p>OpenFaas also comes with a neat UI portal to invoke our function.</p><p>Navigate to localhost:8080 in your browser. A prompt will ask you for username and password. Use <strong><em>admin </em></strong>for<strong> </strong>username and for password check the password we generated earlier. In case you forgot, you can always repeat the earlier command, i.e.</p><pre>PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath=&quot;{.data.basic-auth-password}&quot; | base64 --decode; echo) &amp;&amp; echo $PASSWORD</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pSG_9cokxgEucPhJW6ZdAQ.png" /><figcaption>OpenFaas UI</figcaption></figure><h4>Build more functions 🔨</h4><p>Now that you have your cluster deployed with OpenFaas, you can create function with ease and deploy them. If you want to create a new function, all you have to do is:</p><ol><li>scaffold a new function using faas-cli new</li><li>write your function</li><li>faas-cli up -f &lt;config-yaml-file&gt;</li></ol><p>faas-cli up is a shortcut for build, push and deploy command.</p><h3>Conclusion</h3><p>Hope this has been a helpful guide in learning how to use OpenFaas for Serverless deployment. If you have any queries or suggestions, let’s talk in the comment section or you can <a href="https://twitter.com/yankexe">DM me on Twitter</a>.</p><h4>Checkout some example functions on GitHub:</h4><p><a href="https://github.com/yankeexe/openfaas-functions">yankeexe/openfaas-functions</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3b564e6fd18" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>