<?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 Shazia Massey  on Medium]]></title>
        <description><![CDATA[Stories by Shazia Massey  on Medium]]></description>
        <link>https://medium.com/@masseys.s123?source=rss-69c759034d1c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0hRqhErnx9-vyiJATCb94A.jpeg</url>
            <title>Stories by Shazia Massey  on Medium</title>
            <link>https://medium.com/@masseys.s123?source=rss-69c759034d1c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:58:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@masseys.s123/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[The Role of Shell Scripting in Corporate IT Automation]]></title>
            <link>https://medium.com/@masseys.s123/the-role-of-shell-scripting-in-corporate-it-automation-29b3943ad901?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/29b3943ad901</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Sat, 15 Feb 2025 13:14:22 GMT</pubDate>
            <atom:updated>2025-02-15T13:14:22.911Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R_LYdLvE14vHb-L870QtEA.jpeg" /></figure><p>Integrating shell scripting into corporate IT environments enhances automation, optimizes system reliability, and strengthens security. These scripts streamline system monitoring, DevOps workflows, backup management, and network security, significantly reducing manual intervention while mitigating the risks of downtime and operational failures.</p><h4>1- Backup Script:</h4><p>This Bash script is designed to create a timestamped backup of files from a source directory (/home/ubuntu/source) to a destination directory (/home/ubuntu/destination).</p><pre><br>1-Backup Script:<br><br>#!/bin/bash<br>SOURCE=&quot;/home/ubuntu/source&quot;<br>DESTINATION=&quot;/home/ubuntu/destination&quot;<br>DATE=$(date +%Y-%m-%d_%H-%M-%S)<br># Create backup directory and copy files<br>mkdir -p $DESTINATION/$DATE<br>cp -r $SOURCE $DESTINATION/$DATE<br>echo &quot;Backup completed on $DATE&quot;<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wvEQIFdTXpk3c0UNcb5NYg.jpeg" /></figure><p>Explanation of the script:</p><pre><br><br><br><br>SOURCE holds the path of the directory containing the files you want to back up.<br>DESTINATION holds the path of the directory where backups will be stored.<br><br><br>The date command generates the current date and time.<br>The format +%Y-%m-%d_%H-%M-%S ensures:<br>%Y → Year (e.g., 2025)<br>%m → Month (e.g., 02 for February)<br>%d → Day (e.g., 15)<br>_ → Separates date from time<br>%H → Hour (24-hour format)<br>%M → Minute<br>%S → Second<br>Example output: 2025-02-15_14-30-45<br>This timestamp is used to create a unique backup directory.<br><br><br>mkdir is used to create a new directory.<br>The -p flag ensures:<br>The directory is created if it does not exist.<br>No error occurs if the directory already exists.<br>$DESTINATION/$DATE results in a new folder inside the DESTINATION directory named after the current date and time.<br>Example:<br>If DESTINATION=&quot;/home/ubuntu/destination&quot;<br>And DATE=&quot;2025-02-15_14-30-45&quot;<br>Then the new backup directory will be /home/ubuntu/destination/2025-02-15_14-30-45/<br><br><br><br>cp is the copy command.<br>The -r flag (recursive) ensures that if SOURCE is a directory, all its contents (files and subdirectories) are copied.<br>This effectively creates a backup of everything inside /home/ubuntu/source into the newly created timestamped directory.<br><br><br><br>echo prints a message to the terminal.<br>The message confirms the backup process, including the timestamp of when it was completed.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wvEQIFdTXpk3c0UNcb5NYg.jpeg" /></figure><h4>cronjob script:</h4><p>Scheduling with Cron To run the backup script at regular intervals, use crontab -e to edit the crontab file and add: * * * * * /home/ubuntu/backup.sh This example runs the script every minute. Adjust the schedule as needed.</p><pre><br># crontab -e <br># google crontab.guru<br># *****  (At evert minute)<br># ***** /home/ubuntu/backup.sh    (paste it on nano editor)<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CCe1g6dG-K0xduxsmJVP4g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*on9_SZo5wX5oU4qax8vDLg.jpeg" /></figure><h4>2-Disk Usage Check Script:</h4><p>This Bash script <strong>monitors disk usage</strong> and <strong>issues a warning</strong> if any partition’s usage exceeds a specified threshold (80% in this case).</p><pre>#!/bin/bash<br>THRESHOLD=80<br># Check disk usage and print a warning if usage is above the threshold<br>df -H | grep -vE &#39;^Filesystem|tmpfs|cdrom&#39; | awk &#39;{ print $5 &quot; &quot; $1 }&#39; |<br>while read output;<br>do<br> usage=$(echo $output | awk &#39;{ print $1}&#39; | cut -d&#39;%&#39; -f1)<br> partition=$(echo $output | awk &#39;{ print $2 }&#39;)<br>if [ $usage -ge $THRESHOLD ]; then<br> echo &quot;Warning: Disk usage on $partition is at ${usage}%&quot;<br>fi<br>done<br><br><br><br><br># vi dus.sh<br># sudo chmod +x dus.sh<br># ./dus.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F_yifOEbCUYngy39LldjKA.jpeg" /></figure><p>Explanation of script:</p><pre><br><br># THRESHOLD is set to 80, meaning the script will issue a warning if disk usage on any partition is 80% or higher.<br>This is a variable that can be changed depending on the requirement.<br><br># df (disk free) reports the amount of used and available disk space.<br>The -H flag makes the output human-readable, using power-of-10 units (e.g., MB, GB).<br><br><br># grep -vE excludes (-v) lines that match the patterns Filesystem, tmpfs, or cdrom.<br>^Filesystem → Removes the header row.<br>tmpfs → Excludes temporary filesystems (RAM disks).<br>cdrom → Excludes CD-ROM devices.<br><br><br># awk &#39;{ print $5 &quot; &quot; $1 }&#39;: Extracts the usage percentage and partition name.<br>while read output: Iterates over each line of the filtered output.<br>usage=$(echo $output | awk &#39;{ print $1}&#39; | cut -d&#39;%&#39; -f1): Extracts the usage percentage.<br>partition=$(echo $output | awk &#39;{ print $2 }&#39;): Extracts the partition name.<br>if [ $usage -ge $THRESHOLD ]; then: Checks if the usage exceeds the threshold.<br>echo &quot;Warning: Disk usage on 𝑝𝑎𝑟𝑡𝑖𝑡𝑖𝑜𝑛𝑖𝑠𝑎𝑡{usage}%&quot;: Prints a warning message.<br><br></pre><h4>3- Service Health Check Script:</h4><p>This Bash script <strong>checks if a service (e.g., Nginx) is running</strong> and <strong>restarts it if it is not running</strong>.</p><pre># nginx<br># sudo apt install nginx<br># sudo systemctl start nginx<br># vi service-status-check.sh</pre><pre>#!/bin/bash<br>SERVICE=&quot;nginx&quot;<br># Check if the service is running, if not, start it<br>if systemctl is-active --quiet $SERVICE; then<br>echo &quot;$SERVICE is running&quot;<br>else<br>echo &quot;$SERVICE is not running&quot;<br> systemctl start $SERVICE<br>fi</pre><pre># ls<br># sudo chmod +x service-status-check.sh<br># ./service-status-check.sh</pre><h4>Explanation:</h4><pre><br>The #!/bin/bash at the top is called the shebang.<br>It tells the system to execute the script using the Bash shell (/bin/bash).<br><br><br># Assigns the service name (nginx) to the variable $SERVICE.<br>This allows flexibility—changing &quot;nginx&quot; to another service (e.g., &quot;apache2&quot;) will make the script work for that service.<br><br><br># systemctl is-active $SERVICE → Checks if the service is currently running.<br>--quiet → Suppresses output, meaning nothing is printed if the service is running.<br>if ...; then → Starts an if condition:<br>If the service is running, execute the code inside the then block.<br>If not running, execute the else block.<br><br><br># If the service is not running, the else block executes.<br>echo &quot;$SERVICE is not running&quot; → Prints a message.<br>systemctl start $SERVICE → Starts the service.<br><br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VQcwcLrhtH8XYKLRexWS3A.jpeg" /></figure><h4>4. Network Connectivity Check Script:</h4><p>This script <strong>checks whether a host (e.g., </strong><strong>google.com) is reachable</strong> using the ping command. If the host is reachable, it logs a success message; otherwise, it logs a failure message.</p><pre>#!/bin/bash<br>HOST=&quot;google.com&quot;<br># Output file<br>OUTPUT_FILE=&quot;/home/ubuntu/output.txt&quot;<br># Check if the host is reachable<br>if ping -c 1 $HOST &amp;&gt; /dev/null<br>then<br>echo &quot;$HOST is reachable&quot; &gt;&gt; $OUTPUT_FILE<br>else<br>echo &quot;$HOST is not reachable&quot; &gt;&gt; $OUTPUT_FILE<br>fi</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VpTEvEgOwHvjodWS3G1rlg.jpeg" /></figure><p>Explanation:</p><pre><br># HOST variable stores the domain name (google.com) that the script will check.<br>This value can be changed to another domain (e.g., &quot;example.com&quot;) or an IP address.<br><br># The script writes logs to /home/ubuntu/output.txt.<br>If the file does not exist, it will be automatically created.<br>If it already exists, new logs will be appended (&gt;&gt; operator).<br><br># ping -c 1 $HOST<br><br>ping → Sends ICMP echo requests (used to check connectivity).<br>-c 1 → Sends only 1 packet (instead of continuous pinging).<br>$HOST → Uses the variable (google.com).<br>&amp;&gt; /dev/null<br><br># Redirects both stdout (standard output) and stderr (error output) to /dev/null.<br>This prevents the command output from displaying on the terminal.<br></pre><h4>5- Database Backup Scripts:</h4><p>This Bash script automates the <strong>backup of a MySQL database</strong> by using mysqldump, stores it in a designated backup directory, and appends a timestamp to ensure each backup file is unique.</p><p>First install MySQL:</p><pre>Install MySQL:<br># sudo apt install mysql-server<br># sudo mysql -u root<br><br><br>Set up MySQL password:<br># ALTER USER &#39;root&#39;@&#39;localhost&#39; IDENTIFIED WITH mysql_native_password BY<br>&#39;root&#39;;<br>FLUSH PRIVILEGES;<br><br><br><br>mysql&gt; show databases;<br>mysql&gt; create database mydatabase;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EH0jGIwGQrp--_yihaeZiw.jpeg" /></figure><pre><br># mkdir db_back<br># vi db_backup.sh<br><br>cd Script:<br><br>#!/bin/bash<br>DB_NAME=&quot;mydatabase&quot;<br>BACKUP_DIR=&quot;/home/ubuntu/Scripts/db_back&quot;<br>DATE=$(date +%Y-%m-%d_%H-%M-%S)<br># Perform a database backup and save it to the backup directory<br>mysqldump -u root -p $DB_NAME &gt; $BACKUP_DIR/$DB_NAME-$DATE.sql<br>echo &quot;Database backup completed: $BACKUP_DIR/$DB_NAME-$DATE.sql&quot;<br><br><br><br><br># sudo chmod +x db_backup.sh<br># ./db_backup.sh<br># password---root<br><br><br> </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aNBgGKxO2vi9JPjT6u3sOw.jpeg" /></figure><p>Explanation:</p><pre><br># mysqldump -u root -p $DB_NAME &gt; $BACKUP_DIR/$DB_NAME-$DATE.sql<br><br>==&gt;  mysqldump → A MySQL utility that exports a database.<br>==&gt;  -u root → Specifies the username (root).<br>==&gt;  -p → Prompts the user to enter the MySQL password (it will not be displayed on the screen).<br>==&gt;  $DB_NAME → The name of the database to be backed up.<br>==&gt;  &gt; → Redirects the output to a file.<br>==&gt;  $BACKUP_DIR/$DB_NAME-$DATE.sql → The backup file location and name.<br><br><br></pre><h4>6-System Uptime Check Script:</h4><p>This is a simple <strong>Bash script</strong> that retrieves and displays the <strong>system uptime</strong>, which tells how long the system has been running since the last reboot.</p><pre># vi sys-uptime.sh<br><br>#!/bin/bash<br># Print the system uptime<br>uptime -p<br><br><br><br><br># sudo chmod +x sys-uptime.sh<br># ./sys-uptime.sh</pre><h4>Explanation</h4><ul><li><strong>uptime -p</strong>: Prints the system uptime in a human-readable format.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n6J2PEtHfwerD8kCrw9jEQ.jpeg" /></figure><h4>7-Listening Ports Script:</h4><p>This <strong>Bash script</strong> retrieves and displays all <strong>listening network ports</strong> along with their associated services using the netstat command.</p><pre><br> Install net-tools:<br># sudo apt install net-tools<br><br># vi ports.sh<br><br><br>#!/bin/bash<br># List all listening ports and the associated services<br>netstat -tuln | grep LISTEN<br><br><br># sudo chmod +x ports.sh<br># ./ports.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BvynSo5yxwjpOJmAjmuTkg.jpeg" /></figure><p>Explanation:</p><pre><br># netstat -tuln | grep LISTEN<br><br><br>netstat → A command-line tool that provides network-related statistics, including open connections, routing tables, and active ports.<br>-t → Displays TCP connections.<br>-u → Displays UDP connections.<br>-l → Lists only listening ports (services waiting for incoming connections).<br>-n → Displays numeric IP addresses and port numbers instead of resolving hostnames.<br>| grep LISTEN → Filters the output to show only listening services.</pre><h4>8-Automatic Package Updates Scripts:</h4><p>This script is designed to <strong>update</strong> system packages, <strong>upgrade</strong> installed packages, <strong>remove unnecessary packages</strong>, and <strong>clean up package caches</strong> in a Debian-based Linux system (such as Ubuntu).</p><pre>#!/bin/bash<br># Update system packages and clean up unnecessary packages<br>sudo apt-get update &amp;&amp; sudo apt-get upgrade -y &amp;&amp; sudo apt-get autoremove -y &amp;&amp; sudo apt-get <br>clean<br>echo &quot;System packages updated and cleaned up&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MNN1IHLuznSqw7_fOIp5ng.jpeg" /></figure><h4>Explanation:</h4><pre><br>=&gt;   apt-get update: Updates the package list.<br>=&gt;   apt-get upgrade -y: Upgrades all installed packages.<br>=&gt;   apt-get autoremove -y: Removes unnecessary packages.<br>=&gt;   apt-get clean: Cleans up the package cache.<br>=&gt;   echo &quot;System packages updated and cleaned up&quot;: Outputs a message indicating the completion of the update and cleanup.<br></pre><h4>9-HTTP Response Times Script:</h4><p>This script is designed to <strong>check the HTTP response time</strong> for multiple URLs using curl.</p><pre><br># vi response.sh<br><br><br><br>#!/bin/bash<br>URLS=(&quot;https://www.devopsshack.com/&quot; &quot;https://www.linkedin.com/&quot;)<br># Check HTTP response times for multiple URLs<br>for URL in &quot;${URLS[@]}&quot;; do<br> RESPONSE_TIME=$(curl -o /dev/null -s -w &#39;%{time_total}\n&#39; $URL)<br>echo &quot;Response time for $URL: $RESPONSE_TIME seconds&quot;<br>done<br><br><br><br># sudo chmod +x response.sh<br># ./response.sh<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rfr2qmCK3yNZ6LaLoGvbmg.jpeg" /></figure><p>Explanation:</p><pre><br>#!/bin/bash → Specifies that the script should be executed using the Bash shell.<br># URLS=(&quot;https://www.devopsshack.com/&quot; &quot;https://www.linkedin.com/&quot;) → Declares an array containing multiple URLs.<br># for URL in &quot;${URLS[@]}&quot;; do ... done → Loops through each URL in the array.<br># curl -o /dev/null -s -w &#39;%{time_total}\n&#39; $URL → Fetches the HTTP response time for the given URL:<br># -o /dev/null → Discards the response content.<br># -s → Silent mode (hides output and errors).<br># -w &#39;%{time_total}\n&#39; → Extracts and prints the total response time.<br># echo &quot;Response time for $URL: $RESPONSE_TIME seconds&quot; → Prints the response time for each URL.<br></pre><h4>10-Monitor System Processes and Memory Usage Script:</h4><pre><br># vi mem.sh<br><br><br>#!/bin/bash<br># Monitor system processes and their memory usage<br>ps aux --sort=-%mem | head -n 10<br><br><br># sudo chmod +x mem.sh<br># ./mem.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hrcjisnhgOIRuDElyj9UIg.jpeg" /></figure><h4>Explanation:</h4><pre><br>ps aux<br><br>The ps command is used to display information about running processes.<br>a → Shows processes for all users.<br>u → Displays user-oriented format (shows user, CPU/memory usage, etc.).<br>x → Lists processes not attached to a terminal (background processes).<br>--sort=-%mem<br><br>Sorts the processes in descending order (-) based on memory usage (%mem).<br>The highest memory-consuming processes appear at the top.<br>| head -n 10<br><br>| → Pipe operator (passes output from one command to another).<br>head -n 10 → Displays the top 10 processes with the highest memory usage.<br><br></pre><p>The scripts we covered demonstrate how <strong>Bash can be used to enhance efficiency, reliability, and security</strong> in system administration and DevOps workflows.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29b3943ad901" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes Troubleshooting]]></title>
            <link>https://medium.com/@masseys.s123/kubernetes-troubleshooting-db4381b61727?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/db4381b61727</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Tue, 07 Jan 2025 16:14:25 GMT</pubDate>
            <atom:updated>2025-01-07T16:14:25.505Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_T798WIAYCRzt7OjDTw18A.jpeg" /></figure><h3>***Troubleshooting Application Failures:</h3><p>Let’s begin by examining a two-tier application consisting of a web server and a database server.</p><ul><li>The <strong>database pod</strong> hosts the database application and serves the web server through a database service.</li><li>The <strong>web server</strong> is hosted in a web pod and serves users via a web service.</li></ul><h4>Users Report Application Access Issues</h4><p>When users report issues accessing the application, start with the application’s front end:</p><ol><li><strong>Check Application Accessibility</strong></li></ol><p>— If it’s a web application, verify that the web server is accessible. Use tools like cURL to test connectivity on the node port’s IP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p1ay9EQz6ofw7oQ87puUKA.jpeg" /></figure><p><strong>2. Inspect the Web Service</strong></p><p>— Confirm that the service has discovered the endpoints for the web pod.</p><p>— If no endpoints are discovered, verify the service-to-pod mapping:</p><p>Compare the selectors configured in the service with those defined on the pod. Ensure they align.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_jG4RjZkCE4JqF49MdsTbw.jpeg" /></figure><p><strong>3. Examine the Web Pod</strong></p><p>— Verify the pod is in a <strong>Running</strong> state.</p><p>— Check the pod’s status and the number of restarts. Frequent restarts might indicate application-level issues.</p><p>— Use the kubectl describe command to review events associated with the pod.</p><p>— Analyze application logs with the kubectl logs command. If the pod is restarting frequently, use the --previous option to inspect logs from the last instance of the container. Alternatively, use the --follow option to monitor logs in real time and wait for the application to fail again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eHhXS3Mp6FIersmPiIZzLg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MeXYCsoFkjfiX8DTdDY5wA.jpeg" /></figure><p><strong>4. Validate the Database Service and Pod</strong></p><p>— Check the status of the database service, following the same steps as the web service.</p><p>— Investigate the database pod:</p><p>Review its logs for errors or failures in the database application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lPprJmNie_C8BqyAkTEPFg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*38MTKaqyC65XVNJudqKjlw.jpeg" /></figure><h3>***Troubleshooting Control Plane Failures:</h3><h4>1. Check Cluster Node Health</h4><p>Begin by verifying the status of the nodes in the cluster to ensure they are healthy and functioning as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_jEwAETrNAFCn2XZr2z7Zw.jpeg" /></figure><h4>2. Inspect Pod Status</h4><p>Review the status of the pods running in the cluster, particularly those associated with control plane components:</p><ul><li>For clusters deployed using <strong>Kubeadm</strong>, ensure that the pods in the kube-system namespace are running.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bO8BMeMfXzaENfSfohL7bw.jpeg" /></figure><ul><li>For control plane components deployed as services, check the status of the following services on the relevant nodes:</li></ul><p><strong>Master nodes</strong>:</p><ul><li>kube-apiserver</li><li>kube-controller-manager</li><li>kube-scheduler</li></ul><p><strong>Worker nodes</strong>:</p><ul><li>kubelet</li><li>kube-proxy</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oJcUVreP4i4NtR4l-86P6A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X6BkeFbKpcece-hXkIQjqg.jpeg" /></figure><h4>3. Review Control Plane Component Logs</h4><p>Logs provide valuable insights into issues affecting control plane components. Depending on the deployment method, the approach to accessing logs will vary:</p><ul><li><strong>Kubeadm Deployments</strong>:<br>Use the kubectl logs command to view logs from the pods hosting the control plane components in the kube-system namespace.</li><li><strong>Native Service Deployments</strong>:<br>If control plane components are configured as native services on master nodes, use the host system’s logging solution to access the logs. For instance:</li><li>Use the journalctl utility to view logs for services such as kube-apiserver.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VD-xwWFgArvAlaeIFz_9-g.jpeg" /></figure><h3>***Troubleshooting Worker Node Failures:</h3><h4>1. Check Node Status</h4><p>Start by reviewing the status of the nodes in the cluster:</p><ul><li>Determine if the nodes are reported as <strong>Ready</strong> or <strong>Not Ready</strong>.</li><li>For nodes marked as <strong>Not Ready</strong>, use the command:</li></ul><pre>kubectl describe node &lt;node-name&gt;</pre><p>— -This provides detailed information about the node’s conditions, which can help identify potential causes of failure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j5acPT8PvQ7JdJCz6aNhMw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aitd2QkCn4IlDLcrpbyt8w.jpeg" /></figure><h4>2. Analyze Node Conditions</h4><p>Each node has a set of conditions that indicate its health. These conditions are marked as True, False, or Unknown, depending on the status. Common conditions to monitor include:</p><ul><li><strong>OutOfDisk</strong>: Indicates insufficient disk space on the node.</li><li><strong>MemoryPressure</strong>: Set to True when the node is experiencing high memory usage.</li><li><strong>DiskPressure</strong>: Set to True when disk capacity is critically low.</li><li><strong>PIDPressure</strong>: Set to True when there are too many running processes on the node.</li><li><strong>Ready</strong>: Indicates that the node is healthy and operational when set to True.</li></ul><p>If a node stops communicating with the control plane (e.g., due to a crash), its status may appear as Unknown. This could signal a potential node failure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gwuIkQbyTsJZrhM9rSJ-rA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r0gfPTCBrYZbxwdkQr2wdA.jpeg" /></figure><h4>3. Investigate Node Health</h4><ul><li>Check the <strong>last heartbeat time</strong> field to identify when the node stopped communicating.</li><li>Verify whether the node is still online or has crashed. If the node has crashed, restart it.</li><li>Inspect the node’s CPU, memory, and disk resources to ensure they meet the cluster’s requirements.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2BWqj8STb_d3K1vPHL5P1Q.jpeg" /></figure><h4>4. Check kubelet Status and Logs</h4><ul><li>Confirm that the <strong>kubelet service</strong> is running on the worker node.</li><li>Review kubelet logs for any errors or warnings that might explain the issue:</li></ul><pre><br>journalctl -u kubelet</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qC41vMooIids5zO927SVCw.jpeg" /></figure><h4>5. Validate kubelet Certificates</h4><ul><li>Ensure the kubelet certificates are:</li><li>Not expired.</li><li>Assigned to the correct group.</li><li>Issued by the appropriate Certificate Authority (CA).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CpTzswyYsmMyPx4hsrewFg.jpeg" /></figure><h3>***Network Troubleshooting:</h3><p>Network Plugin in kubernetes.</p><p><strong>1. Weave Net:</strong></p><p>kubectl apply -f <a href="https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml">https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml</a></p><p><strong>2. Flannel :</strong></p><p>kubectl apply -f <a href="https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml">https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml</a></p><p><em>Note: As of now flannel does not support kubernetes network policies.</em></p><p><strong>3. Calico :</strong></p><p>curl https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml -O</p><p><em>Apply the manifest using the following command.</em></p><p>kubectl apply -f calico.yaml</p><p>Calico is said to have most advanced cni network plugin.</p><p><em>Note: If there are multiple CNI configuration files in the directory, the kubelet uses the configuration file that comes first by name in lexicographic order.</em></p><h4>DNS in Kubernetes:</h4><p>Kubernetes uses <strong>CoreDNS</strong>. <strong>CoreDNS</strong> is a flexible, extensible DNS server that can serve as the Kubernetes cluster DNS.</p><p><strong>Memory and Pods</strong></p><p>In large scale Kubernetes clusters, CoreDNS’s memory usage is predominantly affected by the number of Pods and Services in the cluster. Other factors include the size of the filled DNS answer cache, and the rate of queries received (QPS) per CoreDNS instance.</p><p>Kubernetes resources for <strong>coreDNS </strong>are:</p><ol><li><em>a service account named </em><strong><em>coredns</em></strong><em>,</em></li><li><em>cluster-roles named </em><strong><em>coredns</em></strong><em> and </em><strong><em>kube-dns</em></strong></li><li><em>clusterrolebindings named </em><strong><em>coredns</em></strong><em> and </em><strong><em>kube-dns</em></strong><em>,</em></li><li><em>a deployment named </em><strong><em>coredns</em></strong><em>,</em></li><li><em>a configmap named </em><strong><em>coredns</em></strong><em> and a</em></li><li><em>service named </em><strong><em>kube-dns</em></strong><em>.</em></li></ol><p>While analyzing the coreDNS deployment you can see that the the <strong><em>Corefile plugin</em></strong> consists of important configuration which is defined as a <strong><em>configmap</em></strong>.</p><p>Port <strong>53</strong> is used for for <em>DNS resolution</em>.</p><pre>  kubernetes cluster.local in-addr.arpa ip6.arpa {<br>       pods insecure<br>       fallthrough in-addr.arpa ip6.arpa<br>       ttl 30<br>    }<br>   </pre><p>This is the backend to k8s for <em>cluster.local and reverse domains</em>.</p><p>proxy . /etc/resolv.conf</p><p>Forward out of cluster domains directly to right <em>authoritative DNS server</em>.</p><h4>Troubleshooting issues related to coreDNS</h4><p>1. If you find <strong>CoreDNS</strong> pods in pending state first check network plugin is installed.</p><p>2. coredns pods have <strong>CrashLoopBackOff or Error state</strong></p><p>If you have nodes that are running SELinux with an older version of Docker you might experience a scenario where the coredns pods are not starting. To solve that you can try one of the following options:</p><p>a)Upgrade to a newer version of Docker.</p><p>b)Disable <strong>SELinux.</strong></p><p>c)Modify the coredns deployment to set <strong>allowPrivilegeEscalation</strong> to <em>true</em>:</p><pre>kubectl -n kube-system get deployment coredns -o yaml | \<br>  sed &#39;s/allowPrivilegeEscalation: false/allowPrivilegeEscalation: true/g&#39; | \<br>  kubectl apply -f -</pre><p>d)Another cause for <strong>CoreDNS </strong>to have CrashLoopBackOff is when a <strong>CoreDNS</strong> Pod deployed in Kubernetes detects a loop.</p><p>There are many ways to work around this issue, some are listed here:</p><ul><li>Add the following to your kubelet config yaml: <strong><em>resolvConf: &lt;path-to-your-real-resolv-conf-file&gt;</em></strong> This flag tells <strong><em>kubelet</em></strong> to pass an alternate <strong><em>resolv.conf</em></strong> to Pods. For systems using<strong> systemd-resolved</strong>, <strong><em>/run/systemd/resolve/resolv.conf</em></strong> is typically the location of the <strong><em>“real” resolv.conf</em></strong>, although this can be different depending on your distribution.</li><li>Disable the local DNS cache on host nodes, and restore <strong><em>/etc/resolv.conf</em></strong> to the original.</li><li>A quick fix is to edit your <strong>Corefile</strong>, replacing forward <strong><em>. /etc/resolv.conf</em></strong> with the IP address of your upstream DNS, for example forward <strong>. 8.8.8.8</strong>. But this only fixes the issue for <strong>CoreDNS</strong>, <strong><em>kubelet</em></strong> will continue to forward the invalid <strong><em>resolv.conf</em></strong> to all default dnsPolicy Pods, leaving them unable to resolve DNS.</li></ul><p>3. If <strong>CoreDNS</strong> pods and the<strong> kube-dns</strong> service is working fine, check the <strong>kube-dns</strong> service has valid <strong><em>endpoints</em></strong>.</p><p><em>kubectl -n kube-system get ep kube-dns</em></p><p>If there are no endpoints for the service, inspect the service and make sure it uses the correct selectors and ports.</p><h4>Kube-Proxy:</h4><p><strong>kube-proxy </strong>is a network proxy that runs on each node in the cluster. <strong>kube-proxy </strong>maintains <em>network rules on nodes</em>. These network rules allow network communication to the Pods from network sessions inside or outside of the cluster.</p><p>In a cluster configured with <strong>kubeadm</strong>, you can find <strong>kube-proxy</strong> as a <strong><em>daemonset</em></strong>.</p><p><strong>kubeproxy</strong> is responsible for watching <em>services and endpoint associated with each service</em>. When the client is going to connect to the service using the <em>virtual IP</em> the<strong> kubeproxy </strong>is responsible for <em>sending traffic to actual pods</em>.</p><p>If you run a kubectl describe ds kube-proxy -n kube-system you can see that the<strong> kube-proxy</strong> binary runs with following command inside the kube-proxy container.</p><pre><br>Command:<br>      /usr/local/bin/kube-proxy<br>      --config=/var/lib/kube-proxy/config.conf<br>      --hostname-override=$(NODE_NAME)</pre><p>So it fetches the configuration from a configuration file ie, <strong><em>/var/lib/kube-proxy/config.conf </em></strong>and we can override the hostname with the node name of at which the pod is running.</p><p>In the config file we define the <strong>clusterCIDR, kubeproxy mode, ipvs, iptables, bindaddress, kube-config</strong> etc.</p><h4>Troubleshooting issues related to kube-proxy:</h4><p>1. Check <strong>kube-proxy</strong> pod in the <strong>kube-system</strong> namespace is running.</p><p>2. Check <strong>kube-proxy </strong>logs.</p><p>3. Check <strong>configmap</strong> is correctly defined and the config file for running <strong>kube-proxy </strong>binary is correct.</p><p>4. <strong>kube-config</strong> is defined in the <strong>config map</strong>.</p><p>5. check <strong>kube-proxy</strong> is <em>running</em> inside the container</p><pre><br># netstat -plan | grep kube-proxy<br>tcp        0      0 0.0.0.0:30081           0.0.0.0:*               LISTEN      1/kube-proxy<br>tcp        0      0 127.0.0.1:10249         0.0.0.0:*               LISTEN      1/kube-proxy<br>tcp        0      0 172.17.0.12:33706       172.17.0.12:6443        ESTABLISHED 1/kube-proxy<br>tcp6       0      0 :::10256                :::*                    LISTEN      1/kube-proxy</pre><h3>Final Thoughts:</h3><p>Effective Kubernetes troubleshooting involves accurately identifying the root cause, implementing the necessary fixes to restore functionality, and thoroughly verifying the resolution. Key insights gained from the process include identifying and addressing configuration gaps, enhancing monitoring systems, and adopting preventive measures to mitigate similar issues in the future. This iterative approach not only resolves immediate challenges but also strengthens the overall reliability and resilience of Kubernetes environments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db4381b61727" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Hidden Power of Kubernetes Networking: Beyond Basics]]></title>
            <link>https://medium.com/@masseys.s123/the-hidden-power-of-kubernetes-networking-beyond-basics-96f876cc52fb?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/96f876cc52fb</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Mon, 30 Dec 2024 10:21:40 GMT</pubDate>
            <atom:updated>2024-12-30T10:21:40.790Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0fOXbQOYc-h9PDDN1dtbCQ.jpeg" /></figure><h3>***Key Concepts in Networking</h3><p>Prerequisite — Switching Routing</p><ol><li><strong>Basic Networking Principles</strong>:</li></ol><ul><li><strong>Switches</strong> enable communication within a single network by connecting devices.</li><li><strong>Routers</strong> interconnect networks, facilitating communication across different subnets.</li><li><strong>Gateways</strong> serve as access points to external networks, including the internet.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ylK-I4qEgvYvhAOEEojtg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u6oQLt2TXRj__S43Mw3EAw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dqg_mZ2Uor2wC9-drCt55w.jpeg" /></figure><p><strong>2. Linux Networking Tools</strong>:</p><ul><li>ip link: View and modify network interfaces.</li><li>ip addr: Display or assign IP addresses to interfaces.</li><li>route or ip route: View and configure routing tables.</li></ul><p><strong>3. Network Namespaces</strong>:</p><ul><li>Understand the basics of network namespaces in Linux, which isolate network configurations for applications, ensuring better security and performance.</li></ul><p><strong>4. DNS and CoreDNS</strong>:</p><ul><li>Explore <strong>DNS fundamentals</strong> and how to configure DNS settings on Linux systems.</li><li>Gain a basic introduction to <strong>CoreDNS</strong>, a DNS server often used in Kubernetes.</li></ul><p><strong>5. Routing and IP Forwarding</strong>:</p><ul><li><strong>Routing tables</strong> define how packets travel across networks. Use commands like ip route add to add routing entries.</li><li><strong>IP forwarding</strong> in Linux governs the ability to route packets between interfaces. Enable forwarding via /proc/sys/net/ipv4/ip_forward for private networks.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TNwoPGgkeMrLul77sjBgyQ.jpeg" /></figure><p><strong>6. Persistent Changes</strong>:</p><ul><li>Ensure that network configurations persist across reboots by modifying files like /etc/network/interfaces or /etc/sysctl.conf.</li></ul><h3>Connecting Networks</h3><p>Consider a scenario where systems A and B are connected to one subnet (e.g., 192.168.1.0/24), and systems C and D are on another (192.168.2.0/24). A router links these subnets by assigning IPs on both networks (e.g., 192.168.1.1 and 192.168.2.1). Adding routes on each system ensures seamless communication between them.</p><p>For example:</p><ul><li>Add a route on system A</li></ul><pre>ip route add 192.168.2.0/24 via 192.168.1.1</pre><p>. Add a reciprocal route on system C</p><pre>ip route add 192.168.1.0/24 via 192.168.2.1</pre><p>To further enable communication, configure the router (e.g., system B) to forward packets between its interfaces.</p><h3>Advanced Scenarios</h3><p>When scaling networks or connecting to the internet, additional configurations are required:</p><ul><li>Define a <strong>default gateway</strong> for systems to access external networks</li></ul><pre><br>ip route add default via 192.168.1.1</pre><ul><li>Manage complex setups with multiple routers by specifying distinct routes for internal and external traffic.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TNwoPGgkeMrLul77sjBgyQ.jpeg" /></figure><h3>***DNS in Linux for Absolute Beginners</h3><p><strong>Introduction to DNS</strong><br>DNS (Domain Name System) is critical for resolving hostnames to IP addresses.</p><h3>Key Concepts:</h3><h4>1. Local Name Resolution:</h4><ul><li>Hosts can map names to IP addresses locally using the /etc/hosts file.</li><li>Example: Adding 192.168.1.11 db to /etc/hosts on system A allows referencing system B as db instead of its IP address.</li><li><strong>Note:</strong> Entries in /etc/hosts override actual hostnames and can map multiple names to the same system.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0m7j7xZLPJcmmiAQTEaXhg.jpeg" /></figure><p><strong>Key Insight</strong>:<br>The /etc/hosts file allows multiple aliases for the same host. For example, system B can also be mapped as www.google.com, fooling system A into treating B as Google&#39;s server.</p><h4>2. Centralized Name Resolution:</h4><ul><li>For scalable environments, managing mappings in /etc/hosts becomes inefficient. A <strong>DNS server</strong> is used for centralized management.</li><li>All hosts are configured to refer to a DNS server via the /etc/resolv.conf file:</li><li>Example: nameserver 192.168.1.100.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zzA_QQbZlsu83BVZyVlr1w.jpeg" /></figure><h4>3. Resolution Order:</h4><ul><li>Hosts resolve names based on the order specified in /etc/nsswitch.conf:</li><li>Default: files (local /etc/hosts) first, then dns (DNS server).</li><li>This order can be customized for specific needs.</li></ul><h4>4. Public and Private DNS:</h4><ul><li>DNS servers in organizations handle internal and external domains:</li><li><strong>Internal:</strong> Custom domains like web.mycompany.com or db.mycompany.com.</li><li><strong>External:</strong> Public domains such as www.google.com are resolved via public DNS servers (e.g., Google&#39;s 8.8.8.8).</li><li><strong>Forwarding:</strong> Internal DNS servers can forward unknown queries to public DNS servers.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k9AM25MpjVJc9UBq0Y8myA.jpeg" /></figure><h4>5. Domain Names and Subdomains:</h4><ul><li>DNS uses a hierarchical naming structure:</li><li><strong>Top-Level Domains (TLDs):</strong> .com, .edu, .org, etc.</li><li><strong>Domain Names:</strong> google.com is Google&#39;s domain.</li><li><strong>Subdomains:</strong> Group related services, e.g., maps.google.com and drive.google.com.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pYQdHdwAE0w1NMQkYLwsRA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*imxn_66C20tg3b2QbX79VA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iXfQRv7FGN-5sALrh6lOZA.jpeg" /></figure><h4>6. Search Domains:</h4><ul><li>Use the search entry in /etc/resolv.conf to append domain names automatically:</li><li>Example: search mycompany.com prod.mycompany.com resolves web to web.mycompany.com or web.prod.mycompany.com.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N7q81rnKG4vR9Wh1m7TRvg.jpeg" /></figure><h4>7. DNS Record Types:</h4><ul><li><strong>A Record:</strong> Maps a hostname to an IPv4 address.</li><li><strong>AAAA Record:</strong> Maps a hostname to an IPv6 address.</li><li><strong>CNAME Record:</strong> Maps one hostname to another, useful for aliases.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FlnPK_85R4hzehTvIuVnMQ.jpeg" /></figure><h4>8. Tools for DNS Testing:</h4><ul><li><strong>ping:</strong> Tests name resolution using /etc/hosts or DNS.</li><li><strong>nslookup:</strong> Queries only DNS servers, bypassing /etc/hosts.</li><li><strong>dig:</strong> Provides detailed DNS query results.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CjDNfEcOUWJ_OpE1ymAiMw.jpeg" /><figcaption>1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L9-25n47jbK2KG66Tk6MYA.jpeg" /></figure><h4>9. Challenges in Large Environments:</h4><ul><li>DNS servers address scalability issues:</li><li>Easier management when IP addresses change.</li><li>Cache results for faster lookups.</li></ul><h3>***Understanding Network Namespaces</h3><p><strong>Namespaces</strong> are like individual rooms within the house, offering privacy to each child (or process). Each “child” can only see what’s inside their room, while the parent (the host) has visibility across all rooms. In the same way, namespaces isolate processes or containers, ensuring they operate independently from the host and from one another.</p><p>When you create a container, a <strong>network namespace</strong> is allocated, isolating its network environment. As far as the container is concerned, it operates as if it were on its own host, with no awareness of the underlying host or other containers. The host, however, has complete visibility and control.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E2RpHDNmfvJ2FSuorF94XQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2bymDC-mEz6MTTsm-i3GWQ.jpeg" /></figure><h3>Network Namespace Basics</h3><p>To demonstrate, we use the command ip netns to create and interact with network namespaces:</p><ol><li><strong>Create namespaces</strong>: Use ip netns add [namespace_name].</li><li><strong>List namespaces</strong>: Run ip netns.</li><li><strong>Execute commands inside namespaces</strong>: Use ip netns exec [namespace_name] [command].</li></ol><p>Within a network namespace, you can define virtual network interfaces, ARP tables, and routing rules, fully isolated from the host system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BiQ26X-QZ2A3dVBMb3kH-w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5cx3kboukHsLTwLPHM6MMg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z11U8WG5WKBePRp-DfxCeA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nbD-1W_oESd0yrp2zNu-yg.jpeg" /></figure><h3>Connecting Namespaces</h3><p>Initially, namespaces have no network connectivity. To enable communication:</p><ol><li><strong>Create a virtual Ethernet pair</strong>: ip link add [interface1] type veth peer name [interface2].</li><li><strong>Assign interfaces to namespaces</strong>: Use ip link set [interface] netns [namespace_name].</li><li><strong>Configure IP addresses</strong>: Assign IPs with ip addr add and bring interfaces up with ip link set [interface] up.</li><li><strong>Test connectivity</strong>: Use ping between namespaces to verify the setup.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OsMT39-G3VwrU9agsIWlFA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DAFdLDNNjoXxl0S3rO3ZTQ.jpeg" /></figure><h3>Scaling with Virtual Bridges</h3><p>For multiple namespaces, direct connections become cumbersome. A <strong>virtual switch</strong>, such as a Linux bridge, simplifies this by acting as a central network hub:</p><ol><li><strong>Create a bridge</strong>: ip link add [bridge_name] type bridge.</li><li><strong>Connect namespaces to the bridge</strong>: Use virtual Ethernet pairs as before, attaching one end to the namespace and the other to the bridge.</li><li><strong>Assign IPs and enable communication</strong>: Each namespace gets a unique IP, enabling communication across the virtual network.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tmLe5_jb6eNLFKXxg4OCfw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Az4KRpzzjYk3TvlLCjQ73g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fkGw3M-DMK_vqxooWSdbFg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rTXh1U0UHGasNAaJA4uEtQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pU3WhFdcP7rBAqe5XHQ79g.jpeg" /></figure><h3>External Network Connectivity</h3><p>Namespaces are isolated from external networks. To enable external communication:</p><ol><li><strong>Assign a gateway</strong>: Configure a route in the namespace routing table, pointing to the host interface on the bridge network.</li><li><strong>Enable NAT</strong>: Use iptables to masquerade traffic from the namespace network with the host’s external IP.</li></ol><p>This allows namespaces to access the internet while maintaining isolation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kA40IIP7FvJvTmxNwg38YQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TwwZ4QtKee7RSysIwJROVg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6I2WmaEY_0Rpz8HYfRhXbA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4I7SWOt7inSPAUmHZV10oQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5tALaVX1E5jURzM0BLoPyA.jpeg" /></figure><h3>Incoming Connectivity</h3><p>To expose services within a namespace (e.g., a web application on port 80):</p><ol><li><strong>Port Forwarding</strong>: Use iptables to forward traffic from the host&#39;s port 80 to the namespace&#39;s private IP and port 80.</li><li><strong>Optional Routing</strong>: Alternatively, configure external hosts with a route to the namespace network through the host.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yQkC84UKfkBtpgKZMpum2A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JkZNmWE6LpTy9UID-JD0Aw.jpeg" /></figure><h3>***Docker Networking:</h3><p>When you run a container, Docker offers various networking options to suit different use cases. Let’s explore these in detail:</p><h4>1. None Network</h4><ul><li><strong>Description</strong>: Containers are not attached to any network, ensuring total isolation.</li><li><strong>Characteristics</strong>:</li><li>The container cannot access external networks or be accessed externally.</li><li>Multiple containers in this mode cannot communicate with each other.</li></ul><p><strong>Use Case</strong>: Scenarios where absolute isolation is required, such as debugging or testing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DIoohKvJoZBV6Nb1SRiywg.jpeg" /></figure><h4>2. Host Network</h4><ul><li><strong>Description</strong>: Containers share the host’s network stack.</li><li><strong>Characteristics</strong>:</li><li>No network isolation; the container operates as if running directly on the host.</li><li>Applications running in the container are accessible on the same port as the host.</li><li>Port conflicts occur if multiple containers try to use the same port.</li></ul><p><strong>Use Case</strong>: High-performance scenarios where bypassing network virtualization is necessary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EpqT-sKLGs_gFY-dHaLosQ.jpeg" /></figure><h4>3. Bridge Network (Default)</h4><ul><li><strong>Description</strong>: Creates a private internal network connecting the Docker host and its containers.</li><li><strong>Characteristics</strong>:</li><li>Each container gets an IP within the network (e.g., 172.17.0.0/16 by default).</li><li>Containers can communicate with each other but require <strong>port mapping</strong> for external access.</li></ul><p><strong>Use Case</strong>: Standard deployments requiring isolated container communication and selective external exposure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HuMwJlMVapuyIGA0FICe1g.jpeg" /></figure><h3>Bridge Network: A Deeper Dive</h3><p>The <strong>Bridge Network</strong> is the default networking mode in Docker, and understanding its mechanics is essential.</p><h4>1. Creation and Identification</h4><ul><li>When Docker is installed, it creates an internal bridge network named <strong>Bridge</strong> (CLI view) or <strong>docker0</strong> (host system).</li><li>The bridge network is assigned an IP address (e.g., 172.17.0.1) and acts as:</li><li>A <strong>switch</strong> for containers to communicate internally.</li><li>An <strong>interface</strong> for communication with the host.</li></ul><p>You can view these networks using:</p><ul><li>docker network ls (lists Docker networks).</li><li>ip link (shows the docker0 interface on the host).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0rk5J4bictRpLnQTH94CvA.jpeg" /></figure><h4>2. Attaching Containers to the Bridge Network</h4><p>For every container:</p><ol><li><strong>Network Namespace Creation</strong>: Docker creates a namespace for the container.</li><li><strong>Virtual Ethernet Pair</strong>:</li></ol><ul><li>A virtual cable is created, with one end connected to the container’s namespace and the other to the docker0 bridge.</li><li>Each interface is assigned an IP address (e.g., container 172.17.0.2).</li></ul><p><strong>3. Verification</strong>:</p><ul><li>Use ip link or ip addr commands to inspect interfaces on the host and container.</li><li>Use docker inspect to see namespace details.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NZo2va6OSmodMwv_EVuKoQ.jpeg" /></figure><h4>Interface Pairing and Attachment</h4><p>When a container is created:</p><ol><li><strong>Namespace Creation</strong>: Docker sets up a network namespace for the container.</li><li><strong>Virtual Ethernet Pair</strong>: A virtual cable with two endpoints is established:</li></ol><ul><li>One end connects to the container’s namespace.</li><li>The other end connects to the bridge network on the host (docker0).</li></ul><p><strong>3. Numbering System</strong>: Interface pairs are identified by their numerical sequence (e.g., 9–10, 11–12).</p><p><strong>4. Inter-Container Communication</strong>: Once connected, all containers within the same bridge network can communicate directly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pOT1Cj2fiQl4vnuX0L7h4w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q2ABA6R_KJoW-xQnOCGHEA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*45UrpM_sObjFIapUtR02tA.jpeg" /></figure><h4>Port Mapping and External Access</h4><p>By default, containers in the bridge network are isolated from external access. To enable external communication, <strong>port mapping</strong> is used.</p><ol><li><strong>Default Scenario</strong>:</li></ol><ul><li>A container (e.g., running an Nginx web application on port 80) is only accessible:</li><li>From other containers in the same network.</li><li>From the Docker host itself.</li><li>External users cannot access the application.</li></ul><p><strong>2. Enabling External Access</strong>:</p><ul><li>Use the <strong>port publishing</strong> option to map a host port to a container port during container creation.</li><li>Example: -p 8080:80 maps port 8080 on the Docker host to port 80 in the container.</li></ul><ol><li><strong>Outcome</strong>:</li></ol><ul><li>External users can access the application using the Docker host’s IP and the mapped port (e.g., http://&lt;host-ip&gt;:8080).</li><li>Traffic to port 8080 on the host is forwarded to port 80 in the container.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0JEMzXNa4NjLsdatvokscw.jpeg" /></figure><h4>Mechanics of Port Forwarding</h4><p>Docker implements port forwarding using <strong>NAT (Network Address Translation)</strong>.</p><ul><li><strong>How it Works</strong>:</li><li>A <strong>NAT rule</strong> is created in the iptables <strong>PREROUTING chain</strong>, specifying:</li><li><strong>Source Port</strong>: The port on the Docker host (e.g., 8080).</li><li><strong>Destination</strong>: The container’s IP address and port (e.g., 172.17.0.3:80).</li><li>Docker dynamically manages these rules and adds them to the Docker-specific chain in iptables.</li></ul><p><strong>Verification</strong>:</p><ul><li>Use iptables commands to view and inspect the rules Docker creates.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xzPQ4aQdHb0HjoNgml2A3g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nDsPs8La5Z-B-yKTiCR9yw.jpeg" /></figure><h3>***Container Networking Interface(CNI)</h3><h4>Understanding CNI</h4><p>The <strong>Container Networking Interface (CNI)</strong> defines a set of standards for creating <strong>networking plugins</strong> and integrating them with container runtimes.</p><ul><li><strong>Plugins</strong>: Programs that handle container networking (e.g., connecting a container to a network).</li><li><strong>Bridge Plugin Example</strong>: A plugin named Bridge performs tasks such as assigning IPs, attaching containers to namespaces, and managing routing.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oNPT54h1DdyyihPVwtDKPQ.jpeg" /></figure><h4>CNI’s Responsibilities</h4><ol><li><strong>For Container Runtimes</strong>:</li></ol><ul><li>Create a <strong>network namespace</strong> for each container.</li><li>Identify the networks to which a container should connect.</li><li>Invoke the plugin when a container is created or deleted, using:</li><li>ADD command (on container creation).</li><li>DEL command (on container deletion).</li><li>Configure plugins using a <strong>JSON file</strong>.</li></ul><p><strong>2. For Plugins</strong>:</p><ul><li>Support commands like ADD, DEL, and CHECK, which accept parameters such as container ID and network namespace.</li></ul><p><strong>Handle tasks such as:</strong></p><ul><li>Assigning IP addresses to containers.</li><li>Configuring routes to enable communication between containers.</li><li>Return results in a standardized format.</li></ul><p>As long as both runtimes and plugins adhere to these standards, they work seamlessly together, ensuring compatibility across platforms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b6nGGSfdz6rIf1wfQnONtw.jpeg" /></figure><h4>Built-In Plugins and Third-Party Integrations</h4><p>CNI includes several prebuilt plugins, such as:</p><ul><li><strong>Bridge</strong></li><li><strong>VLAN</strong>, <strong>IP VLAN</strong>, <strong>MAC VLAN</strong></li><li><strong>Windows-specific plugins</strong></li><li><strong>IPAM Plugins</strong> like Host Local and DHCP</li></ul><p>Additionally, third-party plugins, such as <strong>Weave</strong>, <strong>Flannel</strong>, <strong>Calico</strong>, <strong>Cilium</strong>, and <strong>VMware NSX</strong>, implement the CNI standard, enabling diverse networking capabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lB_pIPjpTEsIoiYGzn926A.jpeg" /></figure><h4>CNI vs. Docker’s CNM</h4><p>While many container runtimes (e.g., Kubernetes, Mesos) adopt the CNI standard, Docker uses its own networking model, the <strong>Container Network Model (CNM)</strong>.</p><p><strong>Differences</strong>:</p><ul><li>CNM provides an alternative approach to container networking, making Docker incompatible with CNI plugins by default.</li><li>As a result, Docker containers cannot natively use CNI plugins.</li></ul><p><strong>Workaround</strong>:</p><ul><li>Create a Docker container with no network configuration.</li><li>Manually invoke the desired CNI plugin (e.g., Bridge).</li></ul><p><strong>This is precisely how Kubernetes interacts with Docker:</strong></p><ul><li>Kubernetes creates containers with no network (none).</li><li>Configured CNI plugins handle the rest of the networking setup.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YwsdG4StUa4wgmO1ouSbVA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PrHJ1XleXrLhnvSHDOoWtw.jpeg" /></figure><h3>***Cluster Networking</h3><h4>Basic Networking Requirements</h4><p>A Kubernetes cluster consists of <strong>master</strong> and <strong>worker nodes</strong>, and each node must meet the following criteria:</p><ol><li><strong>Network Interface</strong>:</li></ol><ul><li>Each node should have at least one network interface connected to a network.</li><li>The interface must have a unique <strong>IP address</strong>.</li></ul><p><strong>2. Host Identity</strong>:</p><p>Each node must have:</p><ul><li>A <strong>unique hostname</strong>.</li><li>A <strong>unique MAC address</strong> for its network interface</li><li><strong><em>Important</em></strong>: If you created the nodes by cloning existing virtual machines, ensure these identifiers are updated to avoid conflicts.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rEDSQ9HAwbDNAYuTLUfeYA.jpeg" /></figure><h4>Required Open Ports</h4><p>To facilitate communication between Kubernetes components, certain ports need to be open:</p><ol><li><strong>API Server (Port 6443)</strong>:</li></ol><ul><li>The master node’s <strong>API server</strong> listens on this port.</li><li>Used by:</li><li>Worker nodes.</li><li>kubectl tool.</li><li>External users.</li><li>Other control plane components.</li></ul><p><strong>2. Kubelet (Port 10250)</strong>:</p><ul><li><strong>Kubelet agents</strong> on both master and worker nodes listen on this port.</li><li><em>Note</em>: The master node may also run a kubelet.</li></ul><p><strong>3. Scheduler (Port 10259)</strong>:</p><ul><li>The <strong>kube-scheduler</strong> component requires this port to be open.</li></ul><p><strong>4. Controller Manager (Port 10257)</strong>:</p><ul><li>The <strong>kube-controller-manager</strong> requires this port for communication.</li></ul><p><strong>5. Worker Node Service Ports (30000–32767)</strong>:</p><ul><li>These ports are used to expose services on worker nodes for external access.</li></ul><p><strong>6. ETCD Server Ports</strong>:</p><ul><li>Port 2379: Used by <strong>ETCD server</strong> to listen for incoming requests.</li><li>Port 2380: Required for communication between <strong>ETCD instances</strong> in multi-master configurations.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jrdh5Br6DBqUghOuONms_A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dYmU8M-K9laWwd_m4E3Tpg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U4Byh4RNQiTcWmDHtB6pZQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2AAdzfkv2iBYWIRHZ-JiIw.jpeg" /></figure><h4>Considerations for Multi-Master Clusters</h4><p>If you have multiple master nodes, ensure all the ports listed above are open on every master node. This is especially important for <strong>ETCD communication</strong> (port 2380).</p><h4>Network Security Configurations</h4><p>When configuring networking for your Kubernetes cluster, keep these considerations in mind:</p><ul><li><strong>Firewall Rules</strong>: Ensure the necessary ports are allowed through firewalls.</li><li><strong>IP Tables Rules</strong>: Review and configure rules to permit communication between nodes.</li><li><strong>Cloud Security Groups</strong>: If deploying on platforms like <strong>AWS</strong>, <strong>Azure</strong>, or <strong>GCP</strong>, adjust the <strong>network security groups</strong> accordingly.</li></ul><p>If something is not working as expected, checking these configurations is a good starting point during troubleshooting.</p><h3>***Pod Networking</h3><h3>Understanding the Need for Pod Networking</h3><p>In Kubernetes, the network setup that connects nodes together is distinct from the networking required for pods. As the cluster grows to accommodate numerous pods and services, key questions arise:</p><ul><li>How are pods addressed?</li><li>How do they communicate with each other?</li><li>How can services running on these pods be accessed, both internally and externally?</li></ul><p>Kubernetes does not provide a built-in solution for these challenges. Instead, it expects users to implement a networking model that meets specific requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RUptdC17J6eUedD3P-Ji4A.jpeg" /></figure><h3>Kubernetes Pod Networking Requirements</h3><p>Kubernetes defines the following expectations for pod networking:</p><ol><li><strong>Unique Pod IPs</strong>: Every pod must have a unique IP address.</li><li><strong>Intra-Node Communication</strong>: Pods within the same node should be able to communicate using their IP addresses.</li><li><strong>Inter-Node Communication</strong>: Pods on different nodes must also communicate seamlessly using the same IP addresses, without requiring Network Address Translation (NAT).</li></ol><p>The specifics of IP ranges and subnets are left to the implementer, provided these criteria are satisfied.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lbb6kNBAwtwKoVDD0NFiAQ.jpeg" /></figure><h3>Implementing Pod Networking</h3><p>While there are several solutions available to address these requirements, such as Container Network Interface (CNI) plugins, understanding the fundamentals helps in grasping how these solutions work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8xBHfIYu5dWz3_u3WObz9Q.jpeg" /></figure><h4>Setting the Foundation</h4><p>Let’s begin with a <strong>three-node cluster</strong> where each node has an external IP address (e.g., 192.168.1.11, 192.168.1.12, 192.168.1.13). The nodes participate in a common network, and Kubernetes creates <strong>network namespaces</strong> for containers (pods) upon their creation. To facilitate communication, we:</p><ol><li><strong>Create a Bridge Network</strong>: A bridge network is established on each node to interconnect the containers.</li><li><strong>Assign IP Subnets</strong>: Each node’s bridge network is assigned a unique subnet (e.g., 10.240.1.0/24 for Node 1, 10.240.2.0/24 for Node 2, etc.).</li></ol><h4>Connecting Containers</h4><p>When a container is created:</p><ol><li>A <strong>virtual network cable</strong> (veth pair) is generated using ip link add.</li><li>One end is attached to the container, and the other to the bridge using ip link set.</li><li>An IP address is assigned to the container using ip addr.</li><li>A default route is configured to the bridge network’s gateway.</li></ol><p>This process is repeated for each container, enabling them to communicate within the same node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bUfBZGEY_L15AfrpLGkbqQ.jpeg" /><figcaption>f</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N8SLZmAB65_xXxL7aLWNHg.jpeg" /></figure><h4>Inter-Node Communication</h4><p>To enable communication between nodes:</p><ol><li>Routes are added to each node’s routing table, directing traffic to the appropriate subnet of other nodes. For example:</li></ol><ul><li>Node 1 routes traffic to 10.240.2.0/24 via Node 2’s external IP (192.168.1.12).</li></ul><ol><li>Alternatively, a central router can manage these routes, simplifying configuration as the network grows.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GNWqgfaVOyNZMtZKMl0wFw.jpeg" /></figure><h4>Unified Network Architecture</h4><p>The individual bridge networks now form a single logical network (e.g., 10.244.0.0/16), enabling seamless pod-to-pod communication across nodes.</p><h3>Automation with CNI</h3><p>Manual configuration of pod networking is impractical for large-scale environments. This is where <strong>CNI (Container Network Interface)</strong> comes into play.</p><ul><li><strong>CNI Overview</strong>:</li><li>CNI provides a standard framework for networking containers.</li><li>It ensures that when a pod is created, a pre-defined script is executed to handle network setup.</li><li><strong>Script Adaptation</strong>:</li><li>The script must adhere to CNI standards, including:</li><li>An <strong>add section</strong> for connecting containers to the network.</li><li>A <strong>delete section</strong> for cleaning up resources when containers are removed.</li></ul><p>When a container is created, the container runtime (e.g., Docker, CRI-O) uses the CNI configuration to locate and execute the script, automating the networking process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gwkjxcX7-aZ57u_nAtaLUQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zO8PF_6-ihN8IpbLv-8-Xw.jpeg" /></figure><h3>***Container Networking Interface(CNI) in Kubernetes:</h3><h3>CNI Responsibilities in Kubernetes</h3><p>CNI defines the <strong>responsibilities of the container runtime</strong>. For Kubernetes, this means:</p><ul><li>Creating <strong>container network namespaces</strong>.</li><li>Attaching these namespaces to the appropriate networks by invoking the specified <strong>CNI plugin</strong>.</li></ul><p>The container runtime, such as <strong>Containerd</strong> or <strong>CRI-O</strong>, is the Kubernetes component responsible for creating containers and triggering the appropriate network plugin after a container is created.</p><h3>Configuring Kubernetes to Use CNI Plugins</h3><h4>CNI Plugin Installation</h4><ol><li><strong>Plugin Directory</strong>:</li></ol><ul><li>All network plugins are installed in /opt/cni/bin.</li><li>This directory contains executable files for each plugin, such as bridge, flannel, dhcp, etc.</li></ul><p><strong>2. Configuration Files</strong>:</p><ul><li>Plugin configurations are stored in /etc/cni/net.d.</li><li>Each file defines how a specific plugin should be used.</li><li>If multiple configuration files exist, the container runtime selects the one with the <strong>alphabetically first name</strong>.</li><li><strong>Key Elements</strong>:</li><li>name: The name of the network (e.g., &quot;mynet&quot;).</li><li>type: The plugin type (e.g., &quot;bridge&quot;).</li><li>isGateway: Determines whether the bridge interface gets an IP address to act as a gateway.</li><li>ipMasq: Enables NAT rules for IP masquerading.</li><li>ipam: Handles IP address management.</li><li>type: Indicates whether IPs are managed locally (host-local) or by an external DHCP server.</li><li>subnet: Defines the IP address range for the network.</li></ul><p>This configuration ties together concepts like <strong>bridging, routing, NAT, and IP allocation</strong>, which were covered in earlier lectures.</p><h3>Key Takeaways</h3><ol><li>Kubernetes relies on the <strong>container runtime</strong> (e.g., Containerd, CRI-O) to invoke CNI plugins and manage pod networking.</li><li>CNI plugins and their configurations are managed through standard directories:</li></ol><ul><li>Executables: /opt/cni/bin</li><li>Configurations: /etc/cni/net.d</li></ul><p>3. The <strong>CNI configuration file</strong> defines the network type, IP allocation, routing rules, and NAT behavior.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H2OFOjNEIJqAKasRoQV9_A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dnPECDaCY0uk9VGn3sbnjw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V8LRcIioAg0S97wvwH-XJA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t5n1oTjFkJ9yliPSF_RTLg.jpeg" /></figure><h3>***Understanding Weaveworks Weave CNI Plugin in Kubernetes</h3><h3>Challenges of Scaling Pod Networking</h3><p>In the basic networking solution we built earlier, a <strong>routing table</strong> mapped networks across nodes. When a packet was sent between pods on different nodes, it traversed the network using this routing table. While this approach works in small, simple environments, it becomes impractical as the scale increases.</p><h4>Scaling Limitations</h4><ul><li><strong>Routing Table Size</strong>: Managing a large number of routes becomes inefficient as clusters grow to hundreds of nodes with thousands of pods.</li><li><strong>Complexity</strong>: Maintaining accurate routing tables in large, dynamic environments is challenging.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NdE3t22gS2qXK1Ax3liQ-w.jpeg" /></figure><h3>Analogy: Scaling Office Communication</h3><p>To simplify the concept, imagine the Kubernetes cluster as a <strong>company</strong>, where:</p><ul><li><strong>Nodes</strong> are different office sites.</li><li><strong>Pods</strong> are departments or offices within each site.</li></ul><p>Initially, employees (pods) sent packages directly to other offices using local couriers, relying on addresses and direct routes. As the company expanded globally, this approach failed due to logistical complexities.</p><p><strong>Solution</strong>: A professional <strong>shipping company</strong> was engaged to streamline communication:</p><ol><li><strong>Agents</strong>: Representatives were placed at each site to manage shipments.</li><li><strong>Coordination</strong>: Agents communicated to share information about all sites, departments, and offices.</li><li><strong>Encapsulation</strong>: Packages were re-wrapped for efficient transport between sites.</li><li><strong>Delivery</strong>: Agents ensured packages reached the correct department upon arrival.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mmogdv7Xy6HT8ZSXfQ7Wgg.jpeg" /></figure><h3>How Weaveworks Solves Networking Challenges</h3><p>Weaveworks employs a similar model:</p><ol><li><strong>Weave Agents</strong>: A Weave agent (or peer) is deployed on each Kubernetes node. These agents exchange information about nodes, networks, and pods to maintain a complete topology of the cluster.</li><li><strong>Bridge Network</strong>:</li></ol><ul><li>Weave creates a bridge network named <strong>weave</strong> on each node.</li><li>IP addresses are assigned to the network and its pods dynamically.</li></ul><h4>Packet Flow in Weave</h4><ul><li><strong>Source Node</strong>: When a packet is sent from a pod, Weave intercepts it and identifies the destination pod.</li><li><strong>Encapsulation</strong>:</li><li>The packet is encapsulated into a new one with updated source and destination information.</li><li><strong>Transmission</strong>: The encapsulated packet is sent across the network to the destination node.</li><li><strong>Destination Node</strong>:</li><li>The Weave agent on the receiving node decapsulates the packet and forwards it to the correct pod.</li></ul><p>This approach removes the need for extensive routing tables, ensuring efficient and scalable communication in large clusters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vM--G7hCQpOA_6mRbfM2ZQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KEBjK-O3xL5ptgM2DR54iQ.jpeg" /></figure><h3>Weave Deployment in Kubernetes</h3><h4>Deployment Methods</h4><ol><li><strong>Manual Deployment</strong>:</li></ol><ul><li>Weave agents can be installed as standalone services or daemons on each node.</li></ul><p><strong>2. Kubernetes Integration</strong>:</p><ul><li>The preferred method is deploying Weave as pods in the Kubernetes cluster using <strong>DaemonSets</strong>.</li></ul><h4>Why DaemonSets?</h4><p>A <strong>DaemonSet</strong> ensures that a specific pod type is deployed on all nodes in the cluster. For Weave, this guarantees that each node runs a Weave agent, facilitating seamless communication.</p><h4>Deployment Process</h4><ul><li>With a <strong>kubectl apply</strong> command, Weave can be deployed in a running Kubernetes cluster.</li><li>This command sets up:</li><li>Weave agents (peers) as a <strong>DaemonSet</strong>.</li><li>Additional components necessary for the plugin’s functionality.</li></ul><h4>Post-Deployment Observation</h4><ul><li>If your cluster is set up using tools like <strong>kubeadm</strong>, you can verify the Weave installation:</li><li>Use kubectl get pods -n kube-system to list the Weave pods.</li><li>Each node will have a Weave pod deployed.</li></ul><h3>Troubleshooting Weave</h3><p>For debugging:</p><ol><li>Use kubectl logs to check logs from Weave pods.</li><li>Investigate the configuration files and IP address allocation to identify potential issues.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mFOYoclCbfPJbwWDXfCk7A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sw081OjXyoVlAn5F6eszPw.jpeg" /></figure><h3>***IP Address Management- Weave</h3><h4>Who Manages IP Address Allocation?</h4><p>As per the <strong>Container Network Interface (CNI) standards</strong>, the responsibility for managing IP address assignment lies with the <strong>CNI plugin</strong> or the <strong>network solution provider</strong>.</p><h4>IP Management in a Basic CNI Plugin</h4><p>Previously, we implemented a custom CNI plugin, where we directly handled the assignment of IP addresses within the plugin itself. This included managing the allocation of IPs to container network namespaces.</p><p>While Kubernetes itself doesn’t dictate <strong>how</strong> IPs should be assigned, it mandates:</p><ul><li><strong>No duplicate IPs</strong> across the network.</li><li>Proper management of IP allocations.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n-8PaO2qTpXuC-9xC6D3KQ.jpeg" /></figure><h3>Approaches to IP Address Management</h3><h4>Local File-Based Management</h4><p>A straightforward method to manage IPs is to:</p><ul><li>Maintain a list of allocated IP addresses in a file stored locally on each host.</li><li>Include logic in the CNI plugin script to read, update, and validate entries in this file.</li></ul><h4>CNI’s Built-in Plugins</h4><p>Instead of implementing manual logic, CNI provides <strong>built-in IPAM plugins</strong> to handle this task.</p><ol><li><strong>Host-Local Plugin</strong>:</li></ol><ul><li>Assigns IPs locally on each host, ensuring no overlap.</li><li>Requires the CNI plugin script to invoke it explicitly.</li></ul><p><strong>2. Dynamic Configuration</strong>:</p><ul><li>CNI configuration files contain an IPAM section specifying the type of plugin, subnet, and routes to be used.</li><li>A dynamic script can read these configurations and invoke the appropriate plugin.</li></ul><h4>Integration with Network Providers</h4><p>Each network solution provider has its unique method of managing IPs. For example, let’s look at how Weave works<strong> Weave</strong> handles this process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2JwQ0ptsDnxiTAExkeTo0A.jpeg" /></figure><h3>Weave works Weave IP Address Management</h3><p>Weave uses an efficient and scalable approach to IP allocation:</p><ol><li><strong>Default IP Range</strong>:</li></ol><ul><li>By default, Weave allocates the range <strong>10.32.0.0/12</strong> to the cluster. This provides approximately one million usable IPs, from <strong>10.32.0.1</strong> to <strong>10.47.255.254</strong>, for pod networking.</li></ul><p><strong>2. IP Range Distribution</strong>:</p><ul><li>Weave peers (agents) on each node split the IP range equally among themselves.</li><li>Each node is assigned a subset of the IP range for use by its pods.</li></ul><p><strong>3. Configuration Options</strong>:</p><ul><li>The default range is configurable. Additional parameters can be specified during the deployment of the Weave plugin, allowing customization based on the cluster’s requirements.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s2wRzC63BG5T8FWnmG3CAg.jpeg" /></figure><h4>Key Takeaways</h4><ul><li><strong>CNI Plugins</strong> are responsible for IP allocation and management.</li><li>Kubernetes does not enforce specific methods but requires unique IPs across the cluster.</li><li><strong>Weave</strong> uses a distributed approach for efficient IP range allocation, making it scalable for large clusters.</li><li>IPAM configurations can be customized through CNI configuration files and dynamic scripting.</li></ul><h3>***Understanding Service Networking in Kubernetes:</h3><h4>What are Services?</h4><ul><li>A <strong>service</strong> is an abstraction that provides a stable network endpoint to access applications running in pods.</li><li>Each service is assigned:</li><li>A unique <strong>IP address</strong>.</li><li>A <strong>name</strong> (for DNS-based access).</li></ul><h4>Types of Services</h4><ol><li><strong>Cluster IP (Default)</strong>:</li></ol><ul><li>Makes the service accessible <strong>only within the cluster</strong>.</li><li>Ideal for internal applications like databases.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rjrr5tUwKdWDdiD7guIu8Q.jpeg" /></figure><p><strong>2. Node Port</strong>:</p><ul><li>Extends accessibility <strong>outside the cluster</strong> by exposing the service on a specific port of all nodes.</li><li>Useful for external-facing applications.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B5OKXF0h-u9i3MzMpdm-Ww.jpeg" /></figure><h3>How Services Work in Kubernetes</h3><h4>Cluster-Wide Accessibility</h4><p>Unlike pods, which are tied to specific nodes, services exist across the cluster. They are:</p><ul><li>Virtual objects, meaning they do not involve actual processes, namespaces, or interfaces.</li><li>Managed entirely by Kubernetes components such as <strong>kube-proxy</strong>.</li></ul><h4>Service IP Address Assignment</h4><ul><li>Kubernetes assigns service IPs from a <strong>predefined range</strong> specified by the --service-cluster-ip-range flag in the API server.</li><li>Example:</li><li>Service CIDR: 10.96.0.0/12</li><li>Pod CIDR: 10.244.0.0/16</li><li>The ranges must not overlap to avoid conflicts between pod and service IPs.</li></ul><h3>Role of kube-proxy</h3><p>The <strong>kube-proxy</strong> component:</p><ul><li>Runs on every node.</li><li>Monitors the Kubernetes API server for service-related events.</li><li>Configures the network to route traffic to the appropriate pod(s).</li></ul><h4>How kube-proxy Operates</h4><ol><li>When a service is created, kube-proxy:</li></ol><ul><li>Retrieves the service’s assigned IP address and port.</li><li>Sets up <strong>forwarding rules</strong> on each node to map service IP traffic to pod IPs.</li></ul><p>2. Supported Modes:</p><ul><li><strong>iptables (Default)</strong>: Uses NAT rules to redirect traffic to backend pods.</li><li><strong>IPVS</strong>: Implements a scalable, performance-optimized load balancer.</li><li><strong>User space</strong>: Legacy mode, listens on service ports and proxies requests to pods.</li></ul><p>The default mode is iptables, but it can be customized using the --proxy-mode option.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rmxFgP5ACaKc8_2WPcji1w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d4qWE_8T8k_FUJuhlMDOYQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zx6jD92pnEuN55kUgLIQMg.jpeg" /></figure><h3>Example: Cluster IP Service</h3><h4>Scenario</h4><ul><li>A pod named db runs on <strong>Node 1</strong> with IP 10.244.1.2 and exposes a database on port <strong>3306</strong>.</li><li>A <strong>ClusterIP</strong> service is created to make this pod accessible within the cluster.</li></ul><h4>Process</h4><ol><li>Kubernetes assigns the service an IP, e.g., 10.103.132.104.</li><li>kube-proxy adds <strong>DNAT rules</strong> to iptables:</li></ol><ul><li>Incoming traffic to the service IP 10.103.132.104:3306 is redirected to the pod IP 10.244.1.2:3306.</li></ul><h4>Verification</h4><ul><li>Use iptables to view NAT table entries.</li><li>Rules created by kube-proxy include comments with the service name for easy identification.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EF67JprR4RDrGtJ_2e979w.jpeg" /></figure><h3>Example: NodePort Service</h3><h4>Scenario</h4><ul><li>A web application is hosted on a pod and requires external access.</li><li>A <strong>NodePort</strong> service is created, exposing the application on a specific port across all nodes.</li></ul><h4>Process</h4><ol><li>kube-proxy sets up iptables rules to forward traffic from the node’s port to the pod’s IP and port.</li><li>External users access the application using the &lt;NodeIP&gt;:&lt;NodePort&gt; combination.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yDZ2YAL8-UZk-Au54lMK-Q.jpeg" /></figure><h3>Logging and Troubleshooting</h3><p>To troubleshoot kube-proxy configurations:</p><ol><li>Check the <strong>kube-proxy logs</strong> for details about:</li></ol><ul><li>Proxy mode in use (e.g., iptables, IPVS).</li><li>Service creation or deletion events.</li></ul><p>2. Ensure appropriate log verbosity levels for detailed insights.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hy1RsGUXi7_L2uGQyXPM6g.jpeg" /></figure><h3>Key Considerations</h3><ul><li><strong>Separate CIDR Ranges</strong>: Ensure the service and pod CIDR ranges do not overlap.</li><li><strong>Dynamic Updates</strong>: kube-proxy dynamically updates iptables rules when services are added or removed.</li><li><strong>Virtual Nature</strong>: Services are purely conceptual, relying on forwarding rules for functionality.</li></ul><h3>***Cluster DNS</h3><h3>DNS in Kubernetes Clusters:</h3><p>Kubernetes deploys a <strong>built-in DNS server</strong> (usually Core DNS) by default. This server is responsible for managing name resolution within the cluster, enabling seamless communication between components like pods and services.</p><h4>Key Points:</h4><ul><li>All pods and services have unique IP addresses.</li><li>The cluster networking is correctly configured.</li><li>DNS primarily focuses on <strong>pods</strong> and <strong>services</strong>; nodes and their external DNS configuration are outside the scope of this lecture.</li></ul><h3>Example: DNS for Pods and Services</h3><h4>Scenario Setup</h4><ul><li>A <strong>test pod</strong> with IP 10.244.1.5.</li><li>A <strong>web pod</strong> with IP 10.244.2.5.</li><li>A <strong>web-service</strong> created to expose the web pod, assigned IP 10.107.37.188.</li></ul><h4>Service DNS Records</h4><ol><li>When the service is created, Kubernetes automatically creates a <strong>DNS record</strong> for it.</li></ol><ul><li>The service name (web-service) is mapped to its IP address (10.107.37.188).</li></ul><p>2. Pods in the same namespace (default) can access the service using its name, web-service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QUZr0EAut5DHnkXlHNbknw.jpeg" /></figure><h3>Understanding Namespaces and DNS Resolution</h3><h4>Within the Same Namespace</h4><ul><li>Services and pods can be accessed using their <strong>first names</strong> (e.g., web-service).</li></ul><h4>Across Namespaces</h4><ul><li>To access a service in a different namespace:</li><li>Use the format &lt;service-name&gt;.&lt;namespace&gt; (e.g., web-service.apps).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ukd4oWGYVDGRIyQ-48o1FQ.jpeg" /></figure><h4>Subdomains and Fully Qualified Domain Names (FQDN)</h4><ol><li>Kubernetes organizes services and pods within <strong>subdomains</strong> based on namespaces.</li><li>Services are grouped into an additional subdomain, svc.</li><li>By default, the cluster’s root domain is cluster.local.</li><li>A service’s FQDN is structured as:</li></ol><ul><li>&lt;service-name&gt;.&lt;namespace&gt;.svc.cluster.local</li><li>Example: web-service.apps.svc.cluster.local</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5K86ftTyq9D0l-n4V8oPLw.jpeg" /></figure><h3>DNS for Pods</h3><h4>Pod Records</h4><ul><li><strong>Pod DNS records</strong> are not created by default but can be enabled explicitly.</li><li>Kubernetes does not use pod names for DNS. Instead, it generates a name by replacing dots in the pod’s IP address with dashes.</li></ul><h4>Pod FQDN</h4><ol><li>A pod’s DNS record includes:</li></ol><ul><li>The converted IP address as the hostname.</li><li>Namespace as a subdomain.</li><li>Root domain set to cluster.local.</li></ul><p>Example:</p><ul><li>Pod IP: 10.244.1.5</li><li>DNS record: 10-244-1-5.default.pod.cluster.local</li></ul><p>This record resolves to the pod’s IP address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bwl5tnL96UtcvBcGKhzZ2g.jpeg" /></figure><h3>How DNS Facilitates Communication</h3><h4>Services</h4><ul><li>Services provide stable names and IPs for accessing applications hosted on pods.</li><li>DNS ensures that any pod can access a service using its name, even if the service’s backend pods change.</li></ul><h4>Pods</h4><ul><li>While direct pod-to-pod communication using DNS is possible, it’s less common. Services are typically used as intermediaries to abstract and manage backend pods.</li></ul><h3>***Core DNS in Kubernetes:</h3><p><strong>How Kubernetes Implement DNS?</strong></p><p>In large clusters where thousands of pods are dynamically created and destroyed, Kubernetes centralizes DNS resolution through a dedicated <strong>DNS server</strong>.</p><h3>Centralized DNS Server in Kubernetes</h3><ol><li><strong>DNS Resolution Mechanism</strong>:</li></ol><ul><li>Pods are configured to point to a centralized DNS server via their /etc/resolv.conf file.</li><li>The DNS server’s IP (typically 10.96.0.10) is specified as the <strong>nameserver</strong>.</li></ul><p><strong>2. Automated DNS Management</strong>:</p><ul><li>When a pod or service is created, Kubernetes updates the DNS server with the appropriate DNS records.</li><li>Each pod’s /etc/resolv.conf is automatically configured to use the DNS server.</li></ul><p><strong>3. DNS Record Management</strong>:</p><ul><li>Services get DNS records automatically.</li><li>Pods, however, only receive records if explicitly enabled via the <strong>Core DNS</strong> configuration (disabled by default).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ol1uCcmHSBe5tVtaQTr7MA.jpeg" /></figure><h3>Core DNS: The Default Kubernetes DNS Server</h3><p>Starting with Kubernetes 1.12, <strong>Core DNS</strong> replaced the legacy <strong>kube-dns</strong> as the default DNS server. Core DNS is deployed within the cluster as:</p><ul><li>A <strong>replica set</strong> with two pods for redundancy.</li><li>Located in the kube-system namespace.</li></ul><h4>CoreDNS Configuration</h4><ol><li><strong>Core file</strong>:</li></ol><ul><li>Core DNS uses a configuration file named Corefile, located at /etc/coredns.</li><li>This file specifies plugins to handle various functionalities, such as:</li><li><strong>Errors</strong>: Logging and handling DNS errors.</li><li><strong>Health</strong>: Reporting the DNS server’s health.</li><li><strong>Cache</strong>: Caching DNS responses.</li><li><strong>Kubernetes Plugin</strong>: Integrates CoreDNS with the Kubernetes cluster.</li></ul><p><strong>2. Kubernetes Plugin</strong>:</p><ul><li>Configures the cluster’s top-level domain, e.g., cluster.local.</li><li>Manages DNS records for services and optionally for pods.</li><li>Converts pod IP addresses into dashed hostnames when the pods option is enabled.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bfkvafxLmzIsMBxS8rtC-Q.jpeg" /></figure><p><strong>3. Forwarding Requests</strong>:</p><ul><li>If a DNS query cannot be resolved within the cluster, CoreDNS forwards it to the nameserver specified in its /etc/resolv.conf.</li></ul><p><strong>4. ConfigMap Integration</strong>:</p><ul><li>The Corefile is managed via a ConfigMap, allowing dynamic updates to CoreDNS configuration without redeploying the pods.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XH-FWaRHzyef-Quwe2d6vg.jpeg" /></figure><h3>DNS Configuration for Pods</h3><ol><li><strong>Service DNS Records</strong>:</li></ol><ul><li>Kubernetes automatically creates a service for Core DNS, typically named <strong>kube-dns</strong>.</li><li>This service assigns an IP (e.g., 10.96.0.10), which pods use to resolve names.</li></ul><p><strong>2. Kubelet’s Role</strong>:</p><ul><li>The <strong>kubelet</strong> is responsible for configuring each pod’s DNS settings when it is created.</li><li>The DNS server’s IP and the cluster domain are included in the kubelet’s configuration file.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GTL-N6VLVSZGrExLdemCeA.jpeg" /></figure><h3>Accessing Services and Pods</h3><h4>Service Name Resolution</h4><ul><li>Services can be accessed using various formats:</li><li>web-service</li><li>web-service.default</li><li>web-service.default.svc</li><li>web-service.default.svc.cluster.local (FQDN)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wdYAtdFC_LhgGI32DbUkNg.jpeg" /></figure><h4>Search Domains</h4><ul><li>The /etc/resolv.conf file on pods includes search domains such as:</li><li>default.svc.cluster.local</li><li>svc.cluster.local</li><li>cluster.local</li></ul><p>These allow flexible resolution of service names without needing the full FQDN. For example, querying web-service will automatically append default.svc.cluster.local for resolution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tQb5xVCUn4ZFDa3yVYnZ0Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s1Gx_Swwasq0jpEy6-zr2A.jpeg" /></figure><h4>Pod Name Resolution</h4><ul><li>Pods do not get short-name DNS entries by default.</li><li>For pod name resolution, you must use the full FQDN (e.g., 10-244-1-5.default.pod.cluster.local).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G9Sbd6Lb7OtIV4K0XpYm-A.jpeg" /></figure><h3>How DNS Works in Practice</h3><ol><li>When a pod makes a DNS query:</li></ol><ul><li>The query is sent to the Core DNS service via the configured nameserver.</li><li>Core DNS checks its database for a matching record.</li></ul><p>2. If a pod queries an external domain (e.g., <a href="http://www.google.com)/">www.google.com):</a></p><ul><li>Core DNS forwards the request to an external DNS server defined in its /etc/resolv.conf.</li></ul><p>3. For internal service queries (e.g., web-service):</p><ul><li>Core DNS resolves the name to the service’s IP and returns it to the querying pod.</li></ul><h3>***Ingress</h3><p><strong>Understanding Ingress in Kubernetes</strong></p><p><strong>What is the difference between Services and Ingress, and when should you use each?</strong></p><h3>Scenario: Deploying an Application</h3><p>Imagine you’re deploying an online store application for a company that sells products. The application needs to be accessible at <strong>myonlinestore.com</strong>. Let’s break down the steps:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tyZEXNvI9vATPWnWekeErA.jpeg" /></figure><ol><li><strong>Application Deployment</strong>:</li></ol><ul><li>Package your application as a Docker image and deploy it as a <strong>Pod</strong> in a Kubernetes <strong>Deployment</strong>.</li><li>The application uses a MySQL database, deployed as a Pod and exposed internally using a <strong>ClusterIP Service</strong> named mysql-service.</li></ul><p><strong>2. External Access</strong>:</p><ul><li>To expose your application to the outside world, create a <strong>NodePort Service</strong>, which makes the application available on a high port (e.g., 30880) on all cluster nodes.</li><li>Users can now access your application using a URL like http://&lt;Node_IP&gt;:30880.</li></ul><p><strong>3. Scaling</strong>:</p><ul><li>Increase the number of Pod replicas to handle more traffic. The <strong>Service</strong> ensures traffic is distributed among these replicas.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hi82CW1Um1xg1cvgEaQyew.jpeg" /></figure><h3>Challenges with NodePort Services</h3><ol><li><strong>DNS and Port Management</strong>:</li></ol><ul><li>Manually managing IPs or high ports (e.g., 30880) isn’t user-friendly.</li><li>Configure your DNS to point to the Node’s IP, but users still need to include the port in the URL.</li></ul><p><strong>2. Proxy for Better Access</strong>:</p><ul><li>Introduce a proxy server to route traffic on port 80 (standard HTTP) to the NodePort.</li><li>Users can now access your application at myonlinestore.com without specifying a port.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kW73PmFpKPVHndUaFRPwyQ.jpeg" /></figure><p><strong>3. Cloud Environments</strong>:</p><ul><li>In a public cloud, instead of using NodePort, you can create a <strong>LoadBalancer Service</strong>.</li><li>Kubernetes requests the cloud provider (e.g., GCP) to provision a load balancer that routes traffic to your application.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FIU7eYQ_Ig8-_utLw5fTiw.jpeg" /></figure><h3>Scaling Applications with Multiple Services</h3><p>As the business grows, new services (e.g., video streaming at myonlinestore.com/watch) are introduced:</p><ul><li>Deploy the new service as a separate Deployment and expose it using another <strong>LoadBalancer Service</strong>.</li><li>Each LoadBalancer incurs additional cost, and managing multiple external IPs becomes cumbersome.</li></ul><p>To simplify this, you need a centralized mechanism to:</p><ul><li>Route traffic to different services based on the URL (e.g., /watch or /shop).</li><li>Enable SSL for secure communication.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7H5VFPL0uh5xbU8wWSbTjw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6brvpb63LAn4q2ZmMMvEEw.jpeg" /></figure><h3>Ingress to the Rescue</h3><p><strong>Ingress</strong> provides a unified solution:</p><ul><li>Acts as a Layer 7 load balancer that can route traffic based on URL paths or domain names.</li><li>Manages SSL termination in one place.</li><li>Is defined using Kubernetes primitives, making it easy to version-control and manage alongside other Kubernetes resources.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1BfX8WTWbYjs9PmQrTCCNQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4cE3eDDEQr5lYyVaL2Ko9g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cjuh28-hsBTFzGHEpgVxdQ.jpeg" /></figure><h3>How Ingress Works</h3><ol><li><strong>Ingress Controller</strong>:</li></ol><ul><li>Ingress is powered by an <strong>Ingress Controller</strong>, which monitors the cluster for <strong>Ingress Resources</strong> (rules) and configures itself accordingly.</li><li>Popular Ingress Controllers include <strong>NGINX</strong>, <strong>Traefik</strong>, and <strong>GCE (Google’s HTTP Load Balancer)</strong>.</li></ul><p><strong>2. Ingress Resource</strong>:</p><ul><li>Specifies routing rules, such as forwarding traffic to specific services based on URLs or hostnames.</li></ul><p><strong>3. Configuration Example</strong>:</p><ul><li>Route myonlinestore.com/shop to the shop-service and myonlinestore.com/watch to the video-service.</li><li>Enable SSL and define a default backend for unmatched traffic (e.g., a 404 error page).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*baVnghYPXMetcDDzP-5PkQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mdyo4f6KhE1L4l4vNcnk5w.jpeg" /></figure><h3>Deploying an Ingress Controller</h3><p>To set up an <strong>NGINX Ingress Controller</strong>:</p><ol><li>Deploy the controller as a <strong>Deployment</strong> with the necessary image and configuration.</li><li>Expose it using a <strong>NodePort</strong> or <strong>LoadBalancer Service</strong> for external access.</li><li>Create a <strong>ConfigMap</strong> to manage NGINX settings.</li><li>Provide a <strong>ServiceAccount</strong> with the required permissions to allow the controller to monitor and manage Ingress Resources.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q3P-kRzQfEHIOH1Hmyfvnw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ZNuccWEyKh2okV4NzQ-zA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cgdvXRlnGALGwhxAFydbOg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_VV1cf-cwbjWkD7yoaSMJg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rN8ebSYG3WWwN5jSNmqcGg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*80dIenJ66DcVIQTIMUA4QA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HmnZDncwTPdeTGYJho275g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPHYJbXTIA1dd-DYp22bfw.jpeg" /></figure><h3>Configuring Ingress Resources</h3><ol><li><strong>Simple Routing</strong>:</li></ol><ul><li>Route all traffic to a single service.</li></ul><p><strong>2. Path-Based Routing</strong>:</p><ul><li>Define multiple paths within a single rule to route traffic to different services.</li><li>Example:</li><li>/shop → shop-service.</li><li>/watch → video-service.</li></ul><p><strong>3. Hostname-Based Routing</strong>:</p><ul><li>Use multiple rules to route traffic based on domain names.</li><li>Example:</li><li>shop.myonlinestore.com → shop-service.</li><li>watch.myonlinestore.com → video-service.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mF8wPP_69OIUvL18fyjQvg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P5e50JiL6GkSYw4YesaEXA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E9-JO5gFBLmWjN3evkxhUg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VA4kf3Q7nYzx5_xbe2E0yA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cnMuMxoiU1Criqiv0xMLpg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ZCe3mZxheKj2BiHPqpi9w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C2LM459Nij_MGb1WqHGObg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dqFNM80Do3A5VyjJYsz14Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vN-60ZfiNFdXacjHeuAEAQ.jpeg" /></figure><p>🎉🎉In conclusion, Kubernetes redefines networking for modern application architecture with its innovative approach to services, load balancing and traffic management, empowering us to build scalable, secure and user-friendly applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96f876cc52fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Navigating Kubernetes Storage: A Full Spectrum Guide]]></title>
            <link>https://medium.com/@masseys.s123/navigating-kubernetes-storage-a-full-spectrum-guide-d356a91cf568?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/d356a91cf568</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Mon, 02 Dec 2024 09:50:02 GMT</pubDate>
            <atom:updated>2024-12-02T09:50:02.095Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g0Ef0IhB7EgKT4JBDp3LrQ.jpeg" /></figure><p><strong>Kubernetes storage</strong> refers to the management of persistent and ephemeral data for applications running in Kubernetes clusters. It plays a key role in ensuring that applications can store and retrieve data outside the lifecycle of the pods (containers), which by default are ephemeral and may be deleted or replaced at any time. Kubernetes provides a variety of tools and resources for managing storage needs for cloud-native applications.</p><h3>***Key Concepts of Docker Storage Drivers and File Systems:</h3><h4>Understanding Docker Data Storage</h4><ul><li>Docker stores data and manages container file systems using a structured approach.</li><li>By default, Docker stores its data under the /var/lib/docker/ directory, which includes:</li><li><strong>AUFS</strong></li><li><strong>Containers</strong></li><li><strong>Images</strong></li><li><strong>Volumes</strong></li></ul><p>Each folder serves a specific purpose:</p><ul><li>Container files are in the containers folder.</li><li>Image data resides in the image folder.</li><li>Volumes created by containers are stored in the volumes folder.</li></ul><h4>Layered Architecture of Docker Images</h4><ul><li>Docker builds images using a <strong>layered approach</strong>:</li><li>Each instruction in the Dockerfile generates a new layer.</li><li>Layers are cached and reused for efficiency.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cy5nwUf8WJKriYXwtDpKhQ.jpeg" /><figcaption>.</figcaption></figure><h4>Writable Layer in Containers</h4><ul><li>When a container is created, a <strong>writable layer</strong> is added on top of the image layers:</li><li>Stores temporary files, logs, and modifications.</li><li>Data in the writable layer is ephemeral and deleted when the container is removed.</li></ul><h4>Copy-on-Write Mechanism</h4><ul><li>Docker uses a <strong>Copy-on-Write</strong> strategy:</li><li>Modifications to files in image layers create a copy in the writable layer.</li><li>Image layers remain unchanged.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I9EFFb6TsYyY_Vho44_Jvw.jpeg" /></figure><h4>Persisting Data with Volumes</h4><ul><li>To retain data beyond the container lifecycle, use <strong>volumes</strong>:</li><li>Create a volume: docker volume create &lt;volume_name&gt;</li><li>Mount the volume: docker run -v &lt;volume_name&gt;:&lt;container_path&gt; &lt;image_name&gt;</li><li>Data persists on the host even if the container is deleted.</li></ul><h4>Bind Mounts</h4><ul><li>Bind mounts allow containers to access existing directories on the host:</li><li>Example: docker run -v /host_path:/container_path &lt;image_name&gt;</li><li>Useful for sharing host files with containers.</li></ul><h4>Volume Mounts vs. Bind Mounts</h4><ul><li><strong>Volume Mounts</strong>: Use Docker-managed volumes stored in /var/lib/docker/volumes.</li><li><strong>Bind Mounts</strong>: Use specific host directories for data storage.</li></ul><h4>New Mount Syntax</h4><ul><li>Modern Docker uses --mount for more detailed configurations</li></ul><pre><br>docker run --mount type=bind,source=/host_path,target=/container_path &lt;image_name&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*enLNyc02Yxg1mBQ3FJQjHg.jpeg" /></figure><h4>Role of Storage Drivers</h4><ul><li>Storage drivers manage Docker’s layered architecture and enable features like writable layers and Copy-on-Write.</li><li>Common drivers include:</li><li><strong>AUFS</strong> (default on Ubuntu)</li><li><strong>Overlay2</strong> (widely used for newer systems)</li><li><strong>Device Mapper</strong>, <strong>BTRFS</strong>, and <strong>ZFS</strong></li><li>Selection depends on the operating system and application needs.</li></ul><h4>Performance and Compatibility</h4><ul><li>Each storage driver offers unique performance and stability characteristics.</li><li>Docker automatically selects an optimal driver based on the host system.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2HVyJA5yK3jtyvxuwJJN8w.jpeg" /></figure><h3>***Volume Drivers:</h3><h4>Storage Drivers vs. Volume Drivers</h4><ul><li><strong>Storage Drivers</strong>: Handle the layered storage architecture for images and containers, managing file systems and ensuring efficient operations.</li><li><strong>Volume Drivers</strong>: Dedicated plugins for managing persistent volumes independently of storage drivers.</li></ul><h4>Default Volume Driver: Local Plugin</h4><ul><li>By default, Docker uses the <strong>local volume plugin</strong> to create volumes.</li><li>Volumes managed by the local plugin are stored on the Docker host under the /var/lib/docker/volumes directory.</li></ul><h4>Third-Party Volume Driver Plugins</h4><ul><li>Docker supports a variety of third-party volume drivers to provision storage across different platforms.</li><li>Examples of volume drivers include:</li><li><strong>Azure File Storage</strong></li><li><strong>Convoy</strong></li><li><strong>DigitalOcean Block Storage</strong></li><li><strong>Flocker</strong></li><li><strong>Google Compute Persistent Disks</strong></li><li><strong>NetApp</strong></li><li><strong>REX-Ray</strong></li><li><strong>Portworx</strong></li><li><strong>VMware vSphere Storage</strong></li></ul><h4>Multi-Provider Support</h4><p>Some volume drivers integrate with multiple storage providers. For instance:</p><ul><li><strong>REX-Ray</strong>: Supports provisioning across platforms such as:</li><li>Amazon EBS and S3</li><li>EMC storage arrays (e.g., Isilon and ScaleIO)</li><li>Google Persistent Disk</li><li>OpenStack Cinder</li></ul><h4>Using Specific Volume Drivers</h4><ul><li>When running a container, you can specify a volume driver to leverage third-party storage solutions.</li><li>Example: Using <strong>REX-Ray EBS</strong>, a volume can be provisioned directly from Amazon EBS and attached to a container. The data persists in the cloud, ensuring durability even after the container exits.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*szWTXm1-zVPyKLB82QUk8w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mswRIw37uRutAhxk81aEaw.jpeg" /></figure><h3>***Container Storage Interface(CSI):</h3><h4><strong>1. Kubernetes and Container Runtime Evolution</strong></h4><ul><li>Initially, Kubernetes relied solely on <strong>Docker</strong> as its container runtime engine, with the integration directly embedded into Kubernetes’ source code.</li><li>With the emergence of alternative runtimes like <strong>RKT</strong> and <strong>CRI-O</strong>, the need for a more flexible and extensible approach became evident.</li></ul><h4><strong>2. Introduction of the Container Runtime Interface (CRI)</strong></h4><ul><li>The <strong>CRI</strong> standard was introduced to decouple Kubernetes from specific container runtimes.</li><li>CRI defines how orchestration platforms like Kubernetes communicate with container runtimes.</li></ul><p><strong>Benefits of CRI:</strong></p><ul><li>Support for multiple container runtimes without modifying Kubernetes source code.</li><li>New runtimes can work with Kubernetes by adhering to CRI standards.</li></ul><h4><strong>3. Parallels with Other Interfaces</strong></h4><p>Similar to the <strong>CRI</strong>, Kubernetes introduced other interfaces for extensibility:</p><ul><li><strong>Container Networking Interface (CNI)</strong>: Enables Kubernetes to work with various networking solutions.</li><li><strong>Container Storage Interface (CSI)</strong>: Supports integration with multiple storage providers.</li></ul><h4><strong>4. Overview of the Container Storage Interface (CSI)</strong></h4><ul><li><strong>Purpose</strong>: A universal standard that allows any container orchestration tool (e.g., Kubernetes, Cloud Foundry, Mesos) to work with any storage vendor via supported plugins.</li><li><strong>Adoption</strong>: Storage vendors like Portworx, Amazon EBS, Azure Disk, NetApp, Dell EMC, and others have developed their own CSI drivers.</li></ul><p><strong>5. How CSI Works</strong></p><ul><li>CSI defines <strong>Remote Procedure Calls (RPCs)</strong> to manage storage operations.</li></ul><p><strong>Example RPCs:</strong></p><ul><li>CreateVolume: Kubernetes calls this RPC to request a new volume, and the storage driver provisions the volume and returns results.</li><li>DeleteVolume: Kubernetes calls this RPC to delete a volume, and the driver handles decommissioning the volume.</li></ul><p><strong>CSI specifies:</strong></p><ul><li>Parameters exchanged during operations.</li><li>Expected results and error codes.</li></ul><p><strong>6. Advantages of CSI</strong></p><ul><li>Simplifies storage integration by defining clear standards.</li><li>Supports extensibility for new storage vendors without requiring changes to Kubernetes code.</li><li>Universality: Not specific to Kubernetes, enabling compatibility with other orchestrators.</li></ul><p><strong>7. For Further Details</strong></p><ul><li>The <strong>CSI specification</strong> is available on GitHub for those interested in the technical details of its implementation and standards.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wC0mradeazgZgSdReiXAUQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ucOYXVKGRL8Yyh_6D4ifHQ.jpeg" /></figure><h3>***Volumes:</h3><h3>Understanding Volumes in Kubernetes</h3><h3>1. Volumes in Docker</h3><p>Containers in Docker are designed to be transient, meaning:</p><ul><li>They exist temporarily to process tasks and are destroyed after completing their work.</li><li><strong>Data inside Docker containers is also ephemeral</strong>, and once a container is deleted, its data is lost.</li></ul><p>To persist data processed by Docker containers, <strong>volumes</strong> are attached to the containers at creation.</p><ul><li>The volume serves as an external storage location, ensuring that data remains accessible even after the container is deleted.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xtqvvcvTo_9kX_7TjaNBmw.jpeg" /></figure><h3>2. Volumes in Kubernetes</h3><p>Similarly, <strong>Pods</strong> in Kubernetes are transient by nature:</p><ul><li>When a Pod is created, it processes tasks, generates data, and is eventually destroyed.</li><li>Without volumes, the data processed by a Pod is also lost upon deletion.</li></ul><p>To address this, Kubernetes provides <strong>volumes</strong> that can be attached to Pods.</p><ul><li>The data generated by the Pod is stored in the volume.</li><li>Even after the Pod is deleted, the data persists in the volume.</li></ul><h3>3. Simple Implementation of Volumes</h3><p>Consider a scenario where:</p><ol><li>A <strong>Pod</strong> generates a random number and writes it to a file (/opt/number.out) inside the container.</li><li>Without a volume, the random number is deleted along with the Pod.</li></ol><p>To retain the data:</p><ul><li>A <strong>volume</strong> is created and configured to use a directory on the host node (e.g., /data).</li><li>The <strong>volume</strong> is mounted to a specific directory inside the container (/opt).</li></ul><p>Now:</p><ul><li>The random number written to /opt/number.out inside the container is saved in the volume.</li><li>This volume is stored at /data on the host, ensuring the data remains even if the Pod is deleted.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HtSXIEyz3XPOlGrI8vSe6g.jpeg" /></figure><h3>4. Volume Storage Options</h3><p>Kubernetes supports various storage backends for volumes. Some common options include:</p><h4>a. HostPath:</h4><ul><li>Uses a directory on the host node as storage for the volume.</li><li>Suitable for <strong>single-node clusters</strong> but not ideal for multi-node clusters, as:</li><li>Each node’s /data directory is independent.</li><li>Data consistency cannot be ensured across nodes.</li></ul><h4>b. Networked Storage:</h4><ul><li>Provides shared storage across multiple nodes.</li><li>Common options:</li><li><strong>NFS (Network File System)</strong></li><li><strong>CephFS</strong></li><li><strong>GlusterFS</strong></li></ul><h4>c. Cloud-Based Solutions:</h4><ul><li>Integrate with public cloud providers:</li><li><strong>AWS EBS (Elastic Block Store)</strong></li><li><strong>Azure Disk/File</strong></li><li><strong>Google Persistent Disk</strong></li><li>Cloud-based volumes are ideal for scalable, highly available setups.</li></ul><h3>5. Configuring a Volume with Storage Backends</h3><p>Let’s look at examples of using different storage backends:</p><h4>a. HostPath Example:</h4><pre><br>volumes:<br>  - name: data-volume<br>    hostPath:<br>      path: /data</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_tzxVAwDLggmzb8KJgcb4g.jpeg" /></figure><h4>b. AWS EBS Example:</h4><pre><br>volumes:<br>  - name: data-volume<br>    awsElasticBlockStore:<br>      volumeID: &quot;&lt;volume-id&gt;&quot;<br>      fsType: ext4<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WS2DNY_BBm-JEi9UuLBMHQ.jpeg" /></figure><h3>6. Persistent Volumes</h3><p>For dynamic and scalable storage management, Kubernetes provides:</p><ul><li><strong>Persistent Volumes (PVs):</strong> Pre-provisioned or dynamically provisioned storage resources in the cluster.</li><li><strong>Persistent Volume Claims (PVCs):</strong> Requests for storage made by Pods.</li></ul><p>The <strong>PVC binds to a PV</strong>, allowing data persistence even across Pod lifecycles.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UwlfhpSLbp21vzRNLxeUgg.jpeg" /></figure><h3>***Persistent Volumes in Kubernetes</h3><h4>Challenges with Pod-Level Volume Configuration</h4><p>We configured <strong>volumes</strong> directly within the <strong>Pod definition file</strong>. While this approach works, it introduces challenges in large environments with many users and applications:</p><ul><li><strong>Repeated Configuration:</strong> Users must specify storage settings for each Pod, duplicating effort.</li><li><strong>Maintenance Overhead:</strong> Any changes to storage configurations require updates across all Pod definitions, increasing complexity.</li><li><strong>Scalability Issues:</strong> Managing storage at the Pod level is inefficient in environments with high workloads and multiple users.</li></ul><p>To address these challenges, Kubernetes introduces <strong>Persistent Volumes (PVs)</strong>.</p><h3>Persistent Volumes</h3><p>Persistent Volumes decouple storage provisioning from Pod deployment:</p><ul><li><strong>Cluster-Wide Storage Pool:</strong> Administrators can create a shared pool of storage that spans the entire cluster.</li><li><strong>Centralized Management:</strong> Storage is provisioned centrally, allowing users to focus on deploying applications without worrying about storage configurations.</li><li><strong>Reusable Storage Resources:</strong> Users can request storage from the shared pool using <strong>Persistent Volume Claims (PVCs)</strong>.</li></ul><h4>How Persistent Volumes Work</h4><ol><li><strong>Administrator Role:</strong></li></ol><ul><li>Administrators define Persistent Volumes with specific storage backends, capacity, and access modes.</li><li>These volumes are available for users to claim and use in their applications.</li></ul><p><strong>2. User Role:</strong></p><ul><li>Users create <strong>Persistent Volume Claims (PVCs)</strong>, specifying their storage requirements (e.g., size, access mode).</li><li>Kubernetes binds the PVC to an appropriate PV from the pool, allowing Pods to use the allocated storage.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_8YaZjsqcc1-TOGG4apTDA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5r_LViEQX2gdITjVEyy3hA.jpeg" /></figure><h3>***Persistent Volume Claims in Kubernetes</h3><h3>1. Relationship Between Persistent Volumes and Persistent Volume Claims</h3><p>In Kubernetes, <strong>Persistent Volumes (PVs)</strong> and <strong>Persistent Volume Claims (PVCs)</strong> are two separate objects within the Kubernetes namespace.</p><ul><li><strong>Administrators</strong> create Persistent Volumes, which define storage resources available across the cluster.</li><li><strong>Users</strong> create Persistent Volume Claims to request and use specific storage from this pool.</li></ul><p>When a PVC is created, Kubernetes attempts to bind it to a suitable PV based on:</p><ul><li><strong>Capacity:</strong> The PV must meet or exceed the storage requested by the PVC.</li><li><strong>Access Modes:</strong> The PV’s access modes (e.g., ReadWriteOnce, ReadOnlyMany) must match the PVC’s requirements.</li><li><strong>Other Properties:</strong> Such as storage class, volume mode, and labels.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O2nw51FLFXZYtCLjco8NPQ.jpeg" /></figure><h3>2. Binding Process</h3><p>Once the binding process begins:</p><ul><li>Kubernetes selects a PV that satisfies all PVC criteria.</li><li><strong>One-to-One Relationship:</strong> A PVC is bound to a single PV, and no other PVC can use the remaining capacity in the PV.</li><li><strong>Smaller Claims, Larger Volumes:</strong> If no exact matches exist, a smaller PVC may be bound to a larger PV, as long as other criteria are satisfied.</li></ul><p>If no suitable PVs are available:</p><ul><li>The PVC remains in a <strong>Pending</strong> state until a new PV is provisioned or created.</li><li>Once a suitable PV becomes available, the PVC is automatically bound.</li></ul><p>For precise control, you can use <strong>labels and selectors</strong> to ensure specific PVCs bind to specific PVs.</p><h3>3. Creating a Persistent Volume Claim</h3><p>Here’s how to create a PVC with an example template:</p><h4>PVC Template Example:</h4><pre><br>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: my-claim<br>spec:<br>  accessModes:<br>    - ReadWriteOnce<br>  resources:<br>    requests:<br>      storage: 500Mi</pre><h3>4. PVC Binding in Action</h3><ol><li><strong>Create the PVC:</strong></li></ol><pre>kubectl create -f pvc.yaml</pre><p>2. View the PVC:</p><pre>kubectl get pvc</pre><ul><li>The PVC will initially appear in a <strong>Pending</strong> state if no suitable PV is available.</li></ul><p><strong>3. Binding Process:</strong></p><ul><li>Kubernetes checks for PVs that meet the PVC’s requirements.</li><li>If a suitable PV exists (e.g., 1 GB capacity with ReadWriteOnce mode), it binds the PVC to the PV, even if the PVC requests less than the PV’s total capacity.</li></ul><p><strong>4. Verify the Binding:</strong></p><pre>kubectl get pv</pre><p>. The PV will show as <strong>Bound</strong> to the PVC.</p><h3>5. Deleting Persistent Volume Claims</h3><p>When a PVC is deleted, its associated PV is affected based on its <strong>Reclaim Policy</strong>:</p><h4>Reclaim Policies:</h4><ol><li><strong>Retain (Default):</strong></li></ol><ul><li>The PV remains in the cluster, but it is not available for reuse.</li><li>Manual intervention by an administrator is required to delete or reuse the PV.</li></ul><p><strong>2. Delete:</strong></p><ul><li>The PV and the associated storage on the backend are deleted automatically, freeing up resources.</li></ul><p><strong>3. Recycle (Deprecated):</strong></p><ul><li>The PV’s data is scrubbed (wiped), and the PV becomes available for other claims.</li></ul><p>Deleting a PVC:</p><pre>kubectl delete pvc my-claim</pre><h3>Key Considerations for PVCs</h3><ul><li><strong>Dynamic Provisioning:</strong> Many environments support dynamic provisioning through storage classes, enabling PVs to be created on-demand when a PVC is submitted.</li><li><strong>Pending State:</strong> Ensure that PVs with matching specifications are available to avoid PVCs getting stuck in a pending state.</li><li><strong>Optimal Use:</strong> Use labels and selectors to bind PVCs to specific PVs when dealing with multiple PVs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EebjQIeyWJEnIg3Fjdi9zw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wj1EIYpZiRvYxvLQ2SY_qQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xEilbJZF_vmg79ux3Kxa0A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sIQd1VLfFsm7Zm2M3QJSDg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q9i9NzrWaLmM8y1el6OI5Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GHjGXPyc1zlfEABd4BO0Ng.jpeg" /></figure><h3>***Storage Class</h3><h3>Dynamic Storage Provisioning with Kubernetes Storage Classes</h3><p>In previous discussions, we explored the process of creating <strong>Persistent Volumes (PVs)</strong> and <strong>Persistent Volume Claims (PVCs)</strong> to allocate storage, which can then be referenced in Pod definitions as volumes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QVamQdqO3xs048kpl5UlIw.jpeg" /></figure><h4>Challenges with Static Provisioning</h4><p>Consider the scenario of provisioning a PVC using a Google Cloud Persistent Disk. Before creating the PV:</p><ol><li>You must manually provision the disk on Google Cloud.</li><li>You then need to create a Persistent Volume definition in Kubernetes, referencing the exact disk.</li></ol><p>This process, known as <strong>static provisioning</strong>, can be tedious and inefficient, especially in environments where storage requirements are frequent and dynamic.</p><h3>What Are Storage Classes?</h3><p>To address the limitations of static provisioning, Kubernetes introduced <strong>Storage Classes</strong>, which enable <strong>dynamic storage provisioning</strong>.</p><p>Dynamic provisioning automates the creation of storage resources whenever a PVC is submitted. A <strong>Storage Class</strong> specifies a <strong>provisioner</strong>, which handles the interaction with the underlying storage system to provision resources automatically.</p><p>For example, a Storage Class using Google Cloud Persistent Disk can dynamically create and attach the required storage to Pods when a PVC requests it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wmOJT7VHm3dhyrxjSfRj9A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i74s22IBtT-wOisBk3hSPA.jpeg" /></figure><h3>How It Works</h3><ol><li><strong>Defining a Storage Class</strong><br>A Storage Class defines how storage is provisioned and managed. It includes:</li></ol><ul><li><strong>Provisioner</strong>: Specifies the underlying storage system (e.g., kubernetes.io/gce-pd for Google Cloud Persistent Disk).</li><li><strong>Parameters</strong>: Additional options like disk type, replication settings, or performance configurations.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qTrHtXMrsIRVIv1SLNVv3w.jpeg" /></figure><p>2 . <strong>Linking a PVC to a Storage Class</strong><br>When creating a PVC, you reference the desired Storage Class using the storageClassName field.</p><p>Example PVC Definition:</p><pre><br>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: example-pvc<br>spec:<br>  accessModes:<br>    - ReadWriteOnce<br>  resources:<br>    requests:<br>      storage: 50Gi<br>  storageClassName: fast-storage</pre><p><strong>3. Automatic Provisioning</strong></p><ul><li>When the PVC is created, Kubernetes uses the specified Storage Class (fast-storage) to provision the required storage.</li><li>The provisioner automatically creates a Persistent Disk with the requested size and parameters.</li><li>A Persistent Volume is then created and bound to the PVC, enabling immediate usage by the Pod.</li></ul><p>This eliminates the need for manual PV creation, as the process is fully automated by the Storage Class.</p><h3>Provisioners and Customization</h3><p>Kubernetes supports various provisioners for different storage systems, including:</p><ul><li><strong>Cloud Storage Solutions</strong>:</li><li>Google Cloud Persistent Disk (kubernetes.io/gce-pd)</li><li>AWS Elastic Block Store (kubernetes.io/aws-ebs)</li><li>Azure File/Block (kubernetes.io/azure-file, kubernetes.io/azure-disk)</li><li><strong>On-Premises and Networked Storage</strong>:</li><li>CephFS</li><li>Portworx</li><li>ScaleIO</li></ul><p>Each provisioner allows specific customizations. For instance, with Google Cloud Persistent Disk, you can configure:</p><ul><li><strong>Disk Type</strong>: Choose between pd-standard (HDD) or pd-ssd.</li><li><strong>Replication Mode</strong>: Options like none for single-zone or regional-pd for multi-zone redundancy.</li></ul><h3>Storage Classes as Tiers of Service</h3><p>Administrators can create different <strong>tiers of service</strong> based on performance and cost requirements. For example:</p><ul><li><strong>Standard Class</strong>: Uses cost-effective HDDs.</li><li><strong>Performance Class</strong>: Uses SSDs for faster IOPS.</li><li><strong>Enterprise Class</strong>: Incorporates SSDs with regional replication for high availability.</li></ul><p>Users can select the desired class of storage by referencing the appropriate Storage Class name in their PVCs. This flexibility ensures that storage resources align with application needs while optimizing cost and performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oGAgYxrb52XomzSolvNO0A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5-eBT4ZNcR5R9mViWl4ERw.jpeg" /></figure><h3>Key Benefits of Dynamic Provisioning</h3><ol><li><strong>Automation</strong>: Automatically creates storage resources, saving time and effort.</li><li><strong>Flexibility</strong>: Supports a wide variety of storage backends and configurations.</li><li><strong>Scalability</strong>: Dynamically adjusts to evolving application storage needs.</li><li><strong>Simplified Management</strong>: Reduces manual intervention by administrators.</li></ol><h3>Conclusion:</h3><p>As technology evolves, so must our understanding of the platforms that drive innovation. By fostering knowledge sharing, exchanging ideas, and collaborating within the Kubernetes community, we can continue to push the boundaries of what’s possible. My journey of learning, sharing, and contributing represents a small but meaningful part of this broader collective effort to build a more capable, collaborative, and forward-thinking cloud-native future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d356a91cf568" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“How Kubernetes Handles Authentication and Authorization”]]></title>
            <link>https://medium.com/@masseys.s123/how-kubernetes-handles-authentication-and-authorization-b188ed77c1bb?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b188ed77c1bb</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Mon, 25 Nov 2024 06:08:32 GMT</pubDate>
            <atom:updated>2024-11-25T06:08:32.031Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*99fAoOVy83LmDeoO2YwYoQ.jpeg" /></figure><h3>Authentication and Security in Kubernetes:</h3><p><strong>Authentication</strong> ensures that only authorized users or systems can access the Kubernetes API, identifying the entity making an API request.</p><p><strong>Security</strong> in Kubernetes is vital due to its role in managing containerized applications across clusters. These containers often hold sensitive data and run critical workloads, making robust security measures essential to protect against unauthorized access and potential threats.</p><h3>***Securing Kubernetes with TLS Certificates</h3><p>To secure a Kubernetes cluster, TLS certificates are essential for encrypting communication and authenticating components.</p><h3>Key Concepts:</h3><ol><li><strong>Public/Private Keys</strong>: Servers use a private key to decrypt messages encrypted with its public key.</li><li><strong>Certificates</strong>: A public key paired with identification, signed by a trusted <strong>Certificate Authority (CA)</strong>.</li><li><strong>Types of Certificates</strong>:</li></ol><ul><li><strong>Server Certificates</strong>: Used by servers (API server, etcd, kubelets) for secure communication.</li><li><strong>Client Certificates</strong>: Used by clients (admins, scheduler, controller) to authenticate themselves.</li><li><strong>Root Certificates (CA)</strong>: The CA signs and verifies other certificates.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*Hb5WpNCNt1Qrivt0.jpeg" /></figure><h3>Kubernetes Components and Certificates:</h3><ul><li><strong>API Server</strong>: Requires a server certificate (apiserver.crt and apiserver.key) to secure communication with clients.</li><li><strong>ETCD Server</strong>: Requires its own certificate pair (etcdserver.crt and etcdserver.key).</li><li><strong>Kubelets</strong>: Worker node components requiring a certificate (kubelet.crt and kubelet.key).</li><li><strong>Clients</strong>: Components like the <strong>admin user</strong> (using admin.crt and admin.key), <strong>scheduler</strong>, and <strong>kube-proxy</strong> need certificates to authenticate with the API server.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*6BJ8q6JHy4td1Bpj.jpeg" /></figure><h3>Certificate Authority (CA):</h3><p>A <strong>CA</strong> signs the certificates used in the cluster. Kubernetes can use one CA for the entire cluster or separate CAs for different components like etcd. The CA’s certificates are referred to as ca.crt and ca.key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*Q-ldJdoGVzSfbTMG.jpeg" /></figure><h3>***Generating Certificates for Kubernetes Cluster</h3><ol><li><strong>Tools for Certificate Generation</strong>: Use tools like <strong>OpenSSL</strong>, <strong>Easy-RSA</strong>, or <strong>CFSSL</strong>. This lecture focuses on using <strong>OpenSSL</strong>.</li><li><strong>Creating CA Certificates</strong>:</li></ol><ul><li>Generate a <strong>private key</strong> with openssl genrsa.</li><li>Create a <strong>Certificate Signing Request (CSR)</strong> specifying the common name (e.g., Kubernetes-CA).</li><li><strong>Sign</strong> the CSR using OpenSSL to create a <strong>self-signed certificate</strong> for the CA.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*_8vI2gn8hVgZwDFd.jpeg" /></figure><p><strong>3. Generating Client Certificates (e.g., Admin User)</strong>:</p><ul><li>Create a <strong>private key</strong> for the admin using OpenSSL.</li><li>Generate a CSR, specifying the name (e.g., Kube Admin).</li><li><strong>Sign</strong> the certificate using the <strong>CA key</strong> to make it valid within the cluster.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*5NMUBcoSQxzXgzhZ.jpeg" /></figure><p><strong>4. User Identification</strong>: Specify the user’s group (e.g., System Masters) in the certificate to give administrative privileges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*v8iRCxdIvNFCni9U.jpeg" /></figure><p><strong>5. Generating Certificates for Other Components</strong>:</p><ul><li>Follow the same process for components like <strong>kube-scheduler</strong>, <strong>kube-controller-manager</strong>, and <strong>kube-proxy</strong>.</li><li>Use appropriate prefixes (system-) for system components.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*NLbgC1Pll9DmMHBJ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*HUdEqP3F2MQcxtGc.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*Na_1YN5t2nz_P-e0.jpeg" /></figure><p><strong>6. Server-Side Certificates</strong>:</p><ul><li><strong>ETCD Server</strong>: Generate certificates for secure communication, including <strong>peer certificates</strong> for high-availability clusters.</li><li><strong>Kube API Server</strong>: Generate a certificate with all DNS names and IP addresses using an OpenSSL configuration file. This ensures the API server can be reached by different names (e.g., Kubernetes.default.svc).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*OD-03kqmZZvnbnFJ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*x-E_E691c4gECcvH.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*XNzV8u3R8s7Q5IiN.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*jkukSaPTSDS6azjM.jpeg" /></figure><p><strong>7. Using Certificates</strong>:</p><ul><li>For client-server authentication, certificates are specified in the <strong>kubeconfig</strong> file.</li><li>The <strong>CA root certificate</strong> is used by all components to validate each other’s certificates.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*fI1DOw9p3M9UfBst.jpeg" /></figure><p><strong>8. Kubelet Certificates</strong>:</p><ul><li>Each <strong>node</strong> in the cluster requires a unique certificate, named after the node (e.g., node01, node02).</li><li>Use the appropriate <strong>kubelet config</strong> with node certificates and the CA certificate.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*4-p_znlcVM0I68_g.jpeg" /></figure><p><strong>9. Kube API Server Permissions</strong>:</p><ul><li>Nodes must be added to the <strong>System Nodes</strong> group for proper permissions, similar to how the admin user gets administrative privileges.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*QZlCGw3HcoXvRiz9.jpeg" /></figure><h3>***Viewing and Checking Certificates in a Kubernetes Cluster</h3><p>As a new administrator in a Kubernetes environment, you’re tasked with performing a health check on the cluster’s certificates. Here’s a streamlined approach:</p><ol><li><strong>Understand the Cluster Setup:</strong></li></ol><ul><li>If the cluster is manually deployed, you generated certificates yourself. If deployed with <strong>Kubeadm</strong>, the tool automatically handles certificates.</li></ul><p><strong>2. Identify All Certificates</strong>:</p><ul><li>Locate the Kubernetes API server definition file, typically found in /etc/kubernetes/manifests (for Kubeadm setups).</li><li>List all certificate files, their paths, names, alternate names, organizations, issuers, and expiration dates.</li></ul><p><strong>3. Examine Certificate Details</strong>:</p><ul><li>Use the <strong>OpenSSL X509</strong> command to inspect certificate details.</li><li>Check the <strong>subject</strong> (e.g., kube-apiserver), <strong>alternate names</strong>, <strong>validity period</strong>, and <strong>issuer</strong> (should be the Kubernetes CA).</li><li>Ensure certificates have the correct names, are issued by the right CA, and have not expired.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*VJ5bvKSNPcaISoHv.jpeg" /></figure><p><strong>4. Logs for Troubleshooting</strong>:</p><ul><li>For a manually configured cluster, check system service logs.</li><li>For Kubeadm-based clusters, check pod logs using kubectl logs &lt;pod-name&gt;.</li><li>If core components (API server, etcd) are down, use Docker commands (docker ps -a and docker logs &lt;container-id&gt;) to access logs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*JQCbOnDZzk3yLGNP.jpeg" /></figure><h3>***Key Concepts: Kubeconfigs in Kubernetes</h3><ol><li><strong>Purpose of Kubeconfigs</strong>:</li></ol><ul><li>Simplifies Kubernetes cluster access and user authentication by storing configuration details in a single file.</li></ul><p><strong>2. Structure of Kubeconfig File</strong>:</p><ul><li><strong>Clusters</strong>: Lists the Kubernetes clusters you access (e.g., dev, test, prod).</li><li><strong>Users</strong>: Stores user credentials (e.g., admin or dev user) for cluster access.</li><li><strong>Contexts</strong>: Links a user to a cluster for specific operations (e.g., admin@production).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KKLR4CXJeC9oShTOjpCo_A.jpeg" /></figure><p><strong>3. Default Location</strong>:</p><ul><li>By default, kubectl looks for a config file in the .kube directory in the user&#39;s home folder.</li><li>This eliminates the need to specify the file path in every kubectl command.</li></ul><p><strong>4. Config Example</strong>:</p><ul><li>YAML format with three sections: clusters, users, and contexts.</li><li>Example:</li><li><strong>Clusters</strong>: Includes the server address and CA certificate.</li><li><strong>Users</strong>: Defines client certificate and key for authentication.</li><li><strong>Contexts</strong>: Maps a user to a cluster.</li></ul><p><strong>5. Setting a Default Context</strong>:</p><ul><li>Use the current-context field in the kubeconfig file to specify the default context.</li><li>Switch contexts with the kubectl config use-context command.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ry9PuIpXxKytDsnnreoqkA.jpeg" /></figure><p><strong>6. Customizing Contexts</strong>:</p><ul><li>Add namespaces to contexts to automatically work within a specific namespace.</li><li>Example: Configure a context to use a namespace like dev-namespace.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FOfCqRq9qDAMG_zn2g3b-Q.jpeg" /></figure><p><strong>7. Base64 Encoding for Certificates</strong>:</p><ul><li>Instead of file paths, certificate data (e.g., CA or client certificates) can be embedded directly in the kubeconfig in Base64-encoded format.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3UMyhNMlIa4W9XFAGcP8vw.jpeg" /></figure><p><strong>8. Viewing and Managing Kubeconfigs</strong>:</p><ul><li>View the current kubeconfig with kubectl config view.</li><li>Modify the file (e.g., update clusters, users, or contexts) using commands like kubectl config set-context.</li></ul><p><strong>9. Using Multiple Kubeconfig Files</strong>:</p><ul><li>Specify a custom kubeconfig file using the --kubeconfig flag.</li><li>Useful for managing multiple clusters or environments.</li></ul><p><strong>10. Practical Usage</strong>:</p><ul><li>Move frequently used kubeconfig files to the default .kube directory for convenience.</li><li>Update and troubleshoot configurations with kubectl config commands.</li></ul><h3>Authorization in Kubernetes:</h3><p><strong>Authorization</strong> in Kubernetes determines what actions a user or system is allowed to perform after successful authentication. It ensures that authenticated entities can only access resources and perform operations they are permitted to.</p><h3>***Role-Based Access Control (RBAC) in Kubernetes</h3><p>RBAC in Kubernetes provides fine-grained access control by assigning roles and permissions to users, groups, or service accounts. It ensures that users can only perform authorized actions within their scope.</p><h3>Creating and Managing Roles in RBAC</h3><h4>Defining a Role</h4><p>A <strong>Role</strong> specifies permissions for resources within a namespace. Here’s how to create one:</p><ol><li><strong>Role Object Definition:</strong></li></ol><ul><li>The apiVersion is set to rbac.authorization.k8s.io/v1.</li><li>The kind is Role.</li><li>Rules are defined with three key sections:</li></ul><p><strong>* API Groups</strong>: Defines the API group (e.g., &quot;&quot; for the Core group).</p><p><strong>*Resources</strong>: Specifies the resources (e.g., Pods, ConfigMaps).</p><p><strong>*Verbs</strong>: Lists the allowed actions (get, list, create, delete).</p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: Role<br>metadata:<br>  namespace: default<br>  name: developer<br>rules:<br>- apiGroups: [&quot;&quot;]<br>  resources: [&quot;pods&quot;]<br>  verbs: [&quot;get&quot;, &quot;list&quot;, &quot;create&quot;, &quot;delete&quot;]<br>- apiGroups: [&quot;&quot;]<br>  resources: [&quot;configmaps&quot;]<br>  verbs: [&quot;create&quot;]</pre><p><strong>2. Creating the Role:</strong> Use the following command:</p><pre>kubectl create -f role-definition.yaml</pre><h4>Linking Roles to Users</h4><p>To assign a role to a user, create a <strong>RoleBinding</strong>.</p><ol><li><strong>RoleBinding Definition:</strong></li></ol><ul><li>The kind is RoleBinding.</li><li><strong>Subjects</strong> specify the user, group, or service account.</li><li><strong>RoleRef</strong> links the binding to a specific role.</li></ul><pre><br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: RoleBinding<br>metadata:<br>  namespace: default<br>  name: dev-user-binding<br>subjects:<br>- kind: User<br>  name: dev-user<br>  apiGroup: rbac.authorization.k8s.io<br>roleRef:<br>  kind: Role<br>  name: developer<br>  apiGroup: rbac.authorization.k8s.io</pre><p>2.Creating the RoleBinding:</p><pre>kubectl create -f rolebinding-definition.yaml</pre><h3>***Namespace Scope of Roles and RoleBindings</h3><ul><li><strong>Roles and RoleBindings</strong> are namespace-specific.</li><li>To apply permissions in a different namespace, specify it in the metadata section.</li><li>Use ClusterRole and ClusterRoleBinding for cluster-wide permissions.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XZWMlC92GMG1fHk4PzAb2A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RPIxHtXsHVbDf8ovvW8Xkw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2qkOokffymrNYvKAMwAkbg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W2bFjOltzr3AE5XD8PZIxQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SN-4PExA7M_wfcucipyU0A.jpeg" /></figure><h3>***Cluster Roles and Cluster Role Bindings in Kubernetes</h3><p>In Kubernetes, <strong>ClusterRoles</strong> and <strong>ClusterRoleBindings</strong> extend Role-Based Access Control (RBAC) to cluster-scoped resources, allowing you to manage permissions for resources that are not tied to specific namespaces.</p><h3>Difference Between Namespace and Cluster-Scoped Resources</h3><ol><li><strong>Namespace Resources:</strong></li></ol><ul><li>Created within a specific namespace and limited to that namespace.</li><li>Examples: Pods, ReplicaSets, Deployments, Services, Secrets, and Roles.</li></ul><p><strong>2. Cluster-Scoped Resources:</strong></p><ul><li>Exist at the cluster level, not associated with any namespace.</li><li>Examples: Nodes, PersistentVolumes, Namespaces themselves, CertificateSigningRequests, ClusterRoles, and ClusterRoleBindings.</li></ul><p>To view a full list of namespaced and non-namespaced resources:</p><pre><br>kubectl api-resources --namespaced=true<br>kubectl api-resources --namespaced=false</pre><h3>Cluster Roles</h3><p>A <strong>ClusterRole</strong> defines permissions for cluster-scoped resources. Unlike regular Roles, ClusterRoles can also be applied to namespaced resources across all namespaces.</p><h4>Creating a ClusterRole</h4><ol><li>Define the ClusterRole in a YAML file</li></ol><pre><br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRoleBinding<br>metadata:<br>  name: cluster-admin-binding<br>subjects:<br>- kind: User<br>  name: admin-user<br>  apiGroup: rbac.authorization.k8s.io<br>roleRef:<br>  kind: ClusterRole<br>  name: cluster-admin<br>  apiGroup: rbac.authorization.k8s.io</pre><p>2. Create the ClusterRoleBinding:</p><pre>kubectl create -f clusterrolebinding.yaml</pre><h3>Key Concepts</h3><ol><li><strong>Namespace Considerations:</strong></li></ol><ul><li>ClusterRoles are typically used for cluster-scoped resources.</li><li>However, they can also be used for namespaced resources, granting access across all namespaces.</li><li>Example: A ClusterRole granting access to Pods applies to Pods in all namespaces.</li></ul><p><strong>2. Default ClusterRoles:</strong></p><ul><li>Kubernetes creates several predefined ClusterRoles (e.g., cluster-admin, view, edit) during cluster setup.</li><li>Use these roles as needed or create custom ClusterRoles.</li></ul><p><strong>3. Combining Roles and Bindings:</strong></p><ul><li>For namespace-specific access, use Roles and RoleBindings.</li><li>For cluster-wide or cross-namespace access, use ClusterRoles and ClusterRoleBindings.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*egKZGf4Kzr48vW008ASZVw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bVVT--WG0ZKskyGiWiIH0A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bUD50sASLQvnYHmqGUKHfA.jpeg" /></figure><h3>***Service Accounts in Kubernetes</h3><p>Service accounts in Kubernetes play a key role in enabling secure access to the Kubernetes API for applications and processes. They differ from user accounts and are designed for non-human entities like applications or automation tools that need to interact with the cluster.</p><h3>What Are Service Accounts?</h3><ol><li><strong>User Accounts vs. Service Accounts:</strong></li></ol><ul><li><strong>User Accounts:</strong> Designed for human users (e.g., administrators or developers) to access and manage the cluster.</li><li><strong>Service Accounts:</strong> Designed for applications or automated tools to interact with the Kubernetes API.</li></ul><p><strong>2. Examples of Service Account Use Cases:</strong></p><ul><li>A monitoring tool like <strong>Prometheus</strong> accessing cluster metrics via the Kubernetes API.</li><li>A CI/CD pipeline like <strong>Jenkins</strong> deploying applications to the cluster.</li></ul><h3>Key Concepts: Service Accounts, Tokens, and Recent Enhancements in Kubernetes:</h3><h4>1. Default Service Account Behavior</h4><ul><li>Every <strong>namespace</strong> in Kubernetes includes a <strong>default service account</strong>.</li><li>By default, when a pod is created:</li><li>The default service account and its token are automatically mounted to the pod as a <strong>volume</strong>.</li><li>The token is stored at /var/run/secrets/kubernetes.io/serviceaccount inside the pod.</li><li>The token can be used by processes in the pod to authenticate with the Kubernetes API.</li><li>The default service account is <strong>highly restricted</strong> and suitable for basic Kubernetes API queries only.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rr0odzIc5ZewUC8Y41OKBQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gc2l1TUPB1GqlaICTWWNrg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wV-JrXeJB5s7OliW32eC5Q.jpeg" /></figure><h4>2. Specifying a Custom Service Account</h4><ul><li>To use a custom service account:</li><li>Update the pod specification by adding the serviceAccountName field and specifying the custom account.</li></ul><pre><br>spec:<br>  serviceAccountName: custom-service-account</pre><p>. Pods cannot have their service accounts changed after creation. You must delete and recreate the pod. However, for deployments, updating the service account in the pod spec will trigger a new rollout, recreating the pods with the updated account.</p><h4>3. Disabling Automatic Mounting</h4><ul><li>Automatic mounting of the service account token can be disabled by setting automountServiceAccountToken: false in the pod specification:</li></ul><pre><br>spec:<br>  automountServiceAccountToken: false</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q_OiqO2BmeAWkIR8qN_U6A.jpeg" /></figure><h4>4. Changes in Kubernetes Versions 1.22 and 1.24</h4><p><strong>Version 1.22: Token Request API</strong></p><ul><li><strong>Issues with Legacy Tokens:</strong></li><li>Tokens from default secrets are not audience-bound, time-bound, or object-bound.</li><li>Legacy tokens lack expiry, posing <strong>security and scalability risks</strong>.</li><li><strong>Enhancements Introduced:</strong></li><li><strong>Token Request API</strong> was implemented via <a href="https://github.com/kubernetes/enhancements/issues/1205">KEP 1205</a>.</li><li>Tokens generated by this API are:</li><li><strong>Audience-bound:</strong> Used only by specific consumers.</li><li><strong>Time-bound:</strong> Have a defined expiration (e.g., one hour by default).</li><li><strong>Object-bound:</strong> Valid only for a specific service account.</li><li>Tokens are mounted to pods as <strong>projected volumes</strong>, replacing the older secret-based tokens.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GGCUBteiduaO_Iw5jeaMGw.jpeg" /></figure><p><strong>Version 1.24: Reduction of Secret-Based Tokens:</strong></p><p><strong>Removal of Default Secret Creation:</strong></p><ul><li>Service accounts no longer automatically create associated secret tokens.</li><li>If a token is required, it must be explicitly generated using</li></ul><pre><br>kubectl create token &lt;service-account-name&gt;</pre><ul><li>Tokens created this way are <strong>time-limited</strong> (default: one hour).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KSdjpsk6t6A-Q1LQnZP02g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e4avC-VFg_yPHfB_dMejzA.jpeg" /></figure><p><strong>Creating Non-Expiring Tokens (Not Recommended):</strong></p><ul><li>If necessary, you can manually create a secret with the kubernetes.io/service-account-token type:</li></ul><pre><br>apiVersion: v1<br>kind: Secret<br>metadata:<br>  name: custom-token<br>  annotations:<br>    kubernetes.io/service-account.name: custom-service-account<br>type: kubernetes.io/service-account-token</pre><p>. Use this only if the <strong>Token Request API</strong> is not viable, as it introduces security risks.</p><h3>***Image Security:</h3><h3>Understanding Image Names and Using Secure Image Repositories</h3><p>Let’s dive deeper into how image names work and how they relate to secure image practices.</p><h3>Image Names and Sources</h3><ol><li><strong>Basic Image Naming Convention:</strong></li></ol><ul><li>Consider a pod definition file where an <strong>Nginx</strong> container is deployed</li></ul><pre><br>containers:<br>  - name: nginx<br>    image: nginx</pre><p>. Here, nginx represents the <strong>image name</strong>. But what does it mean, and where does Kubernetes pull this image from?</p><p><strong>2. Understanding the Structure:</strong></p><ul><li>The name follows Docker’s naming convention:</li><li>nginx is shorthand for library/nginx.</li><li>library is the default <strong>account name</strong> for Docker&#39;s official images, which adhere to best practices and are maintained by a dedicated team</li><li>If you create custom images, the name might look like:</li></ul><pre><br>&lt;account-name&gt;/&lt;repository-name&gt;:&lt;tag&gt;</pre><p><strong>3. Default Registry:</strong></p><ul><li>If no registry is specified, Kubernetes assumes the image is hosted on Docker Hub (DNS: docker.io).</li><li>Example:</li><li>nginx resolves to docker.io/library/nginx.</li></ul><p><strong>4. Alternative Registries:</strong></p><p>Other registries include:</p><p>. Google Container Registry(GCR): gcr.io</p><p>. <strong>Amazon Elastic Container Registry (ECR):</strong> aws_account_id.dkr.ecr.region.amazonaws.com</p><p><strong>. Azure Container Registry (ACR):</strong> &lt;your_registry_name&gt;.azurecr.io</p><p>These registries are used for both public and private images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sw6f9OX3kjsVlVibJnnV-Q.jpeg" /></figure><h3>Key Concepts: Using Private Docker Registries in Kubernetes:</h3><h4>1. Accessing Private Docker Registries in Docker</h4><ul><li><strong>Authentication</strong>:</li><li>Use the docker login command to authenticate with a private Docker registry.</li><li>Provide your <strong>username</strong>, <strong>password</strong>, and optionally, <strong>email</strong> for access.</li><li><strong>Running a Container</strong>:</li><li>Once authenticated, specify the full image path from the private registry to pull and run the container:</li></ul><pre><br>docker run &lt;private-registry&gt;/&lt;image-name&gt;:&lt;tag&gt;</pre><h4>2. Accessing Private Registries in Kubernetes</h4><ul><li><strong>Challenge</strong>:</li><li>Kubernetes worker nodes need credentials to pull private images when creating pods.</li><li>Authentication and credential management must be automated for seamless image pulling.</li></ul><h4>3. Solution: Kubernetes Secret for Docker Registry Credentials</h4><ul><li><strong>Secret Type</strong>:</li><li>Create a Kubernetes Secret of type docker-registry.</li><li><strong>Contents</strong>: Store the credentials for the private registry:</li><li><strong>Registry Server Name</strong>: e.g., <a href="https://index.docker.io/v1/">https://index.docker.io/v1/</a></li><li><strong>Username and Password</strong>: Credentials for the private registry.</li><li><strong>Email Address</strong>: Associated with the registry account.</li></ul><h4>4. Steps to Create and Use the Secret</h4><p><strong>** Create a Secret</strong>:</p><ul><li>Use the kubectl create secret command</li></ul><pre><br>kubectl create secret docker-registry regcred \<br>  --docker-server=&lt;registry-server&gt; \<br>  --docker-username=&lt;username&gt; \<br>  --docker-password=&lt;password&gt; \<br>  --docker-email=&lt;email&gt;</pre><p>. Replace placeholders with actual registry details.</p><p><strong>**Reference the Secret in the Pod Spec</strong>:</p><ul><li>Add the imagePullSecrets field in the pod specification</li></ul><pre><br>spec:<br>  imagePullSecrets:<br>    - name: regcred</pre><p><strong>** Pod Creation</strong>:</p><ul><li>When a pod is created, <strong>kubelet</strong> uses the credentials from the secret to authenticate with the registry and pull the required image.</li></ul><h4>5. Key Kubernetes Concepts</h4><ul><li><strong>Docker Registry Secret</strong>: A built-in Kubernetes secret type for securely storing Docker credentials.</li><li><strong>Image Pull Secrets</strong>: Configured in the pod specification to reference the secret for private registry authentication.</li></ul><p><strong>6. Example Pod Spec Using a Private Image</strong></p><pre><br>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: private-registry-pod<br>spec:<br>  containers:<br>  - name: my-container<br>    image: &lt;private-registry&gt;/&lt;image-name&gt;:&lt;tag&gt;<br>  imagePullSecrets:<br>  - name: regcred</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TLa2omJgM0J9TbDlFFWabw.jpeg" /></figure><h3>***Security Context:</h3><p>Key Concepts: Configuring Security Contexts in Kubernetes</p><h4>1. Configuring Security Context at the Pod Level</h4><ul><li>The securityContext field allows you to define security settings for a <strong>Pod</strong>.</li><li>Example configuration for a <strong>Pod</strong> running an Ubuntu image with a sleep command:</li></ul><pre><br>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: secure-pod<br>spec:<br>  securityContext:<br>    runAsUser: 1000  # Set the user ID for all containers in the Pod<br>  containers:<br>  - name: ubuntu-container<br>    image: ubuntu<br>    command: [&quot;sleep&quot;, &quot;3600&quot;]</pre><ul><li><strong>runAsUser</strong>: Defines the user ID under which processes in the Pod will run, enhancing security by avoiding root-level privileges.</li></ul><h4>2. Configuring Security Context at the Container Level</h4><ul><li>Security settings can also be applied at the <strong>Container</strong> level for more granular control:</li></ul><pre><br>spec:<br>  containers:<br>  - name: ubuntu-container<br>    image: ubuntu<br>    command: [&quot;sleep&quot;, &quot;3600&quot;]<br>    securityContext:<br>      runAsUser: 1000  # Set the user ID for this specific container</pre><h4>3. Adding Linux Capabilities</h4><ul><li>Use the capabilities field under securityContext to fine-tune Linux kernel privileges for the <strong>Container</strong>:</li></ul><pre><br>    securityContext:<br>      capabilities:<br>        add: <br>        - NET_ADMIN  # Add network administration capability<br>        - SYS_TIME   # Allow modification of system time</pre><p><strong>Capabilities</strong>:</p><ul><li><strong>Add</strong>: Grants specific privileges to the container. For example:</li><li>NET_ADMIN: Manage network settings.</li><li>SYS_TIME: Adjust system time</li></ul><h3>Why Use Security Contexts?</h3><ul><li><strong>Enforce Least Privilege</strong>: Prevent containers from running with unnecessary or excessive permissions.</li><li><strong>Granularity</strong>: Define security settings for either the entire Pod or individual Containers.</li><li><strong>Improve Security</strong>: Reduce risks associated with running containers as root or with unrestricted access.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aaq6BqdOliUz18U86J1VPg.jpeg" /></figure><h3>***NetWork Policies:</h3><h4>1. Networking Basics in Applications</h4><ul><li>A typical application has multiple components:</li><li><strong>Web Server</strong>: Serves front-end traffic (e.g., HTTP on port 80).</li><li><strong>API Server</strong>: Processes backend requests (e.g., port 5000).</li><li><strong>Database Server</strong>: Stores and serves data (e.g., port 3306).</li></ul><p><strong>Traffic Types</strong>:</p><ul><li><strong>Ingress</strong>: Incoming traffic to a component (e.g., HTTP requests to port 80).</li><li><strong>Egress</strong>: Outgoing traffic from a component (e.g., API server querying the database).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W25tmOYodkq8rF_qFvK7sQ.jpeg" /></figure><h4>2. Networking in Kubernetes</h4><ul><li>Kubernetes provides each node, pod, and service with a unique IP address.</li><li>Pods can communicate across nodes using a virtual private network (VPN) within the cluster.</li></ul><p><strong>Default Behavior</strong>:</p><ul><li>All pods can communicate with one another without additional configuration.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PLImvdcxUWnnkXSnT_lvLA.jpeg" /></figure><h4>3. Introducing Network Policies</h4><ul><li><strong>Purpose</strong>: Restrict pod-to-pod communication based on security requirements.</li></ul><p><strong>Use Case</strong>:</p><ul><li>By default, all pods (e.g., web server, API server, database) can communicate.</li><li>A <strong>Network Policy</strong> can restrict traffic, e.g., allowing the database to accept traffic only from the API server.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*khrHmbOx_r_0lF3h5ruXZQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8sLLcz2HWjPMC1aya4cYFA.jpeg" /></figure><h4>4. Network Policy Configuration</h4><ul><li>A <strong>Network Policy</strong> is a Kubernetes object defined within a namespace.</li></ul><p><strong>How It Works</strong>:</p><ul><li>Policies apply to selected pods using <strong>labels</strong>.</li><li>Rules specify allowed <strong>ingress</strong> (incoming) or <strong>egress</strong> (outgoing) traffic.</li><li>Once applied, only traffic matching the rules is permitted; all other traffic is blocked.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fl2W7I7uS1Fn22htlUX1uA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jQaHVMK3NYyKXANgOjaerQ.jpeg" /></figure><h4>5. Steps to Configure a Network Policy</h4><ol><li><strong>Create a Policy Object</strong>:</li></ol><ul><li>Define API version, kind, metadata, and spec.</li><li>Use networking.k8s.io/v1 as the API version.</li></ul><pre><br>apiVersion: networking.k8s.io/v1<br>kind: NetworkPolicy<br>metadata:<br>  name: db-policy<br>spec:<br>  podSelector:<br>    matchLabels:<br>      app: database  # Apply policy to pods labeled &quot;app=database&quot;</pre><p><strong>Add Policy Types</strong>:</p><ul><li>Specify ingress, egress, or both under policyTypes.</li></ul><pre><br>  policyTypes:<br>  - Ingress  # Isolate ingress traffic</pre><p><strong>Define Rules</strong>:</p><ul><li>Add <strong>Ingress</strong> or <strong>Egress</strong> rules for specific traffic:</li><li>Define allowed traffic sources using labels and selectors.</li><li>Specify allowed ports.</li></ul><p>Example (allowing API server access to database):</p><pre><br>  ingress:<br>  - from:<br>    - podSelector:<br>        matchLabels:<br>          app: api  # Allow traffic from API server pods<br>    ports:<br>    - protocol: TCP<br>      port: 3306  # Allow traffic on port 3306</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5KAqR3yj32hfnVviTf7t1Q.jpeg" /></figure><h3>***Developing network policies:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C2VHttq7Udn176glJ9Dqyw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tilXy9NBLXnn6KlfYiKY6g.jpeg" /></figure><p><strong>Final Thoughts</strong><br>As I continue my journey to understand Kubernetes, it’s clear that security, especially in the form of authentication and authorization, is not a static implementation but an ongoing commitment. Each concept I’ve learned has reinforced the importance of embedding security at every stage of the Kubernetes lifecycle — from development to deployment.</p><p>Through this process, I’ve gained insights into how Kubernetes empowers DevOps engineers to confidently manage clusters while safeguarding against threats. My efforts to explore these critical aspects have equipped me to implement best practices, adapt to emerging challenges, and contribute to creating secure, resilient applications in Kubernetes environments. This evolving understanding underscores the balance between accessibility and protection, essential for modern application delivery at scale.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b188ed77c1bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“In-Depth Kubernetes Scheduling: Core Concepts, YAML Configurations, and Key Components”]]></title>
            <link>https://medium.com/@masseys.s123/in-depth-kubernetes-scheduling-core-concepts-yaml-configurations-and-key-components-5b5ae9f24653?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b5ae9f24653</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Mon, 18 Nov 2024 15:48:16 GMT</pubDate>
            <atom:updated>2024-11-18T15:48:16.897Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_099FcouyMP-w2xtIMhD4g.jpeg" /></figure><h3>*Manual Scheduling:</h3><p>Every Pod in Kubernetes includes a field called nodeName, which is not set by default when you create a Pod manifest. Typically, you don’t need to specify this field manually. Instead, Kubernetes automatically assigns it during the scheduling process.</p><p>Here’s how it works:</p><ol><li>The Kubernetes Scheduler scans all Pods to identify those without a nodeName set. These Pods are considered <strong>candidates for scheduling</strong>.</li><li>The scheduler then runs its <strong>scheduling algorithm</strong> to determine the most suitable Node for each candidate Pod, based on factors like resource availability, affinity rules, and taints/tolerations.</li><li>Once the appropriate Node is identified, the scheduler assigns the Pod to that Node by setting the nodeName field to the name of the selected Node. This assignment is implemented by creating a <strong>binding object</strong>.</li></ol><p><strong>****What happens if there is no scheduler to monitor and schedule nodes?</strong><br>If there is no scheduler to monitor and assign Pods to Nodes, the Pods remain in a <strong>Pending</strong> state because they have no Node allocated.</p><p><strong>****What can you do about it?</strong><br>To resolve this, you can manually assign Pods to Nodes. The simplest way to do this is by specifying the nodeName field in the Pod specification file at the time of creation. This directly assigns the Pod to the specified Node. However, the nodeName field can only be set during Pod creation.</p><ul><li><strong>***What if the Pod is already created and you want to assign it to a Node?</strong><br>Kubernetes does not allow you to modify the nodeName property of an existing Pod. In such cases, you can create a <strong>binding object</strong> and send a POST request to the Pod&#39;s Binding API, which mimics the scheduler&#39;s behavior. The binding object specifies the target Node, and the POST request includes this data in JSON format. This approach allows you to manually bind a Pod to a specific Node after it has been created.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f_gfo8KQiX6SA1D9w3SA-Q.jpeg" /></figure><h3>*Labels and Selectors:</h3><p>*** how exactly do you specify labels in Kubernetes?</p><p>n a Kubernetes pod definition file, you can add a labels section under the metadata field, where labels are specified in key-value pairs, such as app: app-one. You can add as many labels as needed. Once the pod is created with these labels, you can select the pod using the kubectl get pods command with the --selector option, specifying a condition like app=app-one to filter the pods based on the label. This method helps in efficiently managing and selecting pods in a Kubernetes environment.</p><pre># Kubectl get pods --selector app=App1</pre><h4>This is one use case of labels and selectors. Kubernetes objects use labels and selectors internally to establish connections between different objects.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cK0VufNPF_G7jUTB1mwQgA.jpeg" /></figure><p>In a ReplicaSet definition file, labels are defined in two places: one under the template section, which applies to the pods, and another at the top, which pertains to the ReplicaSet itself. Beginners often confuse these. The labels under the template section are important for connecting the ReplicaSet to the pod. While the ReplicaSet&#39;s labels are used for discovering the ReplicaSet through other objects, our focus is on connecting the ReplicaSet to the pod using the selector field in the ReplicaSet specification, which matches the pod labels. A single label can suffice, but if there are multiple pods with the same label but different functions, using multiple labels ensures the ReplicaSet discovers the correct pods.</p><p>When a <strong>service</strong> is created, it uses the selector defined in the service definition file to match the labels set on the pods in the ReplicaSet definition file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fnHXd9AGU7DzRYMH4YzEyA.jpeg" /></figure><p>Additionally,<strong> annotations</strong> are used to record other informational details. While labels and selectors group and select objects, annotations store supplementary information, such as tool details (name, version, build info) or contact information (phone numbers, email addresses) that may be needed for integration purposes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zJqI9mg4i0JBOzhu7e1KtQ.jpeg" /></figure><h3>*Taints and Tolerations:</h3><p>Taints and tolerations are used to control which pods can be scheduled on a node. In a cluster with three worker nodes (one, two, and three) and pods (A, B, C, D), the scheduler initially places pods evenly across the nodes. To restrict placement on node one, we add a “blue” taint to the node, preventing all pods from being scheduled there. To allow certain pods, like pod D, to be placed on node one, we add a toleration to pod D, making it compatible with the taint. As a result, only pod D is scheduled on node one, while the other pods are placed on different nodes based on the taint.</p><h4>So remember, taints are set on nodes and tolerations are set on pods. So how do you do this?</h4><h3>Taints- Node:</h3><pre># kubectl taint nodes node-name key=value:taint-effect</pre><p>In Kubernetes, taints can have three effects that determine how a pod interacts with a tainted node:</p><ol><li><strong>NoSchedule</strong>: This effect prevents any pod that does not tolerate the taint from being scheduled on the node. Pods without the matching toleration are not allowed to run on the tainted node.</li><li><strong>PreferNoSchedule</strong>: This effect tells the scheduler to avoid placing pods on the tainted node if possible. However, if no other nodes are available, it will still allow the pod to be scheduled on the tainted node.</li><li><strong>NoExecute</strong>: This effect not only prevents new pods without the matching toleration from being scheduled on the node, but also evicts any already running pods that do not tolerate the taint.</li></ol><p>These effects allow fine-grained control over pod placement and management on Kubernetes nodes.</p><h3>Tolerations-Pod:</h3><pre># kubectl taint nodes node1 app=blue:NoSchedule</pre><h4>Pod-definition.yaml:</h4><pre>apiversion:<br>kind: pod<br>metadata:<br>  name: my app-pod<br>spec:<br>  containers:<br>  - name: nginx-container<br>    image: nginx<br>  tolerations:<br>  - keys: &quot;app&quot;<br>  operator: &quot;Equal&quot;<br>  value: &quot;blue&quot;<br>  effect: &quot;NoSchedule&quot;<br><br></pre><p>Tolerations are added to pods to allow them to be scheduled on tainted nodes. To add a toleration, first open the pod definition file. In the spec section, add a tolerations section and include the same values used when creating the taint. For example, set the key to app=blue, the operator to Equal, and the effect to NoSchedule, with all values enclosed in double quotes. When the pods are created or updated with the new tolerations, they will either be scheduled on nodes or evicted from existing nodes, depending on the effect set.</p><h4>Taint-No Execute:</h4><p>The <strong>NoExecute</strong> taint effect prevents non-tolerating pods from being scheduled on a node and evicts any running pods that don’t tolerate the taint. In our example, we taint node one for a specific application and add a toleration to pod D. This results in pod C being evicted from node one, while pod D continues running. Taints and tolerations control which pods can be placed on a node but don’t guarantee that a pod will always be scheduled on a specific node. For node-specific placement, <strong>node affinity</strong> is used.</p><p>Additionally, master nodes have a default taint to prevent scheduling pods, a behavior that can be modified but is generally avoided for application workloads.</p><pre># kubectl describe node kubemaster | grep Taint<br>Taints:          node-role.kubernetes.io/master:NoSchedule</pre><h3>*Node Selectors:</h3><p><strong>Node selectors</strong> in Kubernetes are used to constrain pods to specific nodes based on labels. They allow you to specify conditions for which nodes a pod can be scheduled on.</p><h4>pod-definition.yaml</h4><pre><br>api version:<br>kind: pod<br>metadata:<br>   name: myapp-pod<br>spec:<br>   containers:<br>   - name: data-processor<br>     image: data-processor<br>   nodeSelector:<br>     size: Large</pre><p>To limit this pod to run on the larger node, we add a new property called node selector to the spec section and specify the size as large.</p><p>**How does Kubernetes know which is the large node?</p><p>The key value pair of size and large are in fact labels assigned to the nodes. The scheduler uses these labels to match and identify the right node to place the pods on.</p><pre> # kubectl label nodes &lt;node-name&gt; &lt;label-key&gt;=&lt;label-value&gt;<br><br> # kubeclt labe nodes node-1 size=large<br><br></pre><p>This ensures the pod is scheduled on the larger node. While effective, node selectors are limited to exact matches with single labels. For complex requirements, like specifying multiple conditions (e.g., “place on large or medium nodes”), <strong>node affinity</strong> is needed, which we will explore next.</p><h3>*Node Affinity:</h3><p><strong>Node affinity</strong> is a more advanced and flexible alternative to node selectors in Kubernetes. It allows you to define more complex rules for scheduling pods on specific nodes based on node labels. Node affinity supports conditions such as “preferred” or “required” for scheduling decisions.</p><h3>Types of Node Affinity</h3><ol><li><strong>RequiredDuringSchedulingIgnoredDuringExecution</strong>: Pods can only be scheduled on nodes that meet the specified criteria. If no such nodes exist, the pod will remain unscheduled.</li><li><strong>PreferredDuringSchedulingIgnoredDuringExecution</strong>: Kubernetes attempts to schedule pods on nodes that meet the specified criteria, but it does not enforce this strictly. If no such nodes are available, the pod can still be scheduled elsewhere.</li></ol><h3>Example</h3><p>Here’s how node affinity is specified in a pod definition file:</p><pre><br>spec:<br>  affinity:<br>    nodeAffinity:<br>      requiredDuringSchedulingIgnoredDuringExecution:<br>        nodeSelectorTerms:<br>        - matchExpressions:<br>          - key: size<br>            operator: In<br>            values:<br>            - large<br>            - medium<br>      preferredDuringSchedulingIgnoredDuringExecution:<br>      - weight: 1<br>        preference:<br>          matchExpressions:<br>          - key: region<br>            operator: In<br>            values:<br>            - us-west</pre><h4>Explanation:</h4><ul><li>The requiredDuringSchedulingIgnoredDuringExecution ensures the pod is scheduled only on nodes labeled size=large or size=medium.</li><li>The preferredDuringSchedulingIgnoredDuringExecution makes the scheduler prioritize nodes labeled region=us-west if multiple options are available.</li></ul><h4>Use Case:</h4><p>Node affinity is useful for scenarios like:</p><ul><li>Scheduling workloads close to specific hardware (e.g., GPUs or high-memory nodes).</li><li>Controlling workload placement across geographical zones or availability regions.</li></ul><h4>Type3:</h4><p>The <strong>requiredDuringSchedulingRequiredDuringExecution</strong> feature (planned) would extend Kubernetes&#39; <strong>node affinity</strong> rules to enforce placement conditions not only during scheduling but also throughout the pod&#39;s lifecycle.</p><h4>What It Does</h4><ul><li><strong>Required During Scheduling</strong>: Ensures that a pod is scheduled only on nodes matching the specified labels.</li><li><strong>Required During Execution</strong>: Continuously validates that the pod’s assigned node still meets the affinity criteria. If the node’s labels or conditions change and no longer satisfy the affinity rules, the pod would be evicted or rescheduled.</li></ul><h4>Key Benefits</h4><ol><li><strong>Continuous Enforcement</strong>: Unlike the existing requiredDuringSchedulingIgnoredDuringExecution, this ensures affinity rules are upheld throughout the pod&#39;s lifecycle.</li><li><strong>Dynamic Adaptability</strong>: Automatically relocates pods if nodes become unsuitable, maintaining compliance with workload placement policies.</li><li><strong>Enhanced Control</strong>: Suitable for critical applications that require strict adherence to resource or policy constraints, even after deployment.</li></ol><h3>*Resource Requirement and Limits:</h3><p><strong>Resource Requirements and Limits</strong> in Kubernetes allow you to manage how much CPU and memory a pod or container can use. These settings ensure fair resource distribution and prevent a single workload from monopolizing the cluster’s resources.</p><p><strong>pod-definition.yaml</strong></p><pre><br>apiversion:<br>kind: pod<br>metadata:<br>  name: simple-webapp-color<br>  labels:<br>    name: simple-webapp-color<br>spec:<br>   containers:<br>   - name: simple-webapp-color<br>     image: simple-webapp-color<br>     ports:<br>       - containerport: 8080<br>     resources:<br>        memory: &quot;1Gi&quot;<br>        cpu:<br>     limits:<br>       memory: &quot;2Gi&quot;<br>       cpu: 2<br></pre><h3>LimitRange:</h3><p><strong>LimitRange</strong> is a Kubernetes resource used to enforce default resource requests, limits, and constraints within a namespace. It helps ensure that all pods and containers operate within defined resource boundaries, promoting efficient cluster resource utilization and preventing resource contention.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3-GEi2gI2pjDXY2KePKLwQ.jpeg" /></figure><p><strong>Note</strong>: These limits are enforced when a pod is created. So if you create or change a limit range, it does not affect existing pods. It’ll only affect newer pods that are created after the limit range is created or updated.</p><h4>Resource Quotas:</h4><p><strong>Resource Quotas</strong> in Kubernetes enforce resource usage constraints within a namespace, ensuring fair distribution of resources across teams or applications in a shared cluster. By setting limits on the total resource usage per namespace, administrators can prevent one namespace from monopolizing cluster resources.</p><h4><strong>resource-quota.yaml</strong></h4><pre><br>apiversion: v1<br>kind: ResourceQuota<br>metadata:<br>    name: my-resource-quota<br>spec:<br>  hard:<br>   requests.cpu: 4<br>   requests.memory: 4Gi<br>   limits.cpu: 10<br>   limits.memory: 10Gi<br></pre><h3>*DaemonSets:</h3><p><strong>DaemonSet</strong> in Kubernetes is a specialized controller used to ensure that a specific pod runs on all (or selected) nodes in a cluster. This is useful for deploying system-level applications or tools that need to operate on every node, such as log collectors, monitoring agents, or network plugins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iPJznPvV_YRhrxbtxnvHCg.jpeg" /></figure><h3>Static Pods:</h3><p>Earlier, we covered the Kubernetes architecture and how the kubelet interacts with the kube-apiserver to schedule pods based on the kube-scheduler’s decisions stored in etcd. But what happens if there is no master node, no API server, and no cluster components? Can the kubelet function independently?</p><p>Yes, the kubelet can operate independently on a node without the API server. It can manage containers using Docker and create static pods based on locally stored pod definition files. These files are placed in a designated directory, and the kubelet monitors this directory to create and maintain the pods. If a pod’s definition is modified or deleted, the kubelet reflects those changes automatically.</p><h3>Key Points:</h3><ul><li><strong>Static Pods</strong>: Created and managed by the kubelet without the API server’s intervention.</li><li><strong>Pod Definition Files</strong>: These are placed in a directory (e.g., /etc/kubernetes/manifests), and the kubelet reads them to create pods.</li><li><strong>Kubelet’s Role</strong>: The kubelet ensures the pod’s lifecycle, restarting it if necessary.</li><li><strong>Viewing Static Pods</strong>: Use docker ps to see the pods, as kubectl requires an API server to function.</li><li><strong>Cluster Integration</strong>: In a cluster, the kubelet creates both static pods and those requested by the API server. Static Pods are represented in the API server as read-only mirror objects.</li><li><strong>Static Pods for Control Plane</strong>: You can use static pods to deploy Kubernetes control plane components (e.g., API server, etcd) directly on master nodes.</li></ul><h3>Static Pods vs DaemonSets:</h3><ul><li><strong>Static Pods</strong>: Managed by the kubelet, independent of the Kubernetes control plane.</li><li><strong>DaemonSets</strong>: Managed by the kube-apiserver and ensure a pod runs on all nodes in a cluster.</li></ul><h3>Use Case:</h3><p>Static Pods are particularly useful for deploying control plane components on master nodes, ensuring they are resilient and self-healing, as the kubelet will automatically restart any failed pods.</p><p>This method is employed by tools like kubeadm for setting up Kubernetes clusters, where the control plane components are configured as static pods on master nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nycNsp6aBy1h8GJEX3jMpQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TP5TmWCx6MkbNJDisRaNSQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fwSkzEEvMmGdssgxMA2IYQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F5vpv34CldKnCtFoA8AqQg.jpeg" /></figure><pre><br>Kubelet.service<br># --pod-manifest-path=/etc/kubernetes/manifests\\<br><br># --config=kubeconfig.yaml\\<br><br><br># kubeconfig.yaml<br># static pod path:/etc/kubernetes/manifest<br><br>#ls /etc/kubernetes/manifests<br>etcd.yml   kube-apiserver.yml  kube-controller-manager.yml kube-scheduler.yml<br></pre><h3>*Multiple Schedulers:</h3><p>In Kubernetes, the default scheduler is responsible for placing pods onto nodes. However, there are situations where you might want to use multiple schedulers in a cluster. This can be achieved by configuring additional schedulers besides the default one. Multiple schedulers allow you to have specialized scheduling logic for different types of workloads or use cases.</p><h4>Key Concepts for Multiple Schedulers:</h4><ol><li><strong>Default Scheduler</strong>: This is the built-in scheduler that handles the placement of most pods in a Kubernetes cluster.</li><li><strong>Custom Schedulers</strong>: These are additional schedulers that you can configure to handle specific types of workloads. Custom schedulers can be implemented to meet specific requirements like custom affinity, resource allocation strategies, or scheduling based on specialized hardware.</li><li><strong>Scheduler Name</strong>: When defining a pod, you can specify the scheduler name in the pod specification. By default, this is set to default-scheduler. If you want the pod to be scheduled by a custom scheduler, you set this field to the custom scheduler’s name.</li></ol><h3>Deploy Additional Scheduler as a Pod:</h3><p>In Kubernetes, you can deploy <strong>additional schedulers</strong> alongside the default scheduler to handle specific workloads, custom scheduling policies, or specialized use cases. Kubernetes allows for multiple schedulers to operate within the same cluster, ensuring flexibility for different workloads that require different scheduling logic.</p><h4>Why Use Additional Schedulers?</h4><p>By default, Kubernetes uses a single scheduler (the <strong>kube-scheduler</strong>) to place Pods on Nodes based on various criteria such as resource availability, taints, tolerations, and affinity rules. However, in some use cases, you may require specialized scheduling logic that the default scheduler cannot handle effectively. This is where an additional scheduler can be useful. Some reasons to use additional schedulers include:</p><ul><li><strong>Custom Scheduling Policies</strong>: If your application has specific scheduling requirements not met by the default scheduler (e.g., scheduling based on custom criteria like geographic region, custom resource types, or complex affinity rules).</li><li><strong>Multiple Workload Types</strong>: Running workloads with distinct resource needs that require different handling. For example, one scheduler could be used for high-performance workloads while another handles general-purpose workloads.</li><li><strong>High Availability</strong>: In large clusters, running multiple schedulers can provide fault tolerance and ensure that the scheduling workload is distributed.</li></ul><pre><br><br># wget https://storage.googleapis.com/kubernetes-release/release/v1.12.0/bin/linux/amd64/kube-scheduler<br><br><br><br>kube-scheduler.service<br><br># --config=/etc/kubernetes/config/kube-scheduler.yaml<br><br><br>my-scheduler-2.service<br><br># --config=/etc/kubernetes/config/my-scheduler-2-config.yaml<br><br>my-scheduler.service<br><br># --config=/etc/kubernetes/config/my-scheduler-config.yaml </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GQv5cvfD4Nu1ET2OPCtHYQ.jpeg" /></figure><p>In Kubernetes, <strong>leader election</strong> is a mechanism used to ensure that among multiple instances of a component (such as a custom scheduler or controller), only one instance acts as the “leader” at any given time. This is particularly useful when you have multiple components performing similar tasks, and you want to prevent conflicts or duplicate work. Leader election ensures that only one instance performs a critical operation, while others remain in standby mode, ready to take over in case the current leader fails.</p><h4>How Leader Election Works in Kubernetes:</h4><ul><li><strong>Leader Election Config</strong>: Kubernetes provides leader election via a configuration that utilizes Kubernetes’ <strong>ConfigMaps</strong> or <strong>Endpoints</strong> to coordinate and manage leadership. The component (like a custom scheduler or controller) periodically tries to acquire leadership by updating a shared resource (usually a ConfigMap or Endpoints object).</li><li><strong>Kubernetes API Resources</strong>: Leader election is typically implemented using <strong>leases</strong> in Kubernetes, which are stored in <strong>ConfigMaps</strong> or <strong>Endpoints</strong> resources within the cluster.</li></ul><h4>Leader Election in Practice:</h4><ol><li><strong>Multiple Pods for Leader Election</strong>: If you deploy multiple instances of a service, such as a custom scheduler or a controller, you configure leader election so that only one instance is selected as the leader. The leader performs the actual tasks, while the other instances wait in standby mode.</li><li><strong>Leader Election Process</strong>:</li></ol><ul><li>Each instance attempts to acquire a lease on the ConfigMap or Endpoints resource.</li><li>The first instance to acquire the lease becomes the leader.</li><li>The leader periodically renews the lease to maintain leadership.</li><li>If the leader fails to renew the lease (e.g., due to a crash), another instance can acquire the lease and become the new leader.</li></ul><p><strong>3. Failure Handling</strong>: If the current leader instance fails or becomes unavailable, Kubernetes will automatically trigger the leader election process to elect a new leader from the remaining instances.</p><h3>*Scheduler Profiles:</h3><p>In Kubernetes, <strong>Scheduler Profiles</strong> provide a way to configure different scheduling behaviors for workloads. This feature allows you to use multiple configurations for the scheduler to meet different requirements, such as prioritizing specific workloads, using custom scheduling algorithms, or integrating third-party schedulers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P0um9AtwKfCmiq0kYRQ9GQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uNWi-SWypIadvk4R7LXz3w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NDDMVVuQj7RtSjsLqnK87w.jpeg" /></figure><p>pod-definition.yaml</p><pre>apiversion: v1<br>kind: pod<br>metadate:<br>  name: simple-webapp-color<br>spec:<br>  priorityclassName: high-priority<br>  containers:<br>  - name: simple-webapp-color<br>    image: simple-webapp-color<br>      resources:<br>        requests: <br>           memory: &quot;1Gi&quot;<br>           cpu: 10<br></pre><pre><br>apiversion: scheduling.k8s.io/v1<br>kind: priorityclass<br>metadata:<br>  name: high-priority<br>value: 1000000<br>globalDefault: false<br>description: &quot;This priority class should be used for xyz service pods only.&quot;</pre><p>This blog covered the essential Kubernetes scheduling concepts, including node selectors, affinity, tolerations, and custom schedulers. By understanding these key components and YAML configurations, Kubernetes administrators can optimize resource allocation, ensure workload efficiency, and manage complex applications in a cloud-native environment. Mastering Kubernetes scheduling is essential for achieving scalability and reliability in modern infrastructures.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b5ae9f24653" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unlocking the Power of Linux Commands: Recall and Learn]]></title>
            <link>https://medium.com/@masseys.s123/unlocking-the-power-of-linux-commands-recall-and-learn-1bfa8077efa8?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1bfa8077efa8</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Mon, 04 Nov 2024 10:46:34 GMT</pubDate>
            <atom:updated>2024-11-04T10:46:34.655Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*upgQ8Pq6glF6od89s0hlcQ.jpeg" /></figure><h3>What is Linux:</h3><p>Linux is an open-source, Unix-like operating system kernel initially developed by Linus Torvalds. It serves as a core component in various environments, including servers, desktops, mobile devices, and embedded systems. Known for its stability, security, and flexibility, Linux has become a foundational element in enterprise IT, powering a significant share of the world’s computing infrastructure across industries.</p><h3>Start with the basic commands of Linux:</h3><pre><br># ls-l<br># drwxr-x - - 4 ubuntu ubuntu 4096 Aug 31 10:58 ubuntu<br>d-directory<br>rwx -owner (first ubuntu is the owner of the folder or  the file)<br>r-x -group (second ubuntu is  the group associated with the folder or the file)<br>r-x -other user<br>size- 4096<br>time-Aug 31 10:58<br>ubuntu(last ubuntu is the folder or the file)<br><br>Hidden folder command (.)<br># ls-la<br># ls lart<br><br><br>create a file<br># touch shazia.txt<br># ls <br>shazia.txt <br># mkdir new_folder<br>shazia.txt new_folder<br><br><br>edit the text file<br># vi shazia.txt  (safe the file---&gt;  # :wq    quit the file ---&gt; # :q) <br>see the content of the file<br># cat shazia.txt<br><br>moveing file &amp; copying file<br># mv shazia.txt new_folder<br># cd new_folder<br># ls <br># shazia.txt<br># cd ..<br><br><br></pre><h4>User Management:</h4><pre><br><br>** - -sudo /root user<br># sudo su<br># passwd ubuntu<br>In case you want to see the users<br># cd /etc<br>cat passwd<br># ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash<br>cd ..<br>switch user<br># exit<br><br><br>** - -Normal User<br>** - -Service Account User<br></pre><h4>**ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash</h4><p>The line ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash defines the user ubuntu, with UID 1000, GID 1000, home directory /home/ubuntu, full name Ubuntu, and default shell /bin/bash, with the password stored securely in /etc/shadow.</p><h4>*adduser / useradd:</h4><p>useradd is a low-level command for creating users with minimal setup, while adduser is a higher-level, user-friendly script that automates user creation with prompts for additional details.</p><pre><br># adduser shazia<br># sudo adduser shazia<br># cd /etc<br># ls<br># passwd<br># shazia:x:1001:1001:shazia massey,73,528745875428754,:/home/shazia:/bin/bash</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DK-dfXX1J8xvH98RipA9VA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oBWZmeBc66oaEQFqFaEMow.jpeg" /></figure><h4>***switch the user: ubuntu to shazia:</h4><pre># cd /etc<br># su - shazia<br>password<br># exit</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*11Zmkjl63WuJPNlySTbAqA.jpeg" /></figure><h4>***add user shazia in another group :</h4><pre><br># sudo usermod -g shazia shazia<br># sudo addgroup devops<br><br><br>(primary   (secondary   (secondary<br>  group)     group)       group)<br>shazia       devops         infra<br>  |            |              |<br>shazia      shazia         shazia<br><br><br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gDq3QDZO8fji6DKMbkxi2Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hmDKuoTfvT788KWFfHQFQQ.jpeg" /></figure><h4>***Delete User:</h4><pre><br># sudo deluser shazia<br># sudo delgroup shazia<br># sudo delgroup infra</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2yEbCcpefNthsmWvHOuc_A.jpeg" /></figure><h4>***VI/VIM Editor:</h4><pre> vim a.txt<br><br>gg: GO to the begining of the file<br>G: Go to the end of the file<br>O: Move to the begining of the current line<br>$: Move to the end of the current line<br>w: Move to the beginning of the next word<br>b: Move back to the beginning of the current word<br><br><br>i: Enter Insert Mode before the cursor<br>a: Enter Insert Mode after the cursor<br>I: Enter Insert Mode at the beginning of the line<br>3G: goto line 3<br>/searchchpattern:  searches the pattern<br>: %s/old/new/gc: Replace with confirmation<br>A: Enter Insert Mode at the end of the line<br>o: Open a new line below the current line<br>O: Open a new line above the current line<br>r: Replace the character under the cursor<br>x: Delete the character under the cursor<br>d2: delete 2next lines+currentline<br>dd: Delete the current line<br>u: Undo the last action<br>Ctrl + r: Redo the undone action<br><br><br>:w: Save the file<br>:q: Quit Vim<br>:wq: Save and quit<br>:q! Quit without saving changes <br><br></pre><h3>Permissions on files and folders:</h3><p>To modify permissions on files and folders in a Unix-like operating system, you can use the chmod command. This command allows you to set the permissions for different users (owner, group, others) in terms of read, write, and execute access</p><h4>1. Basic Permissions</h4><p>There are three types of permissions:</p><ul><li><strong>Read (r)</strong>: Allows viewing the file or listing a directory’s contents.</li><li><strong>Write (w)</strong>: Allows modifying the file or directory contents.</li><li><strong>Execute (x)</strong>: Allows running the file as a program or accessing a directory.</li></ul><h4>2. Permission Levels</h4><p>Each file or folder has three levels of users:</p><ul><li><strong>Owner (u)</strong>: The user who owns the file.</li><li><strong>Group (g)</strong>: The group that owns the file.</li><li><strong>Others (o)</strong>: All other users.</li></ul><h4>3. Numeric Notation</h4><p>Each permission type is represented by a number:</p><ul><li><strong>Read</strong> = 4</li><li><strong>Write</strong> = 2</li><li><strong>Execute</strong> = 1</li></ul><h3>To change permissions on files or directories:</h3><ol><li><strong>Setting Permissions with Numeric Notation</strong></li></ol><pre><br>chmod [permission] [filename]<br><br># sudo chmod 777 shazia/</pre><p>2. <strong>Changing Directory Permissions Recursively</strong></p><pre><br># Sudo chmod 777 -R shazia/</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*flP16wMyyyCh0JFvoFFtPA.jpeg" /></figure><p>3. <strong>Setting Permissions with Symbolic Notation</strong></p><p><strong>. Command Format</strong>:</p><pre><br>chmod [user]+[permission] [filename]</pre><p><strong>Examples</strong>:</p><ul><li><strong>Grant execute permission to the owner</strong>:</li></ul><pre><br># chmod u+x myfile.txt</pre><p><strong>. Remove write permission from group:</strong></p><pre># chmod g-w myfile.txt</pre><p>. Set read, write, execute for the owner and read-only for others:</p><pre><br># chmod u=rwx,go=r myfile.txt</pre><p>The rm -rf command is a powerful command in Unix/Linux used to delete files and directories. However, it should be used with caution as it <strong>permanently deletes files and directories without confirmation</strong>. Here’s a breakdown:</p><h3>Explanation of rm -rf</h3><ul><li><strong>rm</strong>: Stands for &quot;remove.&quot; It’s the command to delete files or directories.</li><li><strong>-r</strong>: Recursive. This option is used to delete directories and all contents within them (subdirectories and files).</li><li><strong>-f</strong>: Force. This option forces deletion without prompting for confirmation, even for write-protected files.</li></ul><p><strong>To change the ownership of files and directories </strong>in Linux, you can use the chown command. This command allows you to set the owner and/or group for files and directories.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hc8ewB0-EFmS0vPEgspXdQ.jpeg" /></figure><p>To change both the <strong>owner</strong> and <strong>group</strong> of a file or directory at the same time, use the chown command with the following syntax:</p><pre><br># chown newowner:newgroup filename</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0s_r7x7gPIPpkzUb29RedA.jpeg" /></figure><p>A <strong>package manager</strong> is a tool that automates the process of installing, updating, configuring, and removing software packages on an operating system. Package managers handle dependencies, versioning, and repositories to make it easier to manage software efficiently.</p><p>Both curl and wget are command-line tools used for downloading content from the internet, but they have different strengths and purposes. In summary, use curl for more complex data transfers and API interactions, and choose wget for simple, robust file downloads and recursive directory fetching.</p><h3>**Networking Commands:</h3><p><strong>1. ifconfig</strong>: Displays or configures network interfaces.</p><pre><br># ifconfig</pre><p><strong>2. ip</strong>: A modern alternative to ifconfig for managing network interfaces and routing.</p><pre><br>ip addr show                   # Display IP addresses<br>ip link show                   # Show network interfaces<br>ip route show                  # Display routing table</pre><p>3.<strong>iwconfig</strong>: For configuring wireless network interfaces (e.g., Wi-Fi).</p><pre># iwconfig</pre><h3>**Connection Testing:</h3><p>. <strong>ping</strong>: Sends ICMP packets to a host to test connectivity and measure round-trip time.</p><pre> # ping google.com</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*85Gj7uFOxG4rENhAGDFQBg.jpeg" /></figure><p><strong>. traceroute</strong>: Shows the path packets take to reach a network host.</p><pre># traceroute google.com</pre><h3>**Network Monitoring:</h3><p><strong>. netstat</strong>: Shows network connections, routing tables, and more.</p><pre><br># netstat -tuln                # Show active listening ports<br># netstat -i                   # Display network interface statistics<br></pre><h3>**DNS and Domain Name Resolution:</h3><p><strong>nslookup</strong>: Queries DNS to retrieve domain name or IP address information</p><pre><br>#  nslookup example.com</pre><p><strong>dig</strong>: More advanced DNS lookup tool, showing detailed DNS information</p><pre><br># dig example.com</pre><p><strong>host</strong>: Simple tool to find IP addresses associated with domain names</p><pre> # host example.com</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*de3AZ1B0oLVmoNececrBPw.jpeg" /></figure><p>Mastering Linux commands boosts productivity and deepens system understanding, transforming how we engage with technology. I hope this blog serves as a valuable resource for refreshing your knowledge and sharpening your skills.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1bfa8077efa8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes Pod Autoscaling: Understanding Horizontal and Vertical Scaling]]></title>
            <link>https://medium.com/@masseys.s123/kubernetes-pod-autoscaling-understanding-horizontal-and-vertical-scaling-687f3adf132a?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/687f3adf132a</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Mon, 28 Oct 2024 09:12:24 GMT</pubDate>
            <atom:updated>2024-10-28T09:12:24.972Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sk5Xth8_HmhCULUjFbo3Aw.jpeg" /></figure><p><strong>Horizontal Pod Autoscaling</strong> (HPA) in Kubernetes is a mechanism that automatically adjusts the number of pod replicas in a deployment or replica set based on observed metrics like CPU or memory usage.</p><h4>How HPA Works:</h4><p>HPA constantly monitors the specified metrics, such as CPU usage. If the average utilization across all pods exceeds the target threshold, more pods are added to handle the load. Conversely, if utilization drops <strong>below</strong> the threshold, the number of pods is reduced.</p><h4>Benefits:</h4><ul><li><strong>Efficient Resource Utilization</strong>: Pods are scaled up or down based on demand, ensuring the application runs efficiently.</li><li><strong>Cost Savings</strong>: Scaling down when demand is low can reduce resource costs.</li><li><strong>Improved Performance</strong>: Scaling up during high demand ensures the application remains responsive.</li></ul><p><strong>Vertical Pod Autoscaling</strong> (VPA) in Kubernetes is a feature that dynamically adjusts the resource requests (CPU and memory) of individual pods based on real-time usage. Unlike Horizontal Pod Autoscaling (HPA), which scales the number of pod replicas to handle load, VPA optimizes the resources allocated to each pod, ensuring that they are neither over-provisioned nor under-provisioned. This enables efficient resource utilization and maintains application performance by adjusting pod resources according to their actual needs.</p><h4>How VPA Works:</h4><p>VPA continuously monitors the resource consumption of pods and provides recommendations to increase or decrease resource requests based on actual usage. When configured to operate automatically, VPA reschedules the pod with the updated resource allocations, ensuring optimal resource utilization without manual intervention.</p><h4>Benefits:</h4><ul><li><strong>Efficient Resource Utilization</strong>: Helps ensure that pods have the exact amount of CPU and memory they need, neither under-provisioned nor over-provisioned.</li><li><strong>Cost Savings</strong>: Avoids over-provisioning, ensuring only the necessary resources are used, leading to potential cost savings.</li><li><strong>Simplified Scaling</strong>: Instead of adding more pods when existing pods are underutilized, VPA allows you to scale resource requests dynamically.</li></ul><p>Let’s understand this concept through practical implementation in a project.</p><h3>Provisioned and configured an Amazon EKS cluster on an EC2 instance of type t2.medium.</h3><pre><br><br><br><br>*--------------- Install AWS CLI<br>curl &quot;https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip&quot; -o &quot;awscliv2.zip&quot;<br>sudo apt install unzip<br>unzip awscliv2.zip<br>sudo ./aws/install<br><br>aws configure<br><br><br># git clone https://github.com/shazia-massey/Pod-Auto-Scaling.git<br># cd Pod-Auto-Scaling<br># ls<br># cd Cluster<br># ls<br># eks-rbac.md  main.tf  output.tf  variables.tf<br><br><br>*----------------terraform <br># sudo snap insatll terraform --classic<br># terraform init<br># terraform plan<br># terraform apply --auto-approve<br><br><br><br><br><br>*---------------kubectl<br># sudo snap install kubectl --classic<br># aws eks --region us-east-1 update-kubeconfig --name devopsshack-cluster<br># kubectl get nodes<br>Now our cluster is ready</pre><p>In our manifest YAML file (ds.yml), we define resource limits for the MySQL deployment, specifying the CPU and RAM allocations. We also set resource requests to ensure that the necessary resources are consistently available. Similarly, resource requests are configured for the application deployment to guarantee optimal performance and resource utilization.</p><pre><br># cd Pod-Auto-Scaling<br># ls<br># Cluster Dockerfile Jenkinsfile bankapp-hpa.yml ds.yml mvn mysql-hpa.yml pom.xml src<br># kubectl apply -f ds.yml<br># kubectl get all <br>#  application is running now<br><br><br><br><br>---<br># MySQL Deployment with Resource Requests<br>apiVersion: apps/v1<br>kind: Deployment    <br>metadata:<br>  name: mysql<br>spec:<br>  selector:<br>    matchLabels:<br>      app: mysql<br>  strategy:<br>    type: Recreate<br>  template:<br>    metadata:<br>      labels:<br>        app: mysql<br>    spec:<br>      containers:<br>      - image: mysql:8<br>        name: mysql<br>        env:<br>        - name: MYSQL_ROOT_PASSWORD<br>          value: &quot;Test@123&quot;<br>        - name: MYSQL_DATABASE<br>          value: &quot;bankappdb&quot;<br>        ports:<br>        - containerPort: 3306<br>          name: mysql<br>        resources:<br>          requests:<br>            memory: &quot;1Gi&quot;<br>            cpu: &quot;500m&quot;<br>          limits:<br>            memory: &quot;2Gi&quot;<br>            cpu: &quot;1000m&quot;<br>---<br># MySQL Service<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: mysql-service<br>spec:<br>  ports:<br>  - port: 3306<br>  selector:<br>    app: mysql<br>---<br># Java Application Deployment with Resource Requests<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: bankapp<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: bankapp<br>  template:<br>    metadata:<br>      labels:<br>        app: bankapp<br>    spec:<br>      containers:<br>      - name: bankapp<br>        image: adijaiswal/bankapp:latest<br>        ports:<br>        - containerPort: 8080<br>        env:<br>        - name: SPRING_DATASOURCE_URL<br>          value: jdbc:mysql://mysql-service:3306/bankappdb?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true<br>        - name: SPRING_DATASOURCE_USERNAME<br>          value: root<br>        - name: SPRING_DATASOURCE_PASSWORD<br>          value: Test@123<br>        resources:<br>          requests:<br>            memory: &quot;500Mi&quot;<br>            cpu: &quot;500m&quot;<br>          limits:<br>            memory: &quot;1000Mi&quot;<br>            cpu: &quot;1000m&quot;<br>---<br># Bank Application Service<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: bankapp-service<br>spec:<br>  type: LoadBalancer<br>  ports:<br>  - port: 80<br>    targetPort: 8080<br>  selector:<br>    app: bankapp</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TJkaTX6vp3eGCVzcbMyCaA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZsZawBpnlXsJ9_HdSGPoag.jpeg" /></figure><h4>To handle increased load on a pod and enable horizontal scaling, we can set up the following flow:</h4><p><strong>Install Metrics Server</strong>:<br>The Metrics Server collects resource metrics (like CPU and memory usage) from the cluster nodes and pods. It is required for Kubernetes to scale pods based on resource usage.</p><pre>* - - - Install  metrics-server<br># kubectl apply -f <br>https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml<br><br><br><br><br># kubectl apply -f mysql-hpa.yml<br># kubectl apply -f bankapp-hpa.yml<br># kubectl get hpa<br><br><br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e2_ai59NiIEwfByrhwqKyQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zq7EnElrm79oVgqSFzrATw.jpeg" /></figure><pre><br>Define a Horizontal Pod Autoscaler (HPA):<br>Once the Metrics Server is running, create a Horizontal Pod Autoscaler to automatically scale the pods based on CPU or memory usage.<br><br><br><br><br>---<br># Horizontal Pod Autoscaler for Java Application<br>apiVersion: autoscaling/v2<br>kind: HorizontalPodAutoscaler<br>metadata:<br>  name: bankapp-hpa<br>spec:<br>  scaleTargetRef:<br>    apiVersion: apps/v1<br>    kind: Deployment<br>    name: bankapp<br>  minReplicas: 1<br>  maxReplicas: 10<br>  metrics:<br>  - type: Resource<br>    resource:<br>      name: cpu<br>      target:<br>        type: Utilization<br>        averageUtilization: 50  # Scale when CPU utilization goes above 50%</pre><h4>Explanation of Horizontal Pod Autoscaler (HPA):</h4><p>This YAML file defines a <strong>Horizontal Pod Autoscaler (HPA)</strong> for a Java application deployment named bankapp. The HPA automatically adjusts the number of pods (replicas) in response to changes in the CPU utilization of the application. Here&#39;s a breakdown of its key components:</p><ul><li><strong>apiVersion</strong> and <strong>kind</strong> specify that this is an HPA configuration using version autoscaling/v2.</li><li><strong>metadata</strong> provides a name for the HPA resource, bankapp-hpa.</li><li><strong>scaleTargetRef</strong> tells the HPA which resource to scale. In this case:</li><li><strong>apiVersion</strong> is apps/v1, meaning it refers to a deployment resource.</li><li><strong>kind</strong> is Deployment, indicating that it’s scaling a Kubernetes deployment.</li><li><strong>name</strong> is bankapp, specifying the exact deployment that will be monitored and scaled.</li><li><strong>minReplicas</strong> is set to 1, meaning the deployment will always have at least one pod running.</li><li><strong>maxReplicas</strong> is set to 10, so the number of pods can scale up to a maximum of ten if necessary.</li><li><strong>metrics</strong> specifies the criteria for scaling. In this case, it’s based on <strong>CPU utilization</strong>:</li><li>If the CPU utilization exceeds <strong>50%</strong> (set by averageUtilization), the HPA will automatically increase the number of pods to handle the increased load.</li></ul><p>The scaleTargetRef ensures that the bankapp deployment is dynamically scaled based on its CPU usage, allowing the system to maintain responsiveness by adjusting the number of pods as needed.</p><pre><br><br>---<br>apiVersion: autoscaling/v2<br>kind: HorizontalPodAutoscaler<br>metadata:<br>  name: mysql-hpa<br>spec:<br>  scaleTargetRef:<br>    apiVersion: apps/v1<br>    kind: Deployment<br>    name: mysql<br>  minReplicas: 1<br>  maxReplicas: 5<br>  metrics:<br>  - type: Resource<br>    resource:<br>      name: cpu<br>      target:<br>        type: Utilization<br>        averageUtilization: 60  # Scale when CPU utilization goes above 60%</pre><h4>Explanation of Horizontal Pod Autoscaler(HPD) for MySQL:</h4><p>This YAML configuration defines a <strong>Horizontal Pod Autoscaler(HPA)</strong> for a MySQL application deployment named mysql. The HPA is designed to automatically adjust the number of pod replicas based on CPU utilization metrics to ensure optimal performance and resource usage. Here are its key components:</p><ul><li><strong>apiVersion</strong> and <strong>kind</strong> indicate that this is an HPA configuration using version autoscaling/v2.</li><li><strong>metadata</strong> includes the name of the HPA resource, mysql-hpa.</li><li><strong>scaleTargetRef</strong> specifies the deployment to be scaled:</li><li><strong>apiVersion</strong> is apps/v1, referring to the version for deployment resources.</li><li><strong>kind</strong> is Deployment, indicating the type of resource.</li><li><strong>name</strong> is mysql, pointing to the specific deployment that the HPA will monitor and scale.</li><li><strong>minReplicas</strong> is set to 1, ensuring that at least one pod is always running.</li><li><strong>maxReplicas</strong> is set to 5, allowing the number of pods to scale up to five if necessary.</li><li><strong>metrics</strong> defines the scaling criteria based on <strong>CPU utilization</strong>:</li><li>The HPA will trigger scaling actions if the CPU utilization exceeds <strong>60%</strong> (as indicated by averageUtilization), automatically increasing the number of replicas to handle increased load.</li></ul><p>Overall, this configuration allows the MySQL deployment to dynamically adjust its pod count based on CPU demand, helping maintain performance while efficiently utilizing resources.</p><h4>Vertical-Pod-Autoscaler:</h4><pre><br>*------------autoscaler<br>#  git clone https://github.com/kubernetes/autoscaler.git<br><br># ./hack/vpa-up.sh</pre><pre><br># cd Pod-Auto-Scaling<br># ls<br># Cluster Dockerfile Jenkinsfile bankapp-hpa.yml ds.yml bankapp-vpa.yml mvn mysql-hpa.yml pom.xml src<br># kubectl apply -f bankapp-vpa.yml<br><br><br>apiVersion: autoscaling.k8s.io/v1<br>kind: VerticalPodAutoscaler<br>metadata:<br>  name: bankapp-vpa<br>spec:<br>  targetRef:<br>    apiVersion: &quot;apps/v1&quot;<br>    kind: Deployment<br>    name: bankapp<br>  updatePolicy:<br>    updateMode: &quot;Auto&quot;  # This mode automatically updates the resources<br><br><br><br># kubectl describe vpa bankapp-vpa<br><br></pre><p><strong>Explanation of Horizontal Pod Autoscaler (HPA) for MySQL:</strong></p><p>The Horizontal Pod Autoscaler (HPA) for MySQL is a Kubernetes feature that automatically adjusts the number of MySQL pod replicas based on CPU utilization or other specified metrics, ensuring that the database can scale efficiently in response to varying workloads. By setting a minimum of 1 pod and a maximum of 5 pods, the HPA allows the MySQL deployment to dynamically increase or decrease its capacity, maintaining optimal performance and resource usage when CPU utilization exceeds a defined threshold, such as 60%. This capability enhances the reliability and responsiveness of MySQL in a cloud-native environment.</p><h4>We need to test this now; using a load generator will help evaluate the system’s performance under heavy load.</h4><pre><br>*------load-generator<br><br># kubectl get all<br><br># kubectl exec -it  load-generator -- /bin/sh<br># while true; do wget -q -O- http://bankapp-service/api/transactions; done<br><br>kubectl get vpa</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1jOzixy2T_EeyB_cV729VA.jpeg" /></figure><p>Overall, the project illustrates the significant benefits of using Kubernetes autoscaling features for modern cloud-native applications, reinforcing the need for dynamic resource management in today’s diverse and fluctuating workloads.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=687f3adf132a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Seamless Blue-Green Deployment of Node.js App on Kubernetes]]></title>
            <link>https://medium.com/@masseys.s123/seamless-blue-green-deployment-of-node-js-app-on-kubernetes-856f8a79d53e?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/856f8a79d53e</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Wed, 16 Oct 2024 07:44:17 GMT</pubDate>
            <atom:updated>2024-10-23T12:01:31.871Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i1AkVXC8bts5eNzDFc3soQ.jpeg" /></figure><p>In this project, I gained experience with blue-green deployment in Kubernetes. This approach allows for zero-downtime updates by maintaining two identical environments: ‘blue’ hosts the current version, while ‘green’ runs the updated version. Once the new version is verified, traffic is smoothly switched over to the ‘green’ environment.</p><p><strong>Infrastructure Setup:</strong></p><ul><li><strong>Jenkins</strong> is provisioned on an <strong>EC2 t2.medium</strong> instance.</li><li><strong>SonarQube</strong> operates on an <strong>EC2 t2.medium</strong> instance.</li></ul><p>In order to set up <strong>Jenkins</strong>, we need Java as a prerequisite.</p><pre><br><br># sudo apt update<br># java<br># sudo apt install openjdk-17-jre-headless -y<br><br>***----Install Jenkins<br><br>sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \<br>  https://pkg.jenkins.io/debian/jenkins.io-2023.key<br>echo &quot;deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]&quot; \<br>  https://pkg.jenkins.io/debian binary/ | sudo tee \<br>  /etc/apt/sources.list.d/jenkins.list &gt; /dev/null<br>sudo apt-get update<br>sudo apt-get install jenkins -y <br><br>***-----Start Jenkins<br># sudo systemctl enable jenkins<br><br>Check if Jenkins is installed<br>&lt;ip:8080&gt;<br># sudo cat /var/lib/jenkins/secrets/initialAdminPassword<br><br><br>***-----------Trivy Install:<br>sudo apt-get install wget apt-transport-https gnupg lsb-release<br>wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -<br>echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list<br>sudo apt-get update<br>sudo apt-get install trivy -y<br><br><br>----------Install docker:<br><br>***----------Recommended way to Install Docker---------<br><br><br># Add Docker&#39;s official GPG key:<br>sudo apt-get update<br>sudo apt-get install ca-certificates curl<br>sudo install -m 0755 -d /etc/apt/keyrings<br>sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc<br>sudo chmod a+r /etc/apt/keyrings/docker.asc<br><br># Add the repository to Apt sources:<br>echo \<br>  &quot;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \<br>  $(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;) stable&quot; | \<br>  sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null<br>sudo apt-get update<br><br>***-------To install the latest version,run------<br><br>sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin<br><br>***---Add permissions---<br><br>sudo usermod -aG docker jenkins<br></pre><h4>Provisioned and configured <strong>Amazon EKS</strong> on a virtual machine (VM) hosted by Jenkins.</h4><pre><br><br>*--------------- Install AWS CLI<br>curl &quot;https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip&quot; -o &quot;awscliv2.zip&quot;<br>sudo apt install unzip<br>unzip awscliv2.zip<br>sudo ./aws/install</pre><p>I used <strong>Terraform</strong> to provision and manage the Kubernetes cluster, adhering to infrastructure-as-code practices for streamlined and scalable deployments. The <strong>source code</strong> can be found here: <a href="https://github.com/shazia-massey/3-Tier-NodeJS-MySql-Docker.git">https://github.com/shazia-massey/3-Tier-NodeJS-MySql-Docker.git</a>----&gt; Terraform-files-for-Cluster</p><pre><br># On Jenkins VM<br><br><br># git clone https://github.com/shazia-massey/3-Tier-NodeJS-MySql-Docker.git<br># cd 3-Tier<br># ls<br># main.tf  output.tf  variables.tf<br><br># terraform <br># sudo snap insatll terraform --classic<br># terraform init<br># terraform plan<br># terraform apply --auto-approve<br><br># kubectl<br># sudo snap install kubectl --classic<br># aws eks --region us-east-1 update-kubeconfig --name devopsshack-cluster<br># kubectl get nodes<br>Now our cluster is ready</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bsOi_iF_79g5jNiv9cuDPQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ET3mPx8RgjTSeSeTlgFEQ.jpeg" /></figure><h4>Created Role-Based Access Control (RBAC) for the Kubernetes cluster.</h4><h4>Service Account:</h4><pre><br># On Jenkins VM<br><br><br># Kubectl create namespace webapps<br><br>Create Service Account:<br># vi svc.yml<br><br>apiVersion: v1<br>kind: ServiceAccount<br>metadata:<br>  name: jenkins<br>  namespace: webapps<br><br># kubectl create namespace webapps<br># kubectl apply -f svc.yml</pre><h4>Create Role:</h4><pre><br>Create Role:<br># vi role.yml<br><br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: Role<br>metadata:<br>  name: app-role<br>  namespace: webapps<br>rules:<br>  - apiGroups:<br>        - &quot;&quot;<br>        - apps<br>        - autoscaling<br>        - batch<br>        - extensions<br>        - policy<br>        - rbac.authorization.k8s.io<br>    resources:<br>      - pods<br>      - secrets<br>      - componentstatuses<br>      - configmaps<br>      - daemonsets<br>      - deployments<br>      - events<br>      - endpoints<br>      - horizontalpodautoscalers<br>      - ingress<br>      - jobs<br>      - limitranges<br>      - namespaces<br>      - nodes<br>      - pods<br>      - persistentvolumes<br>      - persistentvolumeclaims<br>      - resourcequotas<br>      - replicasets<br>      - replicationcontrollers<br>      - serviceaccounts<br>      - services<br>    verbs: [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;, &quot;create&quot;, &quot;update&quot;, &quot;patch&quot;, &quot;delete&quot;]<br><br>  # kubectl apply -f role.yml</pre><h4>Bind the role to service account:</h4><pre><br>Bind the Role to Service Account:<br># vi rb.yml<br><br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: RoleBinding<br>metadata:<br>  name: app-rolebinding<br>  namespace: webapps <br>roleRef:<br>  apiGroup: rbac.authorization.k8s.io<br>  kind: Role<br>  name: app-role <br>subjects:<br>- namespace: webapps <br>  kind: ServiceAccount<br>  name: jenkins <br><br># kubectl apply -f rb.yml</pre><h3>Create a Cluster Role for PV access:</h3><pre># create a cluster role for PV(Persistence Volume) access<br><br># vi pv.yaml<br><br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRole<br>metadata:<br>  name: persistent-volume-access<br>rules:<br>  - apiGroups: [&quot;&quot;]<br>    resources: [&quot;persistentvolumes&quot;]<br>    verbs: [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;, &quot;create&quot;, &quot;update&quot;, &quot;patch&quot;, &quot;delete&quot;]<br><br><br><br># kubectl apply -f pv.yml -n webapps<br></pre><h3>Bind the Cluster Role to Jenkins Service Account:</h3><pre><br># vi clusterrolebinding.yml<br><br><br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRoleBinding<br>metadata:<br>  name: jenkins-persistent-volume-access<br>subjects:<br>  - kind: ServiceAccount<br>    name: jenkins<br>    namespace: webapps<br>roleRef:<br>  kind: ClusterRole<br>  name: persistent-volume-access<br>  apiGroup: rbac.authorization.k8s.io<br><br>kubectl apply -f clusterrolebinding.yml</pre><h4>Service-Account-Token(secret):</h4><pre><br><br><br># vi svc-secret.yml<br><br>apiVersion: v1<br>kind: Secret<br>type: kubernetes.io/service-account-token<br>metadata:<br>  name: mysecretname<br>  annotations:<br>    kubernetes.io/service-account.name: jenkins<br><br># kubectl apply -f svc-secret.yml -n webapps<br># kubectl describe secret mysecretname -n webapps<br></pre><p>Install <strong>SonarQube VM</strong>:</p><pre><br><br># docker<br>-----Recommended way to Install Docker---------<br><br><br># Add Docker&#39;s official GPG key:<br>sudo apt-get update<br>sudo apt-get install ca-certificates curl<br>sudo install -m 0755 -d /etc/apt/keyrings<br>sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc<br>sudo chmod a+r /etc/apt/keyrings/docker.asc<br><br># Add the repository to Apt sources:<br>echo \<br>  &quot;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \<br>  $(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;) stable&quot; | \<br>  sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null<br>sudo apt-get update<br><br><br>---To install the latest version,run------<br># sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin<br><br><br>------Add permissions-----<br><br>sudo usermod -aG docker $USER<br>newgrp docker<br>docker version<br><br><br># docker run -d --name sonarqube -p 9000:9000 sonarqube:lts-community<br><br>Check if SonarQube is installed<br># &lt;ip:9000&gt;<br>Log in to SonarQube with the admin username and password, then change it later.<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2h5hF9lP6yn6eUATLhfymA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CzgimrUhD3tQSIzvLyi1eQ.jpeg" /></figure><p><strong>Jenkins Plugins Installation:</strong></p><p>Managed Jenkins by configuring essential plugins, including SonarQube Scanner, Pipeline Stage View, Docker, Docker Pipeline, Kubernetes, and Kubernetes CLI.</p><p><strong>Jenkins Configuration:</strong></p><p><strong>Manage Jenkins — -&gt; Tools:</strong></p><ul><li><strong>SonarQube Scanner installations — -</strong>&gt; Name — — <strong>sonar-scanner</strong> — -&gt; Install automatically ( Install from Maven Central, version: SonarQube Scanner 5.0.1.3006)</li></ul><p><strong>Manage Jenkins — -&gt; System:</strong></p><ul><li>SonarQube servers →SonarQube installations → Name — <strong>sonar </strong>→ Server URL (http://localhost:9000) → Server authentication token (SonarQube server — -Administration — — Security — — User — -generate token — -Name: sonar-token)</li></ul><p><strong>Manage Jenkins — -&gt; Credentials:</strong></p><ul><li><strong>Git</strong> — → Repository URL — →branch — → credential — -Git username — — instead of password we provide token — -&gt; ID — → <strong>git-cred</strong></li><li><strong>Docker — -</strong>&gt; DockerHub username — → personal access token — → ID →<strong>docker.cred</strong></li><li><strong>SonarQube — -</strong>&gt; Kind — -&gt; secret →ID →<strong>sonar-token</strong></li><li><strong>Kubernetes </strong>→ Kind → secret →ID →<strong>k8-token</strong></li></ul><h4>CICD Pipeline:</h4><pre><br><br><br><br>pipeline {<br>    agent any<br>    <br>    parameters {<br>        choice(name: &#39;DEPLOY_ENV&#39;, choices: [&#39;blue&#39;, &#39;green&#39;], description: &#39;Choose which environment to deploy: Blue or Green&#39;)<br>        choice(name: &#39;DOCKER_TAG&#39;, choices: [&#39;blue&#39;, &#39;green&#39;], description: &#39;Choose the Docker image tag for the deployment&#39;)<br>        booleanParam(name: &#39;SWITCH_TRAFFIC&#39;, defaultValue: false, description: &#39;Switch traffic between Blue and Green&#39;)<br>    }<br>    <br>    <br>    environment {<br>        IMAGE_NAME = &quot;masseys/bankapp&quot;<br>        TAG = &quot;${params.DOCKER_TAG}&quot;  // The image tag now comes from the parameter<br>        KUBE_NAMESPACE = &#39;webapps&#39;<br>        SCANNER_HOME= tool &#39;sonar-scanner&#39;<br>    }<br><br><br>    stages {<br>        stage(&#39;Git checkout&#39;) {<br>            steps {<br>               git branch: &#39;main&#39;, credentialsId: &#39;git-cred&#39;, url: &#39;https://github.com/shazia-massey/3-Tier-NodeJS-MySql-Docker.git&#39; <br>            }<br>        }<br>        <br>        stage(&#39;SonarQube Analysis&#39;) {<br>            steps {<br>                withSonarQubeEnv(&#39;sonar&#39;) {<br>                     sh &quot;$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectKey=nodejsmysql -Dsonar.projectName=nodejsmysql&quot;<br>                }<br>            }<br>        }<br>        <br>        stage(&#39;Trivy FS Scan&#39;) {<br>            steps {<br>                sh &quot;trivy fs --format table -o fs.html .&quot;<br>            }<br>        }<br>        <br>        stage(&#39;Docker build&#39;) {<br>            steps {<br>                script {<br>                  withDockerRegistry(credentialsId: &#39;docker-cred&#39;) {<br>                     sh &quot;docker build -t ${IMAGE_NAME}:${TAG} .&quot; <br>                   }  <br>                }<br>            }<br>        }<br>        <br>        stage(&#39;Trivy Image Scan&#39;) {<br>            steps {<br>               sh &quot;trivy image --format table -o image.html ${IMAGE_NAME}:${TAG}&quot;<br>            }<br>        }<br>        <br>        stage(&#39;Docker Push Image&#39;) {<br>            steps {<br>                script {<br>                  withDockerRegistry(credentialsId: &#39;docker-cred&#39;) {<br>                     sh &quot;docker push ${IMAGE_NAME}:${TAG}&quot;<br>                   }  <br>                }<br>            }<br>        }<br>        <br>           stage(&#39;Deploy SVC-APP&#39;) {<br>            steps {<br>                script {<br>                    withKubeConfig(caCertificate: &#39;&#39;, clusterName: &#39;devopsshack-cluster&#39;, contextName: &#39;&#39;, credentialsId: &#39;k8-token&#39;, namespace: &#39;webapps&#39;, restrictKubeConfigAccess: false, serverUrl: &#39;https://865261835B39014782D93B5B3FD0C0DD.gr7.us-east-1.eks.amazonaws.com&#39;) {<br>                sh &quot;&quot;&quot; if ! kubectl get svc app -n ${KUBE_NAMESPACE}; then<br>                          kubectl apply -f app-service.yml -n ${KUBE_NAMESPACE}<br>                        fi<br>                        &quot;&quot;&quot;<br>                   }<br>                }<br>            }<br>        }<br>            <br>            stage(&#39;Deploy to Kubernetes&#39;) {<br>            steps {<br>                script {<br>                    def deploymentFile = &quot;&quot;<br>                    if (params.DEPLOY_ENV == &#39;blue&#39;) {<br>                        deploymentFile = &#39;app-deployment-blue.yml&#39;<br>                    } else {<br>                        deploymentFile = &#39;app-deployment-green.yml&#39;<br>                    }<br><br>                    withKubeConfig(caCertificate: &#39;&#39;, clusterName: &#39;devopsshack-cluster&#39;, contextName: &#39;&#39;, credentialsId: &#39;k8-token&#39;, namespace: &#39;webapps&#39;, restrictKubeConfigAccess: false, serverUrl: &#39;https://865261835B39014782D93B5B3FD0C0DD.gr7.us-east-1.eks.amazonaws.com&#39;) {<br>      sh &quot;kubectl apply -f pv-pvc.yml -n ${KUBE_NAMESPACE}&quot;<br>      sh &quot;kubectl apply -f mysql-ds.yml -n ${KUBE_NAMESPACE}&quot;<br>                        sh &quot;kubectl apply -f ${deploymentFile} -n ${KUBE_NAMESPACE}&quot;<br>      <br>                    }<br>                }<br>            }<br>        }<br>        <br>stage(&#39;Switch Traffic&#39;) {<br>    when {<br>        expression { return params.SWITCH_TRAFFIC }<br>    }<br>    steps {<br>        script {<br>            def newEnv = params.DEPLOY_ENV<br><br>            // Always switch traffic based on DEPLOY_ENV<br>            withKubeConfig(caCertificate: &#39;&#39;, clusterName: &#39;devopsshack-cluster&#39;, contextName: &#39;&#39;, credentialsId: &#39;k8-token&#39;, namespace: &#39;webapps&#39;, restrictKubeConfigAccess: false, serverUrl: &#39;https://865261835B39014782D93B5B3FD0C0DD.gr7.us-east-1.eks.amazonaws.com&#39;) {<br>                sh &#39;&#39;&#39;<br>                    kubectl patch service app -p &quot;{\\&quot;spec\\&quot;: {\\&quot;selector\\&quot;: {\\&quot;app\\&quot;: \\&quot;app\\&quot;, \\&quot;version\\&quot;: \\&quot;&#39;&#39;&#39; + newEnv + &#39;&#39;&#39;\\&quot;}}}&quot; -n ${KUBE_NAMESPACE}<br>                &#39;&#39;&#39;<br>            }<br>            echo &quot;Traffic has been switched to the ${newEnv} environment.&quot;<br>        }<br>    }<br>}<br><br><br><br><br>        <br>        stage(&#39;Verify Deployment&#39;) {<br>            steps {<br>                script {<br>                    def verifyEnv = params.DEPLOY_ENV<br>                    withKubeConfig(caCertificate: &#39;&#39;, clusterName: &#39;devopsshack-cluster&#39;, contextName: &#39;&#39;, credentialsId: &#39;k8-token&#39;, namespace: &#39;webapps&#39;, restrictKubeConfigAccess: false, serverUrl: &#39;https://865261835B39014782D93B5B3FD0C0DD.gr7.us-east-1.eks.amazonaws.com&#39;) {<br>                        sh &quot;&quot;&quot;<br>                        kubectl get pods -l version=${verifyEnv} -n ${KUBE_NAMESPACE}<br>                        kubectl get svc app -n ${KUBE_NAMESPACE}<br>                        &quot;&quot;&quot;<br>                    }<br>                }<br>            }<br>        }<br>        <br>            <br>    }<br>}<br><br></pre><h3>Pipeline Overview</h3><p>This Jenkins pipeline is designed for deploying a Node.js application using Docker and Kubernetes, employing a blue-green deployment strategy. The pipeline incorporates various tools for code quality analysis, security scanning, and streamlined application delivery.</p><ol><li><strong>Parameters Section:</strong></li></ol><ul><li><strong>DEPLOY_ENV</strong>: Allows the user to choose between ‘blue’ or ‘green’ environments for deployment.</li><li><strong>DOCKER_TAG</strong>: Lets the user select the Docker image tag for the deployment.</li><li><strong>SWITCH_TRAFFIC</strong>: A boolean parameter that indicates whether to switch traffic between the blue and green environments.</li></ul><p><strong>2. Environment Variables:</strong></p><ul><li>IMAGE_NAME: Specifies the Docker image name.</li><li>TAG: Derived from the DOCKER_TAG parameter.</li><li>KUBE_NAMESPACE: Defines the Kubernetes namespace where the application will be deployed.</li><li>SCANNER_HOME: Points to the SonarQube scanner tool for code quality checks.</li></ul><h4>Stages:</h4><ol><li><strong>Git Checkout</strong>:</li></ol><p>***Clones the main branch of the specified Git repository using the provided credentials.</p><p><strong>2. SonarQube Analysis</strong>:</p><p>***Runs a static code analysis using SonarQube to ensure code quality and adherence to defined standards.</p><p><strong>3. Trivy FS Scan</strong>:</p><p>***Conducts a security scan of the file system using Trivy to identify vulnerabilities.</p><p><strong>4. Docker Build</strong>:</p><p>***Builds the Docker image for the application, tagging it with the specified image name and tag. This is done securely using Docker registry credentials.</p><p><strong>5. Trivy Image Scan</strong>:</p><p>***Scans the newly built Docker image for vulnerabilities to ensure it meets security compliance.</p><p><strong>6. Docker Push Image</strong>:</p><ul><li>**Pushes the tagged Docker image to DockerHub, making it available for deployment.</li></ul><p><strong>7. Deploy SVC-APP</strong>:</p><p>***Checks if the service (app) exists in the specified namespace. If not, it applies the service configuration from app-service.yml.</p><p><strong>8. Deploy to Kubernetes</strong>:</p><p>***Based on the selected deployment environment (blue or green), it applies the corresponding deployment configuration file (app-deployment-blue.yml or app-deployment-green.yml) along with persistent volume claims and MySQL deployment configurations.</p><p><strong>9. Switch Traffic</strong>:</p><p>***If the SWITCH_TRAFFIC parameter is true, this stage patches the Kubernetes service to switch traffic to the newly deployed environment (blue or green).</p><p><strong>10. Verify Deployment</strong>:</p><p>***Retrieves and displays the status of the pods and services in the specified Kubernetes namespace to verify successful deployment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kGXaGZbPRomwxsB2Evgfbw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q1Q06Q8dQEeTJfkSF7SrCw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TEf825_gQSqJ4xo8dOjVpg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BVHFPEiGVaPqYsgEPYyE2Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hokoIHWoou-M71_EFvpxVw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*duPSoGr5vgUFi6EWYdTWIQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x87BsPypo4jMCIDs3GnyLw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6eQAr8YDSLoB4i1hbggCGA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*If7IvYZ45tyzU4vBbsegrA.jpeg" /></figure><p>This project greatly enhanced my learning by deepening my expertise in Kubernetes, particularly in implementing blue-green deployment strategies for zero-downtime updates. I gained valuable experience in leveraging parameterization for flexible pipelines, integrating code quality checks with SonarQube, and strengthening security through Trivy scanning. Additionally, I automated Kubernetes deployments and ensured efficient traffic transitions between environments. Overall, this project reinforced my understanding of industry best practices for continuous delivery and solidified my skills in deploying reliable applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=856f8a79d53e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“From Code to Deployment: Managing a Multi-Tier-Java Project with Jenkins”]]></title>
            <link>https://medium.com/@masseys.s123/from-code-to-deployment-managing-a-multi-tier-java-project-with-jenkins-b559b706b3fb?source=rss-69c759034d1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b559b706b3fb</guid>
            <dc:creator><![CDATA[Shazia Massey ]]></dc:creator>
            <pubDate>Tue, 08 Oct 2024 13:03:27 GMT</pubDate>
            <atom:updated>2024-10-08T13:03:27.855Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*VVkE_8m9SApdD-ta4nTiHg.png" /></figure><p>The CI/CD pipeline for this project is meticulously crafted to enable seamless integration, delivery, and deployment of a multi-tier Java application, leveraging a comprehensive suite of DevOps tools. This fully automated process, powered by Jenkins, orchestrates the entire development, testing, and deployment lifecycle, ensuring efficiency and consistency throughout the pipeline.</p><p>In this project, I concentrated on building the Docker image by defining the version in a variable format, such as tag=$env.BuildNumber, or using manual tags like v1, v2, etc. This approach is essential for several reasons: it ensures <strong>dynamic versioning</strong>, providing each build with a unique identifier; it facilitates version control, maintaining a clear history of application updates; it enhances <strong>consistency</strong>, offering a straightforward method for versioning images that aids in identification during deployments; it promotes <strong>reproducibility</strong>, enabling the <strong>recreation </strong>of specific environments through known image tags, ensuring uniformity across development, testing, and production; it improves <strong>collaboration</strong> by enhancing communication among team members regarding the application version being used or tested; it supports <strong>automated</strong> <strong>deployment </strong>based on specific criteria; and it provides <strong>traceability</strong>, allowing teams to easily correlate deployed application versions with their corresponding source code changes, simplifying the investigation of issues that arise post-deployment.</p><p><strong>Infrastructure Setup:</strong></p><ul><li><strong>Jenkins</strong> is provisioned on an <strong>EC2 t2.medium</strong> instance.</li><li><strong>SonarQube</strong> operates on an <strong>EC2 t2.medium</strong> instance.</li><li><strong>Nexus</strong> is deployed on an <strong>EC2 t2.medium</strong> instance.</li></ul><p>In order to set up <strong>Jenkins</strong>, we need Java as a prerequisite.</p><pre><br># sudo apt update<br># java<br># sudo apt install openjdk-17-jre-headless -y<br><br>***----Install Jenkins<br><br>sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \<br>  https://pkg.jenkins.io/debian/jenkins.io-2023.key<br>echo &quot;deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]&quot; \<br>  https://pkg.jenkins.io/debian binary/ | sudo tee \<br>  /etc/apt/sources.list.d/jenkins.list &gt; /dev/null<br>sudo apt-get update<br>sudo apt-get install jenkins -y <br><br>***-----Start Jenkins<br># sudo systemctl enable jenkins<br><br>Check if Jenkins is installed<br>&lt;ip:8080&gt;<br># sudo cat /var/lib/jenkins/secrets/initialAdminPassword<br><br><br>***-----------Trivy Install:<br>sudo apt-get install wget apt-transport-https gnupg lsb-release<br>wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -<br>echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list<br>sudo apt-get update<br>sudo apt-get install trivy -y<br><br><br>----------Install docker:<br><br>***----------Recommended way to Install Docker---------<br><br><br># Add Docker&#39;s official GPG key:<br>sudo apt-get update<br>sudo apt-get install ca-certificates curl<br>sudo install -m 0755 -d /etc/apt/keyrings<br>sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc<br>sudo chmod a+r /etc/apt/keyrings/docker.asc<br><br># Add the repository to Apt sources:<br>echo \<br>  &quot;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \<br>  $(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;) stable&quot; | \<br>  sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null<br>sudo apt-get update<br><br>***-------To install the latest version,run------<br><br>sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin<br><br>***---Add permissions---<br><br>sudo usermod -aG docker jenkins<br>newgrp docker<br>docker version<br></pre><h4>Install Nexus:</h4><pre># docker<br>***----Recommended way to Install Docker---------<br><br><br># Add Docker&#39;s official GPG key:<br>sudo apt-get update<br>sudo apt-get install ca-certificates curl<br>sudo install -m 0755 -d /etc/apt/keyrings<br>sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc<br>sudo chmod a+r /etc/apt/keyrings/docker.asc<br><br># Add the repository to Apt sources:<br>echo \<br>  &quot;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \<br>  $(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;) stable&quot; | \<br>  sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null<br>sudo apt-get update<br><br>***-----To install the latest version,run------<br><br># sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin<br><br>***-----Add permissions---<br><br>sudo usermod -aG docker $USER<br>newgrp docker<br>docker version<br><br><br>docker run -d --name nexus -p 8081:8081 sonatype/nexus3<br><br>sign In<br><br>docker ps<br>docker exec -it &lt;docker id&gt; /bin/bash<br>ls<br>cd sonatype-work/<br>cd nexus3/<br>cat admin.password<br><br><br><br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pOHYvtmxjAnypWBNFhGEug.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u5hNkKwR6PFG3Ou7EnD7dw.jpeg" /></figure><p>Install <strong>SonarQube VM</strong>:</p><pre><br># docker<br>-----Recommended way to Install Docker---------<br><br><br># Add Docker&#39;s official GPG key:<br>sudo apt-get update<br>sudo apt-get install ca-certificates curl<br>sudo install -m 0755 -d /etc/apt/keyrings<br>sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc<br>sudo chmod a+r /etc/apt/keyrings/docker.asc<br><br># Add the repository to Apt sources:<br>echo \<br>  &quot;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \<br>  $(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;) stable&quot; | \<br>  sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null<br>sudo apt-get update<br><br><br>---To install the latest version,run------<br># sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin<br><br><br>------Add permissions-----<br><br>sudo usermod -aG docker $USER<br>newgrp docker<br>docker version<br><br><br># docker run -d --name sonarqube -p 9000:9000 sonarqube:lts-community<br><br>Check if SonarQube is installed<br># &lt;ip:9000&gt;<br>Log in to SonarQube with the admin username and password, then change it later.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uufi4bZ33b4kPoSuqY500A.jpeg" /></figure><p><strong>Jenkins Plugins Installation:</strong></p><p>Manage Jenkins — -&gt; Plugins — -&gt;SonarQube Scanner, docker, dockerpipeline, kubernetes, kubernetes cli, config file provider, maven — maven pipeline Integration, maven Integration</p><p><strong>Jenkins Configuration:</strong></p><p><strong>Manage Jenkins — -&gt; Tools:</strong></p><ul><li><strong>SonarQube Scanner installations — -</strong>&gt; Name — — <strong>sonar-scanner</strong> — -&gt; Install automatically ( Install from Maven Central, version: SonarQube Scanner 5.0.1.3006)</li><li><strong>Maven installations — -</strong>&gt; Name — <strong>maven3 — -</strong>&gt;Install automatically (Install from Apache, version:3.6.3)</li></ul><p><strong>Manage Jenkins — -&gt; System:</strong></p><ul><li>SonarQube servers — &gt;SonarQube installations — &gt; Name — <strong>sonar — </strong>&gt; Server URL (http://localhost:9000) — &gt; Server authentication token (SonarQube server — -Administration — — Security — — User — -generate token — -Name: sonar-token)</li></ul><p><strong>Manage Jenkins — -&gt; Credentials:</strong></p><ul><li><strong>Git</strong> —- &gt; Repository URL —- &gt;branch —- &gt; credential — -Git username —- instead of password we provide token — -&gt; ID <strong>git-cred</strong></li><li><strong>Docker — -</strong>&gt; DockerHub username —- &gt; personal access token —- &gt; ID — &gt;<strong>docker.cred</strong></li><li><strong>SonarQube — -</strong>&gt; Kind — -&gt; secret — &gt;ID — &gt;<strong>sonar-token</strong></li><li><strong>Kubernetes — </strong>&gt; Kind — &gt; secret — &gt;ID — &gt;<strong>k8-token</strong></li></ul><h3>Provision and configure Amazon EKS on the Jenkins-hosted virtual machine (VM):</h3><p>Install AWS CLI:</p><pre><br>*--------------- Install AWS CLI<br>curl &quot;https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip&quot; -o &quot;awscliv2.zip&quot;<br>sudo apt install unzip<br>unzip awscliv2.zip<br>sudo ./aws/install</pre><p>I utilized Terraform to provision and manage the Kubernetes cluster, ensuring infrastructure as code principles for efficient and scalable deployments. <strong>Source Code</strong> — -&gt; <a href="https://github.com/shazia-massey/Multi-Tier-Java.git">https://github.com/shazia-massey/Multi-Tier-Java.git</a> → Multi-Tier-Java → Cluster</p><pre># On Jenkins VM<br><br><br># git clone https://github.com/shazia-massey/Multi-Tier-Java.git<br># cd Multi-Tier-Java/<br># ls<br># eks-rbac.md  main.tf  output.tf  variables.tf<br><br># terraform <br># sudo snap insatll terraform --classic<br># terraform init<br># terraform plan<br># terraform apply --auto-approve<br><br># kubectl<br># sudo snap install kubectl --classic<br># aws eks --region ap-south-1 update-kubeconfig --name devopsshack-cluster<br># kubectl get nodes<br>Now our cluster is ready</pre><p><strong>Production Deployment:</strong></p><ul><li>First for Production deployment we need to create<strong> </strong>Service Account and namespace</li></ul><pre># On Jenkins VM<br><br><br># Kubectl create namespace webapps<br><br>Create Service Account:<br># vi svc.yml<br><br>apiVersion: v1<br>kind: ServiceAccount<br>metadata:<br>  name: jenkins<br>  namespace: webapps<br><br># kubectl create namespace webapps<br># kubectl apply -f svc.yml</pre><ul><li>RBAC (role base access control)</li></ul><pre><br>Create Role:<br># vi role.yml<br><br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: Role<br>metadata:<br>  name: app-role<br>  namespace: webapps<br>rules:<br>  - apiGroups:<br>        - &quot;&quot;<br>        - apps<br>        - autoscaling<br>        - batch<br>        - extensions<br>        - policy<br>        - rbac.authorization.k8s.io<br>    resources:<br>      - pods<br>      - secrets<br>      - componentstatuses<br>      - configmaps<br>      - daemonsets<br>      - deployments<br>      - events<br>      - endpoints<br>      - horizontalpodautoscalers<br>      - ingress<br>      - jobs<br>      - limitranges<br>      - namespaces<br>      - nodes<br>      - pods<br>      - persistentvolumes<br>      - persistentvolumeclaims<br>      - resourcequotas<br>      - replicasets<br>      - replicationcontrollers<br>      - serviceaccounts<br>      - services<br>    verbs: [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;, &quot;create&quot;, &quot;update&quot;, &quot;patch&quot;, &quot;delete&quot;]<br><br>  # kubectl apply -f role.yml</pre><ul><li>Bind the role to service account</li></ul><pre><br>Bind the Role to Service Account:<br># vi rb.yml<br><br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: RoleBinding<br>metadata:<br>  name: app-rolebinding<br>  namespace: webapps <br>roleRef:<br>  apiGroup: rbac.authorization.k8s.io<br>  kind: Role<br>  name: app-role <br>subjects:<br>- namespace: webapps <br>  kind: ServiceAccount<br>  name: jenkins <br><br># kubectl apply -f rb.yml</pre><p>. Service-Account-Token(secret)</p><pre><br># vi svc-secret.yml<br><br>apiVersion: v1<br>kind: Secret<br>type: kubernetes.io/service-account-token<br>metadata:<br>  name: mysecretname<br>  annotations:<br>    kubernetes.io/service-account.name: jenkins<br><br># kubectl apply -f svc-secret.yml -n webapps<br># kubectl describe secret mysecretname -n webapps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5zhYnWLn6UHVK5zerXwwzA.jpeg" /></figure><p>CICD Pipeline:</p><pre><br><br>CICD Pipeline------<br><br>pipeline {<br>    agent any<br>    <br>    tools {<br>        maven  &#39;maven3&#39;<br>    }<br>    <br>    environment{<br>        SCANNER_HOME= tool &#39;sonar-scanner&#39;<br>        IMAGE_NAME = &quot;masseys/bankapp&quot;<br>        TAG = &quot;${env.BUILD_NUMBER}&quot;<br>    }<br><br>    stages {<br>        stage(&#39;Git Checkout&#39;) {<br>            steps {<br>              git credentialsId: &#39;git-cred&#39;, url: &#39;https://github.com/shazia-massey/Multi-Tier-Java.git&#39;<br>            }<br>        }<br>        <br>        stage(&#39;Compile&#39;) {<br>            steps {<br>              sh &quot;mvn compile&quot;<br>            }<br>        }<br>        <br>        stage(&#39;Unit tests&#39;) {<br>            steps {<br>                sh &quot;mvn test -DskipTests=true&quot;<br>            }<br>        }<br>        <br>        stage(&#39;Trivy FS Scan &#39;) {<br>            steps {<br>                sh &quot;trivy fs --format table -o fs.html .&quot;<br>            }<br>        }<br>        <br>        stage(&#39;SonarQube Analysis&#39;) {<br>            steps {<br>               withSonarQubeEnv(&#39;sonar&#39;) {<br>                   sh &quot;$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectKey=multitier -Dsonar.projectName=multitier -Dsonar.java.binaries=target&quot;<br>               } <br>            }<br>        }<br>        <br>         stage(&#39;Quality GateCheck&#39;) {<br>            steps {<br>              timeout(time: 1, unit: &#39;HOURS&#39;) {<br>                     waitForQualityGate abortPipeline: false<br>                 }<br>            }<br>        }<br>        <br>         stage(&#39;Build&#39;) {<br>            steps {<br>              sh &quot;mvn package -DskipTests=true&quot;<br>            }<br>        }<br>        <br>         stage(&#39;publish To Nexus&#39;) {<br>            steps {<br>              withMaven(globalMavenSettingsConfig: &#39;maven-settings&#39;, jdk: &#39;&#39;, maven: &#39;maven3&#39;, mavenSettingsConfig: &#39;&#39;, traceability: true) {<br>                  sh &quot;mvn deploy -DskipTests=true&quot;<br>                }<br>            }<br>        }<br>        <br>         stage(&#39;Build  Docker Image&#39;) {<br>            steps {<br>                script {<br>               withDockerRegistry(credentialsId: &#39;docker-cred&#39;) {<br>                   sh &quot;docker build -t ${IMAGE_NAME}:${TAG} .&quot;<br>                     <br>                     }<br>                 }<br>            }<br>        }<br>        <br>         <br>        stage(&#39;Trivy Image Scan &#39;) {<br>            steps {<br>                sh &quot;trivy image --format table -o image.html ${IMAGE_NAME}:${TAG}&quot;<br>            }<br>        }<br>        <br>        <br>         stage(&#39;Push Docker Image&#39;) {<br>            steps {<br>                script {<br>               withDockerRegistry(credentialsId: &#39;docker-cred&#39;) {<br>                   sh &quot;docker push  ${IMAGE_NAME}:${TAG}&quot;<br>                     <br>                     }<br>                 }<br>            }<br>        }<br>        <br>         stage(&#39;Update Kubernetes Manifest&#39;) {<br>            steps {<br>                script {<br>                    // Replace the Docker image tag in line 58 of the ds.yml file<br>                    sh &quot;&quot;&quot;<br>                    sed -i &#39;58s|image: ${IMAGE_NAME}:.*|image: ${IMAGE_NAME}:${TAG}|&#39; ds.yml<br>                    &quot;&quot;&quot;<br>                }<br>            }<br>        }<br>        <br>        <br>              <br>           stage(&#39;Commit and Push Changes&#39;) {<br>    steps {<br>        script {<br>            // Use GitHub credentials from Jenkins<br>            withCredentials([usernamePassword(credentialsId: &#39;git-cred&#39;, usernameVariable: &#39;GIT_USERNAME&#39;, passwordVariable: &#39;GIT_PASSWORD&#39;)]) {<br>                sh &quot;&quot;&quot;<br>                git config --global user.email &quot;masseys.s123@gmail.com&quot;<br>                git config --global user.name &quot;shazia-massey&quot;<br>                git remote set-url origin @github.com/shazia-massey/Multi-Tier-Java.git&quot;&gt;https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/shazia-massey/Multi-Tier-Java.git<br>                git pull origin master || git pull origin main<br>                git add ds.yml<br>                git commit -m &quot;Update image to masseys/bankapp:6&quot;<br>                git push origin HEAD:main || git push origin HEAD:master<br>                &quot;&quot;&quot;<br>            }<br>        }<br>    }<br>}<br><br>        <br>         stage(&#39;Deploy To K8&#39;) {<br>            steps {<br>              withKubeConfig(caCertificate: &#39;&#39;, clusterName: &#39;devopsshack-cluster&#39;, contextName: &#39;&#39;, credentialsId: &#39;k8-token&#39;, namespace: &#39;webapps&#39;, restrictKubeConfigAccess: false, serverUrl: &#39;https://3F5E779C7DF69C01E718C3A9E1325FF8.gr7.us-east-1.eks.amazonaws.com&#39;) {<br>                     sh &quot; kubectl apply -f ds.yml&quot;<br>                     sleep 30<br>                }<br>            }<br>        }<br>        <br>         stage(&#39;Verify k8 Deployment&#39;) {<br>            steps {<br>              withKubeConfig(caCertificate: &#39;&#39;, clusterName: &#39;devopsshack-cluster&#39;, contextName: &#39;&#39;, credentialsId: &#39;k8-token&#39;, namespace: &#39;webapps&#39;, restrictKubeConfigAccess: false, serverUrl: &#39;https://3F5E779C7DF69C01E718C3A9E1325FF8.gr7.us-east-1.eks.amazonaws.com&#39;) {<br>                     sh &quot;kubectl get pods -n webapps&quot;<br>                     sh &quot;kubectl get svc -n webapps&quot;<br>                }<br>            }<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v953U4iJgywkk4Hoy943sQ.jpeg" /></figure><h4>Explanation:</h4><p>The provided Jenkins pipeline automates the complete CI/CD process for a multi-tier Java application, incorporating key DevOps best practices. Below is a breakdown of its stages:</p><h3>Pipeline Overview:</h3><ul><li><strong>Agent</strong>: The pipeline uses a flexible agent (agent any) to run on any available Jenkins node.</li><li><strong>Tools</strong>: Maven (maven3) is specified for Java project compilation and packaging, and SonarQube is used for static code analysis.</li><li><strong>Environment Variables</strong>:</li><li>SCANNER_HOME for SonarQube scanner.</li><li>IMAGE_NAME defines the Docker image name.</li><li>TAG dynamically assigns a tag using the Jenkins build number for versioning.</li></ul><h3>Stages:</h3><ol><li><strong>Git Checkout</strong>:</li></ol><ul><li>This stage pulls the application code from a GitHub repository using a specific Jenkins credential (git-cred).</li></ul><p><strong>2. Compile</strong>:</p><ul><li>Executes Maven commands to compile the Java application.</li></ul><p><strong>3. Unit Tests</strong>:</p><ul><li>Executes unit tests using Maven, though tests are skipped here (-DskipTests=true) for demonstration purposes.</li></ul><p><strong>4. Trivy FS Scan</strong>:</p><ul><li>A Trivy file system scan is performed to check for vulnerabilities in the repository. The output is stored in an HTML file (fs.html).</li></ul><p><strong>5. SonarQube Analysis</strong>:</p><ul><li>This stage runs static code analysis using SonarQube. It uses the SonarQube scanner to analyze the compiled Java binaries.</li></ul><p><strong>6. Quality Gate Check</strong>:</p><ul><li>Ensures that the SonarQube quality gate is passed before proceeding further in the pipeline. If not met within the specified time (1 hour), the pipeline halts.</li></ul><p><strong>7. Build</strong>:</p><ul><li>The application is packaged using Maven, skipping the tests again to focus on the build.</li></ul><p><strong>8. Publish to Nexus</strong>:</p><ul><li>Maven deploys the build artifacts to a Nexus repository. This promotes efficient artifact management and sharing across teams.</li></ul><p><strong>9. Build Docker Image</strong>:</p><ul><li>The Docker image is built using the application artifact and tagged with the dynamic TAG variable (${env.BUILD_NUMBER}).</li></ul><p><strong>10. Trivy Image Scan</strong>:</p><ul><li>The newly created Docker image is scanned for vulnerabilities using Trivy. The results are saved in image.html.</li></ul><p><strong>11. Push Docker Image</strong>:</p><ul><li>The Docker image is pushed to the Docker registry with the appropriate tag. Credentials for the Docker registry are provided by Jenkins (docker-cred).</li></ul><p><strong>12. Update Kubernetes Manifest</strong>:</p><ul><li>The Kubernetes deployment manifest (ds.yml) is updated to use the new Docker image tag. This ensures that the deployment always uses the latest image.</li></ul><p><strong>13. Commit and Push Changes</strong>:</p><ul><li>Commits the updated Kubernetes manifest to the GitHub repository and pushes it back. This ensures version control for the updated deployment files.</li></ul><p><strong>14. Deploy to Kubernetes (K8)</strong>:</p><ul><li>The updated Kubernetes manifest is applied to the EKS cluster, deploying the latest version of the application. The cluster is authenticated using a Jenkins credential (k8-token).</li></ul><p><strong>15. Verify Kubernetes Deployment</strong>:</p><ul><li>This stage verifies that the application is successfully deployed by checking the status of the pods and services in the designated namespace (webapps).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vKEzg7PbOUAGHcOpZxVNRQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1bgxpT9R7s7b9jMil036mg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aZQh-u0D8jii4-k0VwSLcA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XNcCKmbV2usD5zBc58TMGA.jpeg" /></figure><p><strong>Key Learnings</strong> from This Project: I gained valuable insights into end-to-end automation, ensuring the entire process from building, testing, scanning, to deploying the application is seamless. Integrating Trivy scans allowed me to enhance security by detecting vulnerabilities in both the file system and Docker images at various stages. By implementing SonarQube, I reinforced the importance of code quality, with quality gates halting the pipeline if standards were not met. Leveraging dynamic Docker image tagging, I learned to improve traceability for every build. The automation of deployment to an EKS cluster streamlined delivery to production environments, making the entire CI/CD pipeline more reliable, secure, and efficient.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b559b706b3fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>