프라이빗 쿠버네티스 배포환경 구축
목적
•
하버, Gitlab, ArgoCD구조를 활용한 빌드/배포 환경구축
•
쿠버네티스 환경은 마스터1/워커2
•
목적 달성시 볼륨, 모니터링 관련 학습진행
초기설정
플러그인 & 필수 프로그램 설치
#!/bin/bash
# >>> Helm 설치
curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
sudo apt-get install apt-transport-https --yes
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm -y
# >>> Java 설치
sudo apt update
sudo apt install openjdk-17-jdk -y
# JAVA_HOME 환경변수 설정
JAVA_HOME=$(update-java-alternatives -l | awk '{print $3}')
echo "export JAVA_HOME=$JAVA_HOME" | sudo tee -a /etc/profile
echo "export PATH=\$JAVA_HOME/bin:\$PATH" | sudo tee -a /etc/profile
echo "shell을 재시작하거나 'source /etc/profile'명령어를 실행해야 JAVA_HOME이 변경됩니다."
# >>> krew 설치
{
set -x
cd "$(mktemp -d)" || exit
OS="$(uname | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
KREW="krew-${OS}_${ARCH}"
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz"
tar zxvf "${KREW}.tar.gz"
./"${KREW}" install krew
}
# PATH 설정을 ~/.bashrc에 추가
echo 'export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"' >> ~/.bashrc
# ~/.bashrc를 재실행하여 PATH 변경 적용
source ~/.bashrc
# Krew 플러그인 설치
kubectl krew install ctx
kubectl krew install ns
Bash
복사
MetalLB
helm repo add metallb https://metallb.github.io/metallb
helm pull metallb/metallb --untar --version 0.13.7
Bash
복사
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: production
namespace: metallb
spec:
# Production services will go here. Public IPs are expensive, so we leased
# just 4 of them.
addresses:
- 192.168.32.70 - 192.168.32.85
YAML
복사
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: example
namespace: metallb
spec:
ipAddressPools:
- production
# nodeSelectors:
# - matchLabels:
# kubernetes.io/hostname: NodeA
# - matchLabels:
# kubernetes.io/hostname: NodeB
YAML
복사
#!/bin/bash
# 스크립트 파일의 디렉토리로 작업 디렉토리 변경
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
cd "$SCRIPT_DIR" || exit
kubectl create ns metallb
kubectl config set-context --current --namespace=metallb
helm install metallb . --namespace metallb -f my-values.yaml --wait
kubectl apply -f L2Advertisement.yaml
kubectl apply -f IPAddressPool.yaml
Bash
복사
HostPath
#!/bin/bash
# OpenEBS 네임스페이스 생성
kubectl create ns openebs
# 컨텍스트의 네임스페이스를 'openebs'로 변경
# 'kubectl ns'는 표준 명령이 아니며, 일부 환경에서는 작동하지 않을 수 있습니다.
# 아래 명령은 현재 컨텍스트의 네임스페이스를 'openebs'로 설정합니다.
kubectl config set-context --current --namespace=openebs
# OpenEBS 매니페스트 적용
kubectl apply -f https://openebs.github.io/charts/openebs-operator.yaml
Bash
복사
Harbor
helm repo add harbor https://helm.goharbor.io
helm pull harbor/harbor --untar
Bash
복사
#!/bin/bash
# 스크립트 파일의 디렉토리로 작업 디렉토리 변경
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
cd "$SCRIPT_DIR" || exit
# OpenEBS-hostpath StorageClass를 기본 StorageClass로 지정
kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
# Harbor 설치 (Helm 이용)
kubectl create ns harbor
kubectl config set-context --current --namespace=harbor
helm install harbor . --namespace harbor -f my-values.yaml --wait
# /etc/hosts에 harbor.myweb.io 도메인 추가
# 이 작업은 스크립트를 실행하는 시스템에만 적용됩니다.
echo "192.168.32.75 harbor.myweb.io" | sudo tee -a /etc/hosts > /dev/null
# 모든 노드에 대해 위의 /etc/hosts 변경 작업을 수동으로 반복해야 합니다.
Bash
복사
type: loadBalancer
auto:
commonName: "harbor.myweb.io"
loadBalancer:
IP: "192.168.32.75"
externalURL: https://harbor.myweb.io
persistence:
persistentVolumeClaim:
registry:
storageClass: "openebs-hostpath"
YAML
복사
my-values.yaml 수정위치
{
"insecure-registries": ["harbor.myweb.io"]
}
JSON
복사
/etc/docker/daemon.json (없으면 생성)
sudo systemctl restart docker
Bash
복사
docker 재시작
sudo docker pull nginx
sudo docker tag nginx harbor.myweb.io/erp/nginx:0.1
sudo docker login harbor.myweb.io # 에러발생! (설정추가 - daemon.json)
sudo docker push harbor.myweb.io/erp/nginx:0.1
Bash
복사
harbor push test
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: harbor.myweb.io/erp/nginx:0.1
YAML
복사
하버의 이미지를 가져온다
version = 2
// ...
[plugins."io.containerd.grpc.v1.cri".registry]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://registry-1.docker.io"]
[plugins."io.containerd.grpc.v1.cri".registry.configs]
[plugins."io.containerd.grpc.v1.cri".registry.configs."harbor.myweb.io".tls]
insecure_skip_verify = true
TOML
복사
/etc/containerd/config.toml/ (모든 노드에 적용 해야함)
sudo systemctl restart containerd
sudo systemctl status containerd
Bash
복사
containerd 재시작
ArgoCD
helm repo add argo https://argoproj.github.io/argo-helm
helm pull argo/argo-cd --untar
Bash
복사
global:
# -- Mapping between IP and hostnames that will be injected as entries in the pod's hosts files
hostAliases:
- ip: 192.168.32.76
hostnames:
- git.myhostname
## Server service configuration
service:
# -- Server service type
type: LoadBalancer
YAML
복사
#!/bin/bash
# 스크립트 파일의 디렉토리로 작업 디렉토리 변경
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
cd "$SCRIPT_DIR" || exit
kubectl create ns argocd
kubectl config set-context --current --namespace=argocd
helm install argocd . --namespace argocd -f my-values.yaml
sudo curl -sSL -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo chmod +x /usr/local/bin/argocd
---
k get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" | base64 -d
argocd login 192.168.32.71 --username admin # lRvycBy2SeG7NxZa
Bash
복사
#!/bin/bash
argocd repo add https://github.com/scofe97/infra_study.git \
--username scofe97 \
--insecure-skip-server-verification
Bash
복사
Prometheus
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm pull prometheus-community/kube-prometheus-stack --untar
Bash
복사
# 임계값 도달시 경고
defaultRules:
rules:
alertmanager: true
ectd: true
# alertManager 설치
alertmanager:
## Service type
type: NodePort
# 필요에 따라 다양한 모니터링 대상 추가
kubeApiServer:
enabled: true
kubelet:
enabled: true
# 타노스(멀티클라우드 환경) 생략
prometheus:
thanosService:
enabled: false
# 프로메테우스 ServiceMonitor 설정
# helm 설치 시 사용한 네임스페이스 이 외 다른 네임스페이스에서도
# 서비스모니터를 등록 가능하도록 설정
serviceMonitorSelectorNilUsesHelmValues: false
# 프로메테우스 서비스 설정
type: NodePort
retention: 5d
retentionsSize: "10GiB"
storageSpec:
volumeClaimTemplate:
spec:
storageClassName: openebs-hostpath
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 15Gi
YAML
복사
my-values.yaml
service:
type: LoadBalancer
persistence:
type: pvc
enabled: true
storageClassName: openebs-hostpath
YAML
복사
values.yaml (그라파나)
#!/bin/bash
# 스크립트 파일의 디렉토리로 작업 디렉토리 변경
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
cd "$SCRIPT_DIR" || exit
kubectl create ns monitoring
kubectl config set-context --current --namespace=monitoring
helm install prometheus . --namespace monitoring -f my-values.yaml
Bash
복사