0714[ECS,kubernetes]

๋ง์ง€ยท2022๋…„ 7์›” 15์ผ
1

๐Ÿ“Œ AWS - ECS

๐Ÿ“™ ์ž‘์—… ์ •์˜

โœ”๏ธ 0. ECS - ์ž‘์—…์ •์˜ (task)

โœ”๏ธ 1. ์‹œ์ž‘ ์œ ํ˜• ํ˜ธํ™˜์„ฑ - EC2 ์„ ํƒ
โœ”๏ธ 2. ํ…Œ์Šคํฌ ์ •์˜ ์ด๋ฆ„ : MY-TD

โœ”๏ธ 3. ์ž‘์—…ํฌ๊ธฐ ๊ณต๋ž€.
โœ”๏ธ 4. ์ปจํ…Œ์ด๋„ˆ ์ •์˜ - ์ปจํ…Œ์ด๋„ˆ ์ถ”๊ฐ€
ํ‘œ์ค€ ; docker run -d -p 80:80 --name containername img
โœ”๏ธ 4-1. ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ : webserver
โœ”๏ธ 4-2. ์ด๋ฏธ์ง€ : nginx
โœ”๏ธ 4-3. ๋ฉ”๋ชจ๋ฆฌ์ œํ•œ : 128
โœ”๏ธ 4-4. ํฌํŠธ๋งคํ•‘ : 80 : 80
โœ”๏ธ 4-5. ์ถ”๊ฐ€
โœ”๏ธ 4-6. ๋งจ ํ•˜๋‹จ ์ƒ์„ฑ๋ฒ„ํŠผ ํด๋ฆญ

๐Ÿ“™ ํด๋Ÿฌ์Šคํ„ฐ ์ƒ์„ฑ

โœ”๏ธ 1. ํด๋Ÿฌ์Šคํ„ฐ - ํด๋Ÿฌ์Šคํ„ฐ ์ƒ์„ฑ
ํ…œํ”Œ๋ฆฟ : EC2 Linux + ๋„คํŠธ์›Œํ‚น
ํด๋Ÿฌ์Šคํ„ฐ ๊ตฌ์„ฑ ;
ํด๋Ÿฌ์Šคํ„ฐ ์ด๋ฆ„ : MY-CLUSTER
์ธ์Šคํ„ด์Šค ๊ตฌ์„ฑ ;
ํ”„๋กœ๋น„์ €๋‹ ๋ชจ๋ธ - ์˜จ๋””๋งจ๋“œ ์ธ์Šคํ„ด์Šค
์ŠคํŒŸ์ธ์Šคํ„ด์Šค ;๋ณ‘๋ ฌ, ์ˆœ์‹๊ฐ„์— ๋งŽ์€ ๊ณต๊ฐ„์ด ํ•„์š”ํ•  ๋•Œ. ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ๋งŽ์ด ์“ด๋‹ค. ์ €๋ ดํ•ด์„œ.
EC2 ์ธ์Šคํ„ด์Šค ์œ ํ˜• : ์›ํ•˜๋Š” ์ธ์Šคํ„ด์Šค ์œ ํ˜• ์ง์ ‘ ์ž…๋ ฅ, t2.micro
์ธ์Šคํ„ด์Šค ๊ฐœ์ˆ˜ : 2
๋ฃจํŠธ๋ณผ๋ฅจํฌ๊ธฐ : 30
ํ‚ค ํŽ˜์–ด : ๋งŒ๋“ค์–ด๋‘” ๊ฒƒ ์„ ํƒ

โœ”๏ธ ๋„คํŠธ์›Œํ‚น VPC: MY-VPC
์„œ๋ธŒ๋„ท : PUBLIC SUBNET 2A,C
ํผ๋ธ”๋ฆญ IP ์ž๋™ํ• ๋‹น : ํ™œ์„ฑํ™” ๋จ
๋ณด์•ˆ๊ทธ๋ฃน : MY-SG-WEB
์ปจํ…Œ์ด๋„ˆ ์ธ์Šคํ„ด์Šค IAM์—ญํ•  : ๊ทธ๋Œ€๋กœ ๋‘๊ธฐ ์„ ํƒํ•  ๊ฑฐ ์žˆ์Œ ์„ ํƒํ•˜๊ณ .
-์ƒ์„ฑ๋ฒ„ํŠผ ํด๋ฆญ

๐Ÿ“™ ํด๋Ÿฌ์Šคํ„ฐ ์„œ๋น„์Šค ์ƒ์„ฑ

โœ”๏ธ ์„œ๋น„์Šค - ์ƒ์„ฑ ํด๋ฆญ

์‹œ์ž‘ ์œ ํ˜• : EC2
์ž‘์—… ๊ฐœ์ˆ˜ :2
์ตœ์†Œ ์ƒํƒœ ๋ฐฑ๋ถ„์œจ : 50 ; ํ•˜๋‚˜ ์ง€์šฐ๊ณ  ํ•˜๋‚˜ ๋‚จ๊ธฐ๊ณ 
์ตœ๋Œ€ ๋ฐฑ๋ถ„์œจ : 100; 2๊ฐœ๋ฅผ 100์œผ๋กœ ๋ณด๊ธฐ
->๋งŒ์•ฝ 100,200์ด๋ฉด 2๊ฐœ๊ฐ€ ๊ธฐ๋ณธ 4๊ฐœ๊นŒ์ง€ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์Œ. ๊ตฌํ˜• ์ง€์šฐ์ง€ ์•Š๊ณ  4๊ฐœ ๋งŒ๋“  ๋‹ค์Œ ์˜›๋‚ ๊ฑฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์ง€์šฐ๋Š” ๋Š๋‚Œ.

๋ฐฐํฌ์œ ํ˜• : ๋กค๋ง ์—…๋ฐ์ดํŠธ ;๋ฐฑ๋ถ„์œจ
-๋‹ค์Œ๋‹จ๊ณ„
๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์œ ํ˜• : ALB
์ƒํƒœ๊ฒ€์‚ฌ ์œ ์˜ˆ๊ธฐ๊ฐ„ : 150

EC2์ฝ˜์†”์—์„œ ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ๋งŒ๋“ค๊ณ  ์˜ค๊ธฐ(ALB์ƒ์„ฑ)
์ด๋ฆ„ : ECS-ALB
VPC: MY-VPC
์„œ๋ธŒ๋„ท: ํผ๋ธ”๋ฆญ 2a,2c
๋ณด์•ˆ๊ทธ๋ฃน : default ์ง€์šฐ๊ณ  MY-SG-WEB์„ ํƒ

๋Œ€์ƒ๊ทธ๋ฃน ์ƒ์„ฑ ํ•˜๊ณ  ์˜ค๊ธฐ
์ด๋ฆ„ : TG-ECS
ํ”„๋กœํ† ์ฝœ : 80

  • ๋‹ค์Œ
    ๋Œ€์ƒ๊ทธ๋ฃน worker1,2 (์ž‘์—…์ •์˜์—์„œ ๋งŒ๋“ ๊ฑฐ ์„ ํƒํ•ด์„œ ๋ณด๋ฅ˜์ค‘์ธ ๊ฒƒ์œผ๋กœ ํฌํ•จ)
    -์ƒ์„ฑ๋ฒ„ํŠผ ํด๋ฆญ

๋‹ค์‹œ ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์ฝ˜์†”์—์„œ ๋Œ€์ƒ๊ทธ๋ฃน TG-ECS ์„ ํƒํ•˜๊ณ  ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์ƒ์„ฑ ํด๋ฆญ
(๋ช…์‹œ ์•ˆํ•ด๋‘” ๊ฒƒ์€ default)

๋‹ค์‹œ ์„œ๋น„์Šค ์ƒ์„ฑ์œผ๋กœ ์™€์„œ, ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์ด๋ฆ„ ECS-ALB์„ ํƒ
๋ฐธ๋Ÿฐ์‹ฑํ•  ์ปจํ…Œ์ด๋„ˆ : ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ์— ์ถ”๊ฐ€๋ฒ„ํŠผ ํด๋ฆญ
ํ”„๋กœ๋•์…˜ ๋ฆฌ์Šค๋„ˆ ํฌํŠธ : 80:HTTP
๋Œ€์ƒ๊ทธ๋ฃน ์ด๋ฆ„ : TG-ECS

-๋‹ค์Œ๋‹จ๊ณ„

auto scaling

-๋‹ค์Œ - ์„œ๋น„์Šค ์ƒ์„ฑ๋ฒ„ํŠผ ํด๋ฆญ

-> ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ DNS ์ฃผ์†Œ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ.

๋ ˆ์ฝ”๋“œ ์ƒ์„ฑํ•ด์„œ ๊ฐ„์†Œํ™”๋œ ๋„๋ฉ”์ธ ์ด์šฉํ•˜๊ธฐ.

๐Ÿ“™ ์„œ๋น„์Šค ์—…๋ฐ์ดํŠธ

โœ”๏ธ ์ž‘์—… ์ •์˜ -MY-TD ํด๋ฆญ - ์ƒˆ ๊ฐœ์ •์ƒ์„ฑ ํด๋ฆญ

โœ”๏ธ ํ•˜๋‹จ์œผ๋กœ ๋‚ด๋ ค์„œ ์ปจํ…Œ์ด๋„ˆ ์ •์˜ -์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ ํด๋ฆญ - ์ด๋ฏธ์ง€ nginx๋ฅผ mj030kk/web-site:v1.0๋กœ ๋ฐ”๊พธ๊ธฐ. - ์—…๋ฐ์ดํŠธ ํด๋ฆญ

โœ”๏ธ ๋งจ ํ•˜๋‹จ ์ƒ์„ฑ๋ฒ„ํŠผ ํด๋ฆญ

ํด๋Ÿฌ์Šคํ„ฐ - MY-CLUSTER ํด๋ฆญ - ์„œ๋น„์Šค์—์„œ MY-WEBํด๋ฆญ- ์—…๋ฐ์ดํŠธ ํด๋ฆญ

๊ฐœ์ • 2, ์ƒˆ ๋ฐฐํฌ์ ์šฉ ์ฒดํฌ.


๋‹ค๋ฅธ ๊ฒƒ ๋‹ค ๊ทธ๋Œ€๋กœ ๋‘๊ณ  ๋‹ค์Œ๋‹จ๊ณ„ ์ง„ํ–‰.



๐Ÿ“Œ Kubernetes

โœ”๏ธ centos7 ova๊ฐ€์ ธ์˜ค๊ธฐ CPU2, RAM 4GB(์ตœ์†Œ์šฉ๋Ÿ‰)
โœ”๏ธ ์„ค์ • ; ์ด๋ฆ„ : Minikube ๋„คํŠธ์›Œํฌ - ๋งฅ ์–ด๋“œ๋ ˆ์Šค ์ƒˆ๋กœ๊ณ ์นจ - ์‹œ์ž‘
IP : 192.168.1.166

๐Ÿ“™ Minikube ์„ค์น˜

(single Node : Master Node + Worker Node), (Multi Node: Master Node, Worker Node)
์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์ปจํ…Œ์ด๋„ˆ ๋Ÿฐํƒ€์ž„์„ ์œ„ํ•ด ๋„์ปค ์„ค์น˜

# curl -fsSL https://get.docker.com/ | sudo sh
# systemctl enable --now docker
# yum install -y conntrack git
# curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.23.2/minikube-linux-amd64 && chmod +x minikube
# mkdir -p /usr/local/bin/
# install minikube /usr/local/bin/
# minikube version
# minikube start --driver=none ##๊ฐ„ํŽธ์„ค์น˜ ์œ„ํ•ด์„œ ๋“œ๋ผ์ด๋ฒ„ ์„ค์ • X. ๋“œ๋ผ์ด๋ฒ„ ์„ค์ •ํ•˜๋ฉด ๋˜ ๊ทธ ๋“œ๋ผ์ด๋ฒ„ ์œ„ํ•ด์„œ ์„ค์น˜ํ•  ๊ฒƒ๋“คใ…‡ ใ…ฃ์ƒ๊น€.
# minikube status

๐Ÿ“™ kubectl ์„ค์น˜

api์„œ๋ฒ„์™€ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๊ตฌ


# curl -LO https://dl.k8s.io/release/v1.22.2/bin/linux/amd64/kubectl
# install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
# source <(kubectl completion bash)
# echo "source <(kubectl completion bash)" >> ~/.bashrc
# exit
# kubectl version

๐Ÿ“™ Pod


# mkdir workspace && cd $_
# kubectl run nginx-pod --image=nginx
# kubectl get node
# kubectl get pod
# kubectl expose pod nginx-pod --name clusterip --type=ClusterIP --port 80
[root@localhost workspace]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
clusterip    ClusterIP   10.98.216.228   <none>        80/TCP    2m2s
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   110m

[root@localhost workspace]# curl 10.98.216.228
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
->ClusterIP๋Š” ์™ธ๋ถ€ ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์ ‘์† ์•ˆ๋จ ๋‚ด๋ถ€์—์„œ curl๋กœ ํ™•์ธ ํ•ด์•ผํ•จ

# kubectl expose pod nginx-pod --name nodeport --type=NodePort --port 80


# kubectl expose pod nginx-pod --name loadbalancer --type=LoadBalancer --external-ip 192.168.1.166 --port 80

[root@localhost workspace]# kubectl get svc
NAME           TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)        AGE
clusterip      ClusterIP      10.98.216.228    <none>          80/TCP         55m
kubernetes     ClusterIP      10.96.0.1        <none>          443/TCP        163m
loadbalancer   LoadBalancer   10.101.230.101   192.168.1.166   80:30228/TCP   83s
nodeport       NodePort       10.109.64.156    <none>          80:31999/TCP   24m

๐Ÿ“™ ์ด๋ก 

ํŒŒ๋“œ ; ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„
์„œ๋น„์Šค ; ๋„คํŠธ์›Œํฌ์™€ ๊ด€๋ จ๋œ ๋ฆฌ์†Œ์Šค pod๋ฅผ ์™ธ๋ถ€ ๋„คํŠธ์›Œํฌ์™€ ์—ฐ๊ฒฐํ•ด์ฃผ๊ณ  ์—ฌ๋Ÿฌ๊ฐœ์˜ pod๋ฅผ ๋ฐ”๋ผ๋ณด๋Š” ๋‚ด๋ถ€ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์„œ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ.

clusterIP

ํ˜ธ์ŠคํŠธํฌํŠธ๊ฐ€ ๊ณง ๋…ธ๋“œํฌํŠธ๋‹ค.
๋‚ด๋ถ€์—์„œ clusterIP๋กœ curl ํ•˜๋ฉด ๋‚ด๋ถ€์—์„œ ์›น์„œ๋ฒ„ ํ™•์ธ ๊ฐ€๋Šฅ.
nodeport์ด์šฉํ•˜๋ฉด ๋‚ด๋ถ€์—์„œ๋Š” ๋ฌผ๋ก  ๋ฐ”๊นฅ์—์„œ๋„ ํ™•์ธ ๊ฐ€๋Šฅ.
์—ฌ๊ธฐ์„œ ํ™œ์šฉํ•ด์•ผ ํ•  ๊ฒƒ์€ ํฌํŠธ๋ฒˆํ˜ธ์ž„.
ํ˜ธ์ŠคํŠธ IP:nodeport๋ฒˆํ˜ธ

๐Ÿ“™ ์›นํŽ˜์ด์ง€ ๋ฐ”๊พธ๊ธฐ

pod ์ง„์ž…
[root@localhost workspace]# kubectl exec -it nginx-pod -- bash
root@nginx-pod:/#
root@nginx-pod:/# cd /usr/share/nginx/html/

root@nginx-pod:/usr/share/nginx/html# echo "minikube" > index.html

๐Ÿ“™ ์‚ญ์ œ


# kubectl get all
# kubectl delete svc clusterip
# kubectl delete svc --all
# kubectl delete pod nginx-pod

๐Ÿ“™ yamlํŒŒ์ผ ์ด์šฉํ•˜๊ธฐ

 Service
# vi nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx-pod //service์™€์˜ ์—ฐ๊ฒฐ ๊ณ ๋ฆฌ
spec:
  containers:
  - name: nginx-pod-container
    image: nginx
    

# kubectl apply -f nginx-pod.yaml
# kubectl get pod
# docker images
# kubectl get pod -o wide
# kubectl describe pod nginx-pod

# vi clusterip-pod.yaml
apiVersion: v1
kind: Service
metadata:
  name: clusterip-service-pod
spec:
  type: ClusterIP
  selector:
    app: nginx-pod
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

# kubectl apply -f clusterip-pod.yaml
# kubectl get svc -o wide
# kubectl describe svc clusterip-service-pod

------------ ์„œ๋น„์Šค์™€ ์—ฐ๊ฒฐ๋˜๋Š” nginx-pod ๋ฐ”๊ฟ”๋ณด๊ธฐ--
[root@localhost workspace]# kubectl delete svc clusterip-service-pod
[root@localhost workspace]# vi clusterip-pod.yaml
    app: nginx-pod1
  
[root@localhost workspace]# kubectl apply -f clusterip-pod.yaml
service/clusterip-service-pod created
[root@localhost workspace]# kubectl get svc
NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
clusterip-service-pod   ClusterIP   10.96.217.148   <none>        80/TCP    38s
kubernetes              ClusterIP   10.96.0.1       <none>        443/TCP   67m
[root@localhost workspace]# curl 10.96.217.148
curl: (7) Failed connect to 10.96.217.148:80; Connection refused
[root@localhost workspace]#

 
 [root@localhost workspace]# vi clusterip-pod.yaml
 ->ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ 8080์œผ๋กœ
[root@localhost workspace]# kubectl apply -f clusterip-pod.yaml
service/clusterip-service-pod created
[root@localhost workspace]# kubectl get svc
NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
clusterip-service-pod   ClusterIP   10.97.198.129   <none>        8080/TCP   22s
kubernetes              ClusterIP   10.96.0.1       <none>        443/TCP    69m
[root@localhost workspace]# curl 10.97.198.129
^C
[root@localhost workspace]# curl 10.97.198.129:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>


[root@localhost workspace]# kubectl edit svc clusterip-service-pod
service/clusterip-service-pod edited
-> edit์ด์šฉํ•ด์„œ ํฌํŠธ๋ฒˆํ˜ธ ๋‹ค์‹œ 80์œผ๋กœ
[root@localhost workspace]# curl 10.97.198.129
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
-> ๋ฐ”๋กœ ๋จ.

-----------------------------------

 
 
# vi nodeport-pod.yaml
apiVersion: v1
kind: Service
metadata:
  name: nodeport-service-pod
spec:
  type: NodePort
  selector:
    app: nginx-pod
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80 ##๋ฐ”๊พธ๋ฉด ์•ˆ๋จ! ์ปจํ…Œ์ด๋„ˆ ํฌํŠธ์ž„.
    nodePort: 30080 ( 30000~32767 ๊ฐ€๋Šฅ)
# kubectl apply -f nodeport-pod.yaml
# kubectl get svc -o wide
# kubectl describe svc nodeport-service-pod

# vi loadbalancer-pod.yaml
apiVersion: v1
kind: Service
metadata:
  name: loadbalancer-service-pod
spec:
  type: LoadBalancer
  externalIPs:
  - 192.168.1.166
  selector:
    app: nginx-pod
  ports:
  - protocol: TCP
    port: 80
 
 ->ํƒ€๊ฒŸํฌํŠธ ๋„ฃ์ง€ ์•Š์•„๋„ ์ ‘์† ์ž˜ ๋จ. (curl)
# kubectl apply -f loadbalancer-pod.yaml
# kubectl get svc -o wide
# kubectl describe svc loadbalancer-service-pod

โœ”๏ธ์ด๋ฏธ์ง€ ๋ฐ”๊ฟ”๋ณด๊ธฐ

[root@localhost workspace]# kubectl get pod
NAME        READY   STATUS    RESTARTS   AGE
nginx-pod   1/1     Running   0          71m
[root@localhost workspace]# kubectl edit pod nginx-pod

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

๐Ÿ“Œ ๊ธฐํƒ€

โญ๏ธ IaC์ •๋ฆฌ

ํ…Œ๋ผํผ ํ”„๋กœ๋น„์ „
์•ค์„œ๋ธ” ๊ตฌ์„ฑ๊ด€๋ฆฌ
๋„์ปค ์„œ๋ฒ„ํ…œํ”Œ๋ฆฟ
์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜ ๋„๊ตฌ

profile
๊พธ์ค€ํžˆ, ์ฐจ๊ทผ์ฐจ๊ทผ

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