ENV
export EKS_CLUSTER_NAME=<CLUSTER_NAME>
export EKS_NODE_GROUP_NAME=<NODE_GROUP_NAME>
export AWS_REGION=ap-northeast-2
Shell
복사
Create EBS Volume
aws ec2 create-volume --size 10 --volume-type gp3 --availability-zone ap-northeast-2a --tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=wsi-ebs}]'
Shell
복사
Policy
cat << EOF > ebs_policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:CreateSnapshot",
"ec2:DescribeVolumes",
"ec2:DescribeSnapshots"
],
"Resource": "*"
}
]
}
EOF
Shell
복사
Create Policy
NodeGroup Role Attach Policy
NODEGROUP_ROLE_NAME=$(aws eks describe-nodegroup --cluster-name $EKS_CLUSTER_NAME --nodegroup-name $EKS_NODE_GROUP_NAME --query "nodegroup.nodeRole" --output text | cut -d'/' -f2-)
aws iam attach-role-policy \
--policy-arn arn:aws:iam::$(aws sts get-caller-identity --query "Account" --output text):policy/EBSforEKSPolicy \
--role-name $NODEGROUP_ROLE_NAME
Shell
복사
Create Namespace
kubectl create ns skills
Shell
복사
Apply Cronjob
EBS_ID=$(aws ec2 describe-volumes --filters Name=tag:Name,Values=wsi-ebs --query 'Volumes[*].VolumeId' --output text)
Shell
복사
apiVersion: batch/v1
kind: CronJob
metadata:
name: ebs-snapshot-cronjob
namespace: skills
spec:
schedule: "*/10 * * * *" # 10분마다 실행
jobTemplate:
spec:
template:
spec:
containers:
- name: ebs-snapshot
image: amazon/aws-cli
command:
[
"sh",
"-c",
"aws ec2 create-snapshot --volume-id EBS_ID --description 'Automated backup'",
]
restartPolicy: OnFailure
YAML
복사
sed -i "s|EBS_ID|$EBS_ID|g" backup.yaml
Shell
복사
kubectl apply -f backup.yaml
Shell
복사
Check
kubectl get cronjob ebs-snapshot-cronjob -n skills
kubectl get jobs --sort-by=.metadata.creationTimestamp -n skills
Shell
복사