Kubernetes ‐ Install Kubernetes - thought-corner/Backend-PlayGround GitHub Wiki
- 쿠버네티스에 대해 처음부터 공부해보고자 일프로님의 강의와 워밍업 스터디를 통해 진행하게 되었다.
- 나의 Mac PC에 가상 머신을 설치한 뒤 그 위에 Kubernetes를 설치하게 되었다.
# 타임존 설정
$ timedatectl set-timezone Asia/Seoul
$ timedatectl set-ntp true
$ chronyc makestep
# 네트워크 트래픽 제어 및 추가적인 yum 관련 유틸리티를 설치
$ yum install -y yum-utils iproute-tc
# openssl과 openssh-server 두 개의 특정 패키지만 최신 버전으로 업데이트
$ yum update openssl openssh-server -y
# Hosts 설정
$ cat << EOF >> /etc/hosts
192.168.56.30 k8s-master
EOF
# 방화벽 해제
$ systemctl stop firewalld && systemctl disable firewalld
# Swap 비활성화
$ swapoff -a && sed -i '/ swap / s/^/#/' /etc/fstab
# 컨테이너 런타임 설치 전 사전작업 & iptable 세팅
$ cat <<EOF |tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
$ modprobe overlay
$ modprobe br_netfilter
$ cat <<EOF |tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
$ sysctl --system
# containerd 패키지 설치 & docker engine 설치 & repo 설정
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# containerd 설치
$ yum install -y containerd.io-1.6.21-3.1.el9.aarch64
$ systemctl daemon-reload
$ systemctl enable --now containerd
# cri 활성화 - 컨테이너 런타임(Container Runtime)인 containerd를 설정하여 쿠버네티스(Kubernetes) 환경에서 사용하기 적합하도록 CGroup 드라이버를 활성화
$ containerd config default > /etc/containerd/config.toml
$ sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml
$ systemctl restart containerd
# repo 설정 및 쿠버네티스(Kubernetes) 클러스터를 쉽고 빠르게 부트스트랩(설치 및 초기 설정)할 수 있도록 도와주는 공식 커맨드라인 도구인 kubeadm 설치
$ cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.27/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.27/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF
# SELinux 설정
$ setenforce 0
$ sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# kubelet, kubeadm, kubectl 패키지 설치
$ yum install -y kubelet-1.27.2-150500.1.1.aarch64 kubeadm-1.27.2-150500.1.1.aarch64 kubectl-1.27.2-150500.1.1.aarch64 --disableexcludes=kubernetes
$ systemctl enable --now kubelet
# kubeadm으로 클러스터 생성 & 클러스터 초기화 (Pod Network 세팅)
$ kubeadm init --pod-network-cidr=20.96.0.0/12 --apiserver-advertise-address 192.168.56.30
# kubectl 사용 설정
$ mkdir -p $HOME/.kube
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config
# Pod Network 설치 (calico)
$ kubectl create -f https://raw.githubusercontent.com/k8s-1pro/install/main/ground/k8s-1.27/calico-3.26.4/calico.yaml
$ kubectl create -f https://raw.githubusercontent.com/k8s-1pro/install/main/ground/k8s-1.27/calico-3.26.4/calico-custom.yaml
# Master에 Pod를 생성 할수 있도록 설정
$ kubectl taint nodes k8s-master node-role.kubernetes.io/control-plane-
# kubectl 자동완성 기능
$ yum -y install bash-completion
$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ echo 'alias k=kubectl' >>~/.bashrc
$ echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
$ source ~/.bashrc
# Dashboard 설치
$ kubectl create -f https://raw.githubusercontent.com/k8s-1pro/install/main/ground/k8s-1.27/dashboard-2.7.0/dashboard.yaml
# Metrics Server 설치
$ kubectl create -f https://raw.githubusercontent.com/k8s-1pro/install/main/ground/k8s-1.27/metrics-server-0.6.3/metrics-server.yaml# Git 설치
$ yum -y install git
# 로컬 저장소 생성
$ git init monitoring
$ git config --global init.defaultBranch main
$ cd monitoring
# remote 추가
$ git remote add -f origin https://github.com/k8s-1pro/install.git
# sparse checkout 설정
$ git config core.sparseCheckout true
$ echo "ground/k8s-1.27/prometheus-2.44.0" >> .git/info/sparse-checkout
$ echo "ground/k8s-1.27/loki-stack-2.6.1" >> .git/info/sparse-checkout
# Prometheus & Grafana 설치
$ kubectl apply --server-side -f ground/k8s-1.27/prometheus-2.44.0/manifests/setup
$ kubectl wait --for condition=Established --all CustomResourceDefinition --namespace=monitoring
$ kubectl apply -f ground/k8s-1.27/prometheus-2.44.0/manifests
# Loki-Stack 설치
$ kubectl apply -f ground/k8s-1.27/loki-stack-2.6.1
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-1-2-2-1
spec:
selector:
matchLabels:
app: '1.2.2.1'
replicas: 2
strategy:
type: RollingUpdate
template:
metadata:
labels:
app: '1.2.2.1'
spec:
containers:
- name: app-1-2-2-1
image: 1pro/app
imagePullPolicy: Always
ports:
- name: http
containerPort: 8080 # 포트: 8080
startupProbe:
httpGet:
path: "/ready"
port: http
failureThreshold: 20 # startupProbe: 최대 20번 실패 허용 (초기 시작 시간 확보)
livenessProbe:
httpGet:
path: "/ready" # liveness/readinessProbe: /ready 엔드포인트 체크
port: http
readinessProbe:
httpGet:
path: "/ready"
port: http
resources:
requests:
memory: "100Mi" # 요청: CPU 100m, 메모리 100Mi
cpu: "100m"
limits:
memory: "200Mi" # 제한: CPU 200m, 메모리 200Mi
cpu: "200m"apiVersion: v1
kind: Service
metadata:
name: app-1-2-2-1
spec:
selector:
app: '1.2.2.1'
ports:
- port: 8080
targetPort: 8080
nodePort: 31221 # 포트: 31221 (노드) → 8080 (파드)
type: NodePort # NodePort (외부 접근 가능)apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-1-2-2-1
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: app-1-2-2-1
minReplicas: 2
maxReplicas: 4 # 최소 2 → 최대 4 파드
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 40 # 스케일링: CPU 사용률 40% 기준$ [root@k8s-master ~]# while true; do curl http://192.168.56.30:31221/hostname; sleep 2; echo ''; done;
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-88wpr
app-1-2-2-1-78cbbff668-4q6zv
app-1-2-2-1-78cbbff668-4q6zv- 현재 app-1-2-2-1-78cbbff668-4q6zv, app-1-2-2-1-78cbbff668-88wpr 두 파드명이 조회되는 것을 볼 수 있다.
- Traffic Routing은 네트워크 트래픽을 목적지로 전달하는 경로 결정 기능이다.
$ [root@k8s-master ~]# curl 192.168.56.30:31221/memory-leak- Memory Leak를 고의적으로 발생해 파드가 죽은 뒤에 다시 재가동되는지 테스트한다.
$ [root@k8s-master ~]# curl 192.168.56.30:31221/cpu-load
- AutoScaling은 부하에 따라 리소스를 자동으로 증감하는 기능이다.
- 위와 같이 파드가 3개에서 4개로 증가된 것을 볼 수 있다.
- RollingUpdate는 무중단 배포 전략이다.
- 새 버전 Pod을 점진적으로 생성
- 구 버전 Pod을 순차적으로 종료
- 서비스 중단 없이 업데이트 완료