Skip to content

Commit

Permalink
Merge pull request #3 from opstree/development
Browse files Browse the repository at this point in the history
[Development][Add] Added ELK monitoring
  • Loading branch information
iamabhishek-dubey authored Sep 21, 2019
2 parents f075665 + 5ba962c commit 74c3098
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 4 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ This golang app is to provide an environment of golang application's multiple ph
- Added healthcheck URL for redis with status code
- Replaced property file with ini file
- Added json logging
- Structured code a bit :stuck_out_tongue_winking_eye
- Structured code a bit :stuck_out_tongue_winking_eye:

### v0.0.3
##### September 21st, 2019

#### :tada: New Features
- Added docker compose setup for complete application stack
- Added elk docker compose setup for application logging example
- Integrated filebeat for automated log shipping
17 changes: 17 additions & 0 deletions Dockerfile.filebeat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:latest AS build-env
MAINTAINER Opstree Solutions
WORKDIR /app
ENV SRC_DIR=/go/src/gitlab.com/opstree/ot-go-webapp/
ADD . $SRC_DIR
RUN cd $SRC_DIR; go get -v -t -d ./... && \
go build -o ot-go-webapp; cp ot-go-webapp /app/

FROM ubuntu:18.04
WORKDIR /app
RUN apt-get update -y && \
apt-get install libc6 -y
COPY --from=build-env /app/ot-go-webapp /app/
ADD https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.3.1-amd64.deb /opt/
RUN dpkg -i /opt/filebeat-7.3.1-amd64.deb \
&& service filebeat start
ENTRYPOINT ["./ot-go-webapp"]
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ ot-go-webapp
└── template.go ---> This file has the HTML template for Web Interface
```

## Monitoring Application

We have integrated ELK(Elasticsearch, Logstash, and Kibana) for application log monitoring example. The main motive for this to encourage people to use JSON logging because field separation is quite easy in JSON. If you hate writing grok patterns this feature could be your lifesaver.

In addition you will get these beautiful logs visualization

![](./img/kibana1.png)

To run this setup follow these instructions:-

```shell
cd elk
export ELK_VERSION=7.3.1
docker-compose up -d
```

That's it this will lead you to this UI

![](./img/kibana2.png)

## Building Application

#### For non-dockerized environment
Expand Down Expand Up @@ -147,7 +167,7 @@ docker run -itd --name application --link mysql:mysql -e DB_USER=root -e DB_PASS
- [X] Write unit tests
- [X] Fix code if there is any mess
- [X] Integrate dependency management
- [ ] Fill README with more information
- [X] Fill README with more information
- [X] Make application more attractive
- [X] Add healthcheck API
- [X] Add redis healthcheck
Expand All @@ -156,6 +176,7 @@ docker run -itd --name application --link mysql:mysql -e DB_USER=root -e DB_PASS
- [X] Integrate redis for caching purpose
- [ ] Dump manifests file for kubernetes deployment
- [X] Replace property file from ini structure file
- [ ] Structure code in better manner(Refactoring)
- [X] Structure code in better manner(Refactoring)
- [X] Implement json logging
- [ ] Add docker compose setup
- [X] Add docker compose setup
- [X] Add ELK monitoring docker compose stack
34 changes: 34 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3.3'
services:
redis:
image: redis:latest
restart: always
expose:
- 6379

mysql:
image: mysql:5.6
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: employeedb
expose:
- 3306

ot-go-webapp:
build:
context: .
dockerfile: Dockerfile
restart: always
depends_on:
- mysql
- redis
environment:
DB_URL: mysql
DB_PORT: 3306
DB_USER: root
DB_PASSWORD: password
REDIS_HOST: redis
REDIS_PORT: 6379
ports:
- 8080:8080
109 changes: 109 additions & 0 deletions elk/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

version: '3.2'

services:
elasticsearch:
build:
context: elasticsearch/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- type: bind
source: ./elasticsearch/elasticsearch.yml
target: /usr/share/elasticsearch/config/elasticsearch.yml
read_only: true
- type: volume
source: elasticsearch
target: /usr/share/elasticsearch/data
ports:
- "9200:9200"
- "9300:9300"
environment:
ES_JAVA_OPTS: "-Xmx256m -Xms256m"
ELASTIC_PASSWORD: elastic
networks:
- elk

logstash:
build:
context: logstash/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- type: bind
source: ./logstash/logstash.yml
target: /usr/share/logstash/config/logstash.yml
read_only: true
- type: bind
source: ./logstash/conf.d
target: /usr/share/logstash/pipeline
read_only: true
ports:
- "5000:5000"
- "5044:5044"
- "9600:9600"
environment:
LS_JAVA_OPTS: "-Xmx256m -Xms256m"
networks:
- elk
depends_on:
- elasticsearch

kibana:
build:
context: kibana/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- type: bind
source: ./kibana/kibana.yml
target: /usr/share/kibana/config/kibana.yml
read_only: true
ports:
- "5601:5601"
networks:
- elk
depends_on:
- elasticsearch

mysql:
image: mysql:5.6
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: employeedb
ports:
- "3306:3306"
networks:
- elk

ot-go-webapp:
image: opstreedevops/ot-go-webapp:filebeat
volumes:
- type: bind
source: ./ot-go-webapp/filebeat.yml
target: /etc/filebeat/filebeat.yml
read_only: true
- type: bind
source: ./ot-go-webapp/entrypoint.sh
target: /app/entrypoint.sh
ports:
- "8080:8080"
environment:
DB_URL: mysql
DB_PORT: 3306
DB_USER: root
DB_PASSWORD: password
entrypoint:
- ./entrypoint.sh
networks:
- elk
depends_on:
- mysql

networks:
elk:
driver: bridge

volumes:
elasticsearch:
3 changes: 3 additions & 0 deletions elk/elasticsearch/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ARG ELK_VERSION

FROM docker.elastic.co/elasticsearch/elasticsearch:${ELK_VERSION}
9 changes: 9 additions & 0 deletions elk/elasticsearch/elasticsearch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
cluster.name: "ot-go-webapp-elk"
network.host: 0.0.0.0

discovery.type: single-node

xpack.license.self_generated.type: trial
xpack.security.enabled: true
xpack.monitoring.collection.enabled: true
3 changes: 3 additions & 0 deletions elk/kibana/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ARG ELK_VERSION

FROM docker.elastic.co/kibana/kibana:${ELK_VERSION}
7 changes: 7 additions & 0 deletions elk/kibana/kibana.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true

elasticsearch.username: elastic
elasticsearch.password: elastic
3 changes: 3 additions & 0 deletions elk/logstash/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ARG ELK_VERSION

FROM docker.elastic.co/logstash/logstash:${ELK_VERSION}
15 changes: 15 additions & 0 deletions elk/logstash/conf.d/logstash.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
input {
beats {
port => 5044
}
}

output {
elasticsearch {
hosts => ["http://elasticsearch:9200"]
index => "ot-go-webapp-%{+YYYY.MM.dd}"
user => "elastic"
password => "elastic"
}
}

5 changes: 5 additions & 0 deletions elk/logstash/conf.d/ot-go-webapp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filter {
json {
source => "message"
}
}
6 changes: 6 additions & 0 deletions elk/logstash/logstash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.hosts: [ "http://elasticsearch:9200" ]

xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.username: elastic
xpack.monitoring.elasticsearch.password: elastic
16 changes: 16 additions & 0 deletions elk/ot-go-webapp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:latest AS build-env
MAINTAINER Opstree Solutions
WORKDIR /app
ENV SRC_DIR=/go/src/gitlab.com/opstree/ot-go-webapp/
ADD . $SRC_DIR
RUN cd $SRC_DIR; go get -v -t -d ./... && \
go build -o ot-go-webapp; cp ot-go-webapp /app/

FROM ubuntu:18.04
WORKDIR /app
RUN apt-get update -y && \
apt-get install libc6 -y
COPY --from=build-env /app/ot-go-webapp /app/
ADD https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.3.1-amd64.deb /opt/
RUN dpkg -i /opt/filebeat-7.3.1-amd64.deb
ENTRYPOINT ["./ot-go-webapp"]
7 changes: 7 additions & 0 deletions elk/ot-go-webapp/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -ex
sleep 25
service filebeat start

./ot-go-webapp
23 changes: 23 additions & 0 deletions elk/ot-go-webapp/filebeat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
filebeat.inputs:

- type: log
enabled: true
paths:
- /var/log/ot-go-webapp.access.log
- /var/log/ot-go-webapp.error.log
fields:
type: logs
server: ot-go-webapp
index_name: web
fields_under_root: true

filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false

setup.template.settings:
index.number_of_shards: 1

#----------------------------- Logstash output --------------------------------
output.logstash:
hosts: ["logstash:5044"]
Binary file added img/kibana1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/kibana2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 74c3098

Please sign in to comment.