์ด๋ฒ 1์ฃผ์ฐจ ์คํฐ๋์์๋ KOPS ์ค์น ๋ฐ ์ฌ์ฉ๋ฒ์ ํ์ต ํ ์์ ์ด๋ค.
kOps
๋ ํด๋ผ์ฐ๋ ํ๋ซํผ(aws, gcp, azure ๋ฑ)์์ ์ฝ๊ฒ k8s ๋ฅผ ์ค์นํ ์ ์๋๋ก ๋์์ฃผ๋ ๋๊ตฌkOps
๋ ์๋ฒ ์ธ์คํด์ค์ ๋คํธ์ํฌ ๋ฆฌ์์ค ๋ฑ์ ํด๋ผ์ฐ๋์์ ์๋์ผ๋ก ์์ฑํด k8s ๋ฅผ ์ค์นkOps
๋ AWS ์ ๋ค์ํ ์๋น์ค์ ์ ์ฐํ๊ฒ ์ฐ๋๋์ด ์ฌ์ฉ ๊ฐ๋ฅ# yaml ํ์ผ ๋ค์ด๋ก๋
curl -O https://s3.ap-northeast-2.amazonaws.com/cloudformation.cloudneta.net/K8S/kops-new-ec2.yaml
# ๋ฐฐํฌ
# aws cloudformation deploy --template-file ~/Downloads/kops-new-ec2.yaml --stack-name mykops --parameter-overrides KeyName=<My SSH Keyname> SgIngressSshCidr=<My Home Public IP Address>/32 --region <๋ฆฌ์ >
# CloudFormation ์คํ ๋ฐฐํฌ ์๋ฃ ํ EC2 ๊ณต์ธ IP ์ถ๋ ฅ
aws cloudformation describe-stacks --stack-name mykops --query 'Stacks[*].Outputs[*].OutputValue' --output text
์์) 3.35.137.31
# kOps-ec2 ์ SSH ์ ์
์์) ssh -i <My SSH Keyfile> ec2-user@3.35.137.31
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameter
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
SgIngressSshCidr:
Description: The IP address range that can be used to communicate to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
LatestAmiId:
Description: (DO NOT CHANGE)
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
AllowedValues:
- /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
MyVPC:
Type: AWS::EC2::VPC
Properties:
EnableDnsSupport: true
EnableDnsHostnames: true
CidrBlock: 10.0.0.0/16
Tags:
- Key: Name
Value: My-VPC
MyIGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: My-IGW
MyIGWAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref MyIGW
VpcId: !Ref MyVPC
MyPublicRT:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: My-Public-RT
DefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: MyIGWAttachment
Properties:
RouteTableId: !Ref MyPublicRT
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref MyIGW
MyPublicSN:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
AvailabilityZone: !Select [ 0, !GetAZs '' ]
CidrBlock: 10.0.0.0/24
Tags:
- Key: Name
Value: My-Public-SN
MyPublicSNRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref MyPublicRT
SubnetId: !Ref MyPublicSN
KOPSEC2SG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: kops ec2 Security Group
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: KOPS-EC2-SG
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: !Ref SgIngressSshCidr
KOPSEC2:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: kops-ec2
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref MyPublicSN
GroupSet:
- !Ref KOPSEC2SG
AssociatePublicIpAddress: true
PrivateIpAddress: 10.0.0.10
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
hostnamectl --static set-hostname kops-ec2
yum -y install tree tmux jq git
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
curl -Lo kops https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
chmod +x kops
mv kops /usr/local/bin/kops
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
export PATH=/usr/local/bin:$PATH
source ~/.bash_profile
complete -C '/usr/local/bin/aws_completer' aws
ssh-keygen -t rsa -N "" -f /root/.ssh/id_rsa
echo 'alias vi=vim' >> /etc/profile
echo 'sudo su -' >> /home/ec2-user/.bashrc
curl -s https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
wget https://github.com/andreazorzetto/yh/releases/download/v0.4.0/yh-linux-amd64.zip
unzip yh-linux-amd64.zip
mv yh /usr/local/bin/
Outputs:
KopsEC2IP:
Value: !GetAtt KOPSEC2.PublicIp
# IAM User ์๊ฒฉ ๊ตฌ์ฑ : ์ค์ต ํธ๋ฆฌ๋ฅผ ์ํด administrator ๊ถํ์ ๊ฐ์ง IAM User ์ ์๊ฒฉ ์ฆ๋ช
์
๋ ฅ
aws configure
AWS Access Key ID [None]: Aceess Key
AWS Secret Access Key [None]: Secret Access Key
Default region name [None]: ap-northeast-2
Default output format [None]: json
# ์๊ฒฉ ๊ตฌ์ฑ ์ ์ฉ ํ์ธ : ๋
ธ๋ IP ํ์ธ
aws ec2 describe-instances
# aws cli ํ์ด์ง ์ถ๋ ฅ ์ต์
export AWS_PAGER=""
# ๋ฆฌ์์ค๋ฅผ ๋ฐฐ์นํ ๋ฆฌ์ ์ด๋ฆ์ ๋ณ์ ์ง์
REGION=ap-northeast-2 # ์์ธ ๋ฆฌ์ ์ฌ์ฉ
# ๋ฐฐํฌ ์ ์ฐธ๊ณ ํ ์ ๋ณด๋ฅผ ํ๊ฒฝ ๋ณ์์ ์ ์ฅ
export KOPS_CLUSTER_NAME=<์์ ์ ํผ๋ธ๋ฆญ ํธ์คํ
๋ฉ์ธ ์ฃผ์>
export KOPS_STATE_STORE=<s3://(์์์ ์์ฑํ ์์ ์ ๋ฒํท ์ด๋ฆ)>
export AWS_PAGER=""
export REGION=ap-northeast-2
# kops ์ค์ ํ์ผ ์์ฑ(s3) ๋ฐ k8s ํด๋ฌ์คํฐ ๋ฐฐํฌ : 6๋ถ ์ ๋ ์์
## CNI๋ aws vpc cni ์ฌ์ฉ, ๋ง์คํฐ ๋
ธ๋ 1๋(t3.medium), ์์ปค ๋
ธ๋ 2๋(t3.medium), ํ๋ ์ฌ์ฉ ๋คํธ์ํฌ ๋์ญ ์ง์ (172.30.0.0/16)
## --container-runtime containerd --kubernetes-version 1.24.0
kops create cluster --zones="$REGION"a,"$REGION"c --networking amazonvpc --cloud aws \
--master-size t3.medium --node-size t3.medium --node-count=2 --network-cidr 172.30.0.0/16 \
--ssh-public-key ~/.ssh/id_rsa.pub --name=$KOPS_CLUSTER_NAME --kubernetes-version "1.24.9" -y
# validate
kops validate cluster --wait 10m
# kops ํด๋ฌ์คํฐ ์ ๋ณด ํ์ธ
kops get cluster
# ์ธ์คํด์ค๊ทธ๋ฃน ์ ๋ณด ํ์ธ
kops get ig
# ์ธ์คํด์ค ์ ๋ณด ํ์ธ
kops get instances
# ์๋ ์์ฑ ๋ฐ alias ์ถ์ฝ ์ค์
source <(kubectl completion bash)
echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -F __start_kubectl k' >> ~/.bashrc
kubectl ๋ช ๋ น์ด๋ฅผ ํธํ๊ณ ๊ฐ๋จํ๊ฒ ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ํ๋ฌ๊ทธ์ธ
# ์ค์น
curl -fsSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.4.3/krew-linux_amd64.tar.gz
tar zxvf krew-linux_amd64.tar.gz
./krew-linux_amd64 install krew
tree -L 3 /root/.krew/bin
# PATH ์ถ๊ฐ
export PATH="${PATH}:/root/.krew/bin"
echo 'export PATH="${PATH}:/root/.krew/bin"' >>~/.bashrc
# krew ํ์ธ
kubectl krew
kubectl krew update
kubectl krew search
kubectl krew list
kubectl krew
์ฐธ๊ณ 1, ์ฐธ๊ณ 2
๋ค์ํ ํ๊ฒฝ/์ค์ ์์ ํ๋์ ํ
ํ๋ฆฟ์ผ๋ก ์ฌ์ฌ์ฉ, ํ์ํ ๋ชจ๋๋ง ๋ฐฐํฌ ๊ฐ๋ฅ, ์์ ์ ์ธ ํฌ๋ฆ ์ฐจํธ ์ํ๊ณ์์ ํ์ํ ์คํ์์ค ์ค์น
๋งค๋ํ์คํธ ๋ฒ์ฉํ
์ฟ ๋ฒ๋คํฐ์ค๋ YAML ํ์์ผ๋ก ๋งค๋ํ์คํธ๋ฅผ ์์ฑํ๊ณ kubectl์ ์ฌ์ฉํ์ฌ ์ค๋ธ์ ํธ๋ฅผ ์ ์ดํ๋ค.
๊ทธ๋ฌ๋ ์์คํ
์ด ๋๊ท๋ชจ๋ก ๋ฐ๋๋ฉด์ ๋น์ทํ ๋งค๋ํ์คํธ๋ฅผ ๋๋์ผ๋ก ๋ง๋ค์ด์ผ ํ๋ฏ๋ก ์ฌ์ฌ์ฉ์ด๋ ์ผ๊ด ๋ณ๊ฒฝ ์์
์ด ์ด๋ ค์ ์ง๊ฒ ๋๋ค. ๊ทธ๋์ ํ์ํ ๊ฒ์ด ๋งค๋ํ์คํธ ๋ฒ์ฉํ๋ผ๋ ๊ฐ๋
์ด๋ค.
ํฌ๋ฆ ์ฐจํธ(Helm Chart)
์ฐจํธ(Chart) : ์ฟ ๋ฒ๋คํฐ์ค์์ ์คํํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋๋ ๋ฐ ํ์ํ ์ ๋ณด ๋ฌถ์(=์์ถ ํ์ผ)
๋กค๋ง ์ ๋ฐ์ดํธ ๋ฑ์๋ ์ง์ํ๋ ๊ฒ๋ค์ด ๋ง์ ์ฟ ๋ฒ๋คํฐ์ค์์ ์ต์ ํ๋ ์ค์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋ ์ฅ์ ๋ ์๋ค.
ํฌ๋ฆ์ด ๊ด๋ฆฌํ๋ ๊ณต์ ์ฐจํธ๋ Artifact Hub์์ ์ ๊ณต
[๊ณผ์ 1] ๊ฐ์ ์ค์นํ AWS kOps ํด๋ฌ์คํฐ์ ์ ๋ณด๋ฅผ ์ฌ๋ ค์ฃผ์ธ์.
#kops get cluster #ํด๋ฌ์คํฐ ์ ๋ณด ์ถ๋ ฅ
#kops get instances #๊ฐ EC2 ์ธ์คํด์ค์ ์ ๋ณด ์ถ๋ ฅ
[๊ณผ์ 2] Helm ์ผ๋ก ์๋ํ๋ ์ค๋ฅผ ๋ฐฐํฌํ๊ณ ๊ด๋ฆฌํ์ด์ง์ ์ ์ํด์ ๊ธ 1๊ฐ ์์ฑํด์ฃผ์๊ณ ์คํฌ๋ฆฐ์ท์ ์ฌ๋ ค์ฃผ์ธ์.
#๊ฒ์๊ธ์ ์ด๋ฏธ์ง๋ฅผ ์ฌ๋ฆฌ๊ณ ์ ์ฅ
[๊ณผ์ 3] AWS kOps ํ์ฉ - ์์ปค ๋
ธ๋ ์ฆ๊ฐ(ASG = Auto Scaling Group ํ์ฉ)
1. Ig ํธ์ง ์ (maxSize:1, minSize:1)
2. lg ํธ์ง ํ(maxSize:3, minSize:3)
3. ์
๋ฐ์ดํธ ์ node ์
4. ์
๋ฐ์ดํธ ๋ฐ ๋กค๋ง ์
๋ฐ์ดํธ
5. ์ถ๊ฐ Node ํ์ธ
6. AWS ์คํ ์ค์ผ์ผ๋ง ๋ฐ ์ธ์คํด์ค ํ์ธ
์ฒ์ ์ฟ ๋ฒ๋คํฐ์ค๋ฅผ ๊ณต๋ถํ๋ฉด์ ํด๋ฌ์คํฐ๋ฅผ kubeadm์ผ๋ก ๊ตฌ์ฑํ์ฌ ์ฌ์ฉํ์๋ค. ์ง์ ๊ตฌ์ฑํ๋ฉด์ ๋ง์ ์๊ฐ์ด ๊ฑธ๋ ธ๊ณ , ๋งค๋ฒ ๊ตฌ์ฑํ ๋๋ง๋ค ์๋ฌ๋ก ๊ณ ์์ ํ์๋ค. KOPS๋ฅผ ์ ํ๊ณ ์ฟ ๋ฒ๋คํฐ์ค ํด๋ฌ์คํฐ๋ฅผ ๋๋ฌด ์ฝ๊ฒ ๊ตฌ์ฑํ ์ ์์ด์ ๋๋ผ์ ๋ค. Helm๋ ๋ง์ด ๋ค์ด๋ ๋ดค์ง๋ง ์ฒ์ ์ฌ์ฉํด ๋ดค๋ค.
Helm Chart๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๊ณ ํ๋ฒ์ ์ดํ๋ฆฌ์ผ์ด์
์ ๋ฐฐํฌํ ์ ์๊ณ , ๊ตฌ์ฑ ๋ณ๊ฒฝ ๋ฐ ์ ์ฉ ์ yaml์ ํตํด ์ฝ๊ฒ ์
๋ฐ์ดํธ ํ ์ ์์๋ค. ๊ธฐ๋ณธ์ ์ธ ํด๋ฌ์คํฐ๋ง ๊ตฌ์ฑํด์ ์ฌ์ฉํด๋ณธ ๋๋ก์จ๋ ๋ค์ํ ์ฟ ๋ฒ๋คํฐ์ค ํ๊ฒฝ์ ์ฌ์ฉํด ๋ณผ ์ ์์ด์ ์ข์๋ค!!๐