Wednesday, March 16, 2022

Module 20: Deployment of Application with Jenkins

Deployment of  App into Kubernetes Cluster

We will learn how to automate Docker builds using Jenkins and Deploy into Kubernetes Cluster in AWS Cloud. We will use kubectl command to deploy Docker images into EKS 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 Elastic container registry
- Automating Deployments to Kubernetes Cluster using kubectl CLI plug-in





Module: 19 Helm 3

Helm is a tool for managing Kubernetes packages called charts. It is maintained by the CNCF - in collaboration with Microsoft, Google, Bitnami and the Helm contributor community.
chart is a collection of files that describe a related set of Kubernetes resources. With a single chart, you can run a basic application or something more complex, like a full web app inside the Kubernetes cluster. Helm charts help you define, install and upgrade even the most complex Kubernetes application.
What are the main differences between Helm 3 and the previous version?

One of the biggest differences between Helm 2 and Helm 3 is its architecture. Indeed, there is no more Tiller (the server-side component of Helm 2). Tiller was configured to have full access on all Kubernetes cluster. With this new architecture Helm 3 is more secure. Helm interacts directly with Kubernetes API server. The permissions are based on the Kubernetes config file. This means you can restrict user permissions

What is Helm and How to install Helm version 3?

Helm is a package manager for Kubernetes. Helm is the K8s equivalent of yum or apt. It accomplishes the same goals as Linux system package managers like APT or YUM: managing the installation of applications and dependencies behind the scenes and hiding the complexity from the user.

Why use Helm?

As the Kubernetes platform and ecosystem continued to expand, deploying one and only one Kubernetes configuration file (ie: a single YAML) was not the norm anymore. As number of K8S deployment files increased, how to manage those files? Helm solves that problem.


Helm Charts

Helm uses a packaging format called Charts. A Helm Chart is a collection of files that describe a set of Kubernetes resources. Helm Charts helps you define, install, and upgrade even the most complex Kubernetes application. Charts are easy to create, version, share, and publish.


Helm Kubernetes Integration

In helm 3 there is no tiller component. Helm client directly interacts with the Kubernetes API for the helm chart deployment.

Helm 3 can be installed many ways. We will install Helm 3 using scripts option.

Download scripts 
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3

provide permission
sudo chmod 700 get_helm.sh


Execute script to install
sudo ./get_helm.sh



 
Verify installation
helm version --client



Basic commands

To upload a chart to Kubernetes, you can use the following command:

let's do our custom application on the helm chart 

helm create [NAME] [CHART]


helm lint ./mychart/  #To check the file form 

helm install [NAME] [CHART]



If you want to upgrade your chart, use the following command and choose the release you want:


helm upgrade [RELEASE] [CHART]

Otherwise, you can do a rollback. If you do not specify the revision, it will rollback to the previous version.

helm rollback [RELEASE] [REVISION]

helm install --dry-run --debug ./mychart/ --generate-name

 helm install kelly-app mychart/
 kubectl get pods
 kubectl get svc
 helm ls
 kubectl get all
 cd mychart/
 ls
 vi Chart.yaml
 vi values.yaml
then cd templates 
vi deployment.yml and comment out livenessprobe and readinessprobe 


 cd ..
 helm ls
 helm upgrade kelly-app mychart/
 helm ls
 kubectl get all
 helm rollback kelly-app 1
 helm ls
 kubectl get pods


To retrieve a package from a package repository, and download it locally:

helm pull [CHART]

You have two commands to search:

helm search hub

This command search for charts in the Helm Hub.

helm search repo

These commands search repositories that you have added to your local helm client. This search is done over local data, and no public network connection is needed.

If you want to uninstall a release, here is the command:

helm uninstall [RELEASE]

Repository commands

You have a few commands to interact with chart repositories. A repository is a place where you can find and share charts.

You can add a chart repository:

helm repo add [NAME] [URL]

You can list chart repositories:

helm repo list | ls

You can remove a chart repository:

helm repo remove | rm [NAME]

And finally, you can get the latest information about charts from the respective chart repositories:

helm repo update

Monitoring commands

helm status [RELEASE]

This command shows you the status of your release. The status consists of:

  • last deployment time
  • kubernetes namespace in which the release lives
  • state of the release (can be: unknown, deployed, uninstalled, superseded, failed, uninstalling, pending-install, pending-upgrade or pending-rollback)
  • list of resources that this release consists of, sorted by kind
  • details on last test suite run, if applicable
  • additional notes provided by the chart

helm list | helm ls:

You can list all applications that have been installed in the cluster using helm.

helm history [RELEASE]

You can see with this command all the historical revisions for a given release.

Create your own chart

helm create [NAME]


helm package [CHART]

This command packages a chart into a versioned chart archive file.

helm lint [CHART]

This command takes a path to a chart and runs a series of tests to verify that the chart is well-formed.

With these commands, you are now able deploying your app on Kubernetes with Helm 3.



Module 18: How to Upgrade your Clusters

 Upgrading kubeadm clusters

This page explains how to upgrade a Kubernetes cluster created with kubeadm from version 1.22.x to version 1.23.x, and from version 1.23.x to 1.23.y (where y > x). Skipping MINOR versions when upgrading is unsupported.

To see information about upgrading clusters created using older versions of kubeadm, please refer to following pages instead:

The upgrade workflow at high level is the following:

  1. Upgrade a primary control plane node.
  2. Upgrade additional control plane nodes.
  3. Upgrade worker nodes.

Before you begin

  • Make sure you read the release notes carefully.
  • The cluster should use a static control plane and etcd pods or external etcd.
  • Make sure to back up any important components, such as app-level state stored in a database. kubeadm upgrade does not touch your workloads, only components internal to Kubernetes, but backups are always a best practice.
  • Swap must be disabled.

Additional information

  • The instructions below outline when to drain each node during the upgrade process. If you are performing a minor version upgrade for any kubelet, you must first drain the node (or nodes) that you are upgrading. In the case of control plane nodes, they could be running CoreDNS Pods or other critical workloads. For more information see Draining nodes.
  • All containers are restarted after upgrade, because the container spec hash value is changed.
  • To verify that the kubelet service has successfully restarted after the kubelet has been upgraded, you can execute systemctl status kubelet or view the service logs with journalctl -xeu kubelet.

Determine which version to upgrade to

Find the latest patch release for Kubernetes 1.23 using the OS package manager:

apt update
apt-cache madison kubeadm
# find the latest 1.23 version in the list
# it should look like 1.23.x-00, where x is the latest patch

Upgrading control plane nodes

The upgrade procedure on control plane nodes should be executed one node at a time. Pick a control plane node that you wish to upgrade first. It must have the /etc/kubernetes/admin.conf file.

Call "kubeadm upgrade"

For the first control plane node

  • Upgrade kubeadm:

# replace x in 1.23.x-00 with the latest patch version
apt-mark unhold kubeadm && \
apt-get update && apt-get install -y kubeadm=1.23.x-00 && \
apt-mark hold kubeadm

  • Verify that the download works and has the expected version:

    kubeadm version
    
  • Verify the upgrade plan:

    kubeadm upgrade plan
    

    This command checks that your cluster can be upgraded, and fetches the versions you can upgrade to. It also shows a table with the component config version states.

  • Choose a version to upgrade to, and run the appropriate command. For example:

    # replace x with the patch version you picked for this upgrade
    sudo kubeadm upgrade apply v1.23.x
    

    Once the command finishes you should see:

    [upgrade/successful] SUCCESS! Your cluster was upgraded to "v1.23.x". Enjoy!
    
    [upgrade/kubelet] Now that your control plane is upgraded, please proceed with upgrading your kubelets if you haven't already done so.
    
  • Manually upgrade your CNI provider plugin.

    Your Container Network Interface (CNI) provider may have its own upgrade instructions to follow. Check the addons page to find your CNI provider and see whether additional upgrade steps are required.

    This step is not required on additional control plane nodes if the CNI provider runs as a DaemonSet.

For the other control plane nodes

Same as the first control plane node but use:

sudo kubeadm upgrade node

instead of:

sudo kubeadm upgrade apply

Also calling kubeadm upgrade plan and upgrading the CNI provider plugin is no longer needed.

Drain the node

  • Prepare the node for maintenance by marking it unschedulable and evicting the workloads:

    # replace <node-to-drain> with the name of your node you are draining
    kubectl drain <node-to-drain> --ignore-daemonsets
    

Upgrade kubelet and kubectl

  • Upgrade the kubelet and kubectl:

# replace x in 1.23.x-00 with the latest patch version
apt-mark unhold kubelet kubectl && \
apt-get update && apt-get install -y kubelet=1.23.x-00 kubectl=1.23.x-00 && \
apt-mark hold kubelet kubectl

  • Restart the kubelet:

    sudo systemctl daemon-reload
    sudo systemctl restart kubelet
    

Uncordon the node

  • Bring the node back online by marking it schedulable:

    # replace <node-to-drain> with the name of your node
    kubectl uncordon <node-to-drain>
    

Upgrade worker nodes

The upgrade procedure on worker nodes should be executed one node at a time or few nodes at a time, without compromising the minimum required capacity for running your workloads.

Upgrade kubeadm

  • Upgrade kubeadm:

# replace x in 1.23.x-00 with the latest patch version
apt-mark unhold kubeadm && \
apt-get update && apt-get install -y kubeadm=1.23.x-00 && \
apt-mark hold kubeadm

Call "kubeadm upgrade"

  • For worker nodes this upgrades the local kubelet configuration:

    sudo kubeadm upgrade node
    

Drain the node

  • Prepare the node for maintenance by marking it unschedulable and evicting the workloads:

    # replace <node-to-drain> with the name of your node you are draining
    kubectl drain <node-to-drain> --ignore-daemonsets
    

Upgrade kubelet and kubectl

  • Upgrade the kubelet and kubectl:

# replace x in 1.23.x-00 with the latest patch version
apt-mark unhold kubelet kubectl && \
apt-get update && apt-get install -y kubelet=1.23.x-00 kubectl=1.23.x-00 && \
apt-mark hold kubelet kubectl

  • Restart the kubelet:

    sudo systemctl daemon-reload
    sudo systemctl restart kubelet
    

Uncordon the node

  • Bring the node back online by marking it schedulable:

    # replace <node-to-drain> with the name of your node
    kubectl uncordon <node-to-drain>
    

Verify the status of the cluster

After the kubelet is upgraded on all nodes verify that all nodes are available again by running the following command from anywhere kubectl can access the cluster:

kubectl get nodes

The STATUS column should show Ready for all your nodes, and the version number should be updated.

Recovering from a failure state

If kubeadm upgrade fails and does not roll back, for example because of an unexpected shutdown during execution, you can run kubeadm upgrade again. This command is idempotent and eventually makes sure that the actual state is the desired state you declare.

To recover from a bad state, you can also run kubeadm upgrade apply --force without changing the version that your cluster is running.

During upgrade kubeadm writes the following backup folders under /etc/kubernetes/tmp:

  • kubeadm-backup-etcd-<date>-<time>
  • kubeadm-backup-manifests-<date>-<time>

kubeadm-backup-etcd contains a backup of the local etcd member data for this control plane Node. In case of an etcd upgrade failure and if the automatic rollback does not work, the contents of this folder can be manually restored in /var/lib/etcd. In case external etcd is used this backup folder will be empty.

kubeadm-backup-manifests contains a backup of the static Pod manifest files for this control plane Node. In case of a upgrade failure and if the automatic rollback does not work, the contents of this folder can be manually restored in /etc/kubernetes/manifests. If for some reason there is no difference between a pre-upgrade and post-upgrade manifest file for a certain component, a backup file for it will not be written.

How it works

kubeadm upgrade apply does the following:

  • Checks that your cluster is in an upgradeable state:
    • The API server is reachable
    • All nodes are in the Ready state
    • The control plane is healthy
  • Enforces the version skew policies.
  • Makes sure the control plane images are available or available to pull to the machine.
  • Generates replacements and/or uses user supplied overwrites if component configs require version upgrades.
  • Upgrades the control plane components or rollbacks if any of them fails to come up.
  • Applies the new CoreDNS and kube-proxy manifests and makes sure that all necessary RBAC rules are created.
  • Creates new certificate and key files of the API server and backs up old files if they're about to expire in 180 days.

kubeadm upgrade node does the following on additional control plane nodes:

  • Fetches the kubeadm ClusterConfiguration from the cluster.
  • Optionally backups the kube-apiserver certificate.
  • Upgrades the static Pod manifests for the control plane components.
  • Upgrades the kubelet configuration for this node.

kubeadm upgrade node does the following on worker nodes:

  • Fetches the kubeadm ClusterConfiguration from the cluster.
  • Upgrades the kubelet configuration for this node.


Commands : Upgrade Kubernetes Cluster (Hands-On)

***************** Upgrade Control Plane Node ******************


1. Get the Running Node and Version

kubectl get nodes


2. Drain Master Node

kubectl drain <node-to-drain> --ignore-daemonsets


3. Upgrade kubeadm


4. Verify that the download works and has the expected version:

kubeadm version


5. Verify the upgrade plan

sudo kubeadm upgrade plan v1.20.1-00


6. Apply the Upgrade

sudo kubeadm upgrade apply v1.20.1


7. Upgrade kubelet and kubectl packages


8. Restart the kubelet:


10. Get the Running Node and Version

kubectl get nodes


11. Uncordon the Node

kubectl uncordon <node-to-uncordon>



***************** Upgrade Worker Node ******************


1. Drain Worker Node # do the below on master only#

kubectl drain <node-to-drain> --ignore-daemonsets --force

# do the following on worker nodes only#

2. Upgrade kubeadm


3. Verify that the download works and has the expected version:

kubeadm version

#sudo kubeadm upgrade  plan#

4. For worker nodes this upgrades the local kubelet configuration

sudo kubeadm upgrade node


5. Upgrade kubelet and kubectl packages


6. Restart the kubelet:

# do the below on master only#

7. Get the Running Node and Version

kubectl get nodes


8. Uncordon the Node

kubectl uncordon <node-to-uncordon>


uncordon it to bring it back on

 kubectl drain master  --ignore-daemonsets
 kubectl get nodes
 sudo apt-get update
 sudo apt-get install -y --allow-change-held-packages kubeadm=1.20.1-00
 kubeadm version
 sudo kubeadm upgrade plan v1.20.1-00
  sudo apt-get update
  
sudo apt-get install -y --allow-change-held-packages kubelet=1.20.1-00 kubectl=1.20.1-00
 sudo systemctl daemon-reload
 sudo systemctl restart kubelet
 kubectl get nodes
 kubectl uncordon master
 kubectl get nodes
 kubectl drain node1 --ignore-daemonsets --force
 kubectl get nodes


 

Install SonaType Nexus 3 on Ubuntu 24.0.4 - How to configure SonaType Nexus 3 on Ubuntu - Install Nexus on Ubuntu

SonaType Nexus3 is one of the popular binary repository managers, used for storing build artifacts such as Jars,WARs, EARs. It is Java based...