Skip to content

Instantly share code, notes, and snippets.

@taking
Last active December 12, 2024 01:03
Show Gist options
  • Save taking/024d5fdb7393b9ea1f1eb84c07cd992d to your computer and use it in GitHub Desktop.
Save taking/024d5fdb7393b9ea1f1eb84c07cd992d to your computer and use it in GitHub Desktop.
#!/bin/bash
# 작성자: Taking
# Ubuntu 22.04
# Kubernetes v1.31 설치 + 초기화
# Cri-o v1.31
# Flannel CNI
# 클러스터 이름 변경 (호스트명 기준)
# how-to
# > curl {gist_url}/k8s-v1.31-crio-auto-installation.sh | bash k8s-master master
set -e
RED=`tput setaf 1`
GREEN=`tput setaf 2`
NC=`tput sgr0`
CRIO_VERSION="v1.31"
KUBERNETES_VERSION="v1.31"
CNI_VERSION="1.16.0"
HOST_NAME=$1
NODE_TYPE=$2
# 권한 확인
check_root() {
if [ "$(id -u)" != "0" ]; then
echo "${RED}루트 권한으로 실행해주세요.${NC}"
exit 1
fi
}
# Kubernetes 초기화 확인
check_k8s_reset() {
if [ -f ~/.kube/config ]; then
echo "${RED}--Kubernetes 초기화 확인--${NC}"
read -r -p "Kubernetes를 초기화하시겠습니까? [Y/n] " input
case $input in
[yY][eE][sS]|[yY])
kubeadm reset -f --cri-socket /var/run/crio/crio.sock
rm -rf /etc/cni /etc/etcd.env /etc/kubernetes /var/lib/dockershim /var/lib/etcd /var/lib/kubelet /var/run/kubernetes ~/.kube/ /run/flannel /etc/flannel
ip link del cni0 || true
ip link del flannel.1 || true
exit 0
;;
*)
echo "초기화를 건너뜁니다."
;;
esac
fi
}
# 인자 확인
check_args() {
if [ $# -ne 2 ]; then
echo "${RED}사용법: $0 <hostname> <node_type>${NC}"
echo "${RED}node_type은 'master' 또는'worker'를 입력해주세요.${NC}"
exit 1
fi
if [ "$2" != "master" ] && [ "$2" != "worker" ]; then
echo "${RED}node_type은 'master' 또는'worker'를 입력해주세요.${NC}"
exit 1
fi
}
# 호스트명 변경
change_hostname() {
echo "${RED}--호스트명 변경 (중요)--${NC}"
ubuntu_version=$(lsb_release -rs)
if [ "$ubuntu_version" == "22.04" ]; then
# Ubuntu 22.04의 경우
hostnamectl hostname ${HOST_NAME}
else
# 그 이외
hostnamectl set-hostname ${HOST_NAME}
fi
echo "호스트명이 $uhost로 변경되었습니다."
}
# 기본 패키지 설치
install_base_packages() {
apt-get update -y
apt-get install -y vim apt-transport-https curl git wget ca-certificates gpg software-properties-common socat
}
# CNI 플러그인 설치
install_cni_plugins() {
if [ ! -d /opt/cni/bin ]; then
echo "${RED}--CNI 네트워크 플러그인 설치 중...--${NC}"
mkdir -p /opt/cni/bin/
wget -qO- "https://github.com/containernetworking/plugins/releases/download/v${CNI_VERSION}/cni-plugins-linux-amd64-v${CNI_VERSION}.tgz" | tar -xz -C /opt/cni/bin/
echo "${GREEN}CNI 플러그인 설치 완료${NC}"
fi
}
# 키링 설정
setup_keyrings() {
if [ ! -d /etc/apt/keyrings ]; then
mkdir -p /etc/apt/keyrings
fi
if [ ! -f /etc/apt/keyrings/cri-o-apt-keyring.gpg ]; then
curl -fsSL "https://pkgs.k8s.io/addons:/cri-o:/stable:/${CRIO_VERSION}/deb/Release.key" | gpg --dearmor -o /etc/apt/keyrings/cri-o-apt-keyring.gpg
fi
if [ ! -f /etc/apt/keyrings/kubernetes-apt-keyring.gpg ]; then
curl -fsSL "https://pkgs.k8s.io/core:/stable:/${KUBERNETES_VERSION}/deb/Release.key" | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
fi
}
# CRI-O 설치
install_crio() {
if [ ! -f /usr/bin/crio ]; then
echo "${RED}--CRI-O 설치 중...--${NC}"
echo "deb [signed-by=/etc/apt/keyrings/cri-o-apt-keyring.gpg] https://pkgs.k8s.io/addons:/cri-o:/stable:/${CRIO_VERSION}/deb/ /" | tee /etc/apt/sources.list.d/cri-o.list
apt-get update -y
apt-get install -y cri-o
systemctl enable --now crio
echo "${GREEN}CRI-O 설치 완료${NC}"
fi
}
# Kubernetes 설치
install_kubernetes() {
if [ ! -f /usr/bin/kubeadm ]; then
echo "${RED}--Kubernetes 설치 중...--${NC}"
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/${KUBERNETES_VERSION}/deb/ /" | tee /etc/apt/sources.list.d/kubernetes.list
apt-get update -y
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
systemctl enable --now kubelet
echo "${GREEN}Kubernetes 설치 완료${NC}"
fi
}
# Helm 설치
install_helm() {
if [ ! -f /usr/local/bin/helm ]; then
echo "${RED}--Helm 설치 중...--${NC}"
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
echo "${GREEN}Helm 설치 완료${NC}"
fi
}
# 시스템 초기화
initialize_system() {
if [ ! -f /etc/sysctl.d/k8s.conf ]; then
echo "${RED}--시스템 초기화 중...--${NC}"
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
echo '1' > /proc/sys/net/ipv4/ip_forward
cat <<EOF | tee /etc/modules-load.d/crio.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat <<EOF | tee /etc/sysctl.d/99-kubernetes.conf
net.ipv4.ip_forward=1
kernel.keys.root_maxbytes=25000000
kernel.keys.root_maxkeys=1000000
kernel.panic=10
kernel.panic_on_oops=1
vm.overcommit_memory=1
vm.panic_on_oom=0
net.ipv4.ip_local_reserved_ports=30000-32767
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
EOF
sysctl --system
echo "${GREEN}시스템 초기화 완료${NC}"
fi
}
# kubectx, kubens 설치
install_kubectx_kubens() {
if [ ! -f /usr/bin/kubectx ]; then
echo "${RED}--kubectx, kubens 설치 중...--${NC}"
git clone https://github.com/ahmetb/kubectx /tmp/kubectx
cp /tmp/kubectx/kubectx /tmp/kubectx/kubens /usr/bin/
rm -rf /tmp/kubectx
echo "${GREEN}kubectx, kubens 설치 완료${NC}"
fi
}
# Kubernetes 초기화
initialize_kubernetes() {
if [ ! -f ~/.kube/config ]; then
echo "${RED}--Kubernetes 초기화 중...--${NC}"
internal_ip=$(hostname -I | awk '{print $1}')
instance_public_ip=$(curl -s ifconfig.me)
pod_network_cidr="10.244.0.0/16"
kubeadm init --pod-network-cidr=${pod_network_cidr} --apiserver-cert-extra-sans "${internal_ip}" --cri-socket /var/run/crio/crio.sock
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown -R $(id -u):$(id -g) $HOME/.kube
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
_hostname=$(hostname)
# kubectl taint nodes ${_hostname} node-role.kubernetes.io/control-plane-
kubectl taint nodes ${_hostname} node-role.kubernetes.io/control-plane:NoSchedule
kubectl get configmaps -n kube-system kubeadm-config -o yaml | sed "s/ clusterName: kubernetes/ clusterName: ${_hostname}/g" | kubectl replace -f -
kubectl config rename-context kubernetes-admin@kubernetes kubernetes-admin@${_hostname}
sed -i "6s/.*/ name: ${_hostname}/g" $HOME/.kube/config
sed -i "9s/.*/ cluster: ${_hostname}/g" $HOME/.kube/config
echo "${GREEN}Kubernetes 초기화 완료${NC}"
kubectl get nodes
fi
}
# 메인 함수
main() {
check_args "$@"
check_root
check_k8s_reset
change_hostname "$1"
setup_keyrings
install_base_packages
initialize_system
install_cni_plugins
install_crio
install_kubernetes
install_helm
install_kubectx_kubens
if [ "$2" == "master" ]; then
initialize_kubernetes
fi
echo "${GREEN}--스크립트 실행 완료--${NC}"
}
# 스크립트에 전달된 모든 인자를 main 함수로 전달
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment