<?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 Saj Sah on Medium]]></title>
        <description><![CDATA[Stories by Saj Sah on Medium]]></description>
        <link>https://medium.com/@cloudwithsaj?source=rss-ce0bdf3df949------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*rTOAP9LX_6vuwzRX</url>
            <title>Stories by Saj Sah on Medium</title>
            <link>https://medium.com/@cloudwithsaj?source=rss-ce0bdf3df949------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 03:09:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cloudwithsaj/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[Gitflow branching strategy]]></title>
            <link>https://medium.com/@cloudwithsaj/gitflow-branching-strategy-297192164c3c?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/297192164c3c</guid>
            <category><![CDATA[gitflow]]></category>
            <category><![CDATA[gitflow-workflow]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Thu, 11 Jun 2026 09:13:09 GMT</pubDate>
            <atom:updated>2026-06-11T09:13:09.823Z</atom:updated>
            <content:encoded><![CDATA[<p>Gitflow branching strategy</p><h3>Why Do We Need Gitflow Branching Strategy?</h3><p>When teams are small (1–2 devs), you <em>can survive</em> on “commit to main” 😅.<br>But when you grow to <strong>5, 10, 50+ developers</strong>, <strong>multiple features</strong>, <strong>urgent fixes</strong>, <strong>releases happening in parallel</strong> — <strong>chaos starts</strong>.<br>If you don’t have a strong branching strategy:</p><ul><li>🚨 <strong>Hotfixes</strong> will collide with feature releases.</li><li>🐛 <strong>Bug fixes</strong> might accidentally go to production unfinished.</li><li>🚀 <strong>Feature delivery</strong> slows down because everyone fights over merge conflicts.</li><li>😵‍💫 <strong>Release management</strong> becomes hell: what should go live, what should not?</li></ul><p><strong>Gitflow</strong> brings <strong>discipline</strong> and <strong>clear separation</strong> between:</p><p>TypePurposefeature/*New features safely developed in isolationbugfix/*Bug fixes worked separatelyhotfix/*Urgent production patchesrelease/*Prepares a stable version for productiondevelopActive development aggregationmainProduction-ready code only</p><blockquote><strong>PS</strong>: Gitflow helps teams <strong>move fast</strong>, <strong>fix fast</strong>, and <strong>release clean</strong> without breaking each other.</blockquote><h3>GitFlow Overview:</h3><p>👨‍💻 Developer commits feature to a feature/ branch — Work safely without touching critical code.</p><p>🚀 Open PR from feature/ to develop — Merge feature after review and testing.</p><p>🛠️ Bug found? Create bugfix/ branch — Fix and merge back cleanly to develop.</p><p>🚨 Critical issue in production? Start hotfix/ from main — Patch immediately without waiting for releases.</p><p>🔁 Merge hotfix back to both main and develop — Production is saved, development stays updated.</p><p>📦 Ready to release? Create a release/ branch — Prepare a clean, tested production version.</p><p>✅ Open PR from release/ to main after tests pass — Deploy stable code to production.</p><p>🔄 Merge release/ back to develop too — Keep develop updated with latest stable changes.</p><h3>Now, let’s discuss each component in detail as a use case with the required git commands:</h3><h3>👨‍💻 Developer Commits Feature to feature/ Branch</h3><p><strong>Action:</strong> Create a new feature branch from develop.</p><pre>git checkout develop<br># Always pull latest develop<br>git pull origin develop<br># Create new feature branch<br>git checkout -b feature/awesome-feature</pre><p><strong>Manual Step:</strong> Develop new feature locally, commit changes.</p><pre>git add .<br>git commit -m &quot;feat: add new feature&quot;</pre><h3>🚀 Open PR from feature/ to develop</h3><p><strong>Action:</strong> Push branch and open Pull Request (PR).</p><pre>git push origin feature/new feature</pre><p><strong>Manual Step:</strong></p><ul><li>Go to GitHub/GitLab/Bitbucket.</li><li>Open a PR: <strong>feature/awesome-feature → develop</strong>.</li><li>Request code reviews.</li></ul><h3>🛠️ Bug Found? Create bugfix/ Branch</h3><p><strong>Action:</strong> Branch off from develop.</p><pre>git checkout develop<br>git pull origin develop<br>git checkout -b bugfix/login-issue</pre><p><strong>Manual Step:</strong></p><ul><li>Fix the bug.</li><li>Commit the fix.</li></ul><pre>git add .<br>git commit -m &quot;fix: resolve login issue&quot;</pre><p>Push and open PR.</p><pre>git push origin bugfix/login-issue</pre><p>Open PR: <strong>bugfix/login-issue → develop</strong>.</p><h3>🚨 Critical Issue in Production? Start hotfix/ from main</h3><p><strong>Action:</strong> Branch from main immediately.</p><pre>git checkout main<br>git pull origin main<br>git checkout -b hotfix/urgent-patch</pre><p><strong>Manual Step:</strong></p><ul><li>Patch the urgent issue.</li><li>Commit your patch.</li></ul><pre>git add .<br>git commit -m &quot;hotfix: urgent patch for production&quot;</pre><p>Push and open PR.</p><pre>git push origin hotfix/urgent-patch</pre><p>Open PR: <strong>hotfix/urgent-patch → main</strong>.</p><h3>🔁 Merge Hotfix back to both main and develop</h3><p><strong>Action:</strong></p><ul><li>After hotfix PR is merged into main, also merge it into develop.</li></ul><pre>git checkout develop<br>git pull origin develop<br>git merge main</pre><p><strong>Manual Step:</strong></p><ul><li>Resolve conflicts (if any).</li><li>Push updated develop branch.</li></ul><pre>git push origin develop</pre><h3>📦 Ready to Release? Create a release/ Branch</h3><p><strong>Action:</strong> From develop, cut a release branch.</p><pre>git checkout develop<br>git pull origin develop<br>git checkout -b release/v1.0</pre><p><strong>Manual Step:</strong></p><ul><li>Finalize version numbers.</li><li>Final testing, bugfixes.</li><li>Commit if needed.</li></ul><pre>git commit -am &quot;prepare release v1.0&quot;</pre><p>Push branch:</p><pre>git push origin release/v1.0</pre><p>Open PR: <strong>release/v1.0 → main</strong>.</p><h3>✅ Open PR from release/ to main After Tests Pass</h3><p><strong>Manual Step:</strong></p><ul><li>Review and approve PR.</li><li>Merge into main for production deployment.</li></ul><p>Deploy production manually or trigger CI/CD pipelines.</p><h3>🔄 Merge release/ Back to develop Too</h3><p><strong>Action:</strong> Ensure develop branch gets the final release changes too.</p><pre>git checkout develop<br>git pull origin develop<br>git merge release/v1.0</pre><p><strong>Manual Step:</strong></p><ul><li>Resolve conflicts if any.</li><li>Push updated develop.</li></ul><pre>git push origin develop</pre><p>📋 <strong>Few</strong> <strong>Things to Consider</strong></p><p>⚙️ <strong>Automation Pipelines</strong><br>Set up automated pipelines for each environment — from build to deployment — triggered by branch updates (e.g., auto-deploy to test environment when merged into develop).</p><p>🏷️ <strong>Branch Naming Conventions</strong><br>Follow a strict naming convention for branches like feature/*, release/*, and hotfix/* to maintain clarity and consistency.</p><p>🤝 <strong>Collaboration Guidelines</strong><br>Define clear merging rules. Pull Requests (PRs) must be reviewed, and important branches such as main and release/* should have protection rules to prevent direct changes.</p><p>🧪 <strong>Testing Strategies</strong><br>Incorporate testing at every stage. Feature branches should include unit tests, while integration and end-to-end tests should be done after merging into develop or release/*.</p><p><strong>🔥 Key Points:</strong></p><ul><li><strong>Always pull latest before creating branches!</strong></li><li><strong>Use semantic, clear branch names.</strong></li><li><strong>Hotfixes must go to both </strong><strong>main and </strong><strong>develop.</strong></li><li><strong>PR reviews are mandatory to maintain code quality.</strong></li></ul><h3>🧠 Final Thought</h3><p>Gitflow is not just a workflow; it’s a way to bring structure, speed, and safety into your development lifecycle. When applied with discipline, it saves teams from chaos and accelerates delivery. Master it once, and it will become second nature to build, fix, and deploy with confidence.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=297192164c3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[file creation, permission management, and variable handling.]]></title>
            <link>https://medium.com/@cloudwithsaj/file-creation-permission-management-and-variable-handling-ef7a3fac9e90?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef7a3fac9e90</guid>
            <category><![CDATA[bash-script]]></category>
            <category><![CDATA[bash]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Sun, 04 Jan 2026 09:53:08 GMT</pubDate>
            <atom:updated>2026-01-04T09:53:08.503Z</atom:updated>
            <content:encoded><![CDATA[<p>file creation, permission management, and variable handling. Here are the detailed notes along with example code snippets:</p><ol><li><strong>File Creation</strong>:</li></ol><ul><li>We learned to create files with the touch command:</li></ul><pre>touch hello.txt</pre><p>To rename a file, we used:</p><pre>mv hello.txt hello.h</pre><p><strong>Setting Permissions</strong>:</p><ul><li>We discussed how to set file permissions using chmod. For instance, to make a file executable only for the owner:</li></ul><pre>chmod 700 hello.h</pre><p>This grants the owner read, write, and execute permissions, while others have no access.</p><ol><li><strong>User Input</strong>:</li></ol><ul><li>Utilizing the read command to capture user input dynamically:</li></ul><pre>read -p &quot;Enter file name: &quot; name<br>touch &quot;$name.txt&quot;</pre><p>This creates a new file based on user input.</p><ol><li><strong>String Manipulation</strong>:</li></ol><ul><li>Demonstrated string concatenation when creating file names:</li></ul><pre>file_name=&quot;_${name}.txt&quot;<br>touch &quot;$file_name&quot;Capturing Current Date and Time:</pre><ul><li>To retrieve the current date and time:</li></ul><pre>important_time=$(date)<br>echo &quot;Current Date and Time: $important_time&quot;</pre><ol><li><strong>Static vs. Dynamic Variables</strong>:</li></ol><ul><li>important_time is static, retaining the value at the moment of assignment:</li></ul><pre>echo &quot;Static Time: $important_time&quot;</pre><p>For a dynamic variable that retrieves the current date each time:</p><pre>dd=$(date)<br>echo &quot;Dynamic Time: $dd&quot;</pre><p><strong>Variable Scope</strong>:</p><ul><li>It’s important to note the differences in variable scopes, which affect how you retrieve and utilize these values in scripts.</li></ul><p>These concepts form the foundation of effective shell scripting, enabling users to manipulate files and handle variables efficiently.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef7a3fac9e90" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing Your First Shell Script: A Beginner-Friendly Guide]]></title>
            <link>https://medium.com/@cloudwithsaj/writing-your-first-shell-script-a-beginner-friendly-guide-739b7c103eea?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/739b7c103eea</guid>
            <category><![CDATA[bash-script]]></category>
            <category><![CDATA[bash-scripting]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Sat, 03 Jan 2026 20:47:43 GMT</pubDate>
            <atom:updated>2026-01-03T20:47:43.893Z</atom:updated>
            <content:encoded><![CDATA[<p>Everyone remembers their first “Hello World” moment. In this article, we’ll create our <strong>very first shell script</strong>, understand <em>why</em> it works, and learn how Linux knows how to run it.</p><p>Don’t worry if you’re new — this is written for total beginners.</p><h3>Step 1: Creating the Script File</h3><p>Let’s start by creating a simple file. Open your terminal and type:</p><pre>touch myscript.txt</pre><p>For now, we’re just creating a plain text file. Later, we’ll talk about better file extensions for scripts, but this is perfectly fine to begin with.</p><p>Think of this file as an <strong>empty notebook</strong> — we haven’t written any instructions yet.</p><h3>Step 2: What Makes a Script Executable?</h3><p>Our goal is simple:</p><p>👉 When we run the file, it should print:</p><pre>Hello, this is my script</pre><p>But here’s an important question:</p><p><strong>How does the system know how to read what we write inside this file?</strong></p><p>The terminal doesn’t magically know whether your script is written in:</p><ul><li>Bash</li><li>Python</li><li>PHP<br> or something else.</li></ul><p>So we must clearly tell it <strong>which program should interpret the file</strong>.</p><h3>Step 3: Introducing the Shebang (#!)</h3><p>The very first line of a script is special. It tells the system <em>which program to use</em> to run the script.</p><p>This line starts with:</p><pre>#!</pre><p>This combination is called the <strong>shebang</strong>.</p><p>After the shebang, we write the <strong>full path</strong> of the program that should interpret the script.</p><h3>Step 4: Choosing the Right Interpreter (Bash)</h3><p>We are writing a <strong>shell script</strong>, so we want the Bash shell to interpret our commands.</p><p>To find where Bash lives on your system, use:</p><pre>which bash</pre><p>You’ll likely see:</p><pre>/bin/bash</pre><p>That’s the path we need.</p><h3>Step 5: Writing the Script</h3><p>Now open the file using an editor:</p><pre>nano myscript.txt</pre><p>Inside the file, write:</p><pre>#!/bin/bash</pre><pre># This is my first shell script<br>echo &quot;Hello, this is my script&quot;</pre><h3>What’s happening here?</h3><ul><li>#!/bin/bash → tells the system to use Bash</li><li># → starts a comment (ignored by the program)</li><li>echo → prints text to the screen</li></ul><p>Save the file (CTRL + X, then Y, then Enter).</p><h3>Step 6: Making the Script Executable</h3><p>Right now, the file exists — but it can’t be <em>run</em> yet.</p><p>To make it executable, use:</p><pre>chmod +x myscript.txt</pre><p>This is like giving the file <strong>permission to run</strong>, similar to installing an app before opening it.</p><h3>Step 7: Running Your First Script 🎉</h3><p>Now we’re ready.</p><p>Run the script with:</p><pre>./myscript.txt</pre><p>Output:</p><pre>Hello, this is my script</pre><p>🎉 Congratulations! You just ran your first shell script.</p><h3>Step 8: Editing and Improving the Script</h3><p>Scripts are easy to change. Open it again:</p><pre>nano myscript.txt</pre><p>Add another line:</p><pre>echo &quot;I am learning shell scripting!&quot;</pre><p>Save and run it again:</p><pre>./myscript.txt</pre><p>You’ll see the updated output immediately.</p><p>When starting with shell scripting, it’s easy to overlook small details — like file extensions. At first glance, they may seem unimportant, but choosing the right extension can improve your workflow and make scripting much more pleasant.</p><p>Let’s explore why.</p><h3>From .txt to .sh</h3><p>Earlier, we created our script as a simple text file:</p><pre>myscript.txt</pre><p>This works, but it’s <strong>not ideal</strong>.</p><p>Why?</p><p>Because .txt files are treated as plain text by the system and editors. They don’t understand that this file contains shell commands.</p><p>Since this is a <strong>shell script</strong>, the most appropriate extension is:</p><pre>.sh</pre><p>So let’s rename the file:</p><pre>mv myscript.txt myscript.sh</pre><p>Now the file clearly tells both humans and tools:</p><blockquote><em>“This is a shell script.”</em></blockquote><h3>Why File Extensions Matter</h3><p>Changing the extension doesn’t affect how Linux executes the script — but it <strong>does affect how editors handle it</strong>.</p><p>Once we rename the file to .sh, something nice happens:</p><ul><li>Editors recognize it as a shell script</li><li>Keywords like echo, pwd, and bash are highlighted</li><li>Syntax becomes easier to read</li><li>Errors are easier to spot</li></ul><p>This is called <strong>syntax highlighting</strong>, and it makes a huge difference when learning.</p><h3>Use a Proper Code Editor</h3><p>While you <em>can</em> edit scripts in a basic text editor, it’s not recommended.</p><p>Good editors for shell scripting include:</p><ul><li>Sublime Text</li><li>TextMate</li><li>VS Code</li><li>Vim</li><li>Brackets</li></ul><p>These editors are designed for programming. They:</p><ul><li>Highlight keywords</li><li>Auto-complete commands</li><li>Match brackets automatically</li><li>Improve readability</li></ul><h3>Opening Files on Linux (Not macOS)</h3><p>If you’re using Linux (not macOS), the command to open files or folders is:</p><pre>xdg-open myscript.sh</pre><p>The system chooses the appropriate application automatically based on the file extension.</p><p>That’s another reason why .sh is better than .txt.</p><h3>Writing an Interactive Script with read</h3><p>Let’s make our script more interesting by asking for user input.</p><p>Open the script:</p><pre>nano myscript.sh</pre><p>Add the following:</p><pre>#!/bin/bash<br>echo &quot;What is your name?&quot;<br>read username<br>echo &quot;Hello, $username. Nice to meet you!&quot;</pre><p>Save the file and run it:</p><pre>./myscript.sh</pre><p>Example output:</p><pre>What is your name?<br>Frank<br>Hello, Frank. Nice to meet you!</pre><h3>A Cleaner Way: Using read -p</h3><p>We can simplify the script by using the -p option with read:</p><pre>#!/bin/bash</pre><pre>read -p &quot;What is your name? &quot; username<br>echo &quot;Hello, $username. Nice to meet you!&quot;</pre><p>This removes the need for a separate echo statement and feels more natural.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=739b7c103eea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linux Commands]]></title>
            <link>https://medium.com/@cloudwithsaj/the-most-popular-ones-are-bourne-shell-developed-in-the-1970s-for-unix-and-still-in-use-today-dab510389c0b?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/dab510389c0b</guid>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Fri, 02 Jan 2026 21:12:27 GMT</pubDate>
            <atom:updated>2026-01-02T22:29:58.361Z</atom:updated>
            <content:encoded><![CDATA[<p>Linux Commands</p><p>The most popular ones are Bourne Shell, developed in the 1970s for Unix and still in use today which is often called as Bash. These shells may differ in different ways in their core capabilities, but all of them have one common purpose, to facilitate the communication between the user and the operating system. To check the shell being used, use the command echo $SHELL like below. Make sure use the uppercase letters for the word shell.</p><p>Bash is perhaps the most popular shells, specifically because of its features such as Bash completions or brace expansion, which is not available in shells such as the Bourne shell. To change the default shell, use the command chsh. You will be prompted for the password and following that the new value of the shell like below</p><p>Bash supports command auto completion which means is bash will attempt to auto complete commands for you if you type a part of it and press the tab key. This feature can extend to completing a file or a directory name, commands, its arguments and options. This is very handy especially when navigating through the Linux system. You do not have to remember or type in the entire name of directories or files. Just type in the first few characters of a file or directory and use tab space to take advantage of the auto complete feature.</p><p>In bash, we can set custom aliases for the actual commands. In this example, we are setting an alias called dt to the command called date, which as the name suggests, prints the current date. Once the alias is set, you can type the ‘dt’ command every time you want to print the date.</p><p>Use the history command to get a list of previously run commands. This is quite useful to revisit and check some of the commands you ran earlier.</p><p>ENVIRONMET VARIABLES</p><p>Variables, just as in any programming language, are used to store information. In this example echo $SHELL, the word SHELL represents an environmental variable which stores the value of the type of shell used by the user. To print the variable value, we precede the variable name with the dollar symbol. Environment variables in specific store information about the users login session which is used by the shell when executing commands.</p><p>To see a list of all environment variables, use the env command.</p><p>To set an environment variable, we can use the export command. Say you want to create a new environment variable called “country” and set a value of “UK” to it. Use the export command and in the argument use country equals UK.</p><p>This will set the variable for the current shell and any other processes or programs started by the shell. You can also assign the variable without the export command like this. However, this will only apply the variable within the shell. The value is not carried forward to any other process. To make these variables persistent over subsequent login or reboots, add them to the .profile in the user’s home directory.</p><p>If you are using z shell, you can add them to the profile called .zshrc</p><p>Speaking about the environment variables, when a user issues an external command into the shell, the shell uses a PATH variable to search for these external commands. To see the directories defined in the PATH variable, use the command echo $path.</p><p>To check if the location of a command can be identified, use the which command. In this example we are checking the example of program called vagrant.</p><p>we explored the basics of <strong>shell scripting</strong>, which is simply a way to give instructions to a computer using short commands. Think of it like giving step-by-step directions to a very obedient robot. Below are the concepts we learned, explained with easy examples and real-life comparisons.</p><h3>1. Creating and Renaming Files 📄</h3><p>We started with <strong>file creation</strong>.</p><ul><li>Command used:<br> touch hello.txt</li></ul><p>This is like placing a <strong>new empty notebook</strong> on your desk and writing “hello.txt” on the cover.</p><p>Next, we renamed the file from hello.txt to hello.h.</p><p>Renaming a file is just like <strong>changing the label on that notebook</strong> without changing what’s inside. It helps organize things better.</p><h3>2. Setting Permissions (Who Can Do What?) 🔐</h3><p>Every file has <strong>rules</strong>, just like a toy or a book at home.</p><ul><li>Who can <strong>read</strong> it? (open and look)</li><li>Who can <strong>write</strong> to it? (edit or change)</li><li>Who can <strong>execute</strong> it? (run it like a program)</li></ul><p>We used the chmod command to change these rules.</p><p>For example, making a file executable <strong>only for the owner</strong> is like saying:</p><blockquote><em>“Only I can play with this toy. Others can see it, but not use it.”</em></blockquote><p>This helps keep files safe and under control.</p><h3>3. Taking User Input (Asking Questions) 🗣️</h3><p>We then learned how to <strong>ask the user for input</strong>.</p><p>Command example:</p><pre>read -p &quot;Enter file name: &quot; name</pre><p>This is like the computer asking:</p><blockquote><em>“What should I name this file?”</em></blockquote><p>Whatever the user types gets stored in a <strong>box</strong> (called a variable) named name.<br> Later, the computer can use that box to create a file.</p><p>So instead of hard-coding a name, the computer becomes more <strong>interactive and smart</strong>.</p><h3>4. Playing with Strings (Joining Words Together) 🧩</h3><p>We also talked about <strong>string manipulation</strong>, which just means <strong>joining words or symbols together</strong>.</p><p>For example, adding underscores or extra text to a file name is like:</p><ul><li>Taking “homework”</li><li>Adding “_math”</li><li>And getting “homework_math”</li></ul><p>This is useful when you want file names to follow a pattern automatically.</p><h3>5. Knowing the Current Date and Time ⏰</h3><p>We used the date command to get the <strong>current date and time</strong>.</p><p>We stored this information in a variable called important_time.</p><p>Think of this like checking a clock and <strong>writing down the time on paper</strong>.<br> Once written, that time stays the same — even if time passes.</p><p>This is useful for:</p><ul><li>Naming files</li><li>Keeping logs</li><li>Tracking when something happened</li></ul><h3>6. Understanding Variables (Memory Boxes) 📦</h3><p>Finally, we learned about <strong>variables</strong>, which are just <strong>containers that store information</strong>.</p><ul><li>A <strong>static variable</strong> (like important_time) stores a value once and keeps it.</li><li>Like writing today’s date in a diary.</li><li>A <strong>dynamic variable</strong> (like running date directly) updates every time.</li><li>Like looking at a clock whenever you need the time.</li></ul><p>This helps scripts behave differently depending on <strong>when and how</strong> they are run.</p><h3>Linux Commands Are Just Programs (Here’s Where They Live)</h3><p>When you type commands like pwd, ls, or clear in the Linux terminal, something interesting is happening behind the scenes.</p><p>👉 <strong>You are actually running a program.</strong></p><p>Yes — each of these “commands” is a real program stored somewhere on your system. The shell (usually <strong>bash</strong>) simply finds that program and executes it for you.</p><p>Let’s break this down step by step.</p><h3>How Does Linux Know Where a Command Is?</h3><p>Linux provides a very useful command called:</p><pre>which</pre><p>The which command tells you <strong>where a command is located on your system</strong>.</p><h3>Example:</h3><pre>which ls</pre><p>Output:</p><pre>/bin/ls</pre><p>This means:</p><ul><li>The ls command is actually a program</li><li>It lives inside the /bin directory</li></ul><p>Similarly:</p><pre>which pwd</pre><p>Output:</p><pre>/bin/pwd</pre><p>And:</p><pre>which clear</pre><p>Output:</p><pre>/usr/bin/clear</pre><p>So when you type ls, pwd, or clear, Linux is really running:</p><ul><li>/bin/ls</li><li>/bin/pwd</li><li>/usr/bin/clear</li></ul><h3>What Is the /bin Directory?</h3><p>Think of /bin like a <strong>toolbox</strong> 🧰.</p><p>It contains many essential tools (programs) that the system needs to work properly, such as:</p><ul><li>ls (list files)</li><li>pwd (show current directory)</li><li>cat</li><li>echo</li></ul><p>These programs are <strong>not inside your home folder</strong>.<br> They live near the very top of the file system, starting from the <strong>root directory</strong> /.</p><h3>Finding /bin in the File System</h3><p>If you try this:</p><pre>ls</pre><p>You’ll only see files from your <strong>home directory</strong>.</p><p>To reach /bin, you must go up to the root:</p><pre>cd /<br>ls</pre><p>Now you’ll see folders like:</p><ul><li>bin</li><li>usr</li><li>etc</li></ul><p>And yes — <strong>this is the main root of the system</strong>.</p><h3>Why Can’t I See /bin in Finder (macOS)?</h3><p>On macOS:</p><p>Finder hides many system folders by default</p><p>You usually can’t browse /bin directly</p><p>That’s why the <strong>terminal is necessary</strong> to access these locations.</p><p>However, if you open /bin manually (with permissions), you’ll see files like:</p><ul><li>ls</li><li>pwd</li><li>bash</li></ul><p>Each of these is an <strong>executable program</strong>.</p><h3>Commands Are Executable Programs</h3><p>Let’s prove it.</p><p>Instead of typing:</p><pre>ls</pre><p>You can type:</p><pre>/bin/ls</pre><p>Same result.</p><p>Similarly:</p><pre>/bin/pwd</pre><p>And:</p><pre>/usr/bin/clear</pre><p>This works because you’re calling the <strong>exact program file</strong> directly.</p><h3>Even the Shell Is a Program</h3><p>The shell itself (bash) is also just a program.</p><p>Try:</p><pre>which bash</pre><p>Output:</p><pre>/bin/bash</pre><p>When you open a terminal, you are actually <strong>running the bash program</strong>.<br> If you double-click it or execute it again, you open another shell.</p><h3>Why Don’t We Type /bin/ls Every Time?</h3><p>Good question!</p><p>Linux uses a special system variable called <strong>PATH</strong>.</p><p>PATH is like a <strong>list of folders</strong> where Linux automatically looks for programs.</p><p>So when you type:</p><pre>ls</pre><p>The system checks:</p><ul><li>/bin</li><li>/usr/bin</li><li>/usr/local/bin</li></ul><p>As soon as it finds ls, it runs it—no full path needed.</p><p>That’s why typing ls is enough.</p><ul><li>Linux commands are <strong>real programs</strong></li><li>They live in directories like /bin and /usr/bin</li><li>The which command tells you <strong>where a command lives</strong></li><li>The shell (bash) is also just a program</li><li>PATH allows you to run commands without typing full paths</li></ul><p>So the next time you type:</p><pre>ls</pre><p>Remember — you’re actually running:</p><pre>/bin/ls</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dab510389c0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Strategies for Updating and Rolling Back Deployments in Kubernetes]]></title>
            <link>https://medium.com/@cloudwithsaj/strategies-for-updating-and-rolling-back-deployments-in-kubernetes-9a6a666f656b?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a6a666f656b</guid>
            <category><![CDATA[strategy-deployment]]></category>
            <category><![CDATA[recreate]]></category>
            <category><![CDATA[rollout-deployment]]></category>
            <category><![CDATA[rollbacks]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Tue, 09 Jul 2024 14:33:30 GMT</pubDate>
            <atom:updated>2024-07-09T14:33:30.935Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Strategies for Updating and Rolling Back Deployments in Kubernetes</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IRFlP0-G7UOviK4V" /><figcaption>Photo by <a href="https://unsplash.com/@lazizli?utm_source=medium&amp;utm_medium=referral">Lala Azizli</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Imagine you have five copies of your web application running, like five books on a shelf. One way to update all these books to a new edition is to remove all five from the shelf and then place five new ones there. This approach, while straightforward, leaves the shelf empty for a period, making the books unavailable. This is known as the <strong>recreate strategy</strong> and is not the preferred method in Kubernetes.</p><p><strong>Understanding Rollouts and Versioning</strong></p><p>When you first deploy your application in Kubernetes, it triggers a rollout, creating a deployment revision, let’s call it <strong>Revision 1</strong>. Later, when you update the application (say, by updating the container version), a new rollout happens, and <strong>Revision 2</strong> is created. This versioning allows you to track changes and roll back to previous versions if needed.</p><ul><li><strong>Check Rollout Status</strong>: kubectl rollout status &lt;deployment_name&gt;</li><li><strong>View Rollout History</strong>: kubectl rollout history &lt;deployment_name&gt;</li></ul><p><strong>Deployment Strategies</strong></p><p>There are two primary deployment strategies in Kubernetes:</p><ol><li><strong>Recreate Strategy</strong>:</li></ol><ul><li>All old instances are terminated before any new ones are started.</li><li>Pros: Simple.</li><li>Cons: Causes downtime as the application is unavailable between the old version shutting down and the new version starting up.</li></ul><ol><li><strong>Rolling Update Strategy</strong> (default):</li></ol><ul><li>Updates instances one at a time.</li><li>Pros: Ensures continuous availability as some instances of the old version remain running while the new version is deployed.</li><li>Cons: More complex, requiring careful configuration.</li></ul><p><strong>Rolling Update and Recreate</strong></p><p>kubectl describe deployment &lt;deployment_name&gt; to see detailed deployment information and strategies used. Then find the type of strategy under the “strategytype” section.</p><p>Consider updating an application with five instances:</p><ol><li><strong>Recreate Strategy</strong>:</li></ol><ul><li>All five instances are terminated.</li><li>Five new instances are created.</li><li>Downtime occurs during the transition.</li></ul><p><strong>2. Rolling Update Strategy</strong>:</p><ul><li>One old instance is terminated and one new instance is started, sequentially.</li><li>Continuous availability is maintained, minimizing disruption.</li><li><strong>Describe Deployment</strong>: kubectl describe deployment &lt;deployment_name&gt; to see detailed deployment information and strategies used.</li><li><strong>List Replica Sets</strong>: kubectl get replicasets to see old and new replica sets.</li></ul><p><strong>Performing an Upgrade</strong></p><p>When performing an upgrade your application in Kubernetes:</p><ul><li>A new replica set is created for the new version.</li><li>The deployment object manages the transition, following the rolling update strategy.</li><li>Use kubectl get replicasets to see the old replica set with zero pods and the new one with the updated number.</li></ul><p><strong>Rolling Back an Update</strong></p><p>If something goes wrong with the new version:</p><ul><li>Use the rollback command to revert to the previous version.</li><li><strong>Rollback Command</strong>: kubectl rollout undo &lt;deployment_name&gt;</li><li>The deployment will terminate the new pods and restart the old ones when rollback is done.</li></ul><p><strong>Commands</strong></p><ul><li><strong>Create Deployment</strong>: kubectl create -f &lt;deployment_file&gt;</li><li><strong>List Deployments</strong>: kubectl get deployments</li><li><strong>Update Deployment</strong>: kubectl apply -f &lt;deployment_file&gt; or kubectl set image &lt;deployment_name&gt; &lt;container_name&gt;=&lt;new_image&gt;</li><li><strong>Check Rollout Status</strong>: kubectl rollout status &lt;deployment_name&gt;</li><li><strong>Rollback Deployment</strong>: kubectl rollout undo &lt;deployment_name&gt;</li></ul><p>This structured approach, using rolling updates and versioning, ensures that your application remains available and can quickly recover from issues during deployment updates.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a6a666f656b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Labels and Selectors in K8s]]></title>
            <link>https://medium.com/@cloudwithsaj/labels-and-selectors-in-k8s-fe1f02f72b6b?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe1f02f72b6b</guid>
            <category><![CDATA[selectors-in-kubernetes]]></category>
            <category><![CDATA[labels-in-kubernetes]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Mon, 08 Jul 2024 14:46:53 GMT</pubDate>
            <atom:updated>2024-07-08T14:46:53.202Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MIGbSNgXdjbUsr-O" /><figcaption>Photo by <a href="https://unsplash.com/@jontyson?utm_source=medium&amp;utm_medium=referral">Jon Tyson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Imagine you have a collection of animals and you want to organize them based on different criteria. You might sort them by class (mammals, reptiles, birds), by whether they are domestic or wild, or by their color. You don’t just want to group them, but also filter them, such as finding all green animals, or all green animals that are also birds. To do this effectively, you need a system of labels. Labels are properties attached to each item, allowing you to categorize them by class, kind, color, or any other attribute.</p><p><strong>Labels and Selectors</strong></p><p>Labels are similar to the tags you use on YouTube videos or blog posts, helping users find the right content. In an online store, labels help create filters for viewing products, like finding all red shirts or all shirts under $20.</p><p><strong>Labels and Selectors in Kubernetes</strong></p><p>In Kubernetes, you deal with many types of objects: pods, services, replica sets, deployments, etc. These objects can multiply quickly, leading to hundreds or thousands of items in your cluster. To manage and view these objects efficiently, you can use labels and selectors.</p><ul><li><strong>Labels</strong>: Properties you attach to each Kubernetes object. For example, you might label an object with app: webserver or environment: production.</li><li><strong>Selectors</strong>: Tools that let you filter and group objects based on their labels. For instance, you can select all objects with the label app: webserver.</li></ul><p><strong>Practical Use</strong></p><p>By attaching relevant labels to each object, you can easily group and filter them based on your needs. This might involve:</p><ul><li><strong>Grouping by Type</strong>: View all objects of a specific type, such as all pods or all services.</li><li><strong>Application-Based Filtering</strong>: Select objects related to a particular application, like app: frontend.</li><li><strong>Functional Grouping</strong>: Filter objects by their functionality, such as role: database.</li></ul><p><strong>Example</strong></p><p>Let’s say you have several Kubernetes objects, and you label them as follows:</p><ul><li>pod-1: app: webserver, environment: production</li><li>pod-2: app: database, environment: production</li><li>pod-3: app: webserver, environment: development</li></ul><p>Using selectors, you can perform actions like:</p><ul><li>Find all objects in the production environment: environment=production</li><li>Filter all webserver applications: app=webserver</li><li>Combine criteria to find all production webservers: app=webserver, environment=production</li></ul><p>Labels and selectors thus provide a powerful way to manage and organize Kubernetes objects, making it easier to navigate and maintain complex clusters.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe1f02f72b6b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Logging and Monitoring in kubernetes]]></title>
            <link>https://medium.com/@cloudwithsaj/logging-and-monitoring-in-kubernetes-b4959c37f8d7?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4959c37f8d7</guid>
            <category><![CDATA[kubernetes-metric-server]]></category>
            <category><![CDATA[logging-and-monitoring]]></category>
            <category><![CDATA[kubernetes-logging]]></category>
            <category><![CDATA[kubernetes-monitoring]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Sun, 07 Jul 2024 08:42:38 GMT</pubDate>
            <atom:updated>2024-07-07T08:42:38.600Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Kubernetes Logging</strong></p><p>Kubernetes (K8s) logging is essential for monitoring and debugging applications running in a Kubernetes cluster</p><p>Kubernetes logging refers to the process of collecting, storing, and managing log data generated by applications and services running within a Kubernetes cluster. Logs provide insights into the behaviour and performance of applications, helping developers and operators troubleshoot issues and ensure smooth operation.</p><p><strong>How Does Kubernetes Logging Work?</strong></p><p>Let’s imagine you have a movie theatre (the web server) that streams movies (events) to its big screen (standard output). Now, if you were to record the movie and watch it later at home (run the Docker container in the background in detached mode using the -d option), you wouldn&#39;t see the movie live. If you wanted to watch the recording (view the logs), you could use the &quot;playback&quot; function (the docker logs command followed by the container ID). The -f option helps you watch the movie as it&#39;s being recorded (live stream).</p><ol><li><strong>Container Logs:</strong> Each application running in a Kubernetes cluster runs inside a container.</li><li><strong>Pod Logs</strong>: Containers are grouped into pods. Kubernetes allows you to view the logs of individual containers within a pod using the kubectl logs command.</li></ol><p>kubectl logs my-pod</p><p>3. <strong>Viewing Logs</strong>:</p><p>o <strong>Basic Log Retrieval:</strong> To view the logs of a container, you use the kubectl logs &lt;pod-name&gt; command. This displays the logs from the container in the specified pod.</p><blockquote>kubectl logs my-pod</blockquote><p>o <strong>Live Streaming Logs: </strong>To continuously stream logs, similar to a live feed, you use the -f (follow) option: kubectl logs -f &lt;pod-name&gt;.</p><p>Say for example, We create a drive-in theater (a pod) with the same movie (Docker image) using a theater plan (pod definition file). Once the theater is open (the pod is running), we can watch the movie using the kubectl logs command with the theater&#39;s name (pod name). Using the -f option streams the movie live, just like the Docker command.</p><blockquote>kubectl logs -f my-pod</blockquote><ol><li><strong>Multiple Containers:</strong> If a pod has multiple containers, you must specify which container’s logs you want to view. This is done by adding the container name to the command:</li></ol><blockquote>kubectl logs &lt;pod-name&gt; -c &lt;container-name&gt;.</blockquote><p>If there are multiple projectors within a theater, you must specify the name of the projector explicitly in the command; otherwise, it would fail, asking you to specify a name.</p><blockquote>kubectl logs -f my-pod -c my-container</blockquote><ol><li><strong>Log Management Tools: </strong>For more complex log management needs, such as aggregation, storage, and analysis, you can integrate Kubernetes with logging tools like Elasticsearch, Fluentd, and Kibana (EFK stack), or use managed services like Google Cloud Logging.</li></ol><p>Kubernetes logging is a crucial aspect of managing and troubleshooting applications in a Kubernetes environment. By understanding how to access and interpret logs, you can ensure your applications run smoothly and resolve issues more efficiently.</p><p><strong>MONITORING</strong></p><p>Originally, Heapster was the project that enabled monitoring and analysis for Kubernetes. However, Heapster has been deprecated and replaced by the Metric Server. The Metric Server provides in-memory monitoring, meaning it doesn’t store metrics on disk, so you won’t have access to historical data. For historical data, you would need to use more advanced solutions like Prometheus or Datadog.</p><p>Monitoring a Kubernetes cluster can be compared to monitoring a city’s infrastructure like need to know how many buildings (nodes) are in the city, how many of them are in good condition (healthy), and monitor essential services like electricity, water, and roads (CPU, memory, network, and disk utilization). Similarly, you want to monitor each household (pod) to see their energy consumption (CPU and memory usage).</p><p><strong>What to Monitor in a Kubernetes Cluster</strong></p><ol><li><strong>Node-Level Metrics</strong>:</li></ol><ul><li><strong>Number of Nodes</strong>: Think of this as counting the number of buildings in your city.</li><li><strong>Health of Nodes</strong>: Are the buildings in good shape or do they need repairs?</li><li><strong>Performance Metrics</strong>: Check the essential services:</li><li><strong>CPU Utilization</strong>: Like monitoring electricity usage.</li><li><strong>Memory Utilization</strong>: Like checking the water supply.</li><li><strong>Network Utilization</strong>: Similar to ensuring smooth traffic flow on roads.</li><li><strong>Disk Utilization</strong>: Comparable to monitoring storage facilities.</li></ul><ol><li><strong>Pod-Level Metrics</strong>:</li></ol><ul><li><strong>Number of Pods</strong>: How many households are there?</li><li><strong>Performance Metrics for Pods</strong>:</li><li><strong>CPU Consumption</strong>: How much CPU is consumed by pod like how much electricity does each household use?</li><li><strong>Memory Consumption</strong>: How much memory is consumed by a pod like how much water does each household use?</li></ul><p><strong>Available Monitoring Solutions</strong></p><p>Kubernetes doesn’t come with a built-in, full-featured monitoring system. However, there are several open-source and proprietary solutions available:</p><ul><li><strong>Open Source Solutions</strong>:</li><li><strong>Metric Server</strong>: A lightweight monitoring solution.</li><li><strong>Prometheus</strong>: A powerful monitoring and alerting toolkit.</li><li><strong>Elastic Stack</strong>: A set of tools for searching, analyzing, and visualizing data.</li><li><strong>Proprietary Solutions</strong>:</li><li><strong>Datadog</strong>: A monitoring service for cloud-scale applications.</li><li><strong>Dynatrace</strong>: A platform for software intelligence.</li></ul><p><strong>How Metrics are Generated</strong></p><p>Each node in a Kubernetes cluster runs an agent called the Kubelet, which is responsible for communicating with the Kubernetes API server and running pods. The Kubelet includes a component called cAdvisor (Container Advisor) that collects performance metrics from the pods and makes them available to the Metric Server.</p><p><strong>Setting Up the Metric Server</strong></p><p>If you’re using Minikube for your local cluster, you can enable the Metric Server with:</p><blockquote>minikube addons enable metrics-server</blockquote><p>For other environments, deploy the Metric Server by cloning its deployment files from GitHub and using the following command:</p><blockquote>kubectl create -f &lt;metrics-server-deployment-files&gt;</blockquote><p>This command deploys the necessary pods, services, and roles to enable the Metric Server to collect performance metrics from the nodes.</p><p><strong>Viewing Metrics</strong></p><p>After deployment, give the Metric Server some time to collect data. You can view cluster performance using:</p><blockquote>kubectl top node</blockquote><p>This command shows the CPU and memory consumption of each node. For example, it might show that 8% of the CPU on your master node is being used.</p><p>To view the performance metrics of pods, use:</p><blockquote>kubectl top pod</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4959c37f8d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Taints Tolerations Node affinity]]></title>
            <link>https://medium.com/@cloudwithsaj/taints-tolerations-node-affinity-1826229ad621?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/1826229ad621</guid>
            <category><![CDATA[pod-affinity]]></category>
            <category><![CDATA[nodeselector]]></category>
            <category><![CDATA[taints-and-toleration]]></category>
            <category><![CDATA[node-affinity]]></category>
            <category><![CDATA[kubernetes-node-affinity]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Mon, 17 Jun 2024 10:39:22 GMT</pubDate>
            <atom:updated>2024-06-17T10:39:22.339Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Taints and Tolerations</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6qVDDeh-A7hDh_4a" /><figcaption>Photo by <a href="https://unsplash.com/@acton_crawford?utm_source=medium&amp;utm_medium=referral">Acton Crawford</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Imagine your Kubernetes cluster as a hospital building. The hospital (cluster) has multiple wards, and each ward represents a node. The patients are like pods, and the wards have certain rules (taints) about which patients can be admitted there. Some patients have special permissions (tolerations) that allow them to be admitted to specific wards despite the rules.</p><p><strong>Basic Concepts</strong></p><ul><li><strong>Nodes (Wards)</strong>: The individual units (nodes) where work happens, analogous to wards in a hospital.</li><li><strong>Pods (Patients)</strong>: The workloads that need to be scheduled on nodes, analogous to patients needing a bed.</li><li><strong>Taints (Ward Restrictions)</strong>: Rules set on the nodes (wards) that prevent certain pods (patients) from being scheduled there unless they have specific permissions.</li><li><strong>Tolerations (Permissions)</strong>: Permissions given to pods (patients) that allow them to be scheduled on nodes (wards) with matching taints (restrictions).</li></ul><p><strong>Scenario: Initial Setup</strong></p><p>You have a hospital with three wards (nodes): ward1, ward2, and ward3. You also have three patients (pods) : patientA, patientB, and patientC.</p><p>Without any restrictions, patients are free to be admitted to any ward. The hospital manager (Kubernetes scheduler) will try to balance the patients across all wards.</p><p><strong>Step 1: Applying Ward Restrictions (Taints)</strong></p><p>Suppose ward1 is reserved for infectious diseases. You don’t want anyone other than infectious disease patients being admitted there. You set a restriction (taint) on ward1:</p><p>Run the below command to create a taint on node which means to set a restriction on node</p><pre>Kubectl taint node &lt;node-name&gt; key=value:NoSchedule<br>kubectl taint nodes ward1 disease=infectious:NoSchedule</pre><p>This command sets a taint on ward1 indicating only patients with the infectious disease permission can be admitted there.</p><p>How to find if there are any taints exist on node?</p><p>Run the below command to check taint exists</p><pre>kubectl describe node &lt;node-name&gt; | grep -i taints</pre><p>In case, if we wanted to remove the restriction on node, run the below command</p><pre>Kubectl taint node &lt;node-name&gt; key=value:NoSchedule-</pre><p># Remove from node ‘ward1’ the taint with key ‘ disease=infectious ‘ and effect ‘NoSchedule’</p><pre>kubectl taint nodes ward1 disease=infectious:NoSchedule-</pre><p><strong>Step 2: Adding Permissions (Tolerations) to Patients</strong></p><p>To allow specific patients to be admitted to ward1, you give them the necessary permissions (tolerations).</p><p><strong>PatientA (without toleration)</strong></p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: patientA<br>spec:<br>  containers:<br>  - name: mycontainer<br>    image: nginx</pre><p><strong>PatientB (with toleration)</strong></p><p>Patientb.yaml</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: patientB<br>spec:<br>  containers:<br>  - name: mycontainerB<br>    image: busybox<br>  tolerations:<br>  - key: &quot;disease&quot;<br>    operator: &quot;Equal&quot;<br>    value: &quot;infectious&quot;<br>    effect: &quot;NoSchedule&quot;</pre><p>run the kubectl create -f patientb.yaml to create a pod. This will place a pod on the node “ward1” which has taint disease=infectious:NoSchedule</p><p>In this setup:</p><ul><li><strong>PatientA</strong> cannot be admitted to node called ward1 because they don’t have the infectious disease permission.</li><li><strong>PatientB</strong> can be admitted to node called ward1 because they have the infectious disease permission.</li></ul><p><strong>Step 3: Scheduling Patients</strong></p><p>Now, when the hospital manager tries to place the patients:</p><ul><li>PatientA will be placed in nodes called ward2 or ward3.</li><li>PatientB will be placed in node called ward1.</li></ul><p><strong>Different Effects of Taints</strong></p><ul><li><strong>NoSchedule</strong>: Patients that don’t have permission will not be admitted to the ward.</li><li><strong>PreferNoSchedule</strong>: The hospital manager will try to avoid placing patients in this ward, but it’s not guaranteed.</li><li><strong>NoExecute</strong>: Patients already in the ward without permission will be asked to leave, and new patients without permission will not be admitted there.</li></ul><p><strong>Example: NoExecute Taint</strong></p><p>To apply a NoExecute taint to evict patients who don’t have permission:</p><p>kubectl taint nodes ward1 disease=infectious:NoExecute</p><p>And a patient with the toleration:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: patientC<br>spec:<br>  containers:<br>  - name: mycontainerC<br>    image: nginx<br>  tolerations:<br>  - key: &quot;disease&quot;<br>    operator: &quot;Equal&quot;<br>    value: &quot;infectious&quot;<br>    effect: &quot;NoExecute&quot;</pre><p>Patient C without permission will not be admitted there and will be asked to leave.</p><p><strong>Special Case: Administrative Ward</strong></p><p>In a hospital, there might be an administrative ward (master node) reserved for management tasks. The hospital manager ensures that regular patients are not admitted there by default:</p><pre>kubectl describe node admin-ward | grep Taints</pre><p>You can modify this behaviour, but it’s best practice to avoid placing regular patients in the administrative ward to keep it dedicated to management tasks.</p><p>Taints and tolerations in Kubernetes help manage which pods can be scheduled on which nodes, like how ward restrictions and permissions manage which patients can be admitted to which wards in a hospital. This mechanism ensures that certain nodes are reserved for specific workloads, providing control over resource allocation and isolation of different applications.</p><p><strong>Node Affinity and Taints</strong></p><p>Let’s go through a Kubernetes setup and its management using a hospital analogy to make the concepts clearer. We’ll focus on how to manage resources efficiently using node selectors, node affinity, and taints and tolerations.</p><p><strong>Scenario:</strong></p><p>Imagine you are managing a hospital with different departments (nodes) and patients (pods). Some departments are specialized with higher capabilities (larger nodes), and others are more general with fewer resources (smaller nodes). You want to ensure that patients requiring special care are only admitted to specialized departments. Here’s how you can do it in Kubernetes:</p><p><strong>Step 1: Basic Setup</strong></p><p>You have a hospital with three departments:</p><ol><li><strong>Department A (Node 1)</strong>: Large with high resources.</li><li><strong>Department B (Node 2)</strong>: Small with fewer resources.</li><li><strong>Department C (Node 3)</strong>: Small with fewer resources.</li></ol><p>You have different types of patients (workloads) with varying needs. Patients needing intensive care (data processing workloads) should only be placed in Department A. However, by default, any patient can go to any department, which might not be efficient.</p><p><strong>Step 2: Using Node Selectors</strong></p><p>Node selectors allow you to assign specific patients to specific departments. You label your departments to indicate their capacity:</p><ul><li>Department A: size=large</li><li>Department B: size=small</li><li>Department C: size=small</li></ul><p>You then modify the patient admission form (pod definition) to include a node selector:</p><pre>spec:<br>  nodeSelector:<br>    size: large</pre><p>This ensures that the patient is only admitted to Department A, which has the label size=large.</p><p><strong>Example Command to Label Node:</strong></p><pre>kubectl label nodes node1 size=large</pre><p><strong>Patient Admission Form (Pod Definition):</strong></p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: data-processing<br>spec:<br>  containers:<br>  - name: workload<br>    image: data-processing-image<br>  nodeSelector:<br>    size: large</pre><p><strong>Step 3: Limitations of Node Selectors</strong></p><p>Node selectors are simple but limited. They can’t handle complex rules like admitting patients to either large or medium departments or excluding small departments.</p><p><strong>Step 4: Using Node Affinity</strong></p><p>Node affinity provides more flexibility. It allows you to define complex rules for patient placement:</p><ul><li><strong>RequiredDuringSchedulingIgnoredDuringExecution</strong>: Ensures that patients are admitted to suitable departments initially but doesn’t move them if the department changes later.This type of node affinity specifies that the pod must be scheduled on nodes that meet the specified criteria. If no such nodes are available, the pod will not be scheduled.</li></ul><p><strong>RequiredDuringScheduling</strong>: This means that the pod will only be scheduled on nodes that satisfy the specified affinity rules during the scheduling phase. The scheduler will strictly enforce this requirement.</p><p><strong>IgnoredDuringExecution</strong>: Once the pod is running, any changes to the node labels do not affect the already scheduled pod. The affinity rules are only considered during the initial scheduling.</p><p><strong>Example</strong>:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: mypod<br>spec:<br>  affinity:<br>    nodeAffinity:<br>      requiredDuringSchedulingIgnoredDuringExecution:<br>        nodeSelectorTerms:<br>        - matchExpressions:<br>          - key: size<br>            operator: In<br>            values:<br>            - large<br>            - medium</pre><p>In this example, the pod will only be scheduled on nodes labeled with size=large or size=medium. If no such nodes exist, the pod will remain unscheduled.</p><p><strong>PreferredDuringSchedulingIgnoredDuringExecution</strong></p><p><strong>Definition</strong>: This type of node affinity expresses a preference but not a strict requirement for scheduling the pod on nodes that meet the specified criteria. If no such nodes are available, the scheduler will place the pod on any available node. In our hospital example, tries to place patients in suitable departments but can place them anywhere if necessary.</p><ul><li><strong>PreferredDuringScheduling</strong>: The scheduler will try to place the pod on nodes that meet the specified affinity rules, but it is not mandatory. If no such nodes are available, the pod will be scheduled on any available node.</li><li><strong>IgnoredDuringExecution</strong>: Similar to RequiredDuringSchedulingIgnoredDuringExecution, once the pod is running, changes to the node labels do not affect the pod.</li></ul><p><strong>Node Affinity Example:</strong></p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: data-processing<br>spec:<br>  affinity:<br>    nodeAffinity:<br>      requiredDuringSchedulingIgnoredDuringExecution:<br>        nodeSelectorTerms:<br>        - matchExpressions:<br>          - key: size<br>            operator: In<br>            values:<br>            - large<br>            - medium</pre><p>In this example, the scheduler will try to place the pod on nodes labeled with size=large or size=medium. However, if no such nodes are available, it will place the pod on any node.</p><p><strong>Comparison</strong></p><ul><li><strong>RequiredDuringSchedulingIgnoredDuringExecution</strong> ensures strict placement rules during scheduling and ignores node changes during execution. This is used when it is critical that the pod runs on specific nodes.</li><li><strong>PreferredDuringSchedulingIgnoredDuringExecution</strong> provides a soft preference during scheduling and also ignores node changes during execution. This is used when it is preferable but not essential for the pod to run on specific nodes.</li></ul><p>These settings help ensure that pods are placed on nodes that can best handle their resource requirements and operational characteristics, improving the overall efficiency and reliability of your Kubernetes cluster.</p><p><strong>Step 5: Using Taints and Tolerations</strong></p><p>Taints and tolerations restrict which patients can be admitted to which departments, adding another layer of control:</p><ul><li><strong>Tainting a Department</strong>: You might taint Department A with a condition that only patients with a specific toleration can be admitted.</li><li><strong>Tolerating the Taint</strong>: Patients must have a corresponding toleration to be admitted to the tainted department.</li></ul><p><strong>Example Taint Command:</strong></p><pre>kubectl taint nodes node1 accident=emergency:NoSchedule</pre><p><strong>Patient Admission Form with Toleration:</strong></p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: intensive-care<br>spec:<br>  containers:<br>  - name: workload<br>    image: intensive-care-image<br>  tolerations:<br>  - key: &quot;accident&quot;<br>    operator: &quot;Equal&quot;<br>    value: &quot;emergency&quot;<br>    effect: &quot;NoSchedule&quot;</pre><p><strong>Summary</strong></p><p>Using node selectors, node affinity, and taints/tolerations in Kubernetes helps you manage your resources effectively by ensuring workloads are placed on the appropriate nodes. This ensures that high-resource tasks are assigned to nodes that can handle them, improving efficiency and performance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1826229ad621" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Service Accounts]]></title>
            <link>https://medium.com/@cloudwithsaj/service-accounts-f1821debe2ca?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1821debe2ca</guid>
            <category><![CDATA[serviceaccount]]></category>
            <category><![CDATA[kubernetes-serviceaccount]]></category>
            <category><![CDATA[kubernetes-security]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Thu, 13 Jun 2024 10:34:38 GMT</pubDate>
            <atom:updated>2024-06-13T10:34:38.068Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vbwWJUF_Ag8-giEm" /><figcaption>Photo by <a href="https://unsplash.com/@impatrickt?utm_source=medium&amp;utm_medium=referral">Patrick Tomasso</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Service accounts are an essential part of Kubernetes security. They help manage permissions for both users and applications interacting with the cluster. In Kubernetes, there are two main types of accounts: user accounts and service accounts. Imagine Kubernetes as a large, secure office building with many rooms and departments. In this building, there are two types of keycards: one for people (user accounts) and one for automated systems (service accounts).</p><p><strong>User Accounts</strong> are for humans. For instance, an admin accessing the cluster to manage resources or a developer deploying applications. Think of these as personal keycards given to employees, like administrators or developers. An administrator might use their keycard to access different rooms to perform maintenance, while a developer uses theirs to enter a room and deploy new software.</p><p><strong>Service Accounts</strong> are for applications or services. For example, a monitoring tool like Prometheus or a CI/CD tool like Jenkins uses service accounts to interact with the Kubernetes API. These are special keycards for machines or applications. For instance, a cleaning robot needs a keycard to enter various rooms to perform its duties. Similarly, a monitoring application like Prometheus or a build tool like Jenkins needs a service account to interact with the Kubernetes API.</p><p><strong>Example: Kubernetes Dashboard Application</strong></p><p>Imagine you have a simple application called MyK8sDashboard built in Python. This app needs to list all pods in the cluster by querying the Kubernetes API. To authenticate, it needs a service account. Let’s say you have a digital dashboard application called MyK8sDashboard. This application, much like a receptionist, needs to see a list of all rooms (pods) in the building (cluster). To do this, it needs a service account keycard to access the Kubernetes API.</p><p>Here’s how to create and use a service account:</p><ol><li><strong>Create a Service Account:</strong></li></ol><p>To issue a keycard to our receptionist application dashboard, we run:</p><pre>kubectl create serviceaccount &lt;dashboard-name&gt;</pre><p>kubectl create serviceaccount dashboard</p><p>To see the created service accounts, use:</p><pre>kubectl get serviceaccounts</pre><p>If there is a deployment and wanted to update the deployment to use the newly created ServiceAccount. Use the kubectl edit command for the deployment and specify the serviceAccountName field inside the pod spec.</p><p>OR</p><p>Make use of the kubectl set command. Run the following command to use the newly created service account: - kubectl set serviceaccount deploy/web-dashboard dashboard.</p><ol><li><strong>Service Account Token:</strong> When you create a service account, Kubernetes automatically generates a token for it. This token is stored as a secret.</li></ol><p>To view the secret:</p><pre>kubectl describe secret &lt;secret-name&gt;</pre><p>This token is used for authenticating API requests.</p><p>How to find what is the secret token used by the default service account?</p><p>Run the command kubectl describe serviceaccount &lt;serviceaccount-name&gt; and look at the Tokens field.</p><ol><li><strong>Example YAML File for a Pod Using a Service Account:</strong></li></ol><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: my-dashboard-pod<br>spec:<br>  serviceAccountName: dashboard<br>  containers:<br>  - name: my-dashboard<br>    image: nginx</pre><p>In this YAML file, the pod my-dashboard-pod uses the dashboard service account.</p><p>In this setup, our receptionist’s office automatically receives the dashboard service account keycard. The keycard is kept safe in a designated drawer (mounted as a secret in the pod).</p><p><strong>Automatic Token Mounting</strong></p><p>Kubernetes can automatically mount service account tokens into pods. Each namespace has a default service account. If no specific service account is mentioned, Kubernetes uses this default one. Kubernetes ensures that every office (pod) has a keycard. If no specific keycard is assigned, the default keycard is used. This default keycard is automatically placed in a standard drawer (/var/run/secrets/kubernetes.io/serviceaccount).</p><p>When a pod is created, the default service account token is mounted at /var/run/secrets/kubernetes.io/serviceaccount.</p><p>How to find what location is the ServiceAccount credentials available within the pod?</p><p>Run the command kubectl describe pod &lt;pod-name&gt; and look for volume mount path.</p><p><strong>Viewing the Token in a Pod</strong></p><p>Once the pod is running, you can check the token inside the pod:</p><pre>kubectl exec -it my-dashboard-pod - cat /var/run/secrets/kubernetes.io/serviceaccount/token</pre><p><strong>Changes in Kubernetes 1.22 and 1.24</strong></p><p>Earlier, service account tokens had no expiration, which posed security risks. Starting from version 1.22, Kubernetes introduced time-bound and audience-bound tokens using the Token Request API.</p><p>In version 1.24, creating non-expiring tokens automatically was stopped. From version 1.24, you need to manually create them if necessary:</p><p>kubectl create token &lt;service-account-name&gt;</p><p>Here’s an example YAML for creating a secret with a non-expiring token:</p><pre>apiVersion: v1<br>kind: Secret<br>metadata:<br>  name: my-secret<br>  annotations:<br>    kubernetes.io/service-account.name: my-service-account<br>type: kubernetes.io/service-account-token</pre><p><strong>Remember:</strong> Use the Token Request API for better security and scalability.</p><p>In Kubernetes 1.30, several significant updates have been introduced for service accounts and overall security enhancements. Here are some of the key updates related to service accounts and other security improvements:</p><p>1. <strong>Fine-Grained Policy Control</strong>: Kubernetes 1.30 introduces KEP-3716, allowing administrators to define more granular and tailored policies. This update is crucial for environments needing customized security policies to meet specific requirements. For example, administrators can now create more precise validation rules for API operations, enhancing control over security configurations​ (<a href="https://kubernetes.io/blog/2024/03/12/kubernetes-1-30-upcoming-changes/">Production-Grade Container Orchestration</a>)​.</p><p>2. <strong>Data Security Enhancements</strong>:</p><ul><li><strong>Prevent Unauthorized Volume Mode Conversion</strong>: Kubernetes now prevents unauthorized volume mode conversions during volume restores, a feature aimed at protecting data integrity. This is controlled through explicit authorization requirements, ensuring that sensitive operations like volume mode changes are tightly regulated​ (<a href="https://securityboulevard.com/2024/03/kubernetes-1-30-a-security-perspective/">Security Boulevard</a>)​.</li><li><strong>Speed Up SELinux Label Changes</strong>: The process for applying SELinux labels to mounted volumes has been optimized to speed up container startup times, which is particularly beneficial in environments with large volumes and numerous files​ (<a href="https://securityboulevard.com/2024/03/kubernetes-1-30-a-security-perspective/">Security Boulevard</a>)​.</li></ul><p>3. <strong>User Namespace Support</strong>: Support for user namespaces in pods has been introduced, enhancing security by allowing UID and GID remapping within pods. This feature improves isolation and mitigates risks associated with privilege escalation​ (<a href="https://securityboulevard.com/2024/03/kubernetes-1-30-a-security-perspective/">Security Boulevard</a>)​.</p><p>4. <strong>AppArmor and Authorization Improvements</strong>:</p><ul><li><strong>AppArmor Support</strong>: Kubernetes 1.30 now supports AppArmor profiles, enabling administrators to define and enforce security policies at the container level. This update adds a robust layer of protection against various security threats​ (<a href="https://securityboulevard.com/2024/03/kubernetes-1-30-a-security-perspective/">Security Boulevard</a>)​.</li><li><strong>Structured Authorization Configuration</strong>: Structured authorization configuration allows for more granular and customizable authorization controls, enhancing the security posture of Kubernetes clusters. This feature supports the use of multiple authorization webhooks, providing better control over resource access​ (<a href="https://securityboulevard.com/2024/03/kubernetes-1-30-a-security-perspective/">Security Boulevard</a>)​.</li></ul><p>5. <strong>Removal of Deprecated Plugins</strong>: The SecurityContextDeny admission plugin has been officially removed in favor of the PodSecurity admission plugin, which offers a more modern and flexible approach to managing security contexts in pods​ (<a href="https://securityboulevard.com/2024/03/kubernetes-1-30-a-security-perspective/">Security Boulevard</a>)​.</p><p>These updates in Kubernetes 1.30 reflect the ongoing efforts to enhance security, improve data protection, and provide administrators with more control over their Kubernetes environments. For more detailed information, you can refer to the official Kubernetes documentation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1821debe2ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Resources and limits that you need to know]]></title>
            <link>https://medium.com/@cloudwithsaj/resources-and-limits-that-you-need-to-know-0c10f005fd3b?source=rss-ce0bdf3df949------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c10f005fd3b</guid>
            <category><![CDATA[kubernetes-resource]]></category>
            <category><![CDATA[resource-quota]]></category>
            <category><![CDATA[limits-in-kubernetes]]></category>
            <category><![CDATA[resource-management]]></category>
            <dc:creator><![CDATA[Saj Sah]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 12:21:16 GMT</pubDate>
            <atom:updated>2024-06-11T12:21:16.239Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ATx88F_0IDGJaO75" /><figcaption>Photo by <a href="https://unsplash.com/@dhelentjaris?utm_source=medium&amp;utm_medium=referral">Diane Helentjaris</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Understanding Resource Management in a Kubernetes Cluster:</strong></p><p>Imagine we have a two-node Kubernetes cluster, much like a shared workspace with two rooms. Each room has a certain number of desks (CPU) and storage units (memory).</p><p>Every team (pod) that wants to work in one of these rooms needs a specific number of desks and storage units. For example, a team might need 2 desks and 1 storage unit. When a team is assigned to a room, they occupy the resources (desks and storage units) available in that room.</p><p>In Kubernetes, the “office manager” is the scheduler. The scheduler decides which room (node) a team (pod) should go to. It looks at the number of desks and storage units required by the team and those available in each room to determine the best fit. For instance, if Room 2 has enough desks and storage units, the scheduler will assign the new team to Room 2. If no room has enough resources, the team will be left waiting in the lobby (pod in a pending state). Similarly, Kubernetes scheduler that decides which node a pod goes to. The scheduler takes into consideration the number of resources required by a pod and those available on the nodes and identifies the best node to place a pod on. In this case, the scheduler schedules a new pod on node two because there are sufficient resources available on that node. If nodes have no sufficient resources available, the scheduler avoids placing the pod on those nodes and instead places the pod on one where sufficient resource are available. And if there is no sufficient resources available on any of the nodes, then the scheduler holds back scheduling the pod and you will see “The pod in a pending state” and if you look at the events using the Kubectl describe pod command you will see there is an insufficient CPU. Using the kubectl describe pod command, you can check the event logs to see if there are insufficient resources like CPU.</p><p><strong>Resource Requests:</strong></p><p>Now, let’s dive deeper into specifying the resources for each team. When creating a team, you can specify the number of desks (CPU) and storage units (memory) they need.</p><p>For example:</p><pre>Cpu=1<br>Memory=1</pre><p>This means the team requires 1 desk and 1 GB of storage. This is known as a resource request, which is the minimum amount of resources a team needs to function. The scheduler uses these numbers to find a room with enough resources.</p><p>Here’s how you specify it in a pod definition file:</p><p>So to do this in the sample pod definition file, all you need to do is add a section called resources under which add requests and specify the new values for memory and CPU usage.</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: frontend<br>spec:<br>  containers:<br>  - name: app<br>    image: nginx<br>    resources:<br>      requests:<br>        memory: “2Gi”<br>        cpu: 2</pre><p><strong>Understanding CPU Requests:</strong></p><p>One “count” of CPU is akin to one virtual desk. In cloud environments like AWS, it’s equivalent to one vCPU, GCP or Azure. We can request as little as 0.1 CPU, where 0.1 CPU = 100m (milli-CPUs). This flexibility allows you to fine-tune resource allocation based on your team’s needs and the available resources.</p><p><strong>Understanding Resource Limits in Kubernetes Pods:</strong></p><p>Limits: As the name suggest, it sets a certain limit number to use for the resources.</p><p>Imagine you have an office building with various rooms (nodes) where different teams (containers) are working. By default, each team can use as many office resources (CPU and memory) as they want. If one team decides to use all the conference rooms and office supplies, it leaves very little room for other teams to work efficiently, causing a bottleneck.</p><p>To prevent one team from monopolizing all the resources, you can set limits on how much they can use. For instance, you can specify that a team can only use one conference room (1 vCPU) and a certain amount of office supplies (memory). This ensures a fair distribution of resources among all teams in the building.</p><p>In Kubernetes, this is done by setting resource limits for your pods. By specifying these limits, you ensure that no single container can consume more than its fair share of resources, thus preventing it from suffocating other processes or containers on the node.</p><p>Similary, a container that’s part of a pod starts with one CPU on a node, it can go up and consume as much resources as it requires and that suffocates the native processes on the node or other containers of resources. However, we can set a limit for the resource usage on these pods by using “limits”. For example, if you set a limit of one vCPU to the containers a container and that will be limited to consume only one vCPU from that node and the same applies to memory. For example we can set a limit of 512Mi on containers like below.</p><p>Here’s how you can set these limits in your pod definition file:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: limit-pod<br>spec:<br>  containers:<br>  - name: limit-container<br>    image: nginx<br>    resources:<br>      limits:<br>        memory: &quot;512Mi&quot;<br>        cpu: &quot;1&quot;</pre><p><strong>What Happens When a Pod Tries to Exceed Its Resource Limits:</strong></p><p>Imagine you have set clear rules in an office building that each team (container) can only use a certain number of conference rooms (CPU) and a specified amount of office supplies (memory). If a team tries to exceed its allocated resources, it will encounter restrictions.</p><p>For example, if a team is only allowed one conference room but tries to book a second one, they will be denied access to the extra room. Similarly, if they try to use more office supplies than their limit, they will find that they cannot get more supplies until they stay within their assigned quota.</p><p>In Kubernetes, when a pod (team) tries to exceed its resource limits, it gets restricted by the system. If a container tries to use more CPU than allocated, it will be throttled, meaning its CPU usage will be limited to its maximum allowed value. If it tries to use more memory than allowed, it will be terminated and restarted by the system.</p><p>Here’s what happens technically:</p><ol><li><strong>CPU Limits:</strong></li></ol><ul><li><strong>Behavior:</strong> The container will be throttled, meaning it will not be able to use more CPU than specified. It will have to wait for CPU time, leading to reduced performance.</li><li><strong>Error:</strong> No specific error is reported; the container just gets less CPU time.</li></ul><ol><li><strong>Memory Limits:</strong></li></ol><ul><li><strong>Behavior:</strong> If a container exceeds its memory limit, it will be terminated and may be restarted.</li><li><strong>Error:</strong> The pod status will show “OOMKilled” (Out of Memory Killed).</li></ul><p>Here’s a sample pod definition file with resource limits:</p><p>limited-resources-pod.yaml</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: limited-resources-pod<br>spec:<br>  containers:<br>  - name: example-container<br>    image: nginx<br>    resources:<br>      limits:<br>        memory: &quot;512Mi&quot;<br>        cpu: &quot;1&quot;</pre><p>In this YAML file, the container example-container is limited to 1 vCPU and 512 MiB of memory. If the container tries to exceed these limits, the following will occur:</p><ul><li><strong>CPU:</strong> The container will be throttled to 1 vCPU, and any attempt to use more CPU will be restricted.</li></ul><p>When a container exceeds its CPU limit, it gets throttled. The container will not receive more CPU time than its limit, but it won’t produce a specific error message. However, you can monitor the CPU throttling using tools like kubectl top pod and kubectl describe pod.</p><p>Here is an example command and output to check CPU usage and throttling:</p><p>kubectl top pod &lt;pod-name&gt;</p><pre>kubectl top pod limited-resources-pod</pre><p>Output:</p><pre>NAME                     CPU(cores)   MEMORY(bytes)<br>limited-resources-pod    1000m        250Mi</pre><p>This output shows that the pod is using exactly its CPU limit (1000 milli-cores or 1 CPU). If the pod requests more CPU, it will be throttled to this limit.</p><ul><li><strong>Memory:</strong> If the container exceeds 512 MiB of memory usage, it will be terminated with an “OOMKilled” error, and Kubernetes will attempt to restart it.</li></ul><p>When a container exceeds its memory limit, it will be terminated with an “OOMKilled” error. You can see this error using the kubectl describe pod command.</p><p>Here is an example command and output to check the status of the pod:</p><pre>kubectl describe pod limited-resources-pod<br><br>Output:<br>Name:           limited-resources-pod<br>Namespace:      default<br>Priority:       0<br>Node:           node-1/192.168.1.1<br>Start Time:     Mon, 10 Jun 2024 10:00:00 GMT<br>Labels:         &lt;none&gt;<br>Annotations:    &lt;none&gt;<br>Status:         Running<br>IP:             192.168.1.10<br>Containers:<br>  example-container:<br>    Container ID:   docker://1234567890abcdef<br>    Image:          nginx<br>    Image ID:       docker-pullable://nginx@sha256:1234567890abcdef<br>    Port:           &lt;none&gt;<br>    Host Port:      &lt;none&gt;<br>    State:          Terminated<br>      Reason:       OOMKilled<br>      Exit Code:    137<br>      Started:      Mon, 10 Jun 2024 10:00:00 GMT<br>      Finished:     Mon, 10 Jun 2024 10:05:00 GMT<br>    Last State:     Terminated<br>      Reason:       OOMKilled<br>      Exit Code:    137<br>      Started:      Mon, 10 Jun 2024 09:55:00 GMT<br>      Finished:     Mon, 10 Jun 2024 10:00:00 GMT<br>    Ready:          False<br>    Restart Count:  1<br>    Limits:<br>      cpu:     1<br>      memory:  512Mi<br>    Requests:<br>      cpu:     100m<br>      memory:  128Mi<br>    Environment:    &lt;none&gt;<br>    Mounts:<br>      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access (ro)<br>Conditions:<br>  Type              Status<br>  Initialized       True <br>  Ready             False <br>  ContainersReady   False <br>  PodScheduled      True <br>Volumes:<br>  kube-api-access:<br>    Type:        Projected (a volume that contains injected data from multiple sources)<br>    TokenExpirationSeconds: 3607<br>    ConfigMapName: kube-root-ca.crt<br>    ConfigMapOptional: &lt;nil&gt;<br>    DownwardAPI: true<br>QoS Class:       Burstable<br>Node-Selectors:  &lt;none&gt;<br>Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s<br>                 node.kubernetes.io/unreachable:NoExecute for 300s</pre><p>Events:</p><p>Type Reason Age From Message</p><p>— — — — — — — — — — — — -</p><p>Warning OOMKilled 5m kubelet, node-1 Container was killed due to memory</p><p>This belowpod has been deployed in the default namespace. It fails to get to a running state.</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: OOM-demo<br>  namespace: default<br>spec:<br>  containers:<br>  - args:<br>    - --vm<br>    - &quot;1&quot;<br>    - --vm-bytes<br>    - 15M<br>    - --vm-hang<br>    - &quot;1&quot;<br>    command:<br>    - stress<br>    image: polinux/stress<br>    imagePullPolicy: Always<br>    name: mem-stress<br>    resources:<br>      limits:<br>        memory: 20Mi<br>      requests:<br>        memory: 5Mi</pre><pre>saj@controlplane ~ ➜  k describe po OOM-demo<br>Name:             OOM-demo<br>Namespace:        default<br>Priority:         0<br>Service Account:  default<br>Node:             controlplane/192.20.77.9<br>Start Time:       Tue, 11 Jun 2024 09:50:02 +0000<br>Labels:           &lt;none&gt;<br>Annotations:      &lt;none&gt;<br>Status:           Running<br>IP:               10.42.0.10<br>IPs:<br>  IP:  10.42.0.10<br>Containers:<br>  mem-stress:<br>    Container ID:  containerd://6bb955bde3c80973523b1f87a779533697af10696952fdb8678226d186e397ae<br>    Image:         polinux/stress<br>    Image ID:      docker.io/polinux/stress@sha256:b6144f84f9c15dac80deb48d3a646b55c7043ab1d83ea0a697c09097aaad21aa<br>    Port:          &lt;none&gt;<br>    Host Port:     &lt;none&gt;<br>    Command:<br>      stress<br>    Args:<br>      --vm<br>      1<br>      --vm-bytes<br>      15M<br>      --vm-hang<br>      1<br>    State:          Waiting<br>      Reason:       CrashLoopBackOff<br>    Last State:     Terminated<br>      Reason:       OOMKilled<br>      Exit Code:    1<br>      Started:      Tue, 11 Jun 2024 09:50:22 +0000<br>      Finished:     Tue, 11 Jun 2024 09:50:22 +0000</pre><p>The reason for the pod not running is OOMKilled. This means that the pod ran out of memory.</p><p>The status OOMKilled indicates that it is failing because the pod ran out of memory. Identify the memory limit set on the POD.</p><p>The “OOM-demo” pod runs a process that consumes 15Mi of memory. Increase the limit of the pod to 20Mi.</p><p>kubectl get po OOM-demo -o yaml &gt; oom.yaml and edit the file such as memory limit is set to 20Mi as follows:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: OOM-demo<br>  namespace: default<br>spec:<br>  containers:<br>  - args:<br>    - --vm<br>    - &quot;1&quot;<br>    - --vm-bytes<br>    - 15M<br>    - --vm-hang<br>    - &quot;1&quot;<br>    command:<br>    - stress<br>    image: polinux/stress<br>    name: mem-stress<br>    resources:<br>      limits:<br>        memory: 20Mi<br>      requests:<br>        memory: 5Mi</pre><p>then run kubectl replace -f oom.yaml --force. This command will delete the existing one first and recreate a new one from the YAML file.</p><p>In this example, the container was terminated with the reason “OOMKilled” due to exceeding its memory limit. The exit code 137 indicates that the container was terminated by the system’s OOM (Out Of Memory) killer.</p><p>Let’s break down what happens when containers or pods hit resource limits in Kubernetes, and how different configurations affect this behavior in following scenarios.</p><p><strong>Scenario1: No Requests or Limits Specified</strong></p><p>In the default configuration, Kubernetes doesn’t set any CPU or memory requests or limits. This means any pod can use as many resources as it needs on a node. However, this can lead to resource contention where one pod could consume all the available resources, leaving other pods or processes starved. This isn’t ideal for maintaining a balanced and fair resource distribution across your cluster.</p><p><strong>Example Scenario: Competing for CPU Resources</strong></p><p>Imagine two pods competing for CPU resources:</p><ul><li><strong>Pod A</strong> has no resource requests or limits specified.</li><li><strong>Pod B</strong> also has no resource requests or limits specified.</li></ul><p>Without any restrictions, Pod A could potentially consume all the CPU resources on the node, preventing Pod B from getting the CPU cycles it needs. This unregulated resource usage can lead to performance issues and instability in your applications.</p><p><strong>Scenario 2: No Requests Specified, but Limits are Set</strong></p><p>Now, consider a scenario where you don’t specify resource requests, but you do set resource limits:</p><ul><li><strong>Pod A</strong> has no resource requests but a limit of 3 vCPUs.</li><li><strong>Pod B</strong> is configured similarly.</li></ul><p>In this case, Kubernetes automatically assumes the request to be equal to the limit. So, both Pod A and Pod B are guaranteed up to 3 vCPUs each. This ensures that no pod can use more than its specified limit, but it also means each pod is initially allocated up to its limit.</p><p><strong>Scenario 3: Requests and Limits Set</strong></p><p>In a more controlled setup, you specify both requests and limits for your pods:</p><ul><li><strong>Pod A</strong> requests 1 vCPU and has a limit of 3 vCPUs.</li><li><strong>Pod B</strong> requests 1 vCPU and has a limit of 3 vCPUs.</li></ul><p>This configuration ensures that each pod is guaranteed at least 1 vCPU but can use up to 3 vCPUs if needed. This might seem ideal as it provides a balance between guaranteed resources and maximum usage. However, if Pod A needs more CPU cycles and Pod B isn’t using its allocated resources, Pod A can still use the available cycles beyond its request, up to its limit.</p><p><strong>The Ideal Scenario</strong></p><p>In reality, the best configuration often involves setting requests and limits that provide a balance between guaranteeing minimum resources and allowing flexible usage of available resources. For instance:</p><ul><li><strong>Pod A</strong> could request 1 vCPU but have a limit of 2 vCPUs.</li><li><strong>Pod B</strong> could request 1 vCPU but have a limit of 2 vCPUs.</li></ul><p>This setup ensures each pod gets the minimum resources it needs while allowing for some flexibility in resource usage.</p><p><strong>Example Pod Definition</strong></p><p>Here’s a YAML file illustrating how to set resource requests and limits:</p><p>Example-resource-pod.yaml</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: example-resource-pod<br>spec:<br>  containers:<br>  - name: resource-container<br>    image: nginx<br>    resources:<br>      requests:<br>        memory: &quot;1Gi&quot;<br>        cpu: &quot;1&quot;<br>      limits:<br>        memory: &quot;2Gi&quot;<br>        cpu: &quot;2&quot;</pre><p>In this example, the container within example-resource-pod requests 1 GiB of memory and 1 vCPU, but it can use up to 2 GiB of memory and 2 vCPUs if available.</p><p><strong>Scenario 4: Requests Set, but No Limits Specified</strong></p><p>When requests are set but no limits are specified, each pod is guaranteed a certain amount of resources (like 1 vCPU). However, because no limits are set, any pod can consume additional resources if available. For example, if Pod A needs more CPU cycles and Pod B is not using its requested CPU, Pod A can use the extra CPU cycles. This setup is ideal for maximizing resource usage efficiency, but it requires careful management to avoid resource contention.</p><p><strong>Why Set Limits?</strong></p><p>While setting limits can restrict resource usage, it can also prevent misuse. For instance, in our lab environments, we set limits to avoid excessive resource consumption from activities like Bitcoin mining. If you don’t need to restrict your application’s resource usage, you might choose not to set limits. However, ensure that every pod has resource requests set to guarantee a fair distribution of resources.</p><p><strong>Key Points to Remember:</strong></p><ol><li><strong>Requests Only, No Limits:</strong></li></ol><ul><li>Each pod is guaranteed its requested resources.</li><li>Pods can consume more resources if available.</li><li>Ensures flexibility and maximized resource usage.</li></ul><ol><li><strong>Requests and Limits Set:</strong></li></ol><ul><li>Each pod gets guaranteed resources and can use up to a specified limit.</li><li>Balances resource guarantees with usage caps.</li></ul><ol><li><strong>No Requests, No Limits:</strong></li></ol><ul><li>Pods can consume any amount of resources.</li><li>Risk of one pod starving others of resources.</li></ul><ol><li><strong>Requests Set, No Limits:</strong></li></ol><ul><li>Pods are guaranteed their requested resources.</li><li>Can consume additional resources if available.</li></ul><p><strong>Example of Resource Requests and Limits</strong></p><p>Here’s how you can define these in a Pod YAML file:</p><p>resource-req-limit-pod.yaml</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: example-resource-req-limit-pod<br>spec:<br>  containers:<br>  - name: example-container<br>    image: nginx<br>    resources:<br>      requests:<br>        memory: &quot;1Gi&quot;<br>        cpu: &quot;1&quot;<br>      limits:<br>        memory: &quot;2Gi&quot;<br>        cpu: &quot;2&quot;</pre><p><strong>Handling Memory Limits</strong></p><p>Memory management is similar to CPU management, but with a key difference: memory cannot be throttled. If a pod exceeds its memory limit, Kubernetes may kill the pod to free up memory. Ensure memory requests are set to guarantee resources for each pod.</p><ul><li><strong>Pod A</strong> requests 1 GiB of memory.</li><li><strong>Pod B</strong> requests 1 GiB of memory.</li></ul><p>Without limits, a pod can consume as much memory as it needs, potentially starving other pods. If limits are set, each pod can use up to its limit. If a pod needs more memory than available, Kubernetes might kill the pod to free up memory, as memory can’t be throttled like CPU.</p><p><strong>Using Limit Ranges</strong></p><p>To set hard limits,, default resource requests and limits for all pods in a namespace, use Limit Ranges. For example:</p><p>LimitRange.yaml</p><pre>kind: LimitRange<br>metadata:<br>  name: cpu-mem-limit-range<br>spec:<br>  limits:<br>  - default:<br>      cpu: &quot;500m&quot;<br>      memory: &quot;512Mi&quot;<br>    defaultRequest:<br>      cpu: &quot;200m&quot;<br>      memory: &quot;256Mi&quot;<br>    max:<br>      cpu: &quot;1&quot;<br>      memory: &quot;1Gi&quot;<br>    min:<br>      cpu: &quot;100m&quot;<br>      memory: &quot;128Mi&quot;<br>    type: Container</pre><p><strong>Setting Resource Quotas</strong></p><p>To restrict the total resources consumed by all pods in a namespace, use Resource Quotas. For example:</p><p>ResourceQuota.yaml</p><pre>apiVersion: v1<br>kind: ResourceQuota<br>metadata:<br>  name: compute-resources<br>spec:<br>  hard:<br>    requests.cpu: &quot;4&quot;<br>    requests.memory: &quot;4Gi&quot;<br>    limits.cpu: &quot;10&quot;<br>    limits.memory: &quot;10Gi&quot;</pre><p><strong>Conclusion</strong></p><p>Properly configuring resource requests and limits in Kubernetes ensures efficient resource utilization and stability. By understanding these settings, you can prevent resource contention and ensure fair distribution across your applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c10f005fd3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>