Initial commit

This commit is contained in:
Digital Studium
2023-03-08 18:12:15 +03:00
commit 06eb6898c7
23 changed files with 880 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
---
title: "Linux: How to create LVM logical volume"
date: "2022-05-15"
---
### First step: creating a physical volume
After you have attached the disk to a physical server or virtual machine, you need to type
this command:
```bash
sudo fdisk -l
```
<!--more-->
to make sure the drive is recognized by the operating system, and to identify the drive name. Output
of command will be something like this:
```plaintext
Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes
```
Once you have identified the drive name (in our case it is `/dev/vdb`), you can create
physical volume using the command:
```bash
sudo pvcreate /dev/vdb
```
You will see output like this:
```plaintext
kostya@ubuntu-21-04:~$ sudo pvcreate /dev/vdb
Physical volume "/dev/vdb" successfully created.
kostya@ubuntu-21-04:~$
```
### Step two: create the volume group
Now we need to create a volume group. This is done by the following command:
```bash
sudo vgcreate {vgname} {pvname}
```
In our case, the command will look like this:
```bash
sudo vgcreate vg-example /dev/vdb
```
The command output will look like this:
```plaintext
kostya@ubuntu-21-04:~$ sudo vgcreate vg-example/dev/vdb
Volume group "vg-example" successfully created
kostya@ubuntu-21-04:~$
```
### Step three: creating the logical volume
Creating a logical volume can be done with the following command:
```bash
sudo lvcreate --size {size} --name {lv-name} {vg-name}
```
In our case, it will be:
```bash
sudo lvcreate --size 5G --name lv-example vg-example
```
You will see output like this:
```plaintext
kostya@ubuntu-21-04:~$ sudo lvcreate --size 5G --name lv-example vg-example
Logical volume "lv-example" created.
kostya@ubuntu-21-04:~$
```
If you want the logical volume to use all the free space in the volume group, then run the command:
```bash
sudo lvcreate --extents 100%FREE --name lv-example vg-example
```
### Fourth step: creating the filesystem
To create an xfs filesystem, type the command:
```bash
sudo mkfs.xfs /dev/vg-example/lv-example
```
The command output will look like this:
```plaintext
kostya@ubuntu-21-04:~$ sudo mkfs.xfs/dev/vg-example/lv-example
meta-data =/dev/vg-example/lv-example isize = 512 agcount = 4, agsize = 327680 blks
= sectsz = 512 attr = 2, projid32bit = 1
= crc = 1 finobt = 1, sparse = 1, rmapbt = 0
= reflink = 1 bigtime = 0
data = bsize = 4096 blocks = 1310720, imaxpct = 25
= sunit = 0 swidth = 0 blks
naming = version 2 bsize = 4096 ascii-ci = 0, ftype = 1
log = internal log bsize = 4096 blocks = 2560, version = 2
= sectsz = 512 sunit = 0 blks, lazy-count = 1
realtime = none extsz = 4096 blocks = 0, rtextents = 0
Discarding blocks ... Done.
kostya@ubuntu-21-04:~$
```
To create an ext4 filesystem, replace the `mkfs.xfs` command with `mkfs.ext4`
### Step Five: Mount the Logical Volume
For example, suppose you want to mount the newly created logical volume to the `/opt` folder. In this
case, add this line to file `/etc/fstab`:
```bash
/dev/vg-example/lv-example /opt xfs defaults 0 1
```
After that, type the command:
```bash
sudo mount -a
```
You can verify that the logical volume has been mounted successfully using the command:
```bash
df -h /opt
```
The output should be like this:
```plaintext
kostya@ubuntu-21-04:~$ df -h /opt/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg--random-lv--random 5.0G 68M 5.0G 2% /opt
kostya@ubuntu-21-04:~$
```

View File

@@ -0,0 +1,84 @@
---
title: "Linux: How to extend LVM volume"
date: "2022-05-15"
---
### Situation 1: new disk
#### First step: creating a physical volume
After you have attached the disk to a physical server or virtual machine, you need to type command:
```bash
sudo fdisk -l
```
<!--more-->
This is to make sure the drive is recognized by the operating system, and to identify the drive name. Output of the command will be something like this:
```plaintext
Disk /dev/vdc: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes
```
Once you have identified the drive name (in our case it is `/dev/vdc`), you can create physical volume using the command:
```bash
sudo pvcreate /dev/vdc
```
You will see output like this:
```plaintext
kostya@ubuntu-21-04:~$ sudo pvcreate /dev/vdc
Physical volume "/dev/vdc" successfully created.
kostya@ubuntu-21-04:~$
```
#### Step two: extend the volume group
You can now extend the volume group. This is done by the following command:
```bash
sudo vgextend {vg-name} {pv-name}
```
In our case, it will be:
```bash
sudo vgextend vg-example /dev/vdc
```
You will see output like this:
```plaintext
kostya@ubuntu-21-04:~$ sudo vgextend vg-example /dev/vdc
Physical volume "/dev/vdc" successfully created.
Volume group "vg-example" successfully extended
kostya@ubuntu-21-04:~$
```
#### Step three: extending the logical volume
Extending a logical volume can be done with the following command:
```bash
sudo lvextend --size + {size} {vg-name/lv-name}
```
In our case, it will be:
```bash
sudo lvextend --size +2G vg-example/lv-example
```
You will see output like this:
```plaintext
kostya@ubuntu-21-04:~$ sudo lvextend --size +2G vg-example/lv-example
Size of logical volume vg-example/lv-example changed from 5.00 GiB (1280 extents) to 7.00 GiB (1792 extents).
Logical volume vg-example/lv-example successfully resized.
kostya@ubuntu-21-04:~$
```
If you want the logical volume to use all the free space in the volume group, then type
command:
```bash
sudo lvextend --extents +100%FREE vg-example/lv-example
```
#### Fourth step: extending the file system
If you have xfs file system, then the extending can be done with the following command:
```bash
sudo xfs_growfs /dev/{vg-name}/{lv-name}
```
In our case, it will be:
```bash
sudo xfs_growfs /dev/vg-example/lv-example
```
For ext4 filesystem, replace `xfs_growfs` with `resize2fs`
### Situation 2: if the size of the existing disk has changed
Sometimes the size of an existing disk can change, for example, in case of a virtual machine. In this case, the first step will be different, the second step will not be performed, and the rest of the steps will be pretty the same as in the situation with a new disc described above. The first step is not to create a physical volume, but to resize the existing one. It can be done with the command:
```bash
sudo pvresize /dev/DISKNAME
```
For example,
```bash
sudo pvresize /dev/vdc
```

View File

@@ -0,0 +1,60 @@
---
title: "Linux: how to limit /var/log folder size"
date: "2022-06-16"
---
Sometimes the `/var/log` folder grows so large that it causes a shortage of disk space. How to limit the size of this folder? By following the two steps in this article, you can control the size of the `/var/log` folder.<!--more-->
### Step 1. Limiting the size of journald logs
The logs of all systemd services are added to the `/var/log/journal/` folder by the `journald` service. To set the size limit for this folder, run the following commands:
```bash
sudo bash -c 'echo "SystemMaxUse=100M" >> /etc/systemd/journald.conf'
sudo systemctl restart systemd-journald
```
Instead of the `100M` size, you can specify any other size, in `K, M, G, T` units. After the above commands, you can verify that the size of the folder `/var/log` has become the specified size using the command:
```bash
du -sh /var/log/journal/
```
### Step 2. Limiting the number of log files stored by logrotate
Logrotate rotates almost all log files in the `/var/log` folder every day. For example, if I type the command `ls /var/log/kern*`, then I will see that in addition to the file `/var/log/kern.log` I have 4 more files stored that logrotate saved:
```bash
ls /var/log/kern*
```
```plaintext
/var/log/kern.log /var/log/kern.log.2.gz /var/log/kern.log.4.gz
/var/log/kern.log.1 /var/log/kern.log.3.gz
```
To limit the number of log files, edit the file `/etc/logrotate.d/rsyslog`. Looking at the contents of the `/etc/logrotate.d/rsyslog` file, we can see that the default value of the `rotate` parameter is `4`:
```bash
cat /etc/logrotate.d/rsyslog
```
```plaintext
/var/log/syslog
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
rotate 4
weekly
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
```
You can change `4` to some other value, such as `1`, so that only one file is stored. In the `/etc/logrotate.d/` folder you will also find many other configuration files related to other log files, where you can also change the `rotate` setting.

View File

@@ -0,0 +1,77 @@
---
title: "Linux: How to set up monitoring with alerts to Telegram"
date: "2023-03-04"
---
This article describes how to set up monitoring with alerts to Telegram using Grafana, Prometheus, Alertmanager, Node-exporter and Cadvisor.
### First step: Cloning the repository
Log in to the server or go to local terminal and run the following commands:
```bash
git clone https://github.com/digitalstudium/grafana-docker-stack.git
cd grafana-docker-stack
git checkout alertmanager
```
<!--more-->
### Second step: setting the external address of the server
Open the `docker-compose.yml` file and on lines 22 and 38 change the address `127.0.0.1` to the address of the server where you want to install Prometheus.
### Third step: creating a bot in Telegram
In the Telegram search, type `Botfather` and follow the first link:
![Botfather search](/images/botfather1.png "Botfather search")
Then press `Start`:
![Botfather start](/images/botfather2.png "Botfather start")
After that, create a bot using the `Botfather` instructions.
As a result, you should receive a message with an API token:
![Botfather API token](/images/botfather3.png "Botfather API token")
Copy this token and paste it on line 14 of the `configs/alertmanager.yml` file as the value of the `bot_token` parameter.
Then create a telegram group and add the created bot to this group. This group will receive notifications (alerts).
### Fourth step: Get the group id
In the group you added the bot to, write some command, for example: `/my_id foobar`
Then in the browser follow the link
`https://api.telegram.org/botINSERT_BOT_TOKEN_HERE/getUpdates` replacing `INSERT_BOT_TOKEN_HERE` with the token created in step 3.
You should get something like this page:
![Telegram getting chat id](/images/chat_id.png "Telegram getting chat id")
If there is nothing on your page, try sending the command `/my_id foobar` to the group again.
You need to copy the chat id value from this page and paste it on line 15 of the `configs/alertmanager.yml` file as the value of the `chat_id` parameter. Note that the value of the `chat_id` parameter must be without quotes and with a hyphen at the beginning.
### Fifth step: Deploying the docker stack
While in the `grafana-docker-stack` folder, run the following commands:
```bash
docker swarm init
docker stack deploy -c docker-compose.yml monitoring
```
Wait 5-10 minutes then run the command
```bash
docker ps
```
The command output should contain 5 containers: prometheus, grafana, alertmanager, node-exporter, grafana.
```plaintext
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46fba26e7234 gcr.io/cadvisor/cadvisor:v0.47.0 "/usr/bin/cadvisor -…" 5 days ago Up 5 days (healthy) 8080/tcp monitoring_cadvisor.1.q02qcn798dh0rydo1dzslylse
f212e3c66786 prom/alertmanager:v0.25.0 "/bin/alertmanager -…" 6 days ago Up 6 days 9093/tcp monitoring_alertmanager.1.oysziztrqnur7xr0hr82avunz
c16fb14929e2 prom/prometheus:v2.42.0 "/bin/prometheus --c…" 6 days ago Up 6 days 9090/tcp monitoring_prometheus.1.yslspi4fgjp7ic4f5e18gm99a
9bf01ce6b7a1 grafana/grafana:9.3.6-ubuntu "/run.sh" 6 days ago Up 6 days 3000/tcp monitoring_grafana.1.mypn85x12xw37ucprr33knuwk
58efdb46f5c3 kindest/node:v1.25.3 "/usr/local/bin/entr…" 6 days ago Up 6 days 127.0.0.1:46579->6443/tcp kind-control-plane
ae15e453e517 prom/node-exporter:v1.5.0 "/bin/node_exporter …" 7 days ago Up 7 days 9100/tcp monitoring_node-exporter.1.uecim10ow12h1qlpox5lg0c5r
```
If you see this output, then everything turned out successfully. If not, try repeating all previous steps.
### Sixth step: Check if it works
Go to [server ip-address or domain name]:3000
You should get Grafana opened. Enter login `admin` and password `admin`. Grafana will ask you to change your password, change it to any other.
Under Dashboard -> Browse -> General you will see 2 dashboards: Cadvisor exporter and Node Exporter Full. Open them and make sure everything works.
## Summary
Now you can evaluate server performance through Grafana graphs.
You will also receive alerts about problems with the server in the Telegram group. Alert rules can be configured in `configs/prometheus/alert_rules.yml`