Prepare k8s Cluster

Install Kuberctl

MIRROR="files.m.daocloud.io/"
VERSION=$(curl -L -s https://${MIRROR}dl.k8s.io/release/stable.txt)
[ $(uname -m) = x86_64 ] && curl -sSLo kubectl "https://${MIRROR}dl.k8s.io/release/${VERSION}/bin/linux/amd64/kubectl"
[ $(uname -m) = aarch64 ] && curl -sSLo kubectl "https://${MIRROR}dl.k8s.io/release/${VERSION}/bin/linux/arm64/kubectl"
chmod u+x kubectl
mkdir -p ${HOME}/bin
mv -f kubectl ${HOME}/bin

Then, you can choose

Building a K8s Cluster, you can choose one of the following methods.

Mar 7, 2025

Subsections of Prepare k8s Cluster

Kind

kind-logo
Warning

Although this is a easiest way to build a cluster, but I wont recommend you to choose this. SInce there are many unsolved issue in here, check https://kind.sigs.k8s.io/docs/user/known-issues/

Preliminary

  • Kind binary has installed, if not check 🔗link

  • Hardware Requirements:

    1. At least 2 GB of RAM per machine (minimum 1 GB)
    2. 2 CPUs on the master node
    3. Full network connectivity among all machines (public or private network)
  • Operating System:

    1. Ubuntu 22.04/14.04, CentOS 7/8, or any other supported Linux distribution.
  • Network Requirements:

    1. Unique hostname, MAC address, and product_uuid for each node.
    2. Certain ports need to be open (e.g., 6443, 2379-2380, 10250, 10251, 10252, 10255, etc.)

Create your cluster

Creating a Kubernetes cluster is as simple as kind create cluster

kind create cluster --name test
Customize your cluster

If you need to configure your cluster, you can create kind-config.yaml

# 创建配置文件
cat <<EOF > kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
EOF

# 创建集群
kind create cluster --name my-cluster --config kind-config.yaml

# 等待容器启动
sleep 10

# 设置资源限制
docker update --memory="4g" --cpus="2" my-cluster-control-plane
docker update --memory="2g" --cpus="1" my-cluster-worker
docker update --memory="2g" --cpus="1" my-cluster-worker2

echo "✅集群创建完成,资源限制已应用"

Delete your cluster

kind delete cluster

Reference

and the you can visit https://kind.sigs.k8s.io/docs/user/quick-start/ for mode detail.

Mar 7, 2024

K3s

Preliminary

  • Hardware Requirements:

    1. Server need to have at least 2 cores, 2 GB RAM
    2. Agent need 1 core , 512 MB RAM
  • Operating System:

    1. K3s is expected to work on most modern Linux systems.
  • Network Requirements:

    1. The K3s server needs port 6443 to be accessible by all nodes.
    2. If you wish to utilize the metrics server, all nodes must be accessible to each other on port 10250.

Init K3s Server [At Server End]

curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server --cluster-init --flannel-backend=vxlan --node-taint "node-role.kubernetes.io/control-plane=true:NoSchedule" --disable traefik

append --disable traefik

append --disable servicelb 禁用 ServiceLB(如果你计划使用其他负载均衡器如 MetalLB)

append --disable local-storage 禁用本地存储(如果你使用其他存储方案)

Get K3s Token [At Server End]

cat /var/lib/rancher/k3s/server/node-token

Join K3s Worker [At Agent End]

curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_URL=https://<master-ip>:6443 K3S_TOKEN=<join-token> sh -

Copy Kubeconfig [At Server + Agent End]

mkdir -p $HOME/.kube
cp /etc/rancher/k3s/k3s.yaml $HOME/.kube/config

How it works

k3s

Uninstall K3s cluster [At Server + Agent End]

# exec on server
/usr/local/bin/k3s-uninstall.sh

# exec on agent 
/usr/local/bin/k3s-agent-uninstall.sh
Mar 7, 2024

Minikube

Preliminary

  • Minikube binary has installed, if not check 🔗link

  • Hardware Requirements:

    1. At least 2 GB of RAM per machine (minimum 1 GB)
    2. 2 CPUs on the master node
    3. Full network connectivity among all machines (public or private network)
  • Operating System:

    1. Ubuntu 20.04/18.04, CentOS 7/8, or any other supported Linux distribution.
  • Network Requirements:

    1. Unique hostname, MAC address, and product_uuid for each node.
    2. Certain ports need to be open (e.g., 6443, 2379-2380, 10250, 10251, 10252, 10255, etc.)

[Optional] Disable aegis service and reboot system for Aliyun

sudo systemctl disable aegis && sudo reboot

Customize your cluster

minikube start --driver=podman  --image-mirror-country=cn --kubernetes-version=v1.33.1 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --cpus=6 --memory=20g --disk-size=50g --force

Restart minikube

minikube stop && minikube start

Add alias

alias kubectl="minikube kubectl --"

Stop And Clean

minikube stop && minikube delete --all --purge

Forward

# execute on your local machine
        Remote                                                  Local⬇️
   __________________                          ________________________________   
  ╱                  ╲╲      wire/wifi        ╱ [ Minikube ] 17.100.x.y        ╲╲
 ╱                   ╱╱   <------------->    ╱                                 ╱╱
╱ telnet 192.168.a.b ╱                      ╱  > execute ssh... at 192.168.a.b ╱ 
╲___________________╱  IP: 10.45.m.n        ╲_________________________________╱ IP: 192.168.a.b
ssh -i ~/.minikube/machines/minikube/id_rsa docker@$(minikube ip) -L '*:30443:0.0.0.0:30443' -N -f

and then you can visit https://minikube.sigs.k8s.io/docs/start/ for more detail.

FAQ

Q1: couldn’t get resource list for external.metrics.k8s.io/v1beta1: the server is currently unable to handle…

通常是由于 Metrics Server 未正确安装 或 External Metrics API 缺失 导致的

# 启用 Minikube 的 metrics-server 插件
minikube addons enable metrics-server

# 等待部署完成(约 1-2 分钟)
kubectl wait --for=condition=available deployment/metrics-server -n kube-system --timeout=180s

# 验证 Metrics Server 是否运行
kubectl -n kube-system get pods  | grep metrics-server

the possibilities are endless (almost - including other shortcodes may or may not work)

Q2: Export minikube to local directly
minikube start --driver=podman  --image-mirror-country=cn --kubernetes-version=v1.33.1 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers  --listen-address=0.0.0.0 --cpus=6 --memory=20g --disk-size=100g --force