Visualizer

[AWS, Kubernetes] EKS 구축, 간단한 Pod 생성, 배포, 삭제해보기

gmlwlsl 2024. 6. 28. 15:41
더보기

EC2 spec

- AMI : Ubuntu 22.04

- type : t2.medium

- SG : 모든 TCP/UDP/ICMP 허용

- DiskSize : 10GB

- Subnet : public

 

EKS 구축하기

EKS란?

컨트롤 플레인을 직접 구성하지 않고, k8s를 손쉽게 사용할 수 있도록 편리함을 제공하는 기능
VPC, LB, IAM 등을 같이 활용하고자 할 때 유용

 

# kubectl 은 k8s(kubernetes) kubectl 명령을 이용하여 관리한다. 
$ curl -O https://s3.us-west-2.amazonaws.com/amazon-eks/1.28.8/2024-04-19/bin/linux/amd64/kubectl
$ curl -O https://s3.us-west-2.amazonaws.com/amazon-eks/1.28.8/2024-04-19/bin/linux/amd64/kubectl.sha256
sha256sum -c kubectl.sha256  # OK 가 나오면 kubectl 파일은 무결성 검사를 통과!

$ chmod +x ./kubectl    # 다운로드한 kubectl 에게 실행권한을 부여
$ mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$HOME/bin:$PATH
$ echo 'export PATH=$HOME/bin:$PATH' >> ~/.bashrc

$ kubectl version --client   # 아래쪽에 1.28 … 이란 글이 보이면 kubectl  은 정상설치됨


# eksctl 설치하기(eks 클러스터 구현이 복잡하여 eksctl 을 이용한 간단한 스크립트 기반의 설치를 지원한다)

$ 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


$ eksctl create cluster \
--vpc-public-subnets subnet-***,subnet-*** \ # 본인의 public subnet id
--name eks-work-cluster \
--region ap-northeast-3 \ # 본인의 region
--version 1.28 \
--nodegroup-name eks-work-nodegroup \
--node-type t2.small \
--nodes 2 \
--nodes-min 2 \
--nodes-max 4

# kubectl 에서 전달한 명령은 k8s 의 api 전달된다. 단, 둘 사이에 버전 차이가  있을 경우 설치 말미에 버전 오류 메시지가 출력되지만 클러스터 구성은 완료된다. 우리는 이 두 버전을 맞춰주어야 한다.

$ apt update -y && apt install -y zip
$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
$ unzip awscliv2.zip
$ sudo ./aws/install --update
$ mv ~/.kube/config ~/.kube/config.bk
$ aws eks update-kubeconfig --region ap-northeast-3 --name eks-work-cluster

# err1
root@ip-172-31-17-190:~# kubectl get no
Command 'kubectl' not found, but can be installed with:

# err2
root@ip-172-31-17-190:~# snap install kubectl
error: This revision of snap "kubectl" was published using classic confinement and thus may
       perform arbitrary system changes outside of the security sandbox that snaps are usually
       confined to, which may put your system at risk.

       If you understand and want to proceed repeat the command including --classic.
       
# solv
$ snap install kubectl --classic.
$ kubectl get no

ec2 생성 확인

위의 복잡한 과정 때문에, AWS는 IAC(Intrastructure as Code)를 이용

 

간단한 Pod application 배포해보기

1. api 이름 찾기

$ kubectl api-resources

 

2. Pod 만들기

$ mkdir k8s ; cd k8s
$ touch test_pod.yml
$ vi test_pod.yml

# test_pod.yml
apiVersion: v1
Kind:Pod
metadata:
  name: mypod
spec:
  containers:
    - name: myctn
      Image:nginx
      ports:
        - containerPort: 80
          protocol: TCP 
         
         
$ kubectl apply -f test_pod.yml # 배포


$ kubectl exec mypod -- curl -s localhost # 간단하게 동작 여부 확인
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


$ kubectl delete -f test_pod.yml # 삭제

생성된 pod 클러스터는 내부에만 노출되고, 외부로는 노출되지 않기 때문에 외부 사용자들은 접근할 수 없음

NodePort, LoadBalancer와 같은 Service Object를 사용해야 접근이 가능함