Sunday, November 29, 2020

Code error 403 when trying to access Kubernetes cluster | Jenkins Kubernetes Deployment

When ever you are doing deployment from Jenkins to EKS cluster, you may get this error:

Api call failed with code 403, detailed message: {
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },

"status": "Failure",
  "message": "namespaces is forbidden: User \"system:anonymous\" cannot list namespaces at the cluster scope",
  "reason": "Forbidden",
  "details": {
    "kind": "namespaces"
  },
  "code": 403 
Work around or the fix: 

You get this error because you're getting blocked by RBAC policies. Basically, RBAC policies set to restrict the resources you use and limits a few of your action. 

There are two possibilities, either you haven't created an RBAC or it's somehow restricting the cluster access.

By default, your clusterrolebinding has system:anonymous set which blocks the cluster access.

Execute the following command, it will set a clusterrole as cluster-admin which will give you the required access.

kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous

 

Wednesday, November 25, 2020

Deploy Python App into Kubernetes Cluster using kubectl in Jenkins Pipeline | Containerize Python App and Deploy into Kubernetes Cluster

 We will learn how to automate Docker builds using Jenkins and Deploy into Kubernetes Cluster. We will use Python based application. I have already created a repo with source code + Dockerfile. The repo also have Jenkinsfile for automating the following:


- Automating builds using Jenkins
- Automating Docker image creation
- Automating Docker image upload into Docker registry
- Automating Deployments to Kubernetes Cluster
 
Pre-requisites:
1. Jenkins Master is up and running. 
2. Spin up Jenkins slave, install docker in it. Install kubectl on Slave.
3. Docker, Docker pipeline and Kubernetes Deploy plug-ins are installed in Jenkins



4. Docker hub account setup in https://cloud.docker.com
5. Kubernetes Cluster is setup and running.

Step #1 - Login to Jenkins slave instance through command line - Install Docker

sudo apt install docker.io -y
 
Add Jenkins to Docker Group
sudo usermod -aG docker jenkins

sudo systemctl daemon-reload
 
Restart Docker service
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl restart docker


Restart Jenkins service
sudo service jenkins restart
 
Install kubectl command on Jenkins slave.

Step #2 - Create Credentials for Docker Hub
Go to Jenkins UI, click on Credentials -->


Click on Global credentials
Click on Add Credentials


Now Create an entry for your Docker Hub account. Make sure you enter the ID as dockerhub

Step #3 - Create Credentials for Kubernetes Cluster
execute the below command to get kubeconfig info, copy the entire content of the file:
sudo cat ~/.kube/config

Go to Jenkins, Manage Jenkins, Click on Add Credentials, use Kubernetes configuration from drop down.
Enter ID as kubeconfig_raw and upload kubeconfig file


 Click on browse
Upload kube config and enter id as ID kubeconfig-raw

Step # 4 - Create a pipeline in Jenkins
Create a new pipeline job.


Step # 5 - Copy the pipeline code from below
Make sure you change red highlighted values below:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied

pipeline {
     agent {
         label 'myslave'
     }
        environment {
        //once you sign up for Docker hub, use that user_id here
        registry = "your_docker_hub_user_id/mypython-app"
        //- update your credentials ID after creating credentials for connecting to Docker Hub
        registryCredential = 'dockerhub'
        dockerImage = ''
    }
    stages {

        stage ('checkout') {
            steps {
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/akannan1087/myPythonDockerRepo']]])
            }
        }
       
        stage ('Build docker image') {
            steps {
                script {
                dockerImage = docker.build registry
                }
            }
        }
       
         // Uploading Docker images into Docker Hub
    stage('Upload Image') {
     steps{   
         script {
            docker.withRegistry( '', registryCredential ) {
            dockerImage.push()
            }
        }
      }
    }
   
       stage('K8S Deploy') {
        steps{   
            script {
                withKubeConfig([credentialsId: 'kubeconfig-raw', serverUrl: '']) {
                sh ('kubectl apply -f k8s-deployment.yaml')
                }
            }
        }
       }
  
    }  
}

Step # 6 - Build the pipeline
Once you create the pipeline and changes values per your Docker user id and credentials ID, click on 

Step # 7 - Verify deployments to K8S

kubectl get pods


kubectl get deployments
kubectl get services

Steps # 8 - Access Python App in K8S cluster
Once build is successful, go to browser and enter master or worker node public ip address along with port number mentioned above
http://master_or_worker_node_public_ipaddress:port_no_from_above

You should see page like below:




Tuesday, November 24, 2020

How to set up SSH keys in Azure Repos | Setup Java Web App in Azure Git in Azure DevOps using Maven

How to set up SSH keys in Azure Repos and Setup WebApp in Azure Repo


Pre-requisites:
  • Git client installed on your source or local machine.
Steps:

Go to your visual studio home page. You should be able to go by clicking on the below URL.
https://dev.azure.com/

Create a new project by clicking on New project and enter project details like below. This will create a project dashboard.


Once project is created, Click on Repos.


 Click on initialize link to add README.



Click on clone link on the top right hand side.
Click on SSH link
Click Manage SSH keys

Click Add

Go to any virtual machine you had setup that has both Java and Maven installed. If you would like to know how to create a Virtual Machine in Azure Cloud, click here to do so.

Login to your VM

Create the SSH keys by executing the below command:
(If you already have keys generated, you can overwrite it or skip to next step to copy keys)
ssh-keygen

execute the below command to copy the keys:

cat ~/.ssh/id_rsa.pub

Add the public keys.
Once keys added into Azure DevOps, go to Repos, copy the SSH clone url

Go to your machine where you have installed Java, Maven, preferably your EC2. Execute this command:
git clone <ssh_url>

This should download the empty repo from Azure DevOps to local machine(or ec2).
now after cloning, it will create a folder with repo name..

type 

ls -al to see the folder name after you cloned.

go inside that folder by
cd reponame

Install Maven
sudo apt install maven -y 

Now create the maven project executing the below command:
mvn archetype:generate -DgroupId=com.cf -DartifactId=MyAwesomeApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

type git status --> to see the newly created project
git add *
git status

git commit -m "my first project check-in to Azure Git"
git push

Now go to Azure DevOps, Select the Repos —> Files —> you should see the project uploaded here.
 

Please watch the above steps on YouTube video:

Saturday, November 21, 2020

Ansible playbook for Java 11 Installation on Ubuntu | Ansible Java 11 Playbook Ubuntu

1. Login to Ansible management server/machine. Create SSH keys in Ansible host machine by executing the below command: (if you already have keys created, please skip this step)
ssh-keygen 

enter three times..now you will see keys successfully created.
2.  Execute the below command on Ansible management node and copy the public key content:
sudo cat ~/.ssh/id_rsa.pub

copy the above output.
3. Now login to target node, execute the below command to open the file
sudo vi /home/ubuntu/.ssh/authorized_keys
type shift A and then enter now 
    and paste the key in the above file. please do not delete any existing values in this file.

4. Now go back to Ansible mgmt node, do changes in /etc/ansible/hosts file to include the node you will be installing software. Make sure you add public IP address of target node as highlighted below in red color:
sudo vi /etc/ansible/hosts
[My_Group]  
xx.xx.xx.xx ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa  ansible_python_interpreter=/usr/bin/python3

5. make changes in playbooks as given below,
cd ~/playbooks

sudo vi installJava.yml

---
- hosts: My_Group
  tasks:
    - name: Update APT package manager repositories cache
      become: true
      apt:
        update_cache: yes
    - name: Install Java using Ansible
      become: yes
      apt:
        name: "{{ packages }}"
        state: present
      vars:
        packages:
           - openjdk-11-jdk
6. Execute Ansible playbook
ansible-playbook installJava.yml

Friday, November 20, 2020

How to Fix The following signatures couldn’t be verified Error in Ubuntu Linux | The following signatures couldn’t be verified Error in Ubuntu Linux | Jenkins Key Download Error

GPG error: https://pkg.jenkins.io/debian-stable binary/ Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY FEF32E745F2C3D5
E: The repository 'http://pkg.jenkins.io/debian-stable binary/ Release' is not signed.

This is a very common Jenkins install issue on Ubuntu/Debian. It happens because APT no longer trusts the old Jenkins GPG key method.

Fix (Recommended Solution)

Step 1: Remove old Jenkins repo (if exists)

sudo rm -f /etc/apt/sources.list.d/jenkins.list sudo rm -f /etc/apt/sources.list.d/jenkins*.list

Step 2: Download and install Jenkins GPG key (new method)

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

✅ This stores the key in the recommended keyrings location

Step 3: Add Jenkins repository (signed-by)

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 

Step 4: Update & install Jenkins

sudo apt update sudo apt install jenkins -y 

✅ Error should now be gone 

Thursday, November 19, 2020

Slack Azure DevOps Integration | How to Integrate Slack with Azure DevOps | Send push notifications from Azure pipelines to Slack

Slack is popular collaboration tool used at work by many companies. Slack can be integrated with CI tools such as Jenkins, Azure DevOps(VSTS). We will see how to integrate with Azure Build pipelines. 

Pre-requisites:
1. Azure pipelines setup
2. Slack channel setup
 
Azure DevOps & Slack integration steps
 
1. Go to slack.com and sign in to workspace.
2. Go to channel where you would like to receive push notification from Azure DevOps. Click on more and Add apps



3. Add Visual Studio Team Services App

4. Add Configuration

5.  And then select channel to post notification and add the channel

6. In Azure DevOps Dashboard, click on Project settings.
7. Click on Service Hooks tab and then click create subscriptions button to add a new service hook.
 


8. Select Slack from the list of services, and press the Next button.


9. On the Trigger screen, choose the event that you’d like to trigger the notification, and add any filters. Press the Next button to continue.


10. On the Action screen, add the URL as the Slack Webhook URL and press Finish when you are done.
 
 

Now whenever Azure pipelines runs any builds, it will send push notifications to the Slack channel. Please watch the above steps in YouTube channel:

Complete AI-Enabled DevOps Learning Roadmap for 2026 | Skills Required to Become a Modern DevSecOps Engineer

🚀  AI-Enabled DevOps Engineer Roadmap for 2026 The future belongs to AI-Enabled DevOps Engineers, not AI-only Engineers. Strong DevOps fund...