๐Ÿ™‚0804 [EKS CI/CD - Ansible,Jenkins]

๋ง์ง€ยท2022๋…„ 8์›” 4์ผ
1
post-custom-banner

๐Ÿ“Œ EKS CI/CD

๐Ÿ“Œ EKS ์ค€๋น„

๐Ÿ“™ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

์ด๋ฆ„ : eks-server
์ด๋ฏธ์ง€ : amazon linux 2
์œ ํ˜• : t2.micro
ํ‚คํŽ˜์–ด ์„ ํƒ
๋„คํŠธ์›Œํฌ : MY-VPC, PUB2C
๋ณด์•ˆ๊ทธ๋ฃน : DEV-SG
์‚ฌ์šฉ์ž๋ฐ์ดํ„ฐ :

#!/bin/bash
timedatectl set-timezone Asia/Seoul
hostnamectl set-hostname eks-server
cd /tmp
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install
curl -o kubectl https://amazon-eks.s3-us-west-2.amazonaws.com/1.21.2/2021-07-05/bin/linux/amd64/kubectl
chmod +x ./kubectl
mv ./kubectl /usr/local/bin
echo "source <(kubectl completion bash)" >> /home/ec2-user/.bashrc
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
mv /tmp/eksctl /usr/local/bin

๋งŒ๋“ค์–ด๋‘๊ณ  ์ค‘์ง€์‹œํ‚จ jenkins,ansible ์ธ์Šคํ„ด์Šค ์‹คํ–‰.

๐Ÿ“™ ๋ ˆ์ฝ”๋“œ ์ƒ์„ฑ

๐Ÿ“™ mobaxterm์œผ๋กœ ์ง„์ž…ํ•˜์—ฌ ๋ฒ„์ „ํ™•์ธ

[ec2-user@eks-server ~]$ aws --version
aws-cli/2.7.21 Python/3.9.11 Linux/5.10.130-118.517.amzn2.x86_64 exe/x86_64.amzn.2 prompt/off
[ec2-user@eks-server ~]$ kubectl version
Client Version: version.Info{Major:"1", Minor:"21+", GitVersion:"v1.21.2-13+d2965f0db10712", GitCommit:"d2965f0db1071203c6f5bc662c2827c71fc8b20d", GitTreeState:"clean", BuildDate:"2021-06-26T01:02:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
[ec2-user@eks-server ~]$ eksctl version
0.107.0

๐Ÿ“™ ํ‚คํ˜•์‹์—์„œ ํŒจ์Šค์›Œ๋“œ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ

[ec2-user@eks-server ~]$ sudo passwd ec2-user
Changing password for user ec2-user.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[ec2-user@eks-server ~]$ sudo vi /etc/ssh/sshd_config
PasswordAuthentication yes
#PermitEmptyPasswords no
#PasswordAuthentication no
[ec2-user@eks-server ~]$ sudo systemctl restart sshd

๐Ÿ“™ IAM ์—ญํ• 

IAM - ์—ญํ•  - ์—ญํ•  ๋งŒ๋“ค๊ธฐ - ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์—”ํ„ฐํ‹ฐ ; AWS ์„œ๋น„์Šค - ์ผ๋ฐ˜ ์‚ฌ์šฉ ์‚ฌ๋ก€ : EC2 - ๋‹ค์Œ

AmazonEC2FullAccess ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ฒดํฌ - ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ -IAMFullAccess ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ฒดํฌ - AdministratorAccess ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ฒดํฌ -
AWSCloudFormationFullAccess ๊ฒ€์ƒ‰ํ•˜์—ฌ์ฒดํฌ - ๋‹ค์Œ

์—ญํ•  ์ด๋ฆ„ : eksctl_role - ์—ญํ•  ์ƒ์„ฑ ํด๋ฆญ

EC2 ๋Œ€์‹œ๋ณด๋“œ - ์ธ์Šคํ„ด์Šค - eks-server ์„ ํƒ - ๋งˆ์šฐ์Šค ์šฐํด๋ฆญ - ๋ณด์•ˆ - IAM ์—ญํ•  ์ˆ˜์ •

IAM ์—ญํ• ์—์„œ eksctl_role ์„ ํƒํ•˜๊ณ  ์—…๋ฐ์ดํŠธ

๐Ÿ“™ ํด๋Ÿฌ์Šคํ„ฐ ์ƒ์„ฑ ๋ฐ ๋…ธ๋“œ๊ทธ๋ฃน ์ƒ์„ฑ

[ec2-user@eks-server ~]$
eksctl create cluster --name EKS-CLUSTER --region ap-northeast-2 --version 1.21 --vpc-public-subnets subnet-00d4b7ccf0a5221f0,subnet-0833b70ce2cfd51cb --without-nodegroup
[ec2-user@eks-server ~]$
eksctl create nodegroup \
  --cluster EKS-CLUSTER \
  --region ap-northeast-2 \
  --name NODEGROUP \
  --node-type t2.micro \
  --nodes 4 \
  --nodes-min 4 \
  --nodes-max 8 \
  --ssh-access \
  --ssh-public-key docker-key3

๐Ÿ“™ ansible๊ณผ eks์„œ๋ฒ„ ์—ฐ๊ฒฐ

anisible ์„œ๋ฒ„์—์„œ ์ง„ํ–‰


    
    
[ec2-user@ansible-server docker]$ sudo vi /etc/ansible/hosts ## ์ด ๊ฒฝ๋กœ์— eks ๋‚ด๋ถ€ IP ์ถ”๊ฐ€
[eks-server]
10.14.42.197

[ec2-user@ansible-server docker]$ ssh-copy-id 10.14.42.197
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ec2-user/.ssh/id_rsa.pub"
The authenticity of host '10.14.42.197 (10.14.42.197)' can't be established.
ECDSA key fingerprint is SHA256:7mAVCmKs8gPFNHMdMCEEQi4kl8lafBsu/OObvZqJ5Kk.
ECDSA key fingerprint is MD5:b4:c3:ba:25:89:0c:13:cd:96:d4:e2:60:cf:c7:f5:5e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

[ec2-user@ansible-server docker]$ ansible all -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
[WARNING]: Platform linux on host 10.14.9.46 is using the discovered Python interpreter at /usr/bin/python, but
future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
10.14.9.46 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform linux on host 10.14.42.197 is using the discovered Python interpreter at /usr/bin/python,
but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
10.14.42.197 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

 

๐Ÿ“™ ansible์„œ๋ฒ„์—์„œ ์ด๋ฏธ์ง€ ๊ด€๋ฆฌ์— ํ•„์š”ํ•œ build.yml ์‹คํ–‰ํ•ด๋ณด๊ธฐ

[ec2-user@ansible-server ~]$ cd /opt/docker
[ec2-user@ansible-server docker]$ ll
total 944
-rw-rw-r-- 1 ec2-user ec2-user    228 Aug  3 11:48 build.yml
-rw-rw-r-- 1 ec2-user ec2-user    286 Aug  3 12:24 deploy.yml
-rw-rw-r-- 1 ec2-user ec2-user    122 Aug  3 11:23 Dockerfile
-rw-rw-r-- 1 ec2-user ec2-user 951830 Aug  3 14:13 webapp.war

[ec2-user@ansible-server docker]$ vi build.yml
- hosts: ansible-server

  tasks:
  - name: remove docker image
    command: docker rmi -f mj030kk/mytomcat:latest

  - name: create docker image
    command: docker build -t mj030kk/mytomcat:latest .
    args:
      chdir: /opt/docker

  - name: push docker image
    command: docker push mj030kk/mytomcat:latest
    
[ec2-user@ansible-server docker]$ ansible-playbook build.yml

-> ์ •์ƒ ์ž‘๋™ ํ™•์ธ

๐Ÿ“™ ansible ์„œ๋ฒ„ yml, eks ์„œ๋ฒ„ yamlํŒŒ์ผ ์ƒ์„ฑ.

[ec2-user@ansible-server docker]$  cd /opt/docker

[ec2-user@ansible-server docker]$  vi kube-deploy-svc.yml
- hosts: eks-server

  tasks:
  - name: remove deploy
    command: kubectl delete -f /home/ec2-user/test/deployment.yaml
    ignore_errors: yes
  - name: kube deploy
    command: kubectl apply -f /home/ec2-user/test/deployment.yaml
  - name: kube svc
    command: kubectl apply -f /home/ec2-user/test/svc.yaml
  - name: update
    command: kubectl rollout restart deployment/web-site-deployment
[ec2-user@eks-server ~]$ mkdir test && cd $_

[ec2-user@eks-server test]$ vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-site-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web-site-deployment
  template:
    metadata:
      name: web-site-deployment
      labels:
        app: web-site-deployment
    spec:
      containers:
      - name: web-site-deployment-container
        image: mj030kk/mytomcat:latest
        imagePullPolicy: Always
        
[ec2-user@eks-server test]$ vi svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: loadbalancer-service-deployment
spec:
  type: LoadBalancer
  selector:
    app: web-site-deployment
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080

 

์„œ๋น„์Šค์™€ deployment ๋ถ„๋ฆฌ ์ด์œ  : svc๊นŒ์ง€ ์ง€์› ๋‹ค๊ฐ€ ๋‹ค์‹œ๋งŒ๋“ค๋ฉด ์ ‘์† ์ฃผ์†Œ๊ฐ€ ๊ณ„์† ๋ฐ”๋€œ. deploy๋งŒ ๋ฐ”๊ฟ”์ฃผ๋Š”๊ฒŒ ๋” ์ ์ ˆ.

[ec2-user@eks-server test]$ kubectl apply -f deployment.yaml
deployment.apps/web-site-deployment created
[ec2-user@eks-server test]$ kubectl apply -f svc.yaml
service/loadbalancer-service-deployment created

[ec2-user@eks-server test]$ kubectl get all
NAME                                      READY   STATUS    RESTARTS   AGE
pod/web-site-deployment-b455c4dd4-g2dpt   1/1     Running   0          22s
pod/web-site-deployment-b455c4dd4-qpzw2   1/1     Running   0          22s

NAME                                      TYPE           CLUSTER-IP      EXTERNAL-IP                                                                    PORT(S)          AGE
service/kubernetes                        ClusterIP      172.20.0.1      <none>                                                                         443/TCP          50m
service/loadbalancer-service-deployment   LoadBalancer   172.20.160.25   a23e76eb992bf4fc79651a0b4685506d-1520966570.ap-northeast-2.elb.amazonaws.com   8080:30967/TCP   11s

NAME                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/web-site-deployment   2/2     2            2           23s

NAME                                            DESIRED   CURRENT   READY   AGE
replicaset.apps/web-site-deployment-b455c4dd4   2         2         2       23s

๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์ฃผ์†Œ :8080

๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์ฃผ์†Œ :8080/webapp

[ec2-user@eks-server test]$ kubectl delete -f .

๐Ÿ“™ ansible - eks ์—ฐ๊ฒฐ ํ™•์ธ (ansible playbook)

[ec2-user@ansible-server docker]$ ansible-playbook kube-deploy-svc.yml

[ec2-user@eks-server test]$ kubectl get svc
NAME                              TYPE           CLUSTER-IP      EXTERNAL-IP                                                                    PORT(S)          AGE
kubernetes                        ClusterIP      172.20.0.1      <none>                                                                         443/TCP          71m
loadbalancer-service-deployment   LoadBalancer   172.20.216.19   a459dbcb64f494db0a7fd15ccf3e4803-2089817562.ap-northeast-2.elb.amazonaws.com   8080:32673/TCP   2m1s


-> ์ •์ƒ์ž‘๋™ ํ™•์ธ

๐Ÿ“Œ ์  ํ‚จ์Šค์—์„œ ๋นŒ๋“œํ•˜๊ธฐ

์  ํ‚จ์Šค ๋Œ€์‹œ๋ณด๋“œ - ์ƒˆ๋กœ์šด ์•„์ดํ…œ - ์ด๋ฆ„ : Deploy_On_Kube_Ansible - Copy from : Copy_Artifacts_onto_Ansible - OK

์„ค๋ช… : ๋ฉ”์ด๋ธ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ์•ค์„œ๋ธ”์„ ํ™œ์šฉํ•ด์„œ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค(EKS)๋กœ ๋ฐฐํฌํ•˜์ž - ๋‚˜๋จธ์ง€ ๊ทธ๋Œ€๋กœ, ๋นŒ๋“œ ํ›„ ์กฐ์น˜ exec command :

cd /opt/docker;
ansible-playbook build.yml;
sleep 10;
ansible-playbook kube-deploy-svc.yml
  • apply, ์ €์žฅ - ์ง€๊ธˆ ๋นŒ๋“œ

์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธ ์™„๋ฃŒ.

[ec2-user@eks-server test]$ kubectl get all
NAME                                       READY   STATUS    RESTARTS   AGE
pod/web-site-deployment-7c66bb5dc8-lsj2p   1/1     Running   0          29s
pod/web-site-deployment-7c66bb5dc8-n6pf9   1/1     Running   0          55s

NAME                                      TYPE           CLUSTER-IP      EXTERNAL-IP                                                                    PORT(S)          AGE
service/kubernetes                        ClusterIP      172.20.0.1      <none>                                                                         443/TCP          87m
service/loadbalancer-service-deployment   LoadBalancer   172.20.216.19   a459dbcb64f494db0a7fd15ccf3e4803-2089817562.ap-northeast-2.elb.amazonaws.com   8080:32673/TCP   17m

NAME                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/web-site-deployment   2/2     2            2           59s

NAME                                             DESIRED   CURRENT   READY   AGE
replicaset.apps/web-site-deployment-7c66bb5dc8   2         2         2       55s
replicaset.apps/web-site-deployment-b455c4dd4    0         0         0       59s


์—”๋“œํฌ์ธํŠธ ์ ‘์† ์ฃผ์†Œ๋Š” ๋ฐ”๋€Œ์ง€ ์•Š์€ ๋ชจ์Šต.

๐Ÿ“™ ์„œ๋น„์Šค์ฃผ์†Œ์— ๋„๋ฉ”์ธ ํ• ๋‹น


๐Ÿ“™ ์›นํŽ˜์ด์ง€ ์ˆ˜์ •

git bash

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ rm -rf hello-world/

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ git clone https://github.com/mangjini/hello-world.git
Cloning into 'hello-world'...
remote: Enumerating objects: 572, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 572 (delta 0), reused 5 (delta 0), pack-reused 565
Receiving objects: 100% (572/572), 830.88 KiB | 2.25 MiB/s, done.
Resolving deltas: 100% (154/154), done.

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ cd hello-world/

r2com@DESKTOP-DD3FU43 MINGW64 ~/hello-world (master)
$ ls
Dockerfile  pom.xml            regapp-service.yml  test.tml
README.md   regapp-deploy.yml  server/             webapp/

r2com@DESKTOP-DD3FU43 MINGW64 ~/hello-world (master)
$ cd webapp/src/main/webapp/

r2com@DESKTOP-DD3FU43 MINGW64 ~/hello-world/webapp/src/main/webapp (master)
$ ls
WEB-INF/  css/     images/    index.jsp.bak  js/
assets/   gcp.tar  index.jsp  index.jsp.old  test.txt

r2com@DESKTOP-DD3FU43 MINGW64 ~/hello-world/webapp/src/main/webapp (master)
$ vi index.jsp

r2com@DESKTOP-DD3FU43 MINGW64 ~/hello-world/webapp/src/main/webapp (master)
$ git add .

r2com@DESKTOP-DD3FU43 MINGW64 ~/hello-world/webapp/src/main/webapp (master)
$ git commit -m "hi mj index.jsp"
[master 0be2874] hi mj index.jsp
 1 file changed, 1 insertion(+), 1 deletion(-)
 
r2com@DESKTOP-DD3FU43 MINGW64 ~/hello-world/webapp/src/main/webapp (master)
$ git push origin master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 507 bytes | 507.00 KiB/s, done.
Total 7 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/mangjini/hello-world.git
   8a0b4f1..0be2874  master -> master

์ž๋™ ๋นŒ๋“œ ํ™•์ธ

๋ณ€๊ฒฝ๋œ ์›นํŽ˜์ด์ง€ ํ™•์ธ

๐Ÿ“™ eks ์‚ญ์ œ

eks-server์—์„œ ์ง„ํ–‰

kubectl delete all -all
eksctl delete cluster EKS-CLUSTER --region ap-northeast-2

๐Ÿ“Œ SAA - ์—”๋“œํฌ์ธํŠธ

s3 - ๋ฒ„ํ‚ท์ƒ์„ฑ - ์ด๋ฆ„ : s3.lovemj.shop - ๋ฒ„ํ‚ท๋งŒ๋“ค๊ธฐ
๋ฒ„ํ‚ท- s3.lovemj.shop - ์†์„ฑ - ์ •์  ์›น์‚ฌ์ดํŠธ ํ˜ธ์ŠคํŒ… ํ™œ์„ฑํ™” - ์ธ๋ฑ์Šค ๋ฌธ์„œ - index.html - ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ €์žฅ

๋ ˆ์ฝ”๋“œ ์ƒ์„ฑ

ํ”„๋ผ์ด๋น— ์„œ๋ธŒ๋„ท์— ec2 ์ƒ์„ฑ
์ด๋ฆ„ : private-ec2
์ด๋ฏธ์ง€ : amazon linux 2
์œ ํ˜• : t2.micro
ํ‚คํŽ˜์–ด ์„ ํƒ
๋„คํŠธ์›Œํฌ : MY-VPC, PRI2A
๋ณด์•ˆ๊ทธ๋ฃน : DEV-SG

ansible์„œ๋ฒ„์—์„œ private๋กœ ์ง„์ž…
private์— eksctl role ์—ญํ•  ๋ถ€์—ฌ

[ec2-user@ansible-server ~]$ chmod 400 docker-key3.pem
[ec2-user@ansible-server ~]$ ssh -i docker-key3.pem ec2-user@10.14.79.12

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[ec2-user@ip-10-14-79-12 ~]$ ping s3.lovemj.shop
PING s3.lovemj.shop (52.219.58.64) 56(84) bytes of data.
^C
--- s3.lovemj.shop ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1021ms

[ec2-user@ip-10-14-79-12 ~]$ aws s3 ls s3://s3.lovemj.shop
Unable to locate credentials. You can configure credentials by running "aws configure".
[ec2-user@ip-10-14-79-12 ~]$ aws s3 ls s3://s3.lovemj.shop

์—”๋“œํฌ์ธํŠธ ์ด์šฉํ•˜๋ฉด ์•„์›ƒ๋ฐ”์šด๋“œ ๋ฐ์ดํ„ฐํŠธ๋žœ์Šคํผ ๋น„์šฉ ๋ฐœ์ƒ X.
๋‚ด๋ถ€๋ง ์ด์šฉํ•˜๊ธฐ๋•Œ๋ฌธ์— ๋ณด์•ˆ์ ์œผ๋กœ๋„ ์ข‹์Œ.

๐Ÿ“™ โœ”๏ธโœ๏ธ๐Ÿ“ขโญ๏ธ๐Ÿ“Œ

profile
๊พธ์ค€ํžˆ, ์ฐจ๊ทผ์ฐจ๊ทผ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€