아마존 클라우드

[클라우드] AWS CI/CD 사용하기 -3 EKS를 사용하여 클러스터 구성 및 배포하기

트리스탄1234 2023. 5. 11. 05:40
728x90
반응형

안녕하세요 이웃님들 ^.^

좋은 아침 입니다.

오늘은 Codepipeline으로 생성되고, Elastic container registry에 저장되어 있는 이미지를 EKS의 클러스터 구성 후 배포하는 방법에 대해서 포스팅을 해보려고 합니다.

EKS로 클러스터 정의 및 배포 하기

  1. 아래의 명령을 사용하여 AWS의 k8s-test디렉토리를 Baston 호스트의 디렉토리로 복사를 진행 합니다.
sh-4.2$ cd ~ && git clone ssh://$KEYID@git-codecommit.us-west-2.amazonaws.com/v1/repos/k8s-test
Cloning into 'k8s-test'...
warning: You appear to have cloned an empty repository.
sh-4.2$

2. 아래 명령을 사용해서 복제한 디렉토리로 이동을 합니다.

sh-4.2$ cd k8s-test
sh-4.2$

3. 아래 명령을 사용하여 네임스페이와 작업 디렉토리를 생성 합니다.

h-4.2$ mkdir namespaces workloads
sh-4.2$ ls -al
total 0
drwxr-xr-x 5 ssm-user ssm-user 53 May 1 11:35 .
drwxr-xr-x 7 ssm-user root 102 May 1 11:33 ..
drwxr-xr-x 7 ssm-user ssm-user 119 May 1 11:33 .git
drwxr-xr-x 2 ssm-user ssm-user 6 May 1 11:35 namespaces
drwxr-xr-x 2 ssm-user ssm-user 6 May 1 11:35 workloads
sh-4.2$

4. 아래 명령으로 레포지토리의 uri를 확인 합니다.(Elastic container registry정보)

sh-4.2$ aws ecr describe-repositories | jq '.repositories[] | select( .repositoryName == "eks-example")'
{
"repositoryArn": "arn:aws:ecr:us-west-2:158235272462:repository/eks-example",
"registryId": "158235272462",
"repositoryName": "eks-example",
"repositoryUri": "158235272462.dkr.ecr.us-west-2.amazonaws.com/eks-example",
"createdAt": "2023-05-01T07:23:48+00:00",
"imageTagMutability": "MUTABLE",
"imageScanningConfiguration": {
"scanOnPush": false
},
"encryptionConfiguration": {
"encryptionType": "AES256"
}
}
sh-4.2$

5. 레파지토리의 정보를 변수에 아래 명령을 사용하여 저장 합니다.

sh-4.2$ REPO_URI=$(aws ecr describe-repositories | jq -r '.repositories[] | select( .repositoryName == "eks-example") | .repositoryUri')

6. 아래 명령을 사용하여 네임 스페이스를 정하는 yaml파일을 생성 합니다.

h-4.2$ cat << EOF > namespaces/eks-example.yaml
> apiVersion: v1
> kind: Namespace
> metadata:
> labels:
> name: eks-example
> name: eks-example
> EOF

7. 아래 명령을 사용해서 k8s-test 디렉토리에 eks-deployment를 정의하는 yaml파일을 생성 합니다.

h-4.2$ cat << EOF > workloads/eks-example-deployment.yaml
> apiVersion: apps/v1
> kind: Deployment
> metadata:
> name: eks-example
> namespace: eks-example
> labels:
> app: eks-example
> annotations:
> flux.weave.works/automated: "true"
> spec:
> replicas: 1
> selector:
> matchLabels:
> app: eks-example
> template:
> metadata:
> labels:
> app: eks-example
> spec:
> containers:
> - name: eks-example
> image: $REPO_URI
> imagePullPolicy: IfNotPresent
> ports:
> - containerPort: 80
> name: http
> protocol: TCP
> livenessProbe:
> httpGet:
> path: /
> port: http
> readinessProbe:
> httpGet:
> path: /
> port: http
> EOF
sh-4.2$
반응형

8. 아래 명령을 사용해서 eks-example에 대한 service객체를 생성할 yaml파일을 생성 합니다.

h-4.2$ cat << EOF > workloads/eks-example-svc.yaml
> apiVersion: v1
> kind: Service
> metadata:
> name: eks-example
> namespace: eks-example
> labels:
> app: eks-example
> spec:
> type: LoadBalancer
> ports:
> - port: 80
> targetPort: http
> protocol: TCP
> name: http
> selector:
> app: eks-example
> EOF
sh-4.2$

9. 아래 명령을 사용해서 새로 생성한 yaml 파일들을 commit하고 k8s-tes로 push를 합니다.

sh-4.2$ git add .
sh-4.2$ git commit -am "eks-example-deployment"
[main (root-commit) 0453b90] eks-example-deployment
3 files changed, 57 insertions(+)
create mode 100644 namespaces/eks-example.yaml
create mode 100644 workloads/eks-example-deployment.yaml
create mode 100644 workloads/eks-example-svc.yaml
sh-4.2$ git push
Warning: Permanently added the RSA host key for IP address '52.119.161.60' to the list of known hosts.
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 2 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 950 bytes | 950.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Validating objects: 100%
To ssh://git-codecommit.us-west-2.amazonaws.com/v1/repos/k8s-test
* [new branch] main -> main
sh-4.2$

Weave Flux 어플리케이션 배포.

이제 EKS의 클러스터를 구성할 모든 정보를 구성 하고 레포지토리에 업로드한 상태 입니다. 이제 이 정의된 EKS 구성정보를 사용하여 어플리케이션 배포를 실행해 보겠습니다.

  1. 아래 명령을 사용하여 리소스들의 상태를 확인 합니다.
kubectl get namespaces,services,deployments,pods --all-namespaces
NAME STATUS AGE
namespace/default Active 140m
namespace/kube-node-lease Active 140m
namespace/kube-public Active 140m
namespace/kube-system Active 140m
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/kubernetes ClusterIP 172.20.0.1 <none> 443/TCP 140m
kube-system service/kube-dns ClusterIP 172.20.0.10 <none> 53/UDP,53/TCP 140m
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
kube-system deployment.apps/coredns 2/2 2 2 140m
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system pod/aws-node-9ft6j 1/1 Running 0 129m
kube-system pod/aws-node-hxlw9 1/1 Running 0 129m
kube-system pod/aws-node-nfflj 1/1 Running 0 128m
kube-system pod/coredns-85d5b4454c-km44r 1/1 Running 0 139m
kube-system pod/coredns-85d5b4454c-xx4lg 1/1 Running 0 139m
kube-system pod/kube-proxy-2w6kq 1/1 Running 0 129m
kube-system pod/kube-proxy-44hxl 1/1 Running 0 129m
kube-system pod/kube-proxy-74pp5 1/1 Running 0 128m

2. 아래 명령을 사용하여 flux cli를 설치 합니다.

sh-4.2$ curl -s https://fluxcd.io/install.sh | sudo bash
[INFO] Using 2.0.0-rc.1 as release
[INFO] Verifying binary download
which: no shasum in (/sbin:/bin:/usr/sbin:/usr/bin)
[INFO] Installing flux to /usr/local/bin/flux

3. 제대로 설치가 되었는지 아래 명령을 사용하여 확인 합니다.

sh-4.2$ flux check --pre
► checking prerequisites
✔ Kubernetes 1.23.17-eks-ec5523e >=1.20.6-0
✔ prerequisites checks passed
sh-4.2$

4. 아래 명령을 사용하여 Flux를 클러스터에 배포 합니다.

cd ~/k8s-config flux bootstrap git \ --url=ssh://${KEYID}@git-codecommit.us-west-2.amazonaws.com/v1/repos/k8s-config \ --private-key-file=/home/ssm-user/.ssh/id_rsa \ --branch=main \ --interval=1m0s
► cloning branch "main" from Git repository "ssh://APKAV5SEBRCMYYVOXXN5@git-codecommit.us-west-2.amazonaws.com/v1/repos/k8s-config"
✔ cloned repository
► generating component manifests
✔ generated component manifests
✔ committed sync manifests to "main" ("48c155caf6bfa14e187b3e4356b04c07309526ae")
► pushing component manifests to "ssh://APKAV5SEBRCMYYVOXXN5@git-codecommit.us-west-2.amazonaws.com/v1/repos/k8s-config"
✔ installed components
✔ reconciled components
► determining if source secret "flux-system/flux-system" exists
► generating source secret
✔ public key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfw4NDDbGImgaxuVeWPtBTHwuGiLTQAr9/Ix/KDQIJS7TOF991j4PEAh7mwacX+IuhXQqNzUG7wGowe/nA4o17edUSsi8wNM28AClaw+oxlWqKw/BC4fKWs/Kf0C/fn2MXSkh/8vA3qCEps0/zlOKDCttXnTERjvJyr/7es3Tq0n/AlbdUlV9i9NrIaOh1p4iZz/4M266uGHK74Z0euG+7HNbw5gMhI62YkNMds1myhJN/F/CPvDDwu3XLbmpd1g72JfJI0PonN/JOxZs+XGTd4CxveVDJLaLaLj0a5OMXg6yAkZM08Ziy0Oybiwmr5Di/Z9zXdP8yzs0o8gCX5S1J
Please give the key access to your repository: y
► applying source secret "flux-system/flux-system"
✔ reconciled source secret
► generating sync manifests
✔ generated sync manifests
✔ committed sync manifests to "main" ("b21f5f93d93e6a36cf89f4d03f55e24b6dc10f24")
► pushing sync manifests to "ssh://APKAV5SEBRCMYYVOXXN5@git-codecommit.us-west-2.amazonaws.com/v1/repos/k8s-config"
► applying sync manifests
✔ reconciled sync configuration
◎ waiting for Kustomization "flux-system/flux-system" to be reconciled
✔ Kustomization reconciled successfully
► confirming components are healthy
✔ helm-controller: deployment ready
✔ kustomize-controller: deployment ready
✔ notification-controller: deployment ready
✔ source-controller: deployment ready
✔ all components are healthy

5. 아래 명령을 이용하여 레포지토리에 생성된 메니페스트를 로컬로 pull을 실행 시킨 후 그 내용을 살펴 봅니다.

git pull
ls flux-system

6. 이제 생성된 리소스들을 아래 명령을 이용해서 살펴 봅니다.

kubectl get deployments,pods --all-namespaces

이제 모든 작업이 완료 되었습니다.

즐겁고 행복한 하루 되세요 ^.^

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."

728x90
반응형