Category Archives: Uncategorized

How does Microservice Architecture work?

Explain the working of Microservice Architecture.

Microservice architecture is a distinctive method of developing software that focuses on building single-focused modules with well-defined interfaces and operations.

Let’s first see the components that make up microservices architecture.

Clients

Clients are applications requesting a service. These are consumers of the requests made to the microservice.

Identity providers

Identity providers provide the security mechanism, it authenticates every request a client makes.

API Gateway

An API gateway is responsible for request routing. It provides each of the clients with an API

Messaging formats

The client sends a request and waits for a response from the service, this response may come in two ways, asynchronous messaging or synchronous messaging. Synchronous messaging is a way clients have to wait for a response for every request made, but for asynchronous messaging, clients don’t need to wait for a response for a request made.

Databases

Databases are storage areas of the microservice. Every microservice owns its databases. For every microservice, there is a database I.e client management owns its database, so the implementation of each service is completely private from each other.

Static content

Content generated by the microservice is usually relayed back to the client through the content delivery network.

Management

Management is responsible for balancing the services and basically acts as a load balancer and it also identifies failures in every service.

Service discovery

Service discovery helps to pinpoint where each instance is located. It acts as a registry in which the address of all instances are tracked

The flow of microservices architecture operations

A client makes a request. The request is passed on to identity providers where the request is authenticated by the identity provider, if the request is valid, it is then passed on to the API gateway. The API gateway acts as an entry point for clients to send their requests to appropriate microservices. After the intended microservice has received the request, it is communicated back via massaging service. The API will receive the response and it then communicates static content via the cloud delivery network back to the client.

Explain the importance of reports and dashboards in microservices.

A Microservices dashboard is a simple application used to visualize links between microservices and the surrounding environment.

Reports and dashboards play a critical role to all the users of a microservice architecture, ranging from managers to analysts

Importance of dashboards and reports

The analysts, want to know what functionalities are out there to avoid any duplications. This plays out whenever a new feature is required. Analysts will check whether that feature is already available if it’s not available they give a go-ahead to implement the feature.

To the managers, they want a level of change, they want to see a sense of compliance, and every application needs to conform to the set-out rules to serve the purpose intended for.

Developers ought to define their release schedules, this will help the managers know when to expect a particular product to go live and therefore are in a position to inform the public whenever questions arise.

All of these are possible with a smart dashboard with a timeline and on top of it you are in a position to have reported on every event happening such as:

  • One can see which resources are exposed by what microservices
  • You are in a position to see the version of the component being deployed
  • You can see the components which meet the compliance needs
  • Services are impacted whenever there is a failure in a certain component.

What is a Container in Microservices?

A container is a bundling of an application and all its dependencies as a package that allows it to be deployed easily and consistently regardless of the environment.

Containers solve the problem of how to get the software to run reliably when moved from one computing environment to another, eg when moved to the cloud or a local test environment on a different computer. A container will abstract any difference in operating systems distribution and underlying infrastructure.

Containers share an OS kernel so they are more lightweight and boot faster using less memory.

So the importance of having to use containers in a Microservice architecture way more effort because:

  • Containers can improve the deployment time of the applications.
  • Container deployments can’t affect other applications’ performance when being deployed.
  • Developers can replace, move or replicate containers with fair minimal effort.

Distinguishing features between Monolithic, Microservices Architecture, and SOA

A monolithic application is one application built with a single interface and a single code base. Scaling a monolithic app is a challenge considering you will have to change the whole code base to implement a single feature.

The microservice architecture breaks the application into smaller, manageable codes independent from each other, you can implement and scale each service without affecting the whole system. The code base is not dependent on each other which makes scaling so easy to carry out.

Service Oriented Architecture (SOA) is an enterprise-wide approach to application development of components that takes advantage of reusable software components or services. The service interface provides loose coupling, meaning they can be called with little or no knowledge of how the integration is implemented underneath.

Distinguishing features

  • For Microservices, services exist as independently deployable artifacts and can be scaled independently, while for SOA, dependencies between services and reusable sub-components introduce scalability challenges, and for a monolithic application, scaling is always a challenge.
  • Microservices design their services into smaller units expressed with their business API, but for SOA design, services can range from small to bigger units which sometimes become difficult to manage. Monolithic applications evolve into a huge size causing challenges to their maintainers.

How to install Streamlit on Ubuntu 22.04

In this tutorial, we are going to learn how to install Streamlit on Ubuntu 22.04

Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science. This is a faster way to build and share data apps. Streamlit turns data scripts into shareable web apps in minutes.

You don’t need to write the backend or frontend to define Streamlit, you only need to add widgets which is the same as declaring variables. This makes it easy to use and also deploy. Deploying your app is just a click of a button and your application is in the cloud.

Prerequisites

  • Have PIP installed
  • Python 3.7 and above
  • IDE

Something to note here is, have a virtual environment installed so that Streamlit doesn’t impact any other projects you might be working on.

Install Streamlit on Ubuntu 22.04

1. Create a virtual environment

First, we need to create a virtual environment so that we can separate our Streamlit work from others. To create a virtual environment using the following command. First create a project directory.

$ sudo mkdir streamlit 
$ cd streamlit
$ python3 -m venv env # env is the name of the virtual environment.

After creating a virtual environment, you can then open this project from your favorite IDE or you can continue from the terminal.

To activate the virtual environment, use the following command.

$ source env/bin/activate

If it happens you don’t have pip installed, you can do install using the following command.

sudo apt-get install python3-pip

After this we need to install pipenv on our virtual environment.

$ pip3 install pipenv

This will create pipfile in your workspace.

2. Install streamlit

To install Streamlit, use the following command on your terminal.

pip install streamlit

You will be in a position to see an output such as this one.

Collecting streamlit
  Downloading streamlit-1.12.2-py2.py3-none-any.whl (9.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.1/9.1 MB 118.6 kB/s eta 0:00:00
Collecting protobuf<4,>=3.12
  Downloading protobuf-3.20.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 88.4 kB/s eta 0:00:00
Collecting blinker>=1.0.0
  Downloading blinker-1.5-py2.py3-none-any.whl (12 kB)
Collecting python-dateutil
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting importlib-metadata>=1.4
  Downloading importlib_metadata-4.12.0-py3-none-any.whl (21 kB)
Collecting packaging>=14.1
  Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting watchdog
  Downloading watchdog-2.1.9-py3-none-manylinux2014_x86_64.whl (78 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 78.4/78.4 KB 202.5 kB/s eta 0:00:00

We can test whether the installation worked, let’s create an hello file from the terminal

streamlit hello

This will automatically open a web browser on localhost:8501.

Nextgentips: Streamlit hello interface

Create Streamlit App

Let’s create a simple example. Paste the following code on your browser and name it app.py

import streamlit as st

st.write('Hello world')

To run this code go to your terminal and type streamlit run app.py

streamlit run app.py

The output will be an hello world.

Let’s find the square of different numbers.

x = st.slider('x')
st.write(x, 'squared is', x * x)

You will get a slider as an output.

Nextgentips: slider squared

Conclusion

We have successfully installed Streamlit on ubuntu 22.04 and we have learned how to create sliders. This is a very important tool when it comes to data science. Feel free to consult Streamlit documentation.

How to install Monit on Arch Linux

In this tutorial, we will be learning how to install Monit on Arch Linux.

Monit is a small open-source utility for managing and monitoring Unix systems. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

With Monit, systems status can be viewed directly from the command line, or via the native HTTP(s) webserver. It is able to do automatic maintenance, repair, and run meaningful causal actions in error situations.

Uses of Monit.

Monit can have many use cases because of its versatility. Let’s look at the following:

  • Monit can be used to monitor files and directories, Monit monitor this system for any changes such as file timestamp, security changes or file size changes, etc.
  • Monit acts proactively, whereby in case of an error in the system functionality, Monit can send an alert to inform that such a process wasn’t complete.
  • Monit can be used to test programs and scripts such as cron jobs do. You can monitor and check the scripts you want to use in any scenario.
  • We use Monit to monitor any daemon processes or any other process running on the localhost such as sshd, Nginx, or MySQL processes.
  • Monit can be used to monitor general system functionality such as CPU and RAM usage.
  • It can be used to monitor network connections on various servers because Monit has built-in tests for Internet protocols such as SMTP, and HTTP.

Install Monit on Arch Linux

1. Update system repositories.

The first thing, to begin with, is to update repositories in order to make them up to date.

sudo pacman -Syy

2. Install Monit in Arch Linux

To install Monit on Arch Linux we can use the following command pacman -S monit.

sudo pacman -S monit

You should be in a position to see the following from the terminal.

resolving dependencies...
looking for conflicting packages...

Packages (1) monit-5.32.0-1

Total Download Size:   0.35 MiB
Total Installed Size:  0.92 MiB

:: Proceed with installation? [Y/n] y
:: Retrieving packages...
 monit-5.32.0-1-x...   362.0 KiB  1466 KiB/s 00:00 [###################] 100%
(1/1) checking keys in keyring                     [###################] 100%
(1/1) checking package integrity                   [###################] 100%
(1/1) loading package files                        [###################] 100%
(1/1) checking for file conflicts                  [###################] 100%
(1/1) checking available disk space                [###################] 100%
:: Processing package changes...
(1/1) installing monit                             [###################] 100%
:: Running post-transaction hooks...
(1/2) Reloading system manager configuration...
(2/2) Arming ConditionNeedsUpdate...

3. Configuring Monit service

After the installation, we need to enable and start the services.

To enable Monit service use the following command.

sudo systemctl enable monit 

To start Monit service you the following command.

sudo systemctl start monit

To check the status of Monit, use the following command.

sudo systemctl status monit 

If you see the following status, then it is working perfectly.

● monit.service - Pro-active monitoring utility for unix systems
     Loaded: loaded (/usr/lib/systemd/system/monit.service; enabled; preset: di>
     Active: active (running) since Sat 2022-09-03 05:14:21 UTC; 11s ago
       Docs: man:monit(1)
             https://mmonit.com/wiki/Monit/HowTo
   Main PID: 408 (monit)
      Tasks: 2 (limit: 4690)
     Memory: 1.4M
        CPU: 16ms
     CGroup: /system.slice/monit.service
             └─408 /usr/bin/monit -I

Sep 03 05:14:21 localhost systemd[1]: Started Pro-active monitoring utility for>
Sep 03 05:14:21 localhost monit[408]:  New Monit id: 8f8a6e8053c2a85c3639baea87>
                                       Stored in '/root/.monit.id'
Sep 03 05:14:21 localhost monit[408]: Starting Monit 5.32.0 daemon with http in>
Sep 03 05:14:21 localhost monit[408]: 'localhost' Monit 5.32.0 started

How to perform CRUD operations in Django

In this tutorial, we will learn how to operate CRUD functionalities in Django. CRUD functionalities are Create, Read, Update and Delete. These are the main building blocks of any project. At some point you need to perform read operations from the database, you want to do an update or you wish to delete an item from the database. All these are possible thanks to CRUD operations.

In this tutorial, we are building on top of these lectures on How to set up a Python Django Application using Django 4.0 and How to create Django Templates. So make sure to check out first.

If you want to follow along make sure you have the project setup complete, the templates are working fine, the database is up and running and lastly have some data on the database.

Steps to follow

1. Create a Project

To create a project go to your terminal and create a folder name your project whatever you like, the create a virtual environment where all your installations will be staying. To create a virtual environment do the following.

$ mkdir crud_example
$ cd crud_example
#create a virtual environment 
$ python3 -m venv env #env is the name of the virual environment.

To make your work easier open the containing folder in your favorite text editor, for me I will be using Pycharm. Activate the virtual environment as created above.

$ source env/bin/activate

2. Install Django

To install Django in your project, use the following command.

$ pip install django

After Django has been installed make sure you do pip freeze > requirements.txt in order to store all your installed files. This requiremets.txt file is necessary so that someone else who will come back later and want to use your project will just install the requirements from one file.

$ pip freeze > requirements.txt

3. Create a Django project and app

The next step is to create a Django project and an app. To create a project using the following command.

$ django-admin startproject myproject .

Make sure to include the period in the end. It tells Django to create a project in the current directory.

After the project has been created, we need to create an app. To do so use the following command.

$ python manage.py startapp project

When the app has been created, go to the settings.py file and add your app to the installed apps

#installed apps 
...
'project.apps.ProjectConfig',

Still inside the settings.py file create a database of your choice, be it Mysql, PostgreSQL, or db sqlite. For my case, I will be using PostgreSQL. You may want to leave db sqlite and use it for testing purposes, but you will need PostgreSQL or MySQL for production.

4. Create PostgreSQL database

To use PostgreSQL, we need to create it first in our system and then tell django to use it. To create a Postgresql do the following.

Open psql as Postgres user

sudo -u postgres psql
postgres=# 

To create a database, we use the following command inside postgres.

CREATE DATABASE project_db;

Project is the name of the database.

After creating the database, create the user

CREATE USER projectuser WITH PASSWORD 'strpassword';

Then we need to grant privileges to the user created.

GRANT ALL PRIVILEGES ON DATABASE project_db TO projectuser;

Lastly quit the Postgres with \q

We now need to go back to our project and inside settings.py look for databases and change from db sqlite to PostgreSQL

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'project_db',
        'USER': 'projectuser',
        'PASSWORD': 'strpasswrd',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Before we can connect to PostgreSQL, we need to install psycopg2.

pip install psycopg2-binary

Add it to the requirements.txt file

Create superuser

To create a superuser in Django we use the following command. Make sure to follow the prompts in order to create a user, email, and password

python manage.py createsupeuser

Add some data into the tables to play with.

5. Views, models, and Urls

Let’s start with creating models. Models give us the data and how our database will be structured. For this, I will use a simple example.

#models 
from django.db import models
import uuid

class Project(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField(null=True, blank=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):
        return self.title

After this make sure you run makemigrations and migrate in order to populate the database.

$ python manage.py makemigrations
$ python manage.py migrate

Create urls.py and populate

We need to create a urls.py file inside our app because Django doesn’t create it for us.

Your project/urls.py file should look like this

from django.urls import path
from . import views

urlpatterns = [
]

For the main myproject/urls.py it should look like this

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('project.urls'))# add this to serve the app urls 
]

Create Views

This is the engine of your project. To create views use the following

from django.shortcuts import render

def index(request):
    context = {   
    }
    return render(request, 'project/index.html', context)

5. Django Templates

From this part, we need to create templates and from the beginning, I had cover templates. How to create Django Templates.

Create a templates folder and add all the HTML in it. Create an index.html, projectfile.html, and delete.html files.

6. CRUD functionalities

Read Functionality

We can start with Read functionalities. Let’s display what is in the database in our home.

Remember the view we had created earlier, we are going to work from there. To understand more make sure you play with Django ORM.

This will be our final views.py for Read operations.

from django.shortcuts import render
from .models import Project

def index(request):
    projects = Project.objects.all()# fetch all the data from the database

    context = {
        'projects': projects
    }
    return render(request, 'project/index.html', context)

Create functionality

To use create functionality, we need to have a form where we input our data. So now head over to our app and add forms.py file and populate it with the following:

from django.forms import ModelForm

from app.models import Project


class ProjectForm(ModelForm):
    class Meta:
        model = Project
        fields = '__all__'
       

Then go to views.py file and add this also

def createProject(request):
    form = ProjectForm()

    if request.method == 'POST':
        form = ProjectForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
    context = {
        'form': form
    }
    return render(request, 'project/projectfile.html', context)

Create urls to serve this view like this:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='home'),
    path('create/', views.createProject, name='create-file'),
]

Create Projectfile.html and populate with this:

{% csrf_token %} {{ form.as_p }}

Update functionality

We are still going to use the form we created earlier. What is different only is the addition of a primary key to help us know the section we are updating.

On views.py file add the following

def updateProject(request, pk):
    project = Project.objects.get(id=pk)
    form = ProjectForm(instance=project)

    if request.method == 'POST':
        form = ProjectForm(request.POST, instance=project)
        if form.is_valid():
            form.save()
            return redirect('home')

    context = {
        'form': form
    }
    return render(request, 'project/projectfile.html', context)

Here I am still using the same projectfile.html file because its the same form am working on.

On project/urls.py file add this URL link.

path('update//', views.updateProject, name='update-file'),

Inside index.html file we need to add the edit link so that we can redirect to where we need to edit.

This should be the final index.html file.

Projects

{% for project in projects %} # looping inside the function in django using tags {% endfor %}
Title Description Action Action
{{project.title }} {{project.description }} Edit Delete

Delete functionality

Delete functionality is the last and easiest of the three above. What we need to have is the deletefile.html and delete project view.

Let’s start with the deletefile.html. Populate it with the following.

Delete

{% csrf_token %}

Are you sure you want to delete "{{object}}"?

Go back# using url tags

Our URL is this.

path('delete//', views.deleteProject, name='delete-file')

Create a delete view like this

def deleteProject(request, pk):
    project = Project.objects.get(id=pk)
    if request.method == 'POST':
        project.delete()
        return redirect('home')
    context = {
        'object': project
    }

    return render(request, 'project/deletefile.html', context)

Conclusion

We have successfully implemented the CRUD operation in Django. In case of any difficulty refer to my Github repo for the full codebase.

How to install Monit 5 on Fedora 36

In this tutorial, we are going to learn how to install Monit 5 on Fedora 36.

Monit is a small open source utility for monitoring and managing Unix systems. It performs automatic maintenance whenever there is downtime in your system.

Uses of Monit.

Monit can have many use cases because of its versatility. Let’s look at the following:

  • Monit can be used to monitor files and directories, Monit monitor this system for any changes such as file timestamp, security changes or file size changes, etc.
  • Monit acts proactively, whereby in case of an error in the system functionality, Monit can send an alert to inform that such a process wasn’t complete.
  • Monit can be used to test programs and scripts such as cron jobs do. You can monitor and check the scripts you want to use in any scenario.
  • We use Monit to monitor any daemon processes or any other process running on the localhost such as sshd, Nginx, or MySQL processes.
  • Monit can be used to monitor general system functionality such as CPU and RAM usage.
  • It can be used to monitor network connections on various servers because Monit has built-in tests for Internet protocols such as SMTP, and HTTP.

How to Install Monit 5 on Fedora 36

1. Update system repositories.

Before you begin any installation, make sure you update all the system packages in order to make them up to date. To update Fedora use sudo dnf update.

$ sudo dnf update

Follow the prompts in order to complete updates successfully.

2. Install Monit 5 on Fedora 36

Monit is readily available on the Fedora packages, so to install you use the following command sudo dnf install monit.

$ sudo dnf install monit 

You should be in a position to see the following output.

Fedora Modular 36 - x86_64 - Updates             12 kB/s |  15 kB     00:01    
Dependencies resolved.
================================================================================
 Package        Architecture    Version                  Repository        Size
================================================================================
Installing:
 monit          x86_64          5.32.0-1.fc36            updates          391 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 391 k
Installed size: 990 k
Is this ok [y/N]: y
Downloading Packages:
monit-5.32.0-1.fc36.x86_64.rpm                  949 kB/s | 391 kB     00:00    
--------------------------------------------------------------------------------
Total                                           619 kB/s | 391 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : monit-5.32.0-1.fc36.x86_64                             1/1 
  Running scriptlet: monit-5.32.0-1.fc36.x86_64                             1/1 
  Verifying        : monit-5.32.0-1.fc36.x86_64                             1/1 

Installed:
  monit-5.32.0-1.fc36.x86_64                                                    

Complete!

3. Configure Monit service

To configure Monit to start on boot we use the following command sudo systemctl enable monit

$ sudo systemctl enable monit

To start Monit immediately we use the following command.

$ sudo systemctl start monit 

To check the status of Monit we use the following command.

$ sudo systemctl status monit 
● monit.service - Pro-active monitoring utility for unix systems
     Loaded: loaded (/usr/lib/systemd/system/monit.service; enabled; vendor pre>
     Active: active (running) since Thu 2022-08-11 08:00:10 UTC; 12s ago
       Docs: man:monit(1)
             https://mmonit.com/wiki/Monit/HowTo
   Main PID: 15788 (monit)
      Tasks: 2 (limit: 1113)
     Memory: 2.0M
        CPU: 29ms
     CGroup: /system.slice/monit.service
             └─ 15788 /usr/bin/monit -I

Aug 11 08:00:10 localhost.localdomain systemd[1]: Started monit.service - Pro-a>
Aug 11 08:00:10 localhost.localdomain monit[15788]:  New Monit id: d45a290ce0d6>
                                                     Stored in '/root/.monit.id'
Aug 11 08:00:10 localhost.localdomain monit[15788]: Starting Monit 5.32.0 daemo>
Aug 11 08:00:10 localhost.localdomain monit[15788]: 'localhost.localdomain' Mon>

There are packages installed with Monit automatically. To check those files you can use the following command rpm -ql monit.

$ sudo rpm -ql monit
/etc/monit.d
/etc/monitrc
/usr/bin/monit
/usr/lib/.build-id
/usr/lib/.build-id/9a
/usr/lib/.build-id/9a/670fe3396cb159ea1c384f1f673a420b3691ed
/usr/lib/systemd/system/monit.service
/usr/share/doc/monit
/usr/share/doc/monit/CHANGES
/usr/share/doc/monit/COPYING
/usr/share/man/man1/monit.1.gz

NB: Prefix sudo whenever you are working as a regular user.

Conclusion

We have successfully installed and configured Monit on Ubuntu 22.04. For further reference check the Monit documentation.

How to install Prometheus Server on Ubuntu 22.04

In this guide, we will learn how to install the Prometheus server on Ubuntu 22.04. Prometheus is an open-source system monitoring and alerting toolkit. Prometheus collects and stores its metrics as time-series data. Metrics information is stored with the timestamp at which it was recorded, alongside optional key-value pairs called labels.

And now you are wondering what are metrics? Metrics are numeric measurements and time-series mean that the changes are recorded over time.

Install Prometheus on Ubuntu 22.04

1. Update the system repositories.

Let us update the system first in order to make the repositories up to date.

$ sudo apt update && apt upgrade -y

2. Download and install Prometheus

Download the latest release of Prometheus into your system from the Prometheus download page.

$ wget https://github.com/prometheus/prometheus/releases/download/v2.37.0-rc.0/prometheus-2.37.0-rc.0.linux-amd64.tar.gz

After the download is complete, the next step is to extract the archive:

$ tar xvfz prometheus-*.tar.gz

For convenience, we can change the directory for the extracted archive

$ cd prometheus-*

From there we can create a configuration file directory

$ sudo mkdir -p /etc/prometheus

Then we can create the data directory

$ sudo mkdir -p /var/lib/prometheus

Let’s now move the binary files Prometheus and promtool to /usr/local/bin/

Use the following code snippet

$ sudo mv prometheus promtool /usr/local/bin/

Let’s use a Prometheus.yaml file that we can use as an example to see how Prometheus monitors your infrastructure. This file will only monitor itself for now.

$ sudo vi prometheus.yml

you will see the following contents from prometheus.yaml file

global:
  scrape_interval:     15s
  external_labels:
    monitor: 'codelab-monitor'
scrape_configs:
   scraped from this config.
  - job_name: 'prometheus'
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

Move the template configuration file prometheus.yml to /etc/prometheus/ directory

$ sudo mv prometheus.yml /etc/prometheus/prometheus.yml

We have installed Prometheus. Let’s verify the version

$ prometheus --version
prometheus, version 2.37.0-rc.0 (branch: HEAD, revision: 2479fb42f0a9bb45c2e82f11efc73c0a75fc492c)
  build user:       root@b82e7804c2b2
  build date:       20220705-13:33:30
  go version:       go1.18.3
  platform:         linux/amd64

3. Create Prometheus System Group

The following code can help create the Prometheus system group.

$ sudo groupadd --system prometheus

We can then create a Prometheus system user and assign the primary group created

$ sudo useradd -s /sbin/nologin --system -g prometheus prometheus

Set the ownership of Prometheus files and data directories to the Prometheus group and user.

$ sudo chown -R prometheus:prometheus /etc/prometheus/  /var/lib/prometheus/

$ sudo chmod -R 775 /etc/prometheus/ /var/lib/prometheus/

Lastly, we can create a systemd service to run at boot time

$ sudo nano /etc/systemd/system/prometheus.service

Add the following content to prometheus.service

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Restart=always
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file=/etc/prometheus/prometheus.yml \
    --storage.tsdb.path=/var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries \
    --web.listen-address=0.0.0.0:9090

[Install]
WantedBy=multi-user.target

Start the Prometheus service

$ sudo systemctl start prometheus

Now we can check if our Prometheus is up and running

$ sudo systemctl status prometheus
● prometheus.service - Prometheus
     Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor p>
     Active: active (running) since Sat 2022-07-09 06:36:04 UTC; 32s ago
   Main PID: 13447 (prometheus)
      Tasks: 6 (limit: 1033)
     Memory: 17.9M
        CPU: 86ms
     CGroup: /system.slice/prometheus.service
             └─13447 /usr/local/bin/prometheus --config.file=/etc/prometheus/pr>

Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.386Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.389Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.390Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.390Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.392Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.392Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.393Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.394Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.394Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.395Z caller>
lines 1-20/20 (END)

Add firewall rules to allow Prometheus to run on TCP port 9090

$ ufw allow 9090/tcp

Access your server from the browser.

http://or:9090
nextgentips: Prometheus dashboard

Conclusion

Congratulations! you have successfully installed Prometheus on Ubuntu 22.04. To read more consult Prometheus documentation.

How to install Fail2ban on Ubuntu 22.04

Fail2ban is an intrusion prevention software that protects computer servers from brute-force attacks. It can run on POSIX systems that have an interface to a packet-control system or has a firewall installed locally.

In this tutorial, we will learn how to install Fail2ban on Ubuntu 22.04.

How Fail2ban works is that it scans files such as /var/log/auth.log and bans IP addresses conducting too many failed login attempts. It updates firewall system rules and blocks out all those IPs trying to log in with the wrong credentials.

Install Fail2ban on Ubuntu 22.04

Fail2ban most probably comes preinstalled on your server, but if not there you can install using the following steps.

1. Update system repositories.

To make our systems repositories up to date, use the following command.

$ sudo apt update && upgrade -y

2. Install Fail2ban

After the updates are complete, we can go ahead and install fail2ban. Use the following command to do so.

sudo apt install fail2ban

You should be in a position to see the following from your terminal

#output
The following additional packages will be installed:
  python3-pyinotify whois
Suggested packages:
  mailx monit sqlite3 python-pyinotify-doc
The following NEW packages will be installed:
  fail2ban python3-pyinotify whois
0 upgraded, 3 newly installed, 0 to remove and 182 not upgraded.
Need to get 473 kB of archives.
After this operation, 2,486 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

3. Configuring Fail2ban

To configure Fail2ban, we can edit two files, fail2ban.local and jail.local files.

Fail2ban configurations reside in fail2ban.conf file and you are not advised to modify them. What you can do is to copy the contents of fail2ban.conf file and create another file called fail2ban.local file which you can then edit. to do so use the following command.

cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.local

Now we need to open fail2ban.local file on a text editor.

sudo nano /etc/fail2ban/fail2ban.local

Here you don’t have to change anything for now but whenever you need to make any change, always do it at .local file

Jail.local file have configuration options such as, ignoreip where you can set a range of IPs to be ignored.

Bantime use to set the amount of time an IP address is banned from accessing the server. It is 600 seconds.

Maxretry use to set the number of failures before an IP address is banned. It is 5 trials.

Findtime use to set the time in which the host should not use up the maxretry number in order not to get banned (generically set to 10 minutes).

Backend allows you to set the backend configurations for file modifications. The default is always auto.

So to create a jail.local file use the following command.

cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Now you can open in your favorite text editor with the following command.

sudo nano jail.local

After doing your configurations remember to restart the fail2ban service. For me I haven’t started the service, so let’s start fail2ban service.

$ sudo systemctl start fail2ban
$ sudo systemctl status fail2ban
● fail2ban.service - Fail2Ban Service
     Loaded: loaded (/lib/systemd/system/fail2ban.service; disabled; vendor pre>
     Active: active (running) since Sat 2022-07-02 11:16:49 EAT; 9s ago
       Docs: man:fail2ban(1)
   Main PID: 27843 (fail2ban-server)
      Tasks: 5 (limit: 9355)
     Memory: 11.9M
        CPU: 123ms
     CGroup: /system.slice/fail2ban.service
             └─27843 /usr/bin/python3 /usr/bin/fail2ban-server -xf start

Jul 02 11:16:49 zx-pc systemd[1]: Started Fail2Ban Service.
Jul 02 11:16:50 zx-pc fail2ban-server[27843]: Server ready

Fail2ban is now running, you can now make your desired configurations.

To check the version of installed Fail2ban use the following command.

fail2ban-client version
0.11.2

Conclusion

Congratulations you have successfully installed Fail2ban. For more information consult Fail2ban documentation.