跳转至

592_K3s轻量Kubernetes

一句话概述:K3s 是 Rancher/SUSE 开发的轻量级 Kubernetes 发行版,二进制文件不到 100MB,最低 512MB 内存即可运行,专为边缘计算、IoT 和资源受限环境设计,一条命令安装完整 K8s 集群。

核心知识点表

概念白话解释
K3s"K8s 的一半大小",轻量版 Kubernetes
Server控制平面节点,运行 API Server 和调度器
Agent工作节点,运行实际的容器工作负载
containerdK3s 内置的容器运行时(代替 Docker)
TraefikK3s 内置的 Ingress Controller(处理外部流量)
FlannelK3s 内置的 CNI 网络插件
SQLiteK3s 默认的数据存储(单节点),多节点可用 etcd

安装配置

单节点安装(一条命令)

# 安装 K3s Server(控制平面+工作节点)
curl -sfL https://get.k3s.io | sh -

# 安装完成后:
# - K3s 以 systemd 服务运行
# - kubectl 自动可用
# - kubeconfig 文件位于 /etc/rancher/k3s/k3s.yaml

# 验证安装
sudo kubectl get nodes                 # 查看节点
sudo kubectl get pods -A               # 查看所有命名空间的 Pod

多节点集群

# === Server 节点(主节点)===
curl -sfL https://get.k3s.io | sh -

# 获取 Node Token(用于 Agent 加入集群)
sudo cat /var/lib/rancher/k3s/server/node-token

# === Agent 节点(工作节点)===
# 在其他服务器上运行:
curl -sfL https://get.k3s.io | K3S_URL=https://SERVER_IP:6443 K3S_TOKEN=YOUR_TOKEN sh -
# K3S_URL → Server 的地址
# K3S_TOKEN → 从 Server 获取的 Token

高可用集群(3 Server + etcd)

# 第一个 Server 节点
curl -sfL https://get.k3s.io | sh -s - server \
  --cluster-init                       # 启用嵌入式 etcd

# 第二、三个 Server 节点
curl -sfL https://get.k3s.io | K3S_TOKEN=TOKEN sh -s - server \
  --server https://FIRST_SERVER_IP:6443  # 加入已有集群

边缘优化安装

# 禁用不需要的组件以节省资源
curl -sfL https://get.k3s.io | sh -s - \
  --disable=traefik \                  # 不装 Traefik(自己选 Ingress)
  --disable=servicelb \                # 不装 ServiceLB
  --write-kubeconfig-mode=644          # kubeconfig 文件权限

基本使用

kubectl 基础操作

# K3s 自带 kubectl,直接使用:
sudo k3s kubectl get nodes             # 查看节点
# 或设置别名:
echo 'alias kubectl="sudo k3s kubectl"' >> ~/.bashrc
source ~/.bashrc

# === 常用操作 ===
kubectl get pods                       # 查看 Pod
kubectl get svc                        # 查看 Service
kubectl get deployments                # 查看部署
kubectl logs POD_NAME                  # 查看日志
kubectl exec -it POD_NAME -- sh       # 进入 Pod
kubectl apply -f manifest.yaml        # 应用配置
kubectl delete -f manifest.yaml       # 删除资源

部署应用

# app.yaml — 部署一个 Nginx 应用
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx                          # 部署名
spec:
  replicas: 3                          # 3 个副本
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:alpine          # 使用 Nginx 镜像
          ports:
            - containerPort: 80        # 容器端口
          resources:
            requests:
              memory: "64Mi"           # 最低内存
              cpu: "100m"              # 最低 CPU
            limits:
              memory: "128Mi"          # 最大内存
              cpu: "250m"              # 最大 CPU
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: LoadBalancer                   # 暴露到外部
  ports:
    - port: 80                         # Service 端口
      targetPort: 80                   # 容器端口
  selector:
    app: nginx
kubectl apply -f app.yaml              # 部署
kubectl get pods -w                    # 监控 Pod 状态

高级用法

本地镜像仓库

# 在边缘环境中预拉取镜像:
# K3s 支持从 /var/lib/rancher/k3s/agent/images/ 加载本地镜像

# 方法1:导入 Docker 镜像
docker save myapp:v1 -o myapp.tar
sudo k3s ctr images import myapp.tar   # 导入到 containerd

# 方法2:配置私有镜像仓库
# /etc/rancher/k3s/registries.yaml
mirrors:
  "registry.example.com":
    endpoint:
      - "https://registry.example.com"
configs:
  "registry.example.com":
    auth:
      username: user
      password: pass

自动升级

# 使用 system-upgrade-controller 自动升级 K3s
kubectl apply -f https://github.com/rancher/system-upgrade-controller/releases/latest/download/system-upgrade-controller.yaml

# 创建升级计划
kubectl apply -f - <<EOF
apiVersion: upgrade.cattle.io/v1
kind: Plan
metadata:
  name: k3s-server
  namespace: system-upgrade
spec:
  concurrency: 1
  nodeSelector:
    matchExpressions:
      - key: node-role.kubernetes.io/master
        operator: Exists
  upgrade:
    image: rancher/k3s-upgrade
  version: v1.34.6+k3s1
EOF

Helm 应用安装

# K3s 内置 Helm Controller,直接用 HelmChart CRD:
kubectl apply -f - <<EOF
apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
  name: grafana
  namespace: kube-system
spec:
  repo: https://grafana.github.io/helm-charts
  chart: grafana
  targetNamespace: monitoring
  valuesContent: |-
    adminPassword: admin123
    service:
      type: LoadBalancer
EOF

常见报错

报错信息原因解决方案
FATA: unable to start端口 6443 被占用停止占用进程或换端口
Node not ready网络插件未就绪等待或检查 Flannel 日志
ImagePullBackOff镜像拉取失败检查镜像名和网络/仓库配置
CrashLoopBackOff容器反复崩溃kubectl logs POD 查看原因
Insufficient memory内存不足设置 resource requests/limits

速查表

# === 安装/卸载 ===
curl -sfL https://get.k3s.io | sh -   # 安装
/usr/local/bin/k3s-uninstall.sh        # 卸载 Server
/usr/local/bin/k3s-agent-uninstall.sh  # 卸载 Agent

# === 服务管理 ===
sudo systemctl status k3s             # 查看状态
sudo systemctl restart k3s            # 重启
sudo journalctl -u k3s -f             # 查看日志

# === 关键路径 ===
/etc/rancher/k3s/k3s.yaml             # kubeconfig
/var/lib/rancher/k3s/                  # 数据目录
/etc/rancher/k3s/registries.yaml      # 镜像仓库配置

# === 资源需求 ===
# 最低:512MB RAM(轻负载)
# 推荐:2GB RAM + 2 vCPU

同类对比

特性K3sK0sMicroK8s标准 K8s
二进制大小<100MB~160MBSnap包多组件
最低内存512MB300MB540MB2GB+
默认存储SQLite/etcdetcd/SQLitedqliteetcd
内置 IngressTraefik
ARM 支持原生原生支持需编译
安装难度一行命令一行命令snap install复杂
CNCF 认证

选型建议:边缘计算和资源受限环境首选 K3s(最成熟、社区最大);IoT 极端受限环境考虑 K0s;开发测试用 MicroK8s(Ubuntu 生态好)。