Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lê Văn Chiến - k8s homework #137

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2b63d8a
update
lechiennn May 15, 2023
a2c3092
update dockerignore
lechiennn May 16, 2023
63aedd1
restructure
lechiennn May 16, 2023
42d0343
restructure
lechiennn May 16, 2023
6358cf7
add unittest
lechiennn May 16, 2023
b756842
add github actions
lechiennn May 16, 2023
99839b9
check github action
lechiennn May 16, 2023
c1d784d
add github action
lechiennn May 16, 2023
20b286d
update github workflow
lechiennn May 16, 2023
2614d91
update github workflow
lechiennn May 16, 2023
f274959
update github workflow
lechiennn May 16, 2023
a9b140f
update github workflow
lechiennn May 16, 2023
9f7a625
add test requirements
lechiennn May 16, 2023
8ab76ef
add CD
lechiennn May 17, 2023
34d4590
update git ignore
lechiennn May 17, 2023
01296bf
update nginx config
lechiennn May 17, 2023
0cd76c5
fix api
lechiennn May 17, 2023
36279cd
add container name
lechiennn May 17, 2023
5920798
update CD
lechiennn May 17, 2023
7ee2758
update CD
lechiennn May 17, 2023
8086939
update CD
lechiennn May 17, 2023
52421da
update CD
lechiennn May 17, 2023
174e2ea
update CD
lechiennn May 17, 2023
cb487b2
update CD workflow
lechiennn May 17, 2023
27d66ba
move ansible to new folder
lechiennn May 17, 2023
0f14a78
update git ignore
lechiennn May 17, 2023
091e70b
update CD workflow
lechiennn May 17, 2023
71da317
fix python path
lechiennn May 18, 2023
8098085
fix api endpoint
lechiennn May 18, 2023
46ae91d
move ansible to 2.Ansible
lechiennn May 21, 2023
e3db9a5
add loadbalancer role
lechiennn May 21, 2023
0317df7
fix roles
lechiennn May 21, 2023
d480fe0
add lb role
lechiennn May 21, 2023
69a1f49
add log & monitor
lechiennn May 21, 2023
69e8781
add README
lechiennn May 21, 2023
28f6e20
add log & monitor
lechiennn May 21, 2023
ed04130
update README.finalfinal
lechiennn May 21, 2023
44684ed
update for k8s hw
lechiennn Jun 12, 2023
9345c1b
add k8s assignment
lechiennn Jun 15, 2023
f97680f
add k8s assignment
lechiennn Jun 15, 2023
fade3a7
rename folder
lechiennn Jun 15, 2023
8b3f98a
update readme
lechiennn Jun 15, 2023
6a54a44
add yaml
lechiennn Jun 15, 2023
f0184fa
Delete Kubernetes directory
lechiennn Jun 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add README
lechiennn committed May 21, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 69e8781cbc9f4712b13167032b4c4f55873c8e0f
225 changes: 225 additions & 0 deletions 10.GK/LeVanChien/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
# Midterm assignment - VDT Cloud

## 0. Phát triển three-tier web app
Web app có các chức năng đơn giản như liệt kê danh sách các sinh viên, tìm kiếm theo username, xóa, cập nhật thông tin một sinh viên, thêm thông tin sinh viên mới.
![](images/demo-webui.png)

Hệ thống gồm ba dịch vụ:

- Web: Giao diện viết bằng html + css + javascript, triển khai trên nền nginx.
- Api: Sử dụng Flask với các chức năng get, create, delete, update các thông tin. Các chức năng trong api đều có unit tests, sử dụng thư viện pytest.
- Database: Sử dụng Mongodb

## 1. Containerization
Các dịch vụ đều được đóng gói thành các container.

Output câu lệnh build các image (bằng docker-compose):
![](images/output-dc-up.png)
### Web

Dockerfile
```
FROM nginx:1.22.0-alpine
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./static /var/www/html
```
Docker history
![](images/history-nginx.png)
### Api
Dockerfile
```
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
ENV FLASK_APP=app.py
COPY . .
CMD ["python", "app.py"]
```
Docker history
![](images/history-app.png)
### Db
Docker history
![](images/history-mongo.png)

Kích thước các images:
![](images/image-size.png)

## 2. Continuous Integration
File setup công cụ CI:
```
name: CI

on:
push:
branches:
- midterm
pull_request:
branches:
- midterm

jobs:
test:
runs-on: ubuntu-20.04

steps:
- name: checkout code
uses: actions/checkout@v3

- name: setup Python
uses: actions/setup-python@v3
with:
python-version: 3.8

- name: install dependencies
run: |
pip install --upgrade pip
pip install -r 1.\ Containerization/Le\ Van\ Chien/app/tests/test_requirements.txt
- name: run tests
working-directory: 1. Containerization/Le Van Chien/app
run: pytest

```
Việc chỉ định branch `midterm` trong `on.push` và `on.pull_request` là để github actions tự động thực hiện jobs test (chạy các unit tests đã viết ở phần trước) khi có sự kiện push commit hoặc tạo PR vào branch `midterm`

Github Actions thực hiện test sau khi push commit:
![](images/ci-log.png)

## 3. Continuous Delivery

Phần này sử dụng Ansible-playbook để setup và build các container trên một host.

### File setup CD
```
name: Build Dockerfile and push

on:
push:
tags:
- v*

jobs:
docker:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push app image
uses: docker/build-push-action@v4
with:
context: 1. Containerization/Le Van Chien/app/
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/app:${{ github.ref_name }}

- name: Build and push web image
uses: docker/build-push-action@v4
with:
context: 1. Containerization/Le Van Chien/nginx/
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/web:${{ github.ref_name }}

```
Việc chỉ định `tags` trong `on.push` là để Github Actions tự động build các Dockerfile của dịch vụ web và api và push lên docker hub:

- Sau khi push một tag mới:
```
git tag v1.2
git push origin midterm v1.2
```
- Github action tự động build image và push lên dockerhub với tag vừa push:

![](images/cd.png)
![](images/dockerhub.png)

Kế quả thực hiện 2 actions CI, CD trên github mỗi lần push:

![](images/ci-cd.png)

---

### **Cấu trúc cây thư mục các roles và các file playbooks**
```
├── ansible.cfg
├── inventories
│ └── inventory.yaml
├── requirements.yaml
├── roles
│ ├── api
│ │ ├── defaults
│ │ │ └── main.yaml
│ │ └── tasks
│ │ └── main.yaml
│ ├── common
│ │ ├── defaults
│ │ │ └── main.yaml
│ │ ├── handlers
│ │ │ └── main.yaml
│ │ └── tasks
│ │ ├── main.yaml
│ │ ├── setup_centos.yaml
│ │ └── setup_ubuntu.yaml
│ ├── db
│ │ ├── defaults
│ │ │ └── main.yaml
│ │ └── tasks
│ │ └── main.yaml
│ ├── lb
│ │ ├── defaults
│ │ │ └── main.yaml
│ │ ├── files
│ │ │ ├── Dockerfile
│ │ │ └── nginx.conf
│ │ └── tasks
│ │ └── main.yaml
│ └── web
│ ├── defaults
│ │ └── main.yaml
│ └── tasks
│ └── main.yaml
└── setup.yaml
```
Các roles web và api pull images từ dockerhub và triển khai các dịch vụ web và api trên 2 container khác nhau cho mỗi dịch vụ:

![](images/all-service.png)

Các container `nginx1` và `nginx2` đóng vai trò là các web server, container `lb` cũng sử dụng nginx nhưng đóng vai trò là load balancer, reverse proxy, được cấu hình để gửi request đến một trong hai web server:
```
upstream server {
server nginx1:80;
server nginx2:80;
}
```

Output triển khai hệ thống:
![](images/setup-playbook2.png)![](images/setup-playbook1.png)

## 4. Monitoring
Role `monitor` cài đặt các dịch vụ `Node-exporter` và `cadvisor` dưới dạng các container, gửi các thông số giám sát đến hệ thống prometheus tập trung http://27.66.108.93:9090/ với label `username='levanchien'`

![](images/prom.png)

Dashboard giám sát nodes & container trên grafana:
![](images/grafana1.png) ![](images/grafana2.png)

## 5. Logging

Role `log` cài đặt dịch vụ fluentd để collect log từ load balancer `lb`, đẩy log lên hệ thống Elasticsearch tập trung `171.236.38.100:9200` với index `chienlv`

Kibana:

![](images/kibana.png)

Log có các thông tin về thời gian, action, kêt quả (status code),...

![](images/log.png)

Binary file added 10.GK/LeVanChien/images/all-service.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 10.GK/LeVanChien/images/cd.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 10.GK/LeVanChien/images/ci-cd.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 10.GK/LeVanChien/images/ci-log.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 10.GK/LeVanChien/images/demo-webui.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 10.GK/LeVanChien/images/docker-image-list.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 10.GK/LeVanChien/images/dockerhub.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 10.GK/LeVanChien/images/grafana1.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 10.GK/LeVanChien/images/grafana2.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 10.GK/LeVanChien/images/history-app.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 10.GK/LeVanChien/images/history-mongo.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 10.GK/LeVanChien/images/history-nginx.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 10.GK/LeVanChien/images/image-size.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 10.GK/LeVanChien/images/kibana.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 10.GK/LeVanChien/images/log.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 10.GK/LeVanChien/images/output-dc-up.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 10.GK/LeVanChien/images/prom.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 10.GK/LeVanChien/images/setup-playbook1.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 10.GK/LeVanChien/images/setup-playbook2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.