๐ŸŒ AWS EC2 ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํด๋Ÿฌ์Šคํ„ฐ ๊ตฌ์ถ•

๊น€์„ฑ์ธยท2023๋…„ 10์›” 30์ผ
0

[DevOps] ๐ŸณDocker & Kubernetes

๋ชฉ๋ก ๋ณด๊ธฐ
61/62

kube-apiserver ์ง€์† ์žฌ์‹œ์ž‘ ์ข…๋ฃŒ.

Restart ์Šคํƒฏ์„ ๋ณด๋ฉด ๋ช‡๋ฒˆ์ด๋‚˜ ์žฌ ์‹œ์ž‘ํ•œ๊ฒƒ์„ ๋ณผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค..
๋„๋Œ€์ฒด ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๊ณ .. ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•œ๊ฑด๊ฐ€ ์‹ถ๊ธฐ๋„ํ–ˆ์Œ..

t2.medium

ec2์ธ์Šคํ„ด์Šค๋ฅผ t3.small๋กœ ์ฒ˜์Œ์— ์ง€์ •ํ–ˆ๋‹ค๊ฐ€ t2.meduim์œผ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค.


ํด๋Ÿฌ์Šคํ„ฐ ๊ตฌ์ถ•

๋‚ด๊ฐ€ ๊ณต๋ถ€ํ•˜๋ฉด์„œ GKEํ™˜๊ฒฝ์—์„œ ๊ตฌ์ถ•ํ–ˆ๋˜ ํด๋Ÿฌ์Šคํ„ฐ ํ™˜๊ฒฝ๊ณผ
https://velog.io/@99_insung/GKE-VM

๐Ÿ€ํ•ด์™ธ ์‚ฌ์ดํŠธ์„œ ๋ฐœ๊ฒฌํ•œ EC2 ์ธ์Šคํ„ด์Šค๋กœ ํด๋Ÿฌ์Šคํ„ฐ ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•œ ๋ธ”๋กœ๊ทธ๋ฅผ ๋‘˜๋‹ค ์ด์šฉํ•˜์˜€์Œ.
https://nvtienanh.info/blog/cai-dat-kubernetes-cluster-tren-ubuntu-server-22-04
๊ตญ๋‚ด ๋ธ”๋กœ๊ทธ(ํฌํŠธ ๊ฐœ๋ฐฉํ™•์ธ, ์—ฌ๊ธฐ์„œ๋Š” systemd๋ฅผ ์‚ฌ์šฉํ•จ)

https://velog.io/@koo8624/Kubernetes-AWS-EC2-%EC%9D%B8%EC%8A%A4%ED%84%B4%EC%8A%A4%EC%97%90-Kubernetes-%ED%81%B4%EB%9F%AC%EC%8A%A4%ED%84%B0-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0

EC2 HA ๋ฅผ ํ™œ์šฉ
https://blog.zooneon.dev/kubeadm-cloud-provider-aws/

1. ์—…๋ฐ์ดํŠธ

sudo apt update
sudo apt upgrade -y

2. hostname

๋งŽ์€ ec2 ํด๋Ÿฌ์Šคํ„ฐ ํ™˜๊ฒฝ ๊ตฌ์ถ•์—์„œ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„์„ ์„ค์ •ํ•˜๋˜๋ฐ ๋‚˜๋Š” ํ•˜์ง€ ์•Š์•˜๋‹ค.
sudo hostnamectl set-hostname <ํ˜ธ์ŠคํŠธ์ด๋ฆ„>

3. swapoff

# ํ˜„์žฌ ์‹œ์Šคํ…œ์— ์ ์šฉ(๋ฆฌ๋ถ€ํŒ…ํ•˜๋ฉด ์žฌ ์„ค์ • ํ•„์š”)
sudo swapoff -a
# ๋ฆฌ๋ถ€ํŒ… ํ•„์ˆ˜
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

free -h ๋ฅผ ํ†ตํ•ด์„œ ํ™•์ธ

               total        used        free      shared  buff/cache   available
Mem:           7.7Gi       167Mi       7.1Gi       1.0Mi       437Mi   7.3Gi
Swap:             0B          0B          0B

4. ์ปค๋„ ๋ชจ๋“ˆ ๋กœ๋“œ

sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
  • overlay : Overlay ํŒŒ์ผ ์‹œ์Šคํ…œ ๋ชจ๋“ˆ ๋กœ๋“œ.
    • ์ปจํ…Œ์ด๋„ˆ ๋Ÿฐํƒ€์ž„ (Docker, containerd) ์—์„œ ์‚ฌ์šฉ ๋˜๋Š” ๋ ˆ์ด์–ด๋“œ ํŒŒ์ผ ์‹œ์Šคํ…œ ์ค‘ ํ•˜๋‚˜
    • ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€์™€ ํŒŒ์ผ ์‹œ์Šคํ…œ ๊ด€๋ฆฌ์— ์‚ฌ์šฉ
  • br_netfilter: ๋ธŒ๋ฆฌ์ง€ ๋„ทํ•„ํ„ฐ ๋ชจ๋“ˆ ๋กœ๋“œ
    • Linux ๋ธŒ๋ฆฌ์ง€ ๋„คํŠธ์›Œํฌ์™€ ๋„คํŠธ์›Œํฌ ํŒจํ‚ท ํ•„ํ„ฐ๋ง ์ง€์›
    • ์ปจํ…Œ์ด๋„ˆ ๋„คํŠธ์›Œํฌ ์„ค์ •์— ์‚ฌ์šฉ

sudo modprobe overlay
sudo modprobe br_netfilter

Kubernetes ์ปค๋„ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •

sudo tee /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sudo sysctl --system

5. ์ปจํ…Œ์ด๋„ˆ ๋Ÿฐํƒ€์ž„ ์„ค์น˜

sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y containerd.io

6. ์ปจํ…Œ์ด๋„ˆ ๋Ÿฐํƒ€์ž„ ๊ตฌ์„ฑ ์ถ”๊ฐ€

containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd

7. ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์„ค์น˜

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

8. kubeadm init

sudo kubeadm init \
  --pod-network-cidr=10.10.0.0/16 \
  --control-plane-endpoint=ํ˜ธ์ŠคํŠธ๋„ค์ž„ # ํ•ด๋‹น ๋ถ€๋ถ„์€ ์ œ์™ธํ•จ

์—ฌ๊ธฐ์„œ ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ ์„ค์ •์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์•˜๊ณ , ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…ํ•˜๋Š๋ผ ์˜ค๋ž˜ ๊ฑธ๋ฆผ (ํฌํŠธ ๊ฐœ๋ฐฉ์ด ํ•„์ˆ˜)
ํฌ์ŠคํŒ… : https://velog.io/@99_insung/EC2-%ED%81%B4%EB%9F%AC%EC%8A%A4%ED%84%B0-%EA%B5%AC%EC%B6%95

9. ์„ค์น˜ ์™„๋ฃŒํ›„ .kube ์„ค์ • (๋งˆ์Šคํ„ฐ ๋…ธ๋“œ๋งŒ)

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

์„ค์ • ํ›„ ํ™•์ธ

kubectl cluster-info
kubectl get nodes

kube-apiserver ๊ฐ•์ œ ์ข…๋ฃŒ ํ˜„์ƒ ๋ฐ ๋ฐ˜๋ณต ์žฌ์‹คํ–‰

ํฌ์ŠคํŒ… : https://velog.io/@99_insung/EC2-%ED%81%B4%EB%9F%AC%EC%8A%A4%ED%84%B0-%ED%8C%8C%EB%93%9C-%EA%B0%95%EC%A0%9C%EC%A2%85%EB%A3%8C%ED%98%84%EC%83%81

10. ์„ค์น˜ ์™„๋ฃŒ ํ›„ ํด๋Ÿฌ์Šคํ„ฐ join(์›Œ์ปค ๋…ธ๋“œ๋งŒ)

11. ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ์—์„œ ํ™•์ธ

kubectl get nodes

์ฒ˜์Œ์— ๋ชจ๋‘ NotReady ์ƒํƒœ๋กœ ๋…ธ๋“œ๋ฆฌ์ŠคํŠธ ์ƒํƒœ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

12. ๋„คํŠธ์›Œํฌ ํ”Œ๋Ÿฌ๊ทธ์ธ ์ถ”๊ฐ€ (Calico)

curl https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml -O

์„ค์น˜ ํ›„ yamlํŒŒ์ผ ์•ˆ์— ipv4 CIDR ์„ค์ •์„ kubeadm init์— ์„œ ์„ค์ •ํ–ˆ๋˜ cidr ๊ฐ’๊ณผ ๋งž์ถค

---
# The default IPv4 pool to create on startup if none exists. Pod IPs will be
# chosen from this range. Changing this value after installation will have
# no effect. This should fall within `--cluster-cidr`.
- name: CALICO_IPV4POOL_CIDR
  value: '10.10.0.0/16'
# Disable file logging so `kubectl logs` works.
- name: CALICO_DISABLE_FILE_LOGGING
  value: 'true'

kubectl apply -f calico.yaml : ์ ์šฉํ•˜์—ฌ ๋„คํŠธ์›Œํฌ ํ”Œ๋Ÿฌ๊ทธ์ธ ์‹คํ–‰

์‹คํ–‰ ์™„๋ฃŒ ํ›„ kubectl get nodes๋ฅผ ๋‹ค์‹œ ํ•ด๋ณด๋ฉด ๊ฐ ๋…ธ๋“œ๋“ค์ด ์ •์ƒ ๋™์ž‘ํ•˜์˜€๋‹ค.

NAME               STATUS   ROLES           AGE   VERSION
worker-2   Ready    <none>          8h    v1.28.2
worker-1    Ready    <none>          8h    v1.28.2
master-1    Ready    control-plane   8h    v1.28.2

13. kube-system ํ™•์ธ

calico-node๊ฐ€ ๊ณ„์† 0/1 READY ์ƒํƒœ๋ผ์„œ ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…ํ•˜๋Š”๋ฐ ๋งŽ์ด ์•  ๋จน์—ˆ์—ˆ๋‹ค.
ํฌ์ŠคํŒ… : https://velog.io/@99_insung/Calico-BGP-%ED%8A%B8%EB%9F%AC%EB%B8%94-%EC%8A%88%ED%8C%85


ingress-nginx-controller

git clone https://github.com/kubernetes/ingress-nginx/

kubectl apply -k `pwd`/ingress-nginx/deploy/static/provider/baremetal/

kubectl get pod -n ingress-nginx : ์„ค์น˜ ํ™•์ธ
kubectl delete validatingwebhookconfigurations.admissionregistration.k8s.io ingress-nginx-admission : ํ˜„์žฌ ์˜ค๋ฅ˜๋˜๋Š” ๊ธฐ๋Šฅ ์ œ๊ฑฐ

ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: jpapractice-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: "/"
spec:
  rules:
    - host: insung-jpadocker.store  # ์‹ค์ œ ๋„๋ฉ”์ธ ์„ค์ •
      http:
        paths:
          - pathType: Prefix
            path: "/"
            backend:
              service:
                name: jpapractice-service
                port:
                  number: 80

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jpapractice-deployment
spec:
  replicas: 3  
  selector:
    matchLabels:
      app: jpapractice
  template:
    metadata:
      labels:
        app: jpapractice
    spec:
      containers:
        - name: jpapractice
          image: adultkim/jpapractice:latest  
          env:  
            - name: active
              value: "prod"
          ports:
            - containerPort: 8081

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: jpapractice-service
spec:
  selector:
    app: jpapractice
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8081  # Port your Spring Boot application is running on
  type: LoadBalancer # ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ๋ฅผ ์•„์ง ํ• ๋‹น์•ˆํ•ด์„œ ์‹คํ–‰ ํ›„์— NodePort๋กœ ๋ณ€๊ฒฝํ•จ

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฐฐํฌํ•˜์˜€๊ณ ,

$ kubectl get all -o wide
NAME                                          READY   STATUS    RESTARTS   AGE     IP             NODE               NOMINATED NODE   READINESS GATES
pod/jpapractice-deployment-5b4596d8d9-27nv5   1/1     Running   0          7h33m   10.10.32.132   ip-172-31-31-33    <none>           <none>
pod/jpapractice-deployment-5b4596d8d9-cdwhr   1/1     Running   0          7h33m   10.10.32.133   ip-172-31-31-33    <none>           <none>
pod/jpapractice-deployment-5b4596d8d9-lb9mx   1/1     Running   0          7h33m   10.10.120.68   ip-172-31-16-104   <none>           <none>

NAME                          TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE     SELECTOR
service/jpapractice-service   NodePort    10.99.56.67   <none>        80:30353/TCP   7h33m   app=jpapractice
service/kubernetes            ClusterIP   10.96.0.1     <none>        443/TCP        9h      <none>

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS    IMAGES                        SELECTOR
deployment.apps/jpapractice-deployment   3/3     3            3           7h33m   jpapractice   adultkim/jpapractice:latest   app=jpapractice

NAME                                                DESIRED   CURRENT   READY   AGE     CONTAINERS    IMAGES                        SELECTOR
replicaset.apps/jpapractice-deployment-5b4596d8d9   3         3         3       7h33m   jpapractice   adultkim/jpapractice:latest   app=jpapractice,pod-template-hash=5b4596d8d9

์„œ๋น„์Šค๋ฅผ LoadBalancer๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐํฌํ•œ service์˜ ์™ธ๋ถ€ IP๊ฐ€ ๊ณ„์† Pending ์ƒํƒœ์˜€๋‹ค.
NodePort๋กœ ๋ณ€๊ฒฝ ํ›„์— ๋‚ด๋ถ€ ํ™˜๊ฒฝ์—์„œ ์ ‘์†์ด ๋˜๋Š”์ง€ ํ™•์ธํ•จ.
-> ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด๋ถ€์—์„œ ์š”์ฒญ์„ ๋…ธ์ถœํ•˜์—ฌ ํ™•์ธ

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