GitHub - mrbryside/workshop
KBazaar - Online Shopping Application (Workshop)
Pre-requisites
- Java 17
- Gradle
- pre-commit
- Docker
Getting Started
- Clone this repository
- cd to
kbazaardirectory and runmake setup(if not working you can run pre-commit manually) - Run
make testto run unit tests - Run
make runto start the application
API Documentation
- Swagger UI: http://localhost:8080/swagger-ui/index.html
Example for .env
export SONAR_HOST_URL={REPLACE_ME}
export SONAR_TOKEN=${SONAR_TOKEN}
# spring config
export SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/postgres
export SPRING_DATASOURCE_USERNAME=postgres
export SPRING_DATASOURCE_PASSWORD=postgres
export SECURITY_JWT_SECRET={REPLACE_ME}
export ENABLED_FEATURE_PROMOTION_LIST_API=falseExisting Features
- List all Shopper GET /shoppers
- Get Shopper by username GET /shoppers/{username}
- List all Products GET /products
- Get Product by sku GET /products/{sku}
- List all Promotions GET /promotions
- Get Promotion by code GET /promotions/{code}
Infrastructure
STEP0: Initial Repository
- ใช้ repository ที่เตรียมไว้ให้ตามกลุ่ม
- Replace all
<group-id>ด้วย กลุ่มตัวเอง e.ggroup-1 - ทำการเพิ่มสมาชิกใน Github ของทีมเพื่อให้สามารถเข้าถึง Repository ได้
- ไปที่ Settings > Collaborators and teams > Manage access
- กด
Add People - เลือก Role เป็น
Adminทุกคนเลย
STEP1: 🎃 Setup AWS Credential
เพื่อให้ Access AWS ได้เราจะกำหนด Credential เข้าไปให้ Shell ของเราก่อน หรือ set ใน .bashrc, .zshrc ก็ได้
export AWS_ACCESS_KEY_ID=<KEY> export AWS_SECRET_ACCESS_KEY=<SECRET>
STEP2: 🧾 Terraform
1.ติดตั้ง Terraform ให้เรียบร้อย
-
cdไปที่infra/terraformจากนั้นรัน -
สั่งเริ่มต้น Terraform
- สั่งสร้าง Terraform สร้าง Resource ใน AWS
terraform apply -var group_name="group-<ID>" --auto-approve- ตอบ
yesกด enter แล้วรอไปกดกินข้าวก่อน
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes
STEP3: 🚀 Deploy ยังไง?
เรา Deploy ด้วย ArgoCD ที่อยู่บน AWS EKS ผ่าน Terraform เพื่อให้ใช้งานได้ ต้องเตรียมของดังนี้
STEP3.1: 🍻 Setup CI/CD
ตั้งค่าสิทธิ์ในการเข้าถึงของ Github Action
- ไปที่ Settings > Actions > Workflow permissions > Read and write permissions
- Settings > Secrets and variables > Actions > New Repository
- SONAR_HOST_URL
- SONAR_TOKEN
STEP3.2: 🗂️ แก้ไข DATABASE_URL ให้เป็น url ของทีม
- find and replace
<db_dev_url>,<db_dev_username>,<db_dev_password>ให้เป็น database url DEV connection ของทีม - find and replace
<db_prod_url>,<db_prod_username>,<db_prod_password>ให้เป็น database url PRODUCTION connection ของทีม
🛟 Kubernetes
1.ติดตั้ง AWS CLI ให้เรียบร้อย
2.ติดตั้ง Kubernetes CLI ให้เรียบร้อย
3.เนื่องจากเราใช้ AWS EKS เป็น Kubernetes Cluster ดังนั้นเราต้องเอา Kubernetes Context จาก AWS EKS โดยสั่ง (มั่นใจว่าเรา run command ที่ terminal เดียวกันกับเรา export AWS_ACCESS_KEY_ID และ AWS_SECRET_ACCESS_KEY)
aws eks update-kubeconfig --region ap-southeast-1 --name "eks-group-<ID>"4.ลองสั่ง kubectl
ถ้าได้ผลลัพธ์ประมาณนี้เป็นอันใช้ได้
NAME STATUS AGE default Active 3d kube-system Active 3d kube-public Active 3d kube-node-lease Active 3d
เพิ่ม ingress controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.10.0/deploy/static/provider/cloud/deploy.yaml💺 ArgoCD
1.รันคำสั่งสร้าง Namespace
kubectl create namespace argocd2.รันคำสั่งติดตั้ง ArgoCD
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml3.หารหัสผ่าน ArgoCD ของ admin ไว้ก่อน
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo4.Forword Port เพื่อใช้งาน ArgoCD
kubectl port-forward svc/argocd-server -n argocd 8080:443- ไปที่ ArgoCD http://localhost:8080 แล้วใส่ Username
adminและ Password ที่ได้จากข้อ 3.
🚀 Deployment
- setup gitops สำหรับ development env
- กด
+ New Appแล้วใส่ข้อมูลดังนี้ - Application Name:
group-<ID>-dev - Project Name:
default - SYNC POLICY:
Automatic - ✅ PRUNE RESOURCES
- Repository URL:
https://github.com/<your-github>/workshop - Revision:
main - Path:
infra/gitops/dev - Cluster URL:
https://kubernetes.default.svc - กด
Createมุมบนซ้าย
- setup gitops สำหรับ production env
- กด
+ New Appแล้วใส่ข้อมูลดังนี้ - Application Name:
group-<ID>-prod - Project Name:
default - SYNC POLICY:
Automatic - ✅ PRUNE RESOURCES
- Repository URL:
https://github.com/<your-account>/workshop - Revision:
main - Path:
infra/gitops/prod - Cluster URL:
https://kubernetes.default.svc - กด
Createมุมบนซ้าย
💣 ใช้ AWS เสร็จแล้วอย่าลืม Destroy ทิ้งน๊า (ห้ามทำในขณะที่กำลังเรียน Workshop อยู่)
- ก่อนจะ Destroy ด้วย Terraform ควรจะลบ Applicaton ใน Argo ทิ้งก่อน
- สั่งรัน Terraform Destroy
terraform destroy -var group_name="group-<ID>" --auto-approve