写在最前
很多时候我们使用 Kubernetes 只是为了做验证和测试,而不是搭建一套完整的生产集群。如果完全按标准 k8s 的方式部署,往往要处理大量无关的系统配置,比如 hosts、防火墙、内核参数、swap 等,成本和心智负担都很高。
k3s 通过一条命令即可快速安装一个与 Kubernetes 高度兼容的容器管理系统,让我们把精力放在测试本身,而不是环境搭建上。本质上,我们做的是验证测试,而不是建设 k8s 基础设施。
相比标准 k8s,k3s 更轻量、依赖更少、资源占用更低,但在 API 和使用方式上与 k8s 基本一致,绝大多数应用可以直接运行。它更适合测试、验证和临时环境,而非复杂的生产场景。
前置条件
以下安装命令为官方命令,过程中会从 Docker 官方镜像仓库拉取镜像,请提前为 Docker 配置代理,确保能够正常访问外网并完成镜像下载。
1. 开始部署
curl -sfL https://get.k3s.io | sh -
root@tanqidi:~# curl -sfL https://get.k3s.io | sh -
[INFO] Finding release for channel stable
[INFO] Using v1.34.3+k3s1 as release
[INFO] Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.34.3+k3s1/sha256sum-amd64.txt
[INFO] Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.34.3+k3s1/k3s
[INFO] Verifying binary download
[INFO] Installing k3s to /usr/local/bin/k3s
[INFO] Skipping installation of SELinux RPM
[INFO] Creating /usr/local/bin/kubectl symlink to k3s
[INFO] Creating /usr/local/bin/crictl symlink to k3s
[INFO] Skipping /usr/local/bin/ctr symlink to k3s, command exists in PATH at /usr/bin/ctr
[INFO] Creating killall script /usr/local/bin/k3s-killall.sh
[INFO] Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO] env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO] systemd: Creating service file /etc/systemd/system/k3s.service
[INFO] systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO] systemd: Starting k3s
# 开机自启动
root@tanqidi:~# systemctl enable --now k3s
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
root@tanqidi:~# kubectl get no
NAME STATUS ROLES AGE VERSION
tanqidi Ready control-plane 34m v1.34.3+k3s1
root@tanqidi:~#
root@tanqidi:~# kubectl get deploy -A
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
default nginx-deployment 1/1 1 1 27m
kube-system coredns 1/1 1 1 32m
kube-system local-path-provisioner 1/1 1 1 32m
kube-system metrics-server 1/1 1 1 32m
kube-system traefik 1/1 1 1 31m
root@tanqidi:~#
# 2. 验证环境
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30080
评论