1. 安装 K3s:
在您的终端中运行以下命令以安装K3s。此步骤将自动下载并安装K3s,同时禁用不必要的组件:
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--disable=traefik" sh -s -
2.配置 kubeconfig:
配置kubectl命令行工具的访问权限:
mkdir ~/.kube
sudo cat /etc/rancher/k3s/k3s.yaml | tee ~/.kube/config >/dev/null
3.验证K3s安装成功:
安装完成后,使用以下命令确认K3s已正确安装并运行:
4.安装 Helm 工具:
Helm是管理Kubernetes应用的工具。通过以下命令安装Helm:
wget https://get.helm.sh/helm-v3.11.0-linux-amd64.tar.gz
tar -zxvf helm-v3.11.0-linux-amd64.tar.gz
install linux-amd64/helm /usr/local/bin/helm
5.安装 Ingress Nginx:
通过Helm安装Ingress Nginx,为应用添加HTTP路由规则:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm upgrade --install ingress-nginx ingress-nginx \
--repo https://kubernetes.github.io/ingress-nginx \
--namespace ingress-nginx --create-namespace
6.使用挂载盘 /mnt/storage
:
移除默认的 local-path StorageClass
标记:
kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'
创建 storageclass.yaml
文件,并填入以下内容:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: rancher.io/local-path
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Delete
parameters:
path: "/mnt/storage"
应用StorageClass到K3s集群:
kubectl apply -f storageclass.yaml
更新 ConfigMap 路径为 /mnt/storage
:
kubectl patch configmap local-path-config -n kube-system --type=json -p='[{"op": "replace", "path": "/data/config.json", "value":"{\n \"nodePathMap\":[\n {\n \"node\":\"DEFAULT_PATH_FOR_NON_LISTED_NODES\",\n \"paths\":[\"/mnt/storage\"]\n }\n ]\n}"}]'