Kubernetes|How to setup K8S Cluster

瀏覽人次: 1226
2021-12-03 更新

kubernetes cluster

Install Docker

sudo yum install -y yum-utils

sudo yum-config-manager
--add-repo
https://download.docker.com/linux/centos/docker-ce.repo

sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl start docke

Container Runtimes Setup

我們需要先幫 K8S 建立與 Docker 的中介層,官方目前提供三種做法,我們採用
CRI-O:

  1. Containerd
  2. CRI-O
  3. docker
# Create the .conf file to load the modules at bootup
cat <<EOF | sudo tee /etc/modules-load.d/crio.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# Set up required sysctl params, these persist across reboots.
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

sudo sysctl --system

完成上面的設定後,開始安裝 CRI-O

# your centos version
export OS=CentOS_8

# CRI-O version
export VERSION=1.20:1.20.0

sudo curl -L -o /etc/yum.repos.d/devel:kubic:libcontainers:stable.repo https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/devel:kubic:libcontainers:stable.repo
sudo curl -L -o /etc/yum.repos.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.repo https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$VERSION/$OS/devel:kubic:libcontainers:stable:cri-o:$VERSION.repo
sudo yum install cri-o

sudo systemctl daemon-reload
sudo systemctl enable crio --now

Installing kubeadm

透過 kubeadm 可以快速建立 cluster 主節點,後續即可透過 join cluster 的方式將
worker node 加入集群

Disable SWAP

swapoff -a

# 將 swap 那一行注解
vim /etc/fstab
#/dev/mapper/cl-swap  none swap  defaults   0 0

Letting iptables see bridged traffic

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

Installing kubeadm, kubelet and kubectl

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF

# Set SELinux in permissive mode (effectively disabling it)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

sudo systemctl enable --now kubelet

以下為 Master Node 執行即可--

Initializing your control-plane node

kubeadm init

如果看到以下的成功訊息,代表建置成功。

將 kubeadm join 的指令保存,之後可在其他 worker node 上執行加入 cluster。

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a Pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  /docs/concepts/cluster-administration/addons/

You can now join any number of machines by running the following on each node
as root:

  kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

如果是 root 用戶,可以用以下指令設定 kubeconfig 的路徑。

否則就按照上述的 regular user 的三行指令操作。

export KUBECONFIG=/etc/kubernetes/admin.conf

Install CNI (Network Plugin)

k8s cluster container 所需要的溝通媒介。

可以從
https://kubernetes.io/docs/concepts/cluster-administration/addons/
找需要 Plugin,這邊以 weave net 為例:

kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d 'n')"

Worker Node 安裝 K8S 完成後,執行 kubeadm join 指令,並於 master node 查看
cluster node 狀態:

kubectl get nodes

NAME         STATUS   ROLES                  AGE     VERSION
crt-centos   Ready    control-plane,master   2d21h   v1.22.2
ubuntu       Ready    <none>                2d6h    v1.22.2

想看更多主題嗎? 聯絡我們
快速跳轉目錄

✦ 集先鋒 Bimap – 企業建置高速穩定的海量日誌分析平台✦

集中不同的結構化資料和非結構化日誌,並進行關聯性的大數據整合,客製化儀表版、自訂事件告警、機器學習等等,以滿足各種大數據的應用場景和解決方案。