7탄 = 1/5
서울리전에 쿠버네티스를 설치해보자.
VPC > Your VPC > Default VPC만든다.
AWS 기본 5개까지 VPC생성 가능.
쿠버네티스 클러스터를 만들 베스천 서버 1대를 만들자.
dev-cluster
dev-nodes
1
베스천 서버 생성을 위해 퍼블릭 네트워크에 서버 1대를 만든다.
aws에서 제공하는 디폴트 네트워크가 퍼블릭이므로 그곳에 서버 1대를 만들면 된다.
아마존 리눅스2로 만든다.
aws cli등 설치되어 있다.
admin role 부여한다.
쿠버네티스 클러스터를 만드는 명령을 날리려면 vpc생성, 서버 생성등 권한이 필요하다.
2
iam > roles > create roles > admin 추가
eks-admin
3
hostnamectl --static set-hostname aws-com7
sudo su -
1
# EC2 서버에 로그인 , 쿠버네티스를 설치하는 툴이 eksctl 설치해보자.
eksctl 다운로드
curl --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv -v /tmp/eksctl /usr/local/bin
eksctl version
2
# 쿠버네티스에 명령을 내리는 kubectl 을 설치해 보자.
# kubectl 설치 = 쿠버네티스에 명령을 내리는 툴
# 쿠버네티스 마스터에 명령을 내릴때 사용한다. 리소스 만들때 사용하는 툴.
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version
# 다운로드 사이트
https://kubernetes.io/ko/docs/tasks/tools/install-kubectl-linux/
1
리전을 변수로 지정
export AWS_REGION=ap-northeast-2
2
# eksctl create로 쿠버네티스 클러스터를 생성 - dev-cluster , node dev-nodes 2개 , 1.31
eksctl create cluster --name dev-cluster --nodegroup-name dev-nodes --node-type t3.small --nodes 2 --nodes-min 1 --nodes-max 4 --managed --version 1.31 --region ${AWS_REGION}
eksctl create cluster \
--name dev-cluster \
--nodegroup-name dev-nodes \
--node-type t3.small \
--nodes 2 \
--nodes-min 1 \
--nodes-max 4 \
--managed \
--version 1.31 \
--region ${AWS_REGION}
(15분)
https://vclock.kr/timer/#countdown=00:10:00&enabled=0&seconds=0&sound=xylophone&loop=1
3
클라우드 포메이션에서 가서 생성되는 내용 확인이 가능하다.
4
생성 완료후 node 보기.
kubectl get nodes
6
현재 자격증명 확인
aws sts get-caller-identity --query Arn
"arn:aws:iam::476286675138:user/12-24-user"
7
echo 'alias k=kubectl' >> /etc/profile
echo 'complete -F __start_kubectl k' >> /etc/profile
8
# Install kubens kubectx
yum install git -y
git clone https://github.com/ahmetb/kubectx /opt/kubectx
ln -s /opt/kubectx/kubens /usr/local/bin/kubens
ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
1
# 모니터링 하기 , 터미널 2 실행
watch -d kubectl get pods,svc,deploy,rs
2
pod 1개 만들기
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx-app
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
EOF
3
POD가 서버 입니다.
서버 1대가 돌아가네요~
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 21s
// 삭제 필요시 ?
kubectl delete pod nginx-pod
4
# 디플로이먼트로 서버 (pod) 생성?
# nginx이미지로 websrv pod를 만들어보자~
kubectl create deployment websrv --image=nginx --port=80 --replicas=4
웹서버 4대가 바로 만들어 집니다~
5
k get deploy,rs,po
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mario 1/1 1 1 5m34s
deployment.apps/websrv 4/4 4 4 34s
NAME DESIRED CURRENT READY AGE
replicaset.apps/mario-7f7ddc97f4 1 1 1 5m34s
replicaset.apps/websrv-7bf7d5d9ff 4 4 4 34s
NAME READY STATUS RESTARTS AGE
pod/mario-7f7ddc97f4-mwnf9 1/1 Running 0 5m34s
pod/nginx-pod 1/1 Running 0 60s
pod/websrv-7bf7d5d9ff-5hkfc 1/1 Running 0 34s
pod/websrv-7bf7d5d9ff-9jcvz 1/1 Running 0 34s
pod/websrv-7bf7d5d9ff-bx5k5 1/1 Running 0 34s
pod/websrv-7bf7d5d9ff-km5qm 1/1 Running 0 34s
6
외부 노출 ?
로드 밸런서 만드는 법입니다.
L4 만들기 1줄~
kubectl expose deployment websrv --port=80 --type=LoadBalancer
(2분 걸림)
7
만들어진 리소스 확인
kubectl get deploy,rs,pod,svc,ep
8
웹브라우저로 L4 로 접속 해보자
잘된다.
9
kubectl get svc
kubectl describe svc websrv
10
서버수 조절
2대로 조절해보자~
websrv
kubectl scale deployment websrv --replicas=2
kubectl get pods
11
접속자 IP 확인?
아래 명령서 실행시킬 웹브라우저로 서비스 접속해본다.
아래처럼 로그가 나온다.
websrv
kubectl logs -l app=websrv -f --max-log-requests 8
참고로 , 접속자 ip는 198.18.1.201 EKS 네트워크 IP로 보인다~
198.18.1.201 - - [23/Dec/2021:06:35:40 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"
12
삭제
kubectl delete deploy,svc websrv
1
게임 올리기?
cat <<EOF | kubectl create -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-2048
spec:
selector:
matchLabels:
app.kubernetes.io/name: app-2048
replicas: 2
template:
metadata:
labels:
app.kubernetes.io/name: app-2048
spec:
containers:
- image: alexwhen/docker-2048
name: app-2048
ports:
- containerPort: 80
EOF
2
# lb 와 연결하자.
kubectl expose deployment deployment-2048 --port=80 --type=LoadBalancer
3
kubectl get svc,ep
a126d1fdc5218411bac03dd88d67b423-1418180016.ap-northeast-2.elb.amazonaws.com
4
웹브라우저로 접속
5분 걸림.
5
게임이 떴다
방향키로 같은 숫자 합치는 게임입니다.^^
6
pod 수 늘리기 (서버수 늘리기 2대에서 4대로 늘리자~)
kubectl get pods
다음번엔 컨테이너 레지스트리 이미지를 사용해 서비스를 올려보는 부분을 해보자~
실무에서는 레지스트리에 올려놓고 해당 이미지를 불러 사용한다.
확장
kubectl scale 명령어~~~~~~
kubectl scale deployment deployment-2048 --replicas=4
kubectl get pods
7
dns에서 cname으로 연결
원하는 도메인으로 접속 하면 된다.
www 를 CNAME 으로 로드밸런서를 연결하자
www.best10game.com
a126d1fdc5218411bac03dd88d67b423-1418180016.ap-northeast-2.elb.amazonaws.com
8
삭제
kubectl delete deploy,svc deployment-2048
네임스페이스를 디폴트로 해서 파드를 확인한다.
0
# 파드를 하나 만들자.
pod 1개 만들기
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx-app
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
EOF
1
kubectl get pods -n default
kubectl get pods
파드안에 컨테이너수 이다.
2
변수로 저장.
첫번째 pod 이름을 환경 변수에 저장
export MY_POD_NAME=$(kubectl get pods -n default -o jsonpath='{.items[0].metadata.name}')
kubectl -n default describe pod $MY_POD_NAME
3
pod 에서 bash shell에 연결한다.
kubectl exec -it ${MY_POD_NAME} -n default -- /bin/bash
ls
4
apt-get update
apt-get install vim
cd /usr/share/nginx/html
root@nginx-pod:/usr/share/nginx/html# ls
50x.html index.html index.html-backup
root@nginx-pod:/usr/share/nginx/html# vi index.html
5
게임 설치
apk add micro-tetris
tetris
exit
https://brunch.co.kr/@topasvga/2469