Skip to content

Commit

Permalink
feat: add locust for load testing
Browse files Browse the repository at this point in the history
  • Loading branch information
amber committed Oct 22, 2024
1 parent 0293ea0 commit 6962a88
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 146 deletions.
File renamed without changes.
7 changes: 7 additions & 0 deletions helm/templates/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ spec:
name: rabbitmq
port:
number: 15672
- path: /locust
pathType: Prefix
backend:
service:
name: locust-master
port:
number: 8089
File renamed without changes.
27 changes: 27 additions & 0 deletions locust/locust-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: locust-config
namespace: locust
data:
locust.py: |
from locust import HttpUser, task, between
class NshmUser(HttpUser):
wait_time = between(1, 5)
@task
def web_service(self):
self.client.get("/")
@task
def account_service(self):
self.client.get("/api/accounts")
@task
def item_service(self):
self.client.get("/api/items")
@task
def wishlist_service(self):
self.client.get("/api/wishlists")
46 changes: 46 additions & 0 deletions locust/locust-master-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: locust-master
namespace: locust
spec:
replicas: 1
selector:
matchLabels:
app: locust-master
template:
metadata:
labels:
app: locust-master
spec:
containers:
- name: locust
image: locustio/locust:latest
args:
- "-f"
- "/mnt/locust/locust.py"
- "--master"
- "--host=http://www.nshm.store"
ports:
- containerPort: 8089
volumeMounts:
- name: locust
mountPath: /mnt/locust
volumes:
- name: locust
configMap:
name: locust-config
---
apiVersion: v1
kind: Service
metadata:
name: locust-master
namespace: locust
spec:
type: ClusterIP
selector:
app: locust-master
ports:
- protocol: TCP
port: 8089
targetPort: 8089
37 changes: 37 additions & 0 deletions locust/locust-worker-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: locust-worker
namespace: locust
spec:
replicas: 1
selector:
matchLabels:
app: locust-worker
template:
metadata:
labels:
app: locust-worker
spec:
containers:
- name: locust
image: locustio/locust:latest
args:
- "-f"
- "/mnt/locust/locust.py"
- "--worker"
- "--master-host=locust-master"
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "1"
memory: "1Gi"
volumeMounts:
- name: locust
mountPath: /mnt/locust
volumes:
- name: locust
configMap:
name: locust-config
19 changes: 19 additions & 0 deletions locust/locust-worker-hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: locust-worker-hpa
namespace: locust
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: locust-worker
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
1 change: 1 addition & 0 deletions terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.terraform/
*.tfstate*
modules/ec2/setup/*
25 changes: 25 additions & 0 deletions terraform/modules/ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,35 @@ resource "aws_instance" "nshm_bastion" {
#!/bin/bash
sudo apt-get update
sudo apt-get install -y curl unzip gnupg
# Install AWS CLI
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
# Install eksctl
ARCH=amd64
PLATFORM=$(uname -s)_$ARCH
curl -sLO "https://github.com/eksctl-io/eksctl/releases/latest/download/eksctl_$PLATFORM.tar.gz"
tar -xzf eksctl_$PLATFORM.tar.gz -C /tmp && rm eksctl_$PLATFORM.tar.gz
sudo mv /tmp/eksctl /usr/local/bin
# Install kubectl
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin
echo "source <(kubectl completion bash)" >> ~/.bashrc
# Install Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# Deploy ArgoCD Applicaiton
ARGOCD_VERSION=$(curl --silent "https://api.github.com/repos/argoproj/argo-cd/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
curl -sSL -o /tmp/argocd-${ARGOCD_VERSION} https://github.com/argoproj/argo-cd/releases/download/${ARGOCD_VERSION}/argocd-linux-amd64
chmod +x /tmp/argocd-${ARGOCD_VERSION}
sudo mv /tmp/argocd-${ARGOCD_VERSION} /usr/local/bin/argocd
aws s3 cp s3://nus-backend-terraform/setup/ec2-installation.sh .
bash ec2-installation.sh
EOF
Expand Down
22 changes: 0 additions & 22 deletions terraform/modules/ec2/setup/argo-values.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions terraform/modules/ec2/setup/argocd-app.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions terraform/modules/ec2/setup/argocd-project.yaml

This file was deleted.

18 changes: 0 additions & 18 deletions terraform/modules/ec2/setup/argocd-prometheus.yaml

This file was deleted.

18 changes: 0 additions & 18 deletions terraform/modules/ec2/setup/argocd-rabbitmq.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions terraform/modules/ec2/setup/cloudflare-adddns.sh

This file was deleted.

51 changes: 15 additions & 36 deletions terraform/modules/ec2/setup/ec2-installation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,15 @@

aws s3 cp s3://nus-backend-terraform/setup . --recursive
aws s3 cp s3://nus-backend-terraform/envs envs --recursive
source ec2
bash update-envs.sh

# Install eksctl
ARCH=amd64
PLATFORM=$(uname -s)_$ARCH
curl -sLO "https://github.com/eksctl-io/eksctl/releases/latest/download/eksctl_$PLATFORM.tar.gz"
tar -xzf eksctl_$PLATFORM.tar.gz -C /tmp && rm eksctl_$PLATFORM.tar.gz
sudo mv /tmp/eksctl /usr/local/bin

# Install AWS CLI
# curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
# unzip awscliv2.zip
# sudo ./aws/install

# Install kubectl
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin

# Install Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

#
# Update kubeconfig for EKS
aws eks --region ap-southeast-1 update-kubeconfig --name nshm-eks

# Update envs and create secrets for nshm namespace
bash update-envs.sh
bash create-secrets.sh
source envs/ec2

# Deploy autoscaler
helm repo add autoscaler https://kubernetes.github.io/autoscaler
export AWS_ACCESS_KEY_ID=$(awk -F ' = ' '/aws_access_key_id/ {print $2}' ~/.aws/credentials)
Expand Down Expand Up @@ -61,25 +44,21 @@ helm install aws-load-balancer-controller eks/aws-load-balancer-controller \
--set vpcId=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=nshm-bastion" --query "Reservations[*].Instances[*].VpcId" --output text)
kubectl get deployment -n kube-system aws-load-balancer-controller


# Deploy ArgoCD with ALB Ingress
cd argocd
kubectl create namespace argocd
helm repo add argo https://argoproj.github.io/argo-helm
helm install argocd argo/argo-cd -n argocd --create-namespace -f argo-values.yaml
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d

# Add argocd.nshm.store record in cloudflare
bash cloudflare-adddns.sh

# Deploy ArgoCD Applicaiton
ARGOCD_VERSION=$(curl --silent "https://api.github.com/repos/argoproj/argo-cd/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
curl -sSL -o /tmp/argocd-${ARGOCD_VERSION} https://github.com/argoproj/argo-cd/releases/download/${ARGOCD_VERSION}/argocd-linux-amd64
chmod +x /tmp/argocd-${ARGOCD_VERSION}
sudo mv /tmp/argocd-${ARGOCD_VERSION} /usr/local/bin/argocd
kubectl create ns nshm
kubectl apply -f argocd-app.yaml
kubectl apply -f argocd-rabbitmq.yaml
kubectl apply -f argocd-prometheus.yaml
kubectl create ns nshm rabbitmq prometheus locust
kubectl apply -f argo-rabbitmq-app.yaml
kubectl apply -f argo-prometheus-app.yam
kubectl apply -f argo-nshm-app.yaml
kubectl apply -f argo-locust-app.yaml

bash cloudflare-adddns.sh
kubectl get secret argocd-initial-admin-secret -n argocd -o jsonpath="{.data.password}" | base64 --decode > argocd-admin-secret.txt

# Create secrets for nshm namespace
bash create-secrets.sh

0 comments on commit 6962a88

Please sign in to comment.