Let's build a DevOps project : 
Tags: devops, monitoring, webdev
Published: 2020-12-14 08:29:15

# __The Agenda :__
This is a project in which we have to take an application through DevOps lifecycle . 

1. Integrating the source code in a __version control__ environment for mutual development.
3. The application must pass through a __ci/cd pipeline__ .
5. __continuous monitoring__ is enabled for fault tolerance .  

# __Phases in DevOps :__
* Planning 
* Coding 
* Testing 
* Building 
* Release
* Deploy
* Operate 
* Monitor

###  __Planning :__
A better planning of an application can lead to a better growth of our application where we have to design our system where __high level system design__ is involved like flow charts, features , intended users , security , Relationship in the database .
![Planning](https://dev-to-uploads.s3.amazonaws.com/i/ifqy37q6idzt0ljcz55d.png)

### __Coding :__
Then we have to do the coding according to the features . where low level design is involved .
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ux4ftt0fktpgeuvtk9qj.jpg)

### __Testing :__
A simple __unit testing__ is good enough for our application if we are not building a high scale application . Since we are using DevOps so we using Selenium to automate application testing . we use the selenium web driver and TestNG for report writing and Maven for managing  our dependency .
![Testing](https://dev-to-uploads.s3.amazonaws.com/i/8zgucb4oed73sudggkee.jpg) 

### __Building :__

* In the building stage we are going to implement packaging , error testing, basically integrate our application with Jenkins which is a ci/cd pipeline tool which can trigger our code by configuring with webhook and then automating the testing using selenium . 

* So there are mainly two types of servers one is for testing and another is production .

* So our application first should go to the testing servers and then to the production servers .

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4k8i8y16k556qvfa5lir.png)

### __Release :__
In the release state we are releasing a __MVP__ of our application to our intendent uses .
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/81caxitwk38s9vumrmgx.jpg)

### __Configuration management :__
This is a phase of infrastructure as a service where we have to write code for our infrastructure . and to automate it because we are using DevOps and this can be achieved by __Configuration management tools__ such as ansible, puppet, Ceph , SaltStack . Let's suppose our application needed more resources so that we have to allocation new servers and manually we could do that but if we think in a large scale so it is a repeatative task so by using configuration management tools we could make sure that automated servers added to our infrastructure when ever needed .
![Configuration management](https://dev-to-uploads.s3.amazonaws.com/i/m9jujfgq9t4za7naoj7r.png)  

### __Monitor :__
* Monitoring our application is really a essence like monitoring our infrastructure . Monitoring based upon different  metrices 
like resource utilization (Mainly CPU and Memory) .

* When it find potential failure symptoms an __Alert__ has been triggered with the log about which part of our application crashes . 

![Monitoring](https://dev-to-uploads.s3.amazonaws.com/i/zs8ky6rljxyowgsvzdeb.png)
### __Steps :__
 
* We are use two machines one is the worker and another one is the slave machine .

* create two ec2 instances in __AWS__ and connect using SSH .
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s85iouqo51n4873xst92.jpg)

* Install Git and Jenkins(Java as a prerequisite) on the master Node .   

* Fire up Jenkins web dashboard in the usual steps .

![Jenkins](https://www.jenkins.io/images/logo-title-opengraph.png)
* configure setting where we have to create a new node and 
 then configure our node machine by downloading two files and if we are downloading them we have to use the SCP command to copy them to the master node .

```$scp -i /downloads/.pem (name of the files) ubuntu@ip:/home/jenkins``` 

* Configure webhooks for a triggering point whenever we are pushing our code into GitHub .  

* __Coming to the node machine__ we need to install Java and git .
* clone the git repos modify some change .
* add it to the staging area (place where git can track all the files ) .
```$git add .```
```$sudo git commit -m "Initial commit"```  

Then copy the ip address of Jenkins go to the settings of your repository add a webhook . 
![webhook](https://miro.medium.com/max/998/1*7mTqsweysNsTmRF5dMUObA.png)

* recognized as a __payload URL__

```http://testingserverip:8080/github-webhook/ ```

Implement a Job inside Jenkins
create a new item test-job

* Create a docker file

![docker](https://i1.wp.com/itsfoss.com/wp-content/uploads/2019/11/docker_wallpaper.png?fit=800%2C450&ssl=1)

The container is consists of the operating system  and the required software .
It our case in this container __Ubuntu__ is the __OS__ and apache is configured as a __webserver__ .

```
FROM hshar/webapp
ADD . /var/www/html
```

In the Jenkins build environment 

```
sudo docker rm -f $(sudo docker ps -a -q)
sudo docker build /home/ubuntu/website/. -t test
sudo docker run -it -p 82:80 -d test
``` 

# __Now we have to test our application :__
![selenium](https://dev-to-uploads.s3.amazonaws.com/i/xz2c03tqqsj2l6wr3ci3.jpg)

* Install __selenium webdriver__ , the singleton server and the chromedriver as per your operating system . 

###Install in on test-server

```$sudo apt install chromium-browser```

[Go to this link !](https://chromedriver.chromium.org/downloads)

```$wget https://chromedriver.storage.googleapis.com/87.0.4280.20/chromedriver_linux64.zip```
```$unzip chromedriver_linux64.zip```

Lets write a simple 
/home/ubuntu/chromedriver as a path 
execute it on __headless mode__ because our ubuntu server does not have a dashboard . 

```java
public class Test {

   public static void main(String []args) {
	   
	   System.setProperty("webdriver.chrome.driver", "C:\\Selinium\\chromedriver.exe");
	   
	   ChromeOptions options = new ChromeOptions();
	   
	   options.addArguments("--headless");
	   WebDriver driver = new ChromeDriver(options);
	   
	   driver.get("http://(ip address)/");
	   
	   driver.findElement(By.id("id_name"));
   }
}
```

```java -jar hello.jar```

Then transfer it to the node machine __scp__

Lunch a production server instance 
```$sudo apt install open-jdk```
```$sudo apt install docker ```

Go to jenkins 
add a new node called prod server and the location is /home/ubuntu/jenkins 
run the jnlp file 

next create a new job as a prod branch without webhook 
```bash
echo hello
sudo docker rm -f $(sudo docker -ps -q)
sudo docker build /home/ubuntu/jenkins/workspace/test-job/. -t prod
sudo docker run -it -p 80:80 -d prod 
```
```bash
cd jenkins 
cd workspace 
cd test-job
```

* create a new job called __test-job__
* same github url
* label expression __test-server__

```bash
sudo cp ./hello.jar /home/ubuntu
sudo java -jar /home/ubuntu/hello.jar
```

* test-github-webhook
* __post-build-action__ build-website
* save

* build-website 
* remove build triggers
* add test-website post-build

* test-website
* build-other-project -- test-job

* add a new pipeline view 
define the initial job other will be handeled by post-build action
__test-github-webhook__

* Inside the test-server

```shell
sudo mv ./hello.jar /home/ubuntu/website
cd website
git add . 
git commit -m "test jar added"
sudo git push -u origin master 
```

* Lets go to master server for configuration management 
cd jenkins/workspace/test-job


* The ip address of production server paste in on browser .

* Modify it on __test server__
```bash
cd website
change something
commit -m "Final commit"
sudo push -u origin master 
```

New job named as Develop-branch
restrict it to the test-server
```bash
sudo docker rm -f $(sudo docker ps -t -q)
sudo docker build /home/ubuntu/. -t test1
sudo docker run -it -p 85:80 -d test1
```

# __Configuration management using ansible:__

* In this phase we have to install a __Configuration management__ which is ansible in our case . which helps achieve __infrastructure as a code__ basically writing code for our infrastructure .

# __Continuous monitoring using Nagios :__

* The Final step in our loop and most important phase . 

<table>
  <tr>
    <td>
      <a href="https://dev.to/rahulku48837211/ansible-architecture-and-setup-2355" alt="Ansible image">
  <figure>
    <img width="300"     src="https://res.cloudinary.com/practicaldev/image/fetch/s--r4FESc_R--/c_imagga_scale,f_auto,fl_progressive,h_420,q_auto,w_1000/https://dev-to-uploads.s3.amazonaws.com/i/c819w2e2mue3j204clau.png">
<br>
    <figcaption> Ansible setup and architecture</figcaption>
  </figure>
</a>  
    </td>
        <td>
      <a href="https://dev.to/rahulku48837211/continuous-monitoring-using-nagios-205h" alt="Nagios image">
  <figure>
    <img width="300"     src="https://res.cloudinary.com/practicaldev/image/fetch/s--OJFRT0kj--/c_imagga_scale,f_auto,fl_progressive,h_420,q_auto,w_1000/https://dev-to-uploads.s3.amazonaws.com/i/td2ghznu2edhbvnlmntc.jpg">
<br>
    <figcaption> Nagios setup and architecture </figcaption>
  </figure>
</a>  
    </td>
  </tr>
  
</table> 

# __Thank You for reading 🤖🤖🤖__