跳转至

6. 高级配置

本章将介绍 K3s 进阶配置,包括存储管理、负载均衡、HTTPS 证书自动化以及监控系统的部署。

6.1 存储配置 (Storage)

K3s 自带了 Local Path Provisioner,它允许你使用节点上的本地目录作为持久卷(Persistent Volume)。

6.1.1 Local Path 使用示例

默认情况下,K3s 创建 PVC 时会自动在节点的 /var/lib/rancher/k3s/storage 目录下创建对应文件夹。

manifests/06-advanced/pvc-local.yaml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: local-path-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-path
  resources:
    requests:
      storage: 2Gi

参数详解: * accessModes: ReadWriteOnce 表示该卷只能被单个节点挂载读写。 * storageClassName: local-path: 指定使用 K3s 内置的存储类。

6.1.2 替换默认存储路径

如果你想把数据存在数据盘(如 /data)而不是系统盘:

修改 /var/lib/rancher/k3s/server/manifests/local-storage.yaml (不推荐直接改系统文件),或者编辑 ConfigMap:

kubectl edit configmap local-path-config -n kube-system

修改 config.json 部分:

{
  "nodePathMap":[
  {
    "node":"DEFAULT_PATH_FOR_NON_LISTED_NODES",
    "paths":["/data/k3s-storage"]
  }
  ]
}

6.2 负载均衡 (Load Balancer)

6.2.1 MetalLB

K3s 自带的 Klipper LB 只能使用节点端口。如果你想要一个真实的“内网 IP”作为 LoadBalancer IP,可以使用 MetalLB。

  1. 安装 MetalLB

    kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.12/config/manifests/metallb-native.yaml
    

  2. 配置 IP 池: 创建 manifests/06-advanced/metallb-pool.yaml

    apiVersion: metallb.io/v1beta1
    kind: IPAddressPool
    metadata:
      name: first-pool
      namespace: metallb-system
    spec:
      addresses:
      - 192.168.1.200-192.168.1.220 # 预留一段未被使用的内网 IP
    ---
    apiVersion: metallb.io/v1beta1
    kind: L2Advertisement
    metadata:
      name: example
      namespace: metallb-system
    
  3. 禁用 K3s 默认 LB: 安装 K3s 时需添加 --disable servicelb 参数。

6.3 证书管理 (Cert-Manager)

使用 cert-manager 配合 Let's Encrypt 自动签发免费 HTTPS 证书。

6.3.1 安装 cert-manager

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.13.3/cert-manager.yaml

6.3.2 创建 ClusterIssuer

创建 manifests/06-advanced/letsencrypt-issuer.yaml

apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-prod
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    email: your-email@example.com
    privateKeySecretRef:
      name: letsencrypt-prod
    solvers:
    - http01:
        ingress:
          class: traefik

参数详解: * acme: 自动化证书管理环境协议。 * http01: 验证方式。Let's Encrypt 会访问 http://<domain>/.well-known/acme-challenge/... 来验证域名所有权。

6.4 监控告警 (Prometheus Stack)

推荐使用 kube-prometheus-stack Helm Chart,它集成了 Prometheus, Grafana, Alertmanager。

6.4.1 安装 Helm

K3s 默认已安装 Helm Controller,你可以直接应用 HelmChart CRD,也可以手动安装 Helm CLI。

curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

6.4.2 部署监控栈

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  --create-namespace \
  --set grafana.adminPassword="admin"

访问 Grafana: 默认 Service 为 ClusterIP,需通过 Port-forward 访问:

kubectl port-forward -n monitoring svc/kube-prometheus-stack-grafana 3000:80
访问 http://localhost:3000,账号 admin,密码 admin。