Thursday, April 11, 2024

Nexus Installation

https://github.com/kishoraswar22/NexusInstallation/blob/main/Sonatype%20Nexus.docx

Scripted Pipeline

 node {
    stage('Build') {
        echo 'Building...'
        git changelog: false, poll: false, url: 'https://github.com/kishoraswar22/spring-boot-war-example.git'
    }
    
    stage('Test') {
        echo 'Testing...'
        // This stage could include commands to run your tests
    }
    
    stage('Deploy') {
        echo 'Deploying...'
        // This stage could include commands to deploy your application
    }

    try {
        echo 'Pipeline execution completed successfully.'
    } catch (err) {
        echo "Pipeline failed: ${err}"
    }
}


      

Jenkins Sonarqube Pipeline

      pipeline {
    agent any
    
    tools {
        maven 'maven3'
        jdk 'jdk11'
    }
    environment{
        SONAR_SCANNER_HOME= tool 'sonar-scanner'
    }

    stages {
        stage('Code Checkout') {
            steps {
                git changelog: false, poll: false, url: 'https://github.com/kishoraswar22/spring-boot-war-example.git'
            }
        }
        stage('Code Build') {
            steps {
                sh 'mvn compile'
            }
        }
        stage('Code Test') {
             
             tools{
                 jdk 'jdk17'
             }
           
                steps {
                withSonarQubeEnv('sonar') {
                   sh '''
                    $SONAR_SCANNER_HOME/bin/sonar-scanner  -Dsonar.projectName=SpringWebapp -Dsonar.projectKey=SpringWebapp \
                    -Dsonar.java.binaries=.
                    
                   
                    
                  '''
                   
               }
                    
               }
            }
        
     
            
        
        stage('Dev Deploy') {
            steps {
                deploy adapters: [tomcat9(credentialsId: 'tomcat_admin', path: '', url: 'http://43.204.32.171:8080')], contextPath: null, war: '**/*.war'
            }
        }
        
        
    }
}

      

Wednesday, April 10, 2024

Tomcat Installation on Linux

Step 1) Create EC2 instance

Step 2) Install java 

# yum install java -y

Step 3)  Download tomcat package

path: /opt/

#wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.87/bin/apache-tomcat-9.0.87.tar.gz

 Step 4)  Extract tomcat package

#cd /opt

#gunzip apache-tomcat-9.0.87.tar.gz

#tar -xvf apache-tomcat-9.0.87.tar

path: /opt/apache-tomcat-9.0.87

Step 5) Comment out below line in context.xml file

Path: /opt/apache-tomcat-9.0.87/webapps/manager/META-INF

vi context.vml

  <!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"

  allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

path: /opt/apache-tomcat-9.0.87/webapps/host-manager/META-INF

 <!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"

  allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Step 6) Add tomcat user.

path: /opt/apache-tomcat-9.0.87/conf

#vi  tomcat-users.xml --> add user configuration.

<role rolename="admin-gui"/>

  <role rolename="admin-script"/>

  <role rolename="manager-gui"/>

  <role rolename="manager-status"/>

  <role rolename="manager-script"/>

  <role rolename="manager-jmx"/>

  <user name="admin" password="admin" roles="admin-gui,admin-script,manager-gui,manager-status,manager-script,manager-jmx"/>


Step 7)Start tomcat

path: /opt/apache-tomcat-9.0.87/bin

#./startup.sh

Step 8)Access tomcat using below URL.

http://<public-ip>:8080







Friday, April 5, 2024

Create Sample Java Web Application using Maven and Deploy it on Tomcat Server

Install Maven on windows:

https://phoenixnap.com/kb/install-maven-windows

Install maven on Linux:

Install Maven (Latest From Maven Repo)

Follow the steps given below to install the latest maven package from the official maven repo.

Step 1: Go to Maven Downlaods and get the download link of the latest package.

wget wget https://dlcdn.apache.org/maven/maven-3/3.9.0/binaries/apache-maven-3.9.0-bin.tar.gz

Step 2: Untar the mvn package to the /opt folder.

sudo tar xvf apache-maven-3.9.0-bin.tar.gz -C /opt

Step 3: Create a symbolic link to the maven folder. This way, when you have a new version of maven, you just have to update the symbolic link and the path variables remain the same.

sudo ln -s /opt/apache-maven-3.9.0 /opt/maven


Add Maven Folder To System PATH


To access the mvn command systemwide, you need to either set the M2_HOME environment variable or add /opt/maven to the system PATH.

We will do both by adding them to the profile.d folder. So that every time the shell starts, it gets sourced and the mvn command will be available system-wide.

Step 1: Create a script file named maven.sh in the profile.d folder.

sudo vi /etc/profile.d/maven.sh
Step 2: Add the following to the script and save the file.

export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Step 3: Add execute permission to the maven.sh script.

sudo chmod +x /etc/profile.d/maven.sh
Step 4: Source the script for changes to take immediate effect.

source /etc/profile.d/maven.sh
Step 5: Verify maven installation

mvn -version

Maven Command To Create Java Web Application:

mvn archetype:generate  -DgroupId=com.companyname.automobile -DartifactId=trucks -DarchetypeArtifactId=maven-archetype-webapp  -DinteractiveMode=false

Maven Command to generate artifact:

First go the location where pom.xml file is placed... and execute below command

#mvn package

Deploy Sample Web application on tomcat server:

Copy  the artifact from target folder and deploy it on tomcat server(tomcat deployment directory is webapps)











Wednesday, April 19, 2023

ResourceQuota and LimitRange

 Objectbase quota: pod, rs, pv


controlplane $ kubectl create ns myns

namespace/myns created

controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


No resource quota.


No LimitRange resource.

controlplane $ 


==============

controlplane $ kubectl explain resourcequota

KIND:     ResourceQuota

VERSION:  v1


DESCRIPTION:

     ResourceQuota sets aggregate quota restrictions enforced per namespace


FIELDS:

   apiVersion   <string>

     APIVersion defines the versioned schema of this representation of an

     object. Servers should convert recognized schemas to the latest internal

     value, and may reject unrecognized values. More info:

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources


   kind <string>

     Kind is a string value representing the REST resource this object

     represents. Servers may infer this from the endpoint the client submits

     requests to. Cannot be updated. In CamelCase. More info:

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds


   metadata     <Object>

     Standard object's metadata. More info:

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata


   spec <Object>

     Spec defines the desired quota.

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status


   status       <Object>

     Status defines the actual enforced quota and its current usage.

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status




==============


controlplane $ kubectl apply -f rsquota.yml  --dry-run 

W0420 01:28:47.010151   46233 helpers.go:663] --dry-run is deprecated and can be replaced with --dry-run=client.

resourcequota/myquota created (dry run)

controlplane $ 



controlplane $ kubectl apply -f rsquota.yml  --namespace=myns

resourcequota/myquota created

controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


Resource Quotas

  Name:     myquota

  Resource  Used  Hard

  --------  ---   ---

  pods      0     2


No LimitRange resource.



=======================


controlplane $ kubectl run mypod --image=nginx -n myns

pod/mypod created

controlplane $ kubectl get po -n myns

NAME    READY   STATUS              RESTARTS   AGE

mypod   0/1     ContainerCreating   0          6s

controlplane $ kubectl get po -n myns

NAME    READY   STATUS    RESTARTS   AGE

mypod   1/1     Running   0          9s

controlplane $ kubectl run mypod1 --image=nginx -n myns

pod/mypod1 created

controlplane $ kubectl get po -n myns

NAME     READY   STATUS    RESTARTS   AGE

mypod    1/1     Running   0          18s

mypod1   1/1     Running   0          3s

controlplane $ kubectl run mypod2 --image=nginx -n myns

Error from server (Forbidden): pods "mypod2" is forbidden: exceeded quota: myquota, requested: pods=1, used: pods=2, limited: pods=2




controlplane $ kubectl describe ns myns 

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


Resource Quotas

  Name:     myquota

  Resource  Used  Hard

  --------  ---   ---

  pods      2     2


No LimitRange resource.



https://kubernetes.io/docs/concepts/policy/resource-quotas/


controlplane $ kubectl delete -f rsquota.yml  -n myns 

resourcequota "myquota" deleted

controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


No resource quota.


No LimitRange resource.

controlplane $ 


=======================



Compute Base Quota: cpu, memory



controlplane $ cat computequota.yml 

apiVersion: v1

kind: ResourceQuota

metadata:

  name: myquota


spec:

   hard:

     requests.cpu: 0.5

     requests.memory: 500Mi

     limits.cpu: 1

     limits.memory: 1Gi




controlplane $ kubectl apply -f computequota.yml -n myns

resourcequota/myquota created



controlplane $ kubectl  get resourcequotas -n myns

NAME      AGE     REQUEST                                          LIMIT

myquota   2m31s   requests.cpu: 0/500m, requests.memory: 0/500Mi   limits.cpu: 0/1, limits.memory: 0/1Gi

controlplane $ 



controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


Resource Quotas

  Name:            myquota

  Resource         Used  Hard

  --------         ---   ---

  limits.cpu       0     1

  limits.memory    0     1Gi

  requests.cpu     0     500m

  requests.memory  0     500Mi


No LimitRange resource.



controlplane $ kubectl run mypod1 --image=nginx -n myns

Error from server (Forbidden): pods "mypod1" is forbidden: failed quota: myquota: must specify limits.cpu for: mypod1; limits.memory for: mypod1; requests.cpu for: mypod1; requests.memory for: mypod1

controlplane $ 


===================


apiVersion: v1

kind: Pod

metadata:

  name: high-priority

spec:

  containers:

  - name: high-priority

    image: ubuntu

    command: ["/bin/sh"]

    args: ["-c", "while true; do echo hello; sleep 10;done"]

    resources:

      requests:

        memory: "250Mi"

        cpu: "0.1"

      limits:

        memory: "500Mi"

        cpu: "0.5"

==========================


controlplane $ kubectl apply -f mypod.yml -n myns

pod/high-priority created

controlplane $ 




controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


Resource Quotas

  Name:            myquota

  Resource         Used   Hard

  --------         ---    ---

  limits.cpu       500m   1

  limits.memory    500Mi  1Gi

  requests.cpu     100m   500m

  requests.memory  250Mi  500Mi


No LimitRange resource.



controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


Resource Quotas

  Name:            myquota

  Resource         Used    Hard

  --------         ---     ---

  limits.cpu       1       1

  limits.memory    1000Mi  1Gi

  requests.cpu     200m    500m

  requests.memory  500Mi   500Mi


No LimitRange resource.

controlplane $ vi mypod.yml

controlplane $ kubectl apply -f mypod.yml -n myns

Error from server (Forbidden): error when creating "mypod.yml": pods "high-priority2" is forbidden: exceeded quota: myquota, requested: limits.cpu=500m,limits.memory=500Mi,requests.memory=250Mi, used: limits.cpu=1,limits.memory=1000Mi,requests.memory=500Mi, limited: limits.cpu=1,limits.memory=1Gi,requests.memory=500Mi

controlplane $ 



if we assign only limit then request and limit will be same for pod



If we assign only request ..the  we will get error..




============

Now if we not assign any limit and we want it should assign automatically




LimitRange:


https://kubernetes.io/docs/concepts/policy/limit-range/


controlplane $ kubectl explain limitrange

KIND:     LimitRange

VERSION:  v1


DESCRIPTION:

     LimitRange sets resource usage limits for each kind of resource in a

     Namespace.


FIELDS:

   apiVersion   <string>

     APIVersion defines the versioned schema of this representation of an

     object. Servers should convert recognized schemas to the latest internal

     value, and may reject unrecognized values. More info:

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources


   kind <string>

     Kind is a string value representing the REST resource this object

     represents. Servers may infer this from the endpoint the client submits

     requests to. Cannot be updated. In CamelCase. More info:

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds


   metadata     <Object>

     Standard object's metadata. More info:

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata


   spec <Object>

     Spec defines the limits enforced. More info:

     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status


controlplane $ 



controlplane $ kubectl api-resources |grep -i limit

limitranges                       limits       v1                                     true         LimitRange

controlplane $ 




controlplane $ kubectl get limits -n  myns

No resources found in myns namespace.


===========================

controlplane $ cat nslimit.yml 

apiVersion: v1

kind: LimitRange

metadata:

    name: testlimit

    namespace: myns


spec:

   limits:

    - default:

         cpu: 200m

         memory: 500Mi

      type: Container


===================



controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


No resource quota.


Resource Limits

 Type       Resource  Min  Max  Default Request  Default Limit  Max Limit/Request Ratio

 ----       --------  ---  ---  ---------------  -------------  -----------------------

 Container  cpu       -    -    200m             200m           -

 Container  memory    -    -    500Mi            500Mi          -

controlplane $ 


=====================

controlplane $ cat nslimit.yml 

apiVersion: v1

kind: LimitRange

metadata:

    name: testlimit

    namespace: myns


spec:

   limits:

    - default:

         cpu: 200m

         memory: 500Mi

      defaultRequest:

         cpu: 100m

         memory: 250Mi

      type: Container





controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


No resource quota.


Resource Limits

 Type       Resource  Min  Max  Default Request  Default Limit  Max Limit/Request Ratio

 ----       --------  ---  ---  ---------------  -------------  -----------------------

 Container  cpu       -    -    100m             200m           -

 Container  memory    -    -    250m             500Mi          -





controlplane $ cat nslimit.yml 

apiVersion: v1

kind: LimitRange

metadata:

    name: testlimit

    namespace: myns


spec:

   limits:

    - default:

         cpu: 200m

         memory: 500Mi

      defaultRequest:

         cpu: 100m

         memory: 250Mi

      min:

         cpu: 80m

         memory: 250Mi

      max:

         cpu: 700m

         memory: 700Mi

      type: Container




===========

controlplane $ kubectl describe ns myns

Name:         myns

Labels:       kubernetes.io/metadata.name=myns

Annotations:  <none>

Status:       Active


No resource quota.


Resource Limits

 Type       Resource  Min    Max    Default Request  Default Limit  Max Limit/Request Ratio

 ----       --------  ---    ---    ---------------  -------------  -----------------------

 Container  cpu       80m    700m   100m             200m           -

 Container  memory    250Mi  700Mi  250Mi            500Mi          -



apiVersion: v1

  

kind: Pod


metadata:


  name: example-no-conflict-with-limitrange-cpu


spec:


  containers:


  - name: demo


    image: registry.k8s.io/pause:2.0


    resources:


      requests:


        cpu: 700m


      limits:


        cpu: 700m



Monday, April 3, 2023

K8 Deployment Strategy


https://github.com/ContainerSolutions/k8s-deployment-strategies



Recreate:

https://dev.to/cloudskills/kubernetes-deployment-strategy-recreate-3kgn


Ramped/Rolling Update:

https://github.com/ContainerSolutions/k8s-deployment-strategies/tree/master/ramped


Blue-Green Deployment

https://dev.to/pavanbelagatti/kubernetes-deployments-rolling-vs-canary-vs-blue-green-4k9p



Canary Deployment:

https://github.com/NileshGule/canary-demo-kuberentes/tree/main/k8s


Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117