AWS Cloud

[Cloud] Using AWS CI/CD -3 create cluster and deploy by using EKS

트리스탄1234 2023. 5. 14. 10:04
728x90
반응형

Today, I'm going to post about how to deploy an image created with Codepipeline and stored in an Elastic container registry after configuring an EKS cluster.

 

Define cluster and deploy in EKS

1. Use the command below to copy the k8s-test directory in AWS to the directory on the Baston host.

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. Use the command below to move to the cloned directory.

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

3/ Create a namespace and working directory using the commands below.

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. Check the uri of the repository with the command below. (Elastic container registry information)

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. Save the information in the repository to a variable using the command below.

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

6. Create a yaml file defining the namespace using the command below.

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

7. Create a yaml file defining eks-deployment in the k8s-test directory using the command below

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. Use the command below to create a yaml file to create a service object for eks-example.

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. Commit the newly created yaml files using the command below and push them to k8s-tes.

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$

Deploy Weave Flux Application.

You have now configured and uploaded all the information to configure EKS's cluster and uploaded it to the repository. Now, let's run application deployment using this defined EKS configuration information.

10. se the command below to check the status of resources.

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

11. Install flux cli using below command.

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

12. Check if the installation is successful using the command below.

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

13.  Deploy Flux to the cluster using the command below

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

14. Use the command below to locally pull the manifest created in the repository and examine its contents..

git pull
ls flux-system

15.Now, look at the created resources using the command below.

kubectl get deployments,pods --all-namespaces

Now everything is done.

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

728x90
반응형