Saturday, 30 July 2022

How to install K3s

Step 1: Update Ubuntu system

Update and upgrade your system

sudo apt update && sudo apt -y upgrade
sudo reboot

Step 2: Install Single Node k3s Kubernetes

We will deploy a single node kubernetes using k3s lightweight tool. K3s is a certified Kubernetes distribution designed for production workloads in unattended, resource-constrained environments. The good thing with k3s is that you can add more Worker nodes at later stage if need arises.

K3s provides an installation script that is a convenient way to install it as a service on systemd or openrc based systems

Let’s run the following command to install K3s on our Ubuntu system:

curl -sfL https://get.k3s.io | sudo bash -
sudo chmod 644 /etc/rancher/k3s/k3s.yaml

Installation process output:

[INFO]  Finding release for channel stable
[INFO]  Using v1.21.3+k3s1 as release
[INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.21.3+k3s1/sha256sum-amd64.txt
[INFO]  Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.21.3+k3s1/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

Validate K3s installation:

The next step is to validate our installation of K3s using kubectl command which was installed and configured by installer script.

$ kubectl get nodes
NAME        STATUS   ROLES                  AGE   VERSION
ubuntu-01   Ready    control-plane,master   33s   v1.22.5+k3s1

You can also confirm Kubernetes version deployed using the following command:

$ kubectl version --short
Client Version: v1.22.5+k3s1
Server Version: v1.22.5+k3s1

The K3s service will be configured to automatically restart after node reboots or if the process crashes or is killed.

Sunday, 19 June 2022

How Automate Kubernetes/docker with jenkins (Installation)

 Step 1: Install Jenkins on Ubuntu 18.04 instance

apt updateapt install openjdk-11-jre-headless -yjava -version


curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null


Once the Jenkins repository is enabled, update the apt package list and install the latest version of Jenkins by typing:

sudo apt-get updatesudo apt-get install jenkins

systemctl status jenkins

See link for complete configuration guide of jenkins : https://www.devopstreams.com/2020/08/pleasefollow-steps-to-install-java.html


Step 2: Add Jenkins User to Sudoers List


Now configured Jenkins user as administrator to do all operation and connect eks cluster.

$ vi /etc/sudoers

Add at end of file

jenkins ALL=(ALL) NOPASSWD: ALL

Save and exit

:wq!


Now we can use Jenkins as a sudo user


sudo su - jenkins


Step 3:

Docker installation

sudo apt install docker.io -y

Once done you can check the version also.

docker --version

Now add Jenkins user in the docker group

sudo usermod -aG docker jenkins

Now we are installing aws CLI, kubectl, and eksctl command-line utility on the Jenkins server.

Follow the below commands,

Step 4:

sudo apt  install awscli


To configure the AWS the first command we are going to run is

aws configure

Then enter your Access/Secret key, Format: json and region:us-east-2


Step 5:

Install eksctl

 curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp

sudo mv /tmp/eksctl /usr/local/bin

  eksctl version

If you get something like "no command found" enter the below command

cp /usr/local/bin/eksctl /usr/bin -rf

Step 6:

Install kubectl

 curl -o kubectl https://amazon-eks.s3-us-west-2.amazonaws.com/1.14.6/2019-08-22/bin/linux/amd64/kubectl

 chmod +x ./kubectl

mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$HOME/bin:$PATH

sudo mv ./kubectl /usr/local/bin

 kubectl version --short --client


Step 7:

Install aws-iam-authenticator

   curl -o aws-iam-authenticator https://amazon-eks.s3-us-west-2.amazonaws.com/1.14.6/2019-08-22/bin/linux/amd64/aws-iam-authenticator

    chmod +x ./aws-iam-authenticator

   mkdir -p $HOME/bin && cp ./aws-iam-authenticator $HOME/bin/aws-iam-authenticator && export PATH=$PATH:$HOME/bin

   echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc

sudo mv ./aws-iam-authenticator /usr/local/bin

   aws-iam-authenticator help


Step 8: Make sure you Create a role with Administrator Access and attach your role to the instance

 Step 9: Create your jenkins job --->Build Enviroment---->Execute Shell---->enter your kubernetes commands

Or

Create Pipeline and run your commands in stages

Thanks you








Wednesday, 15 June 2022

Git Hooks Simplified

 

ABCs of All Hooks

Today’s top companies give the highest priority to the quality of the code you have written. So, we have a concept of git hook which does not allow you to commit in “master branch” unless your code validation is accepted. I think it is helpful to maintain a better quality of code.

If you’ve ever worked on an open-source project, or you’re working with a team, it’s very likely you’re using some kind of version control. Version Control System (VCS) has become one of the main requirements for any project, Git being the foremost popular one. However, as the team grows in number, it becomes difficult to handle the different code styles and to enforce certain rules across all the contributors. These issues aren’t noticed by a contributor until he has pushed his changes which end in overheads for the core maintenance team. To enforce these rules and validate the code being pushed, Git provides a great feature, called Git Hooks.

So what are Git hooks?

Git hooks are custom scripts that git executes before or after events such as commitpush. Git hooks are a built-in feature there is no need to download anything. They are unique and run locally and resides inside the .git/hooks directory.

Git hooks can be used to:

  • Check commits for errors before they are pushed.
  • Ensure code meets project standards.
  • Notify team members about changes.
  • Push code into a production environment, and more.

There are two types of hooks:

  • Client-Side (Local) Hooks
  • Server-Side (Remote) Hooks

Server-Side Hooks, as the name suggests, are installed on the server and are triggered only just in case of network operations. For example — Post-Receive may be a sort of server-side hook triggered after a successful push. This makes it an ideal place to send notifications to all the contributors.

e.g: pre-receive, update, post-receive

  1. The pre-receive hook is executed every time somebody uses git push to push commits to the repository.
  2. The post-receive hook gets called after a successful push operation.

Client-Side Hooks reside on one’s local repository and are executed when a git event is triggered. Here, a git event can commit, push, rebase, etc. When we run certain git commands, git search for the hooks within the git repository to ascertain if there’s an associated script to run. For example, one could have a pre-push hook to validate that the code enforces certain rules before it’s pushed to the remote repository.

e.g: pre-commit, prepare-commit-msg, commit-msg, post-commit

  1. The pre-commit script is executed every time you run git commit command.
  2. The prepare-commit-msg hook is called after the pre-commit hook to populate the text editor with a commit message. This is where auto-generated commit message is created.
  3. The commit-msg hook is much like the prepare-commit-msg hook, but it is called after the user enters a commit message.
  4. The post-commit hook is called immediately after the commit-msg hook. This is after a commit has taken place.

In this image, we have different hooks name with their event (means which command executes which hook).

Implementing Git Hooks

Git hooks are a built-in feature that comes with every git repository. When initializing a new project git populates the hooks folder with template files.

  1. Navigate to the hooks directory
$ ls .git/hooks/

Notice the files inside, namely:

sample files inside the hooks directory

2. Install your hook

To enable the hook scripts, simply remove the .sample extension from the file name. Git will automatically execute the scripts based on the naming. For my purpose, I renamed the “commit-msg.sample” file to “commit-msg”.

3. Select a language to write your hook script.

The default script files are written in shell scripts, but you can use any scripting language you are familiar with as long as it can be run an executable. This includes BashPythonRubyPerlRustSwift, and Go.

Open up the script file in your code editor and define your language of choice in the first line, using the shebang (#!) sign, so git knows how to interpret the subsequent scripts. Note that you need to include the path of your interpreter. For Mac users who wish to write the scripts in Python, for instance, the Apple-provided build of Python is located in /usr/bin. So, the first line would look like:

#!/usr/bin python

If you want to use Bash, on the other hand, the first line would be:

#!/bin/bash

And for shell:

#!/bin/sh

4. Write your script

From here on, you could write any script and Git will execute it before any commits to this project. For reference, I wrote my script in Bash, and here is what I ended with up.

  • commit-msg hook

#!/bin/bash
Color_Off='\033[0m'
BRed="\033[1;31m" # Red
BGreen="\033[1;32m" # Green
BYellow="\033[1;33m" # Yellow
BBlue="\033[1;34m" # Blue
MSG_FILE=$1
FILE_CONTENT="$(cat $MSG_FILE)"
# Initialize constants here
export REGEX='(Add: |Created: |Fix: |Update: |Rework: )'
export ERROR_MSG="Commit message format must match regex \"${REGEX}\""
if [[ $FILE_CONTENT =~ $REGEX ]]; then
printf "${BGreen}Good commit!${Color_Off}"
else
printf "${BRed}Bad commit ${BBlue}\"$FILE_CONTENT\"\n"
printf "${BYellow}$ERROR_MSG\n"
printf "commit-msg hook failed (add --no-verify to bypass)\n"
exit 1
fi
exit 0

Let’s understand this script:
  • Line 3–7 define text-decoration variables, which are used to enhance my console message. For coloring the variables, I have used Git shell coloring.
  • Line 9–10 gets the commit message form the console.
  • Line 12–13 defines the REGEX pattern from which the commit message will be validated.
  • Line 14–21 is a conditional statement that will check the commit message if it matches with REGEX pattern then successful commit otherwise failed.
  • Notice that if the conditional above evaluates to a 1 status, line 19 extends that by exiting with a 1 status to indicate failure. This prevents changes from being commit.

We have to give executable permission to our script file using the chmod command.

$ chmod +x .git/hooks/commit-msg

After writing your hook scripts, just sit back and watch Git do all the work:

Let’s see the example below:

  1. Commit with an improper commit message
Bad commit

2. Commit with a proper commit message.

Good commit

3. If you want to bypass the commit-msg hook then use --no-verify flag.

Bypass commit
  • pre-push hook

#!/bin/sh
echo "Skip pre-push hooks with --no-verify (not recommended).\n"
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [ "$BRANCH" = "master" ];
then
echo "You are on branch $BRANCH. You must not push to master\n"
exit 1
fi
if [ `date +%w` -ge 5 ] && [ "$BRANCH" = "develop" ];
then
echo "Please, do not push code to develop before the weekend!\n"
exit 1
fi
Let’s understand this script:
  • Line 5 determines the current branch with git rev-parse --abbrev-ref HEAD.
  • Line 6–10 is a conditional statement, will check in which branch you are in. If you’re in the master branch then the script will not allow you to push code in the master branch. If not in master, then you can push.
  • Line 12–16 is also a conditional statement, will check your current branch and also check the date and week. If a week is greater than means you i.e Saturday and Sunday then the script will not allow you to push the code.

This pre-push script has some advantages as well because in the IT industry who wants to work at the weekend, no one really? So, this hook prevents you to push code on a Friday night because it’s having a high chance to get an error when you pushed the code. So, it prevents that’s awesome!

  1. Error while pushing to the master branch.

2. Successfully pushed to another branch on weekdays except for weekends.

  • pre-commit hook

#!/bin/bash
# Git Shell Coloring
RESTORE='\033[0m' # Text Reset means no color change
RED='\033[00;31m' # Red color code
YELLOW='\033[00;33m' # yellow color code
BLUE='\033[00;34m' # blue color code
FORBIDDEN=( 'TODO:' 'console.log' )
FOUND=''
for j in "${FORBIDDEN[@]}"
do
for i in `git diff --cached --name-only`
do
# the trick is here...use `git show :file` to output what is staged
# test it against each of the FORBIDDEN strings ($j)
if echo `git show :$i` | grep -q "$j"; then
FOUND+="${BLUE}$i ${RED}contains ${RESTORE}\"$j\"${RESTORE}\n"
fi
done
done
# if FOUND is not empty, REJECT the COMMIT
# PRINT the results (colorful-like)
if [[ ! -z $FOUND ]]; then
printf "${YELLOW}COMMIT REJECTED\n"
printf "$FOUND"
exit 1
fi
# nothing found? let the commit happen
exit 0
While developing any software, at some point the developers have to debug the code to check the bugs or API response. If you are from the JavaScript background then developers mostly use “console.log()” statement to print something in console. Consider a scenario, where you have debugged the code and forget to remove those console.log()” statements and you’re ready to commit the changes. But, you don’t want those statements to commit. So, in that case, we can take the help of the pre-commit hook in which we have written a script will automatically check the code before commit. If the scripts found any “console.log()” statements then it will not allow you to commit. That’s amazing, right?                                                                                                               

Let’s understand the code:                                                                                      The script uses the grep command to search for the forbidden strings like “TODO:” and “console.log()” in the staged codebase. If strings are found then abort the commit.                                                           Commit Rejected                                                                                                                                                                             Now, If we remove those statements commit will be accepted.                     
Commit Accepted

Be aware of the --no-verify option to git commit. This bypasses the pre-commit hook when committing.

Prevents bad commit or push (git hooks, pre-commit/pre-commit, pre-push/pre-push, post-merge/post-merge, and all that stuff…)

How to upgrade Maven

  java.lang.IllegalStateException I had installed maven in my ubuntu using command  apt install maven This installed maven in path /usr/shar...