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

Done mid term #8

Merged
merged 25 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Binary file added .DS_Store
Binary file not shown.
72 changes: 72 additions & 0 deletions .github/workflows/ci_cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: CI - CD

on:
push:
branches:
- "*"
pull_request:
branches:
- main

env:
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}

jobs:
Build_docker_compose:
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v2
- name: Build the Docker image
run: docker-compose -f 10.GK/Vu_Minh_Hieu/docker-compose.yml build --no-cache --force-rm

Run_unit_test:
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v2
- name: Create fresh env to test
run: docker-compose -f 10.GK/Vu_Minh_Hieu/docker-compose.yml up -d mongodb
- name: Install local dependencies
run: pip install -r 10.GK/Vu_Minh_Hieu/backend/requirements.txt
- name: Run the unittest
run: pytest 10.GK/Vu_Minh_Hieu/backend/

Push_to_Docker_hub:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v3

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

- name: Log in to Docker Hub
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push api image to Docker hub
uses: docker/build-push-action@v3
with:
context: "{{defaultContext}}:10.GK/Vu_Minh_Hieu/backend"
push: true
tags: "hieuminhvuu/mt_backend:latest"
labels: "hieuminhvuu/mt_backend"

- name: Build and push api image to Docker hub
uses: docker/build-push-action@v3
with:
context: "{{defaultContext}}:10.GK/Vu_Minh_Hieu/frontend"
push: true
tags: "hieuminhvuu/mt_frontend:latest"
labels: "hieuminhvuu/mt_backend"
142 changes: 142 additions & 0 deletions 10.GK/Vu_Minh_Hieu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Project giữa kỳ

## I. Phát triển một 3-tier web application đơn giản

### Yêu cầu:

- #### Viết một CRUD web application đơn giản thực hiện các chức năng:
- Liệt kê danh sách sinh viên tham gia khóa đào tạo chương trình VDT 2023 lĩnh vực cloud dưới dạng bảng, cho phép xem chi tiết/thêm/xóa/cập nhật thông tin sinh viên
- Solution:
<img src= images/react.png>
<img src= images/fastapi_docs.png>
- #### Thiết kế hệ thống với ba dịch vụ:

- web: Giao diện web viết bằng HTML + CSS + Javascript được triển khai trên nền web server nginx
- source : [react](./frontend/)
- api: RESTful API viết bằng ngôn ngữ lập trình tùy chọn (prefer Python), có đầy đủ các chức năng: list, get, create, update, delete các bản ghi thông tin sinh viên

- source : [fastapi](./backend/)

- db: Database SQL hoặc NoSQL lưu trữ thông tin sinh viên (dữ liệu khởi tạo của DB này được cho bởi bảng trong Phụ lục I.)
- done
- Viết unit tests cho các chức năng APIs
- source : [unit-test](./backend/test_main.py)
- <strike>\*Viết unit tests cho các chức năng giao diện, viết integration tests</strike>

## II. Triển khai web application sử dụng các DevOps tools & practices

### 1. Containerization

Yêu cầu:

- Viết Dockerfile để đóng gói các dịch vụ trên thành các container image
- Yêu cầu image đảm bảo tối ưu thời gian build và kích thước chiếm dụng, khuyến khích sử dụng các thủ thuật build image đã được giới thiệu (layer-caching, optimized RUN instructions, multi-stage build, etc.)

Output:

- File Dockerfile cho từng dịch vụ kế hệ thống với ba dịch vụ:
- backend : [fastapi](./backend/Dockerfile)
- frontend : [react](./frontend/Dockerfile)
- docker-compose : [docker-compose.yml](./docker-compose.yml)
- Output câu lệnh build và thông tin docker history của từng image:
- output build : [output](./logs/output_docker_compose.log)
- mongodb images history : [mongodb](./logs/mongo_history.log)
- fastapi image history : [fastapi_history](./logs/fastapi_history.log)
- react image history : [react_history](./logs/react_history.log)

### 2. Continuous Integration

Yêu cầu:

- Tự động chạy unit test khi tạo PR vào branch main
- Tự động chạy unit test khi push commit lên một branch
Output:
- File setup công cụ CI
- source : [ci file](../../.github/workflows/ci_cd.yaml)
- Output log của luồng CI
<img src= images/actions.png>
<img src= images/ci_cd_1.png>
<img src= images/run_unit_test.png>
- Các hình ảnh demo khác
<img src= images/github_actions.png>

### 3. Continuous Delivery

Yêu cầu:

- Viết luồng release dịch vụ bằng công cụ CI/CD của GitHub/GitLab, thực hiện build docker image và push docker image lên Docker Hub khi có event một tag mới được developer tạo ra trên GitHub
- Viết ansible playbook thực hiện các nhiệm vụ:

- Setup môi trường: Cài đặt docker trên các node triển khai dịch vụ
- Deploy các dịch vụ theo version sử dụng docker
- <strike>\*Triển khai các dịch vụ trên nhiều hosts khác nhau</strike>

- Đảm bảo tính HA cho các dịch vụ web và api:
- Mỗi dịch vụ web và api được triển khai trên ít nhất 02 container khác nhau
- Requests đến các endpoint web và api được cân bằng tải thông qua các công cụ load balancer, ví dụ: nginx, haproxy và traefik
- \*Các công cụ load balancer cũng được triển khai theo mô hình cluster
- <strike>\*Triển khai db dưới dạng cluster</strike>

Output:

- Ảnh minh họa kiến trúc triển khai và bản mô tả
<img src= images/cluster_architecture.jpg>
<img src= images/all_port.png>

- Thư mục chứa ansible playbook dùng để triển khai dịch vụ, trong thư mục này cần có :
- File inventory chứa danh sách các hosts triển khai
- source : [invenroty.yml](./ansible/inventories/inventory.yml)
- Các file playbook
- source : [invenroty.yml](./ansible)
- Thư mục roles chứa các role :
- common: Setup môi trường trước deploy (done)
- web: Triển khai dịch vụ web (done)
- api: Triển khai dịch vụ api (done)
- db: Triển khai dịch vụ db (done)
- lb: Triển khai dịch vụ load balancing (done)
- File setup CD
- source : [ci-cd file](../../.github/workflows/ci_cd.yaml)
- Output của luồng build và push Docker Image lên Docker Hub
<img src= images/push_to_docker_hub.png>
- Hướng dẫn sử dụng ansible playbook để triển khai các thành phần hệ thống

```
cd Viettel-Digital-Talent-2023/10.GK/Vu_Minh_Hieu/ansible
sudo ansible-playbook -i invenroty/inventory.yml playbook-aio.yml
```

- Output log triển khai hệ thống
- source : [logs](./logs/)

### 4. Monitoring

Yêu cầu:

- Viết ansible playbook roles monitor thực hiện các nhiệm vụ:
- Cài đặt các dịch vụ node exporter và cadvisor dưới dạng container
- Đẩy thông số giám sát lên hệ thống giám sát Prometheus tập trung
- Chú ý: Tên các container có tiền tố là <username>\_ để phân biệt thông số giám sát dịch vụ của các sinh viên trên hệ thống giám sát tập trung. Thông tin <username> của từng sinh viên cho bởi bảng trong Phụ lục I.

Output:

- Role monitor chứa các playbook và cấu hình giám sát cho hệ thống
- source : [moniroring](./ansible/roles/monitoring/)
- Ảnh chụp dashboard giám sát nodes & containers, có thể sử dụng hệ thống prometheus tập trung ở 171.236.38.100:9090
<img src= images/prometheus.png>

### 5. Logging

Yêu cầu:

- Viết ansible playbook thực hiện các nhiệm vụ:
- Cài đặt dịch vụ logstash hoặc fluentd để collect log từ các dịch vụ web, api và db
- Đẩy log dịch vụ lên hệ thống Elasticsearch tập trung 171.236.38.100:9200
- Log phải đảm bảo có ít nhất các thông tin: IP truy cập, thời gian, action tác động, kết quả (thành công/không thành công/status code)
- Log được index với tiền tố <username>\_ để phân biệt log dịch vụ của các sinh viên khác nhau. Thông tin <username> của từng sinh viên cho bởi bảng trong Phụ lục I.

Output:

- Ansible playbook triển khai các dịch vụ collect log (tách module logging)
- source : [moniroring](./ansible/roles/logging/)
- Ảnh chụp sample log từ Kibana 171.236.38.100:5601
<img src= images/elastic.png>
11 changes: 11 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/inventories/inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
all:
children:
controller:
hosts:
local:
ansible_connection: local
ansible_python_interpreter: /usr/bin/python3
ansible_become_user: ubuntu
ansible_become_password: 123
host_name: vth
6 changes: 6 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/playbook-aio.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- hosts: all
roles:
- mongodb
- backend
- nginx
- frontend
3 changes: 3 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/playbook-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: all
roles:
- backend
3 changes: 3 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/playbook-common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: all
roles:
- common
3 changes: 3 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/playbook-db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: all
roles:
- mongodb
3 changes: 3 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/playbook-lb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: all
roles:
- nginx
3 changes: 3 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/playbook-logging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: all
roles:
- logging
3 changes: 3 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/playbook-monitoring.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: all
roles:
- monitoring
3 changes: 3 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/playbook-web.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: all
roles:
- frontend
2 changes: 2 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/roles/backend/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
- include_tasks: service.yml
35 changes: 35 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/roles/backend/tasks/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- name: Run the docker 1 API images
community.docker.docker_container:
name: backend1
image: hieuminhvuu/mt_api_arm64_ver2
state: started
restart: true
ports:
- "8000:8000"
env:
MONGODB_HOST : "172.17.0.1"
command: uvicorn main:app --reload --host 0.0.0.0 --port 8000

- name: Run the docker 2 API images
community.docker.docker_container:
name: backend2
image: hieuminhvuu/mt_api_arm64_ver2
state: started
restart: true
ports:
- "8001:8001"
env:
MONGODB_HOST : "172.17.0.1"
command: uvicorn main:app --reload --host 0.0.0.0 --port 8001

- name: Run the docker 3 API images
community.docker.docker_container:
name: backend3
image: hieuminhvuu/mt_api_arm64_ver2
state: started
restart: true
ports:
- "8002:8002"
env:
MONGODB_HOST : "172.17.0.1"
command: uvicorn main:app --reload --host 0.0.0.0 --port 8002
52 changes: 52 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/roles/common/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
# Edition can be one of: 'ce' (Community Edition) or 'ee' (Enterprise Edition).
docker_edition: 'ce'
docker_packages:
- "docker-{{ docker_edition }}"
- "docker-{{ docker_edition }}-cli"
- "docker-{{ docker_edition }}-rootless-extras"
- "containerd.io"
docker_packages_state: present

# Service options.
docker_service_manage: true
docker_service_state: started
docker_service_enabled: true
docker_restart_handler_state: restarted

# Docker Compose Plugin options.
docker_install_compose_plugin: false
docker_compose_package: docker-compose-plugin
docker_compose_package_state: present

# Docker Compose options.
docker_install_compose: true
docker_compose_version: "v2.11.1"
docker_compose_arch: "{{ ansible_architecture }}"
docker_compose_url: "https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-linux-{{ docker_compose_arch }}"
docker_compose_path: /usr/local/bin/docker-compose

# Enable repo setup
docker_add_repo: true

# Docker repo URL.
docker_repo_url: https://download.docker.com/linux

# Used only for Debian/Ubuntu. Switch 'stable' to 'nightly' if needed.
docker_apt_release_channel: stable
docker_apt_arch: "{{ 'arm64' if ansible_architecture == 'aarch64' else 'amd64' }}"
docker_apt_repository: "deb [arch={{ docker_apt_arch }}] {{ docker_repo_url }}/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} {{ docker_apt_release_channel }}"
docker_apt_ignore_key_error: true
docker_apt_gpg_key: "{{ docker_repo_url }}/{{ ansible_distribution | lower }}/gpg"

# Used only for RedHat/CentOS/Fedora.
docker_yum_repo_url: "{{ docker_repo_url }}/{{ (ansible_distribution == 'Fedora') | ternary('fedora','centos') }}/docker-{{ docker_edition }}.repo"
docker_yum_repo_enable_nightly: '0'
docker_yum_repo_enable_test: '0'
docker_yum_gpg_key: "{{ docker_repo_url }}/centos/gpg"

# A list of users who will be added to the docker group.
docker_users: []

# Docker daemon options as a dict
docker_daemon_options: {}
7 changes: 7 additions & 0 deletions 10.GK/Vu_Minh_Hieu/ansible/roles/common/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: restart docker
service:
name: docker
state: "{{ docker_restart_handler_state }}"
ignore_errors: "{{ ansible_check_mode }}"
when: docker_service_manage | bool
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- name: Converge
hosts: all
become: true

pre_tasks:
- name: Update apt cache.
apt: update_cache=yes cache_valid_time=600
when: ansible_os_family == 'Debian'

- name: Wait for systemd to complete initialization. # noqa 303
command: systemctl is-system-running
register: systemctl_status
until: >
'running' in systemctl_status.stdout or
'degraded' in systemctl_status.stdout
retries: 30
delay: 5
when: ansible_service_mgr == 'systemd'
changed_when: false
failed_when: systemctl_status.rc > 1

roles:
- role: geerlingguy.docker
Loading