Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

Wednesday, February 16, 2022

EXAMPLES: Installing ELK stack via Ubuntu OS through AMI on Nginx/System Server


INSTALLING ELK STACK VIA UBUNTU OS THROUGH AMAZON MACHINE IMAGE (AMI) ON NGINX/SYSTEM WEBSERVER 

Prerequisite
  1. sudo apt update                                 (update the ubuntu in AMI)
  2. sudo apt install openjdk-8-jdk          (install latest java on ubuntu in AMI)
  3. sudo apt-get install -y nginx             (install nginx webserver)
  4. sudo systemctl enable nginx             (enabling nginx to run)

1) Install Elasticsearch
  1. wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-amd64.deb
  2. sudo dpkg -i elasticsearch-7.2.0-amd64.deb

2) Install kibana
  1. sudo wget https://artifacts.elastic.co/downloads/kibana/kibana-7.2.0-amd64.deb
  2. sudo dpkg -i kibana-7.2.0-amd64.deb

3) Install Logstash
  1. sudo wget https://artifacts.elastic.co/downloads/logstash/logstash-7.2.0.deb
  2. sudo dpkg -i logstash-7.2.0.deb

4) Install Dependencies (transport-https package)
  1. sudo apt-get install -y apt-transport-https

5) Install FileBeat
  1. wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.2.0-amd64.deb
  2. sudo dpkg -i filebeat-7.2.0-amd64.deb

6) Modify Elasticsearch yaml file (modify some configurations)
  1. sudo vi /etc/elasticsearch/elasticsearch.yml
    • Make below changes in this file
    • cluster.name: my-application
    • node.name: node-1
    • network.host: localhost (change this line)
    • http.port: 9200
  2. sudo systemctl start elasticsearch

7) Modify Kibana yaml file (modify some configurations)

s
udo vi /etc/kibana/kibana.yml

Make below changes in the file
server.port: 5601 
server.host: "localhost"


sudo systemctl start kibana
sudo apt-get install -y apache2-utils
sudo htpasswd -c /etc/nginx/htpasswd.users kibadmin
sudo vi /etc/nginx/sites-available/default

type in this below:
  server {
    listen 80;
 
    server_name 3.108.42.168;  (THIS CHANGE TO YOUR UNIQUE PUBLIC IP)
 
    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/htpasswd.users;
 
    location / {
        proxy_pass http://localhost:5601;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

sudo systemctl restart nginx

now check by putting 3.110.88.42 in the web browser

put in username and pw (u/n: kibadmin, p/w: kibadmin)






8) Download some sample data
sudo wget https://logz.io/sample-data

type in ls and you will find a list of files and folders:


sudo mv sample-data apache.log (to rename sample-data to apache.log)
Below is an example of a .log file (in this case the apache.log file):


vi /etc/logstash/conf.d/apache.conf   (upload the file through logstash and then search using elastic search then kibana will find it and analyze it)
type in below:
input {
 file {
   path => "/home/ubuntu/apache.log"
   start_position => "beginning"
   sincedb_path => "/dev/null"
 }
}
filter {
      grok {
         match => {  "message" => "%{COMBINEDAPACHELOG}" }
       }
      date {
         match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
      }
      geoip {
         source => "clientip"
       }
    }
output {
  elasticsearch {
  hosts => ["localhost:9200"]
  index => "petclinic-prd-1"   (ANY INDEX YOU WANT SO THAT IT IS SEARCHABLE IN KIBANA)
}
sudo systemctl restart logstash

go to kibana à Management à Index Patterns à Create Index Pattern

Then now you can utilise the Kibana Query Language (KQL)


9) Getting data from filebeat (So what is a filebeat? it basically is a tool to ship from a specific server, so right now we are taking from an nginx webserver)

sudo filebeat modules list        (a lists of enabled and disabled modules will appear)
sudo filebeat modules enable nginx        (enable nginx)
sudo filebeat modules enable system        (enable system)
cd /etc/filebeat/modules.d/

sudo vi nginx.yml        (edit the configuration for nginx)
 access:
    enabled: true
    var.paths: ["/var/log/nginx/access.log*"]
 
  # Error logs
  error:
    enabled: true
    var.paths: ["/var/log/nginx/error.log*"]


sudo vi system.yml        (edit configuration for system)
- module: system
  # Syslog
  syslog:
    enabled: true
    var.paths: ["/var/log/syslog*"]
  
  auth:
    enabled: true
    var.paths: ["/var/log/auth.log*"]

sudo systemctl restart filebeat
sudo systemctl restart logstash


go to kibana à Management à Index Patterns à Create Index Pattern



ASSIGNMENT: ELK - Kibana Query Language (KQL)



It is on the basis of Apache Log data ( https://logz.io/sample-data ).

Run below queries on different time interval (today, last 15 mins...) and check the count of Hits and also check the visualization in form of bar chart.

  1. Find all the records for US users (geoip.country_code2 and geoip.country_code3)
  2. Find all the records for users other than US users  (geoip.country_code2 and geoip.country_code3)
  3. Find all the records for users in CountryCode3( geoip.country_code3) is either US or IN
  4. Find all the records for verb GET.
  5. Find all the records with the request is /category/software and response is 200 for today.
  6. Find all the records where message contains Mozilla
  7. Find all the records where verb is not GET
  8. Find all the records where verb is either GET or POST
  9. Find all the records where clientip is 184.153.198.34 and geoip.cityname Watertown.
  10. Find records with geoip.country_code is US and time zone is America/New_York


Answers:

1.  geoip.country_code2:"US" and geoip.country_code3:"US"
last 15minutes: 106 hits
today: 6,272 hits
last 30 days: 1,759 hits

2. not geoip.country_code2:"US" and not geoip.country_code3:"US"
last 15minutes: 77 hits
today: 5,245 hits
last 30 days: 1,457 hits

3. geoip.country_code3:"US" or geoip.country_code3:"IN"
last 15minutes: 100 hits
today: 6,328 hits
last 30 days: 1,827 hits

4. verb:"GET"
last 15minutes: 162 hits
today: 10,747 hits
last 30 days: 3,074 hits

5. request:"/category/software" and response:200
last 15minutes: 24 hits
today: 1064 hits
last 30 days: 326 hits

6. message:"mozilla"
last 15minutes: 180 hits
today: 720 hits
last 30 days: 3393 hits

7. not verb:"GET"
last 15minutes: 17 hits
today: 770 hits
last 30 days: 237 hits

8. verb:"GET" or verb:"POST"
last 15minutes: 180 hits
today: 11,517 hits
last 30 days: 3426 hits

9. clientip:184.153.198.34 and geoip.city_name:"Watertown"
last 15minutes: 0 hits
today: 12 hits
last 30 days: 2 hits

10. (geoip.country_code2:"US" or geoip.country_code3:"US")  and  geoip.timezone:"America/New_York"
last 15minutes: 9 hits
today: 639 hits
last 30 days: 196 hits


EXAMPLES: Connecting to Raman's AWS to install ELK

 


Raman’s AWS account

He created a key pair and named it elk, with type rsa  and stored it in a file named à elk.pem

He then created an instance using Amazon Machine Image (AMI), he created 9 instances for each of us with 20gb worth of storage, and he gave all traffic permissions and he selected elk | RSA key pair and now the instance is running.



He now gives unique public IP address to us, mine is 3.110.88.42

He now shows how to connect:

(for windows user):

  1. Go to mobaxterm, go to session SSH, and put the unique public ip in remote host and username is ubuntu with port 22.
  2. In advanced click use private key and select the elk.pem then click ok.
(For mac user):
  1. Open SSH client
  2. Locate
  3. chmod 400 elk.pem
  4. ssh -i "elk.pem" ubunt@13.232.149.122


ELK Stack

 


ELK stands for

  • Elastic search – indexing and searching
  • Logstash – log input
  • Kibana – presentation (dashboard)

It refers to a set of open-source products developed by Elastic to help its users to collect the data from different sources and analyze the data and present it in a simple manner.

For example: a .log file is 1GB and can be scaled to Big Data, how would you suppose to read and analyze it? You use tools, such as:

  1. ELK (opensource, machine learning data), 
  2. Splunk (commercial), 
  3. Hadoop (opensource, selection of softwares), 
  4. MongoDB (JSON data, NoSQL)

In a bigdata there are clusters (clusters are computers that are linked together) and there is also a master server. The master server decides which query gets through from which servers.

Elasticsearch:- 
It is a NoSql database that can be used to index and store different types of documents. It also provides the functionality to search the real-time data.

LogStash:-
Logstash is a collection agent used to collect both heterogenous/non-heterogenous data from various sources. It has the capability to screen, break down, and make string alterations in the data it collects. After collecting and filtering the data, it then sends it to Elasticsearch for storage.

Kibana:-
Kibana is a graphical user interface used to display the data that is collected and stored in Elasticsearch. It displays the data with appealing visuals so that the data could be easily understood and analyzed; it does so by using multiple types of visuals like bar chart, pie chart,world map, heat map, cordinate map, etc.

Beats:-
Beats is similar to Logstash in the fact that they both collect data that will be later stored and analyzed, but Beats differs in the method of collection. Beats is a set of multiple small software installed on different servers from where they collect the data and send it to Elasticsearch


Representation of ELK Flow 1:



Another representation of ELK Flow:






First, Beats are attached to remote servers from where these Beats collect information from various sources.

After collecting all the data needed, they either ship the data to Logstash for filtration or directly send it to Elasticsearch.

The data is then stored in Elasticsearch. From here, it will not be directly sent to Kibana. Kibana first needs to find where Elastic is and then go and get the data by itself.


Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...