:::success
# LS Lab 4 - CI/CD or NoSQL
:::
# NoSQL Cluster
## Task 1 - Take NoSQL solution
:::info
**REDIS**
:::
## Task 2 - NoSQL cluster theory
:::info
Give the answers for the following questions what is and for what, will be better to provide explanatory pictures/graphs:
1. Redundancy
2. HA
3. Fault Tolerance
:::
1. Redundancy - this situation occurs when the same piece of data is stored in two or more separate locations, this is necessary when it comes to large-scale databases, where the risk of failure can entail serious consequences.
2. HA - High availability - this is an environment (for example, group of computer working as a cluster) has a minimal service interruption.
3. Fault tolerance - this is an environment in which service is not interrupted, but the cost of such service is much higher then usual systems.
## Task 3 - NoSQL cluster deployment & validation & Fault Tolerance
:::info
1. Deploy your NoSQL cluster (e.g. with master, primary, slave, replica...) and investigate/describe its features.
2. Validate all the features by operating with some dataset. Create new nodes/ destroy oneā¦
3. Describe and validate the election process of the primary.
4. Destroy the nodes and describe what will happened?
5. Try to add new nodes after cluster is created, show them. How hard is it?
:::
I decided set up a dedicated Redis Cluster with three servers, each of them running Ubuntu 20.04.1 LTS. For this I have three VMs.
First of all we need install redis on each machine:
```
sudo apt-get update
sudo apt install redis-server
```
Change Firewall Rules:
```
sudo systemctl disable redis-server.service
sudo ufw allow 7000
sudo ufw allow 7001
sudo ufw allow 17000
sudo ufw allow 17001
```
This script is executed at the end of each multiuser runlevel. By default this script does nothing.
```
sudo vim /etc/rc.local
----------------------
#!/bin/sh -e
echo never > /sys/kernel/mm/transparent_hugepage/enabled
sysctl -w net.core.somaxconn=65535
exit 0
----------------------
sudo chmod +x /etc/rc.local
```
Edit the **/etc/sysctl.conf**:
```
#add the following line at the end of the file
vm.overcommit_memory=1
```
Create some required folders:
```
sudo mkdir /etc/redis/cluster
sudo mkdir /etc/redis/cluster/7000
sudo mkdir /var/lib/redis/7000
sudo mkdir /etc/redis/cluster/7001
sudo mkdir /var/lib/redis/7001
```
Create and edit the file /etc/redis/cluster/7000/redis_7000.conf:
```
sudo vim /etc/redis/cluster/7000/redis_7000.conf
port 7000
dir /var/lib/redis/7000/
appendonly no
protected-mode no
cluster-enabled yes
cluster-node-timeout 5000
cluster-config-file /etc/redis/cluster/7000/nodes_7000.conf
pidfile /var/run/redis/redis_7000.pid
logfile /var/log/redis/redis_7000.log
loglevel notice
requirepass 3L44RKZJIE6P8WCSJ
masterauth 3L44RKZJIE6P8WCSJ
```
Create and edit the file /etc/redis/cluster/7001/redis_7001.conf:
```
sudo vim /etc/redis/cluster/7001/redis_7001.conf
port 7001
dir /var/lib/redis/7001
appendonly no
protected-mode no
cluster-enabled yes
cluster-node-timeout 5000
cluster-config-file /etc/redis/cluster/7001/nodes_7001.conf
pidfile /var/run/redis/redis_7001.pid
logfile /var/log/redis/redis_7001.log
loglevel notice
masterauth 3L44RKZJIE6P8WCSJ
requirepass 3L44RKZJIE6P8WCSJ
```
Create a redis user and a redis group for the Redis Server services and give them the correct permissions:
```
sudo chown redis:redis -R /var/lib/redis
sudo chmod 770 -R /var/lib/redis
sudo chown redis:redis -R /etc/redis
```
Create and edit the file /etc/systemd/system/redis_7000.service. This is the configuration of the daemon service for the Master redis-server process used when the system restarts.
```
[Unit]
Description=Redis key-value database on 7000
After=network.target
[Service]
ExecStart=/usr/bin/redis-server /etc/redis/cluster/7000/redis_7000.conf --supervised systemd
ExecStop=/bin/redis-cli -h 127.0.0.1 -p 7000 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
```
Create and edit the file /etc/systemd/system/redis_7000.service:
```
[Unit]
Description=Redis key-value database on 7001
After=network.target
[Service]
ExecStart=/usr/bin/redis-server /etc/redis/cluster/7001/redis_7001.conf --supervised systemd
ExecStop=/bin/redis-cli -h 127.0.0.1 -p 7001 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=/etc/redis/cluster/7001
RuntimeDirectoryMode=0755
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
```
Tell systemd to start the two services redis_7000.service and redis_7000.service automatically at boot by running the following commands:
```
sudo systemctl enable /etc/systemd/system/redis_7000.service
sudo systemctl enable /etc/systemd/system/redis_7001.service
```
```
sudo reboot
```
<center>

Figure 1 - All starts good
</center>
Now we can create a cluster using the addresses of our virtual machines. The first 3 addresses are the Main Notes, and the next 3 addresses are replica nodes. The --cluster-replicas 1 argument indicates that each master device will have 1 replica.
```
redis-cli --cluster -a create 10.1.1.61:7000 10.1.1.92:7000 10.1.1.95:7000 10.1.1.61:7001 10.1.1.92:7001 10.1.1.95:7001 --cluster-replicas 1
```
<center>

Figure 2 - Creating cluster

Figure 3 - Information about nodes
</center>
Now you can check the information on the nodes and, for example, check the operation of Redis by creating keys.
<center>

Figure 4 - Information about node

Figure 5 - Create some data
</center>
To approve the electoral process, I will use Sentinel mode, which will monitor the status of the node, and perform role switching automatically. For this we need add config on each machines (slave and master):
```
/etc/redis/sentinel.conf
sentinel monitor redis-test 10.1.1.61 6379 2
sentinel down-after-milliseconds redis-test 6001
sentinel failover-timeout redis-test 60000
sentinel parallel-syncs redis-test 1
bind 0.0.0.0
sentinel auth-pass redis-test 123
```
<center>

Figure 6 - Sentinel
</center>