CLUSTER_NAME=<CLUSTER_NAME>
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
Shell
복사
aws sqs create-queue --queue-name wsi-demo-queue
Shell
복사
aws ecr create-repository \
--repository-name sqsconsumer
Shell
복사
cat << EOF > iam_policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetQueueAttributes",
"Effect": "Allow",
"Action": [
"sqs:GetQueueAttributes",
"sqs:ReceiveMessage",
"sqs:GetQueueUrl",
"sqs:ListQueues",
"sqs:deletemessage"
],
"Resource": "*"
}
]
}
EOF
Shell
복사
aws iam create-policy \
--policy-name SqsPolicy \
--policy-document file://iam_policy.json
Shell
복사
eksctl create iamserviceaccount \
--cluster=$CLUSTER_NAME \
--namespace=keda-sqs-guidance \
--name=keda-operator \
--role-name=keda-operator-role \
--attach-policy-arn=arn:aws:iam::$AWS_ACCOUNT_ID:policy/SqsPolicy \
--approve
Shell
복사
helm repo add kedacore https://kedacore.github.io/charts
helm repo update
helm install keda kedacore/keda \
-n keda-sqs-guidance \
--set serviceAccount.operator.create=false \
--set serviceAccount.operator.name=keda-operator
Shell
복사
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: sqsconsumer-hpa
namespace: keda-sqs-guidance
spec:
scaleTargetRef:
name: sqs-consumer-backend
minReplicaCount: 2
maxReplicaCount: 10
pollingInterval: 10
cooldownPeriod: 60
advanced:
horizontalPodAutoscalerConfig:
behavior:
scaleDown:
stabilizationWindowSeconds: 30 # 최근 30초간 메트릭 참고
policies:
- type: Percent
value: 100 # 최대 100%까지 줄일 수 있음
periodSeconds: 15 # 15초마다 스케일 인 평가
triggers:
- type: aws-sqs-queue
metadata:
queueURL: https://sqs.ap-northeast-2.amazonaws.com/362708816803/wsi-demo-queue
activationQueueLength: "0"
queueLength: "5"
awsRegion: ap-northeast-2
identityOwner: operator
YAML
복사
kubectl apply -f scaledobject.yaml
Shell
복사
Deploy App
apiVersion: apps/v1
kind: Deployment
metadata:
name: sqs-consumer-backend
namespace: keda-sqs-guidance
spec:
selector:
matchLabels:
app: sqs-consumer-backend
template:
metadata:
labels:
app: sqs-consumer-backend
spec:
serviceAccountName: keda-operator
containers:
- name: sqs-consumer
image: 362708816803.dkr.ecr.ap-northeast-2.amazonaws.com/sqsconsumer:latest
env:
- name: RELIABLE_QUEUE_NAME
value: wsi-demo-queue
- name: AWS_REGION
value: ap-northeast-2
- name: MAX_MSGS_PER_BATCH
value: "5"
- name: MSG_POLL_BACKOFF
value: "2"
- name: MSG_PROCESS_DELAY
value: "10"
- name: TOT_MSGS_TO_PROCESS
value: "10000"
- name: LOG_LEVEL
value: INFO
YAML
복사
kubectl apply -f deployment.yaml
Shell
복사
SQS Test
QUEUE_URL=$(aws sqs get-queue-url --queue-name wsi-demo-queue --query 'QueueUrl' --output text)
Shell
복사
•
while
for i in `seq 500`; do aws sqs send-message --queue-url ${QUEUE_URL} --message-body "XXXX" --no-cli-pager --output text; done
Shell
복사
•
single
aws sqs send-message --queue-url ${QUEUE_URL} --message-body "Test message1"
Shell
복사