香港VPS部署K8s单节点集群:K3s轻量版 + Helm + Ingress Controller从零上生产环境
Kubernetes 是云原生应用部署的行业标准,但标准版 K8s 对服务器资源要求很高,不适合单台 VPS。K3s 是 Rancher 发布的轻量级 Kubernetes 发行版,内存占用仅约 512MB,完全兼容标准 K8s API,是在香港 VPS 上运行生产级容器编排的最佳选择。
一、K3s vs 标准 K8s 对比
| 维度 | 标准 Kubernetes | K3s |
|---|---|---|
| 最低内存要求 | 4GB(控制面) | 512MB |
| 安装复杂度 | 高(kubeadm 多步骤) | 一条命令 |
| 内置组件 | 需手动安装 CNI/LB/Ingress | 内置 Flannel/Traefik/CoreDNS |
| API 兼容性 | 100%(基准) | 完全兼容(可直接用 kubectl) |
| 适合场景 | 大型生产集群(3节点+) | 单节点/边缘/VPS 生产环境 |
二、K3s 安装(单节点,1分钟完成)
<code"># 服务器要求:Ubuntu 22.04,4核8G 以上,香港 VPS # 一键安装 K3s(含 kubectl、Traefik Ingress、CoreDNS) curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="\ --tls-san 你的服务器公网IP \ --tls-san yourdomain.com \ --disable servicelb \ --write-kubeconfig-mode 644" sh - # 等待安装完成(约1分钟) systemctl status k3s # 验证节点状态 kubectl get nodes # 输出应显示: # NAME STATUS ROLES AGE VERSION # hk-vps-1 Ready control-plane,master 1m v1.29.x+k3s1
<code"># 配置本地 kubectl 远程管理(在本地机器执行) scp root@服务器IP:/etc/rancher/k3s/k3s.yaml ~/.kube/config-hk # 修改 server 地址为公网 IP sed -i 's/127.0.0.1/服务器公网IP/g' ~/.kube/config-hk export KUBECONFIG=~/.kube/config-hk kubectl get nodes # 验证远程连接
三、安装 Helm(Kubernetes 包管理器)
<code"># 在服务器上安装 Helm 3 curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash helm version # 添加常用 Chart 仓库 helm repo add stable https://charts.helm.sh/stable helm repo add bitnami https://charts.bitnami.com/bitnami helm repo add jetstack https://charts.jetstack.io # cert-manager helm repo update
四、配置自动 HTTPS 证书(cert-manager)
<code"># 安装 cert-manager(自动申请和续期 Let's Encrypt 证书)
helm install cert-manager jetstack/cert-manager \
--namespace cert-manager \
--create-namespace \
--set installCRDs=true
# 等待 cert-manager 就绪
kubectl -n cert-manager rollout status deployment/cert-manager
# 创建 Let's Encrypt ClusterIssuer
cat << 'EOF' | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-prod
spec:
acme:
server: https://acme-v02.api.letsencrypt.org/directory
email: admin@yourdomain.com
privateKeySecretRef:
name: letsencrypt-prod
solvers:
- http01:
ingress:
class: traefik # K3s 内置 Traefik Ingress
EOF五、部署示例应用(含 Ingress + 自动 HTTPS)
<code"># 创建命名空间(隔离不同项目)
kubectl create namespace production
kubectl create namespace staging
# 部署 Nginx 示例应用
cat << 'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
namespace: production
spec:
replicas: 2
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web
image: nginx:alpine
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
name: web-app-svc
namespace: production
spec:
selector:
app: web-app
ports:
- port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-app-ingress
namespace: production
annotations:
cert-manager.io/cluster-issuer: "letsencrypt-prod"
traefik.ingress.kubernetes.io/router.middlewares: production-redirect-https@kubernetescrd
spec:
tls:
- hosts:
- app.yourdomain.com
secretName: app-tls
rules:
- host: app.yourdomain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-app-svc
port:
number: 80
EOF六、常用运维命令速查
<code"># 查看所有命名空间的 Pod 状态
kubectl get pods --all-namespaces
# 查看 Pod 日志(实时)
kubectl logs -f deployment/web-app -n production
# 滚动更新镜像(零停机发布)
kubectl set image deployment/web-app web=nginx:1.26 -n production
kubectl rollout status deployment/web-app -n production
# 回滚到上一版本
kubectl rollout undo deployment/web-app -n production
# 查看资源使用情况
kubectl top nodes
kubectl top pods --all-namespaces
# 进入 Pod 调试
kubectl exec -it $(kubectl get pod -n production -l app=web-app -o jsonpath='{.items[0].metadata.name}') -n production -- sh七、K3s 数据备份
<code"># K3s 使用 SQLite 作为默认数据库,定期备份 k3s etcd-snapshot save --name backup-$(date +%Y%m%d) # 或直接备份 SQLite 文件 cp /var/lib/rancher/k3s/server/db/state.db \ /backup/k3s/state-$(date +%Y%m%d).db
八、总结
K3s 将 Kubernetes 的学习和使用门槛大幅降低,在 4核8G 香港 VPS 上可以流畅运行 10~20 个容器应用,同时享受 K8s 的完整生态(Helm Charts、自动扩缩容、滚动发布、健康检查)。对于正在从 Docker Compose 迁移到容器编排的开发团队,K3s 是最平滑的过渡路径。