Kubernetes 作为 IoT 基础 (用于 Home Assistant 等)
目录
我已经有不少应用程序被容器化并由 Docker Compose 管理。维护应用级别的 Docker Compose YAML 文件工作量很小。然而,仍有一些零碎的组件需要我不时手动更新或升级。因此,我决定进行迁移,这是记录该任务的第一篇博客。
安装 k3s
curl -sfL https://get.k3s.io | sh -s - --no-deploy=traefik --default-local-storage-path=/opt/k3s
# 设置 kubectl 自动补全
echo "source <(kubectl completion bash)" >> ~/.bashrc仅启用了最基本的服务:
kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
local-path-provisioner-58fb86bdfd-n2znx 1/1 Running 0 45d
coredns-d798c9dd-d7tcx 1/1 Running 0 45d
svclb-traefik-frmgz 2/2 Running 0 42d
traefik-667499d69f-jtkbk 1/1 Running 0 42d安装 Helm
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
echo "export KUBECONFIG=/etc/rancher/k3s/k3s.yaml" >> ~/.bashrc
helm repo add stable https://kubernetes-charts.storage.googleapis.com
# (可选) 添加 emqx charts 仓库
helm repo add emqx https://repos.emqx.io/charts安装 Traefik
helm install traefik stable/traefik --set dashboard.enabled=true,dashboard.domain=traefik-dash.example.com,serviceType=LoadBalancer,rbac.enabled=true,dashboard.auth.basic.admin='x2@*#$*******####',acme.enabled=true,acme.domains.enabled=true,acme.challengeType=dns-01 --namespace=kube-system对于我的实际设置,我使用如下的 values.yaml:
# 我的 ingress 示例值
# 这是一个 YAML 格式的文件。
# 提示:使用 htpasswd 生成密码。例如 htpasswd -nb admin admin
dashboard:
enabled: true
domain: traefik-dash.example.com
auth:
basic:
admin: 'x2@*#$*******####'
serviceType: LoadBalancer
rbac:
enabled: true
ssl:
enabled: true
enforced: false
acme:
enabled: true
email: my.email@gmail.com
staging: false
logging: false
persistence:
storageClass: local-path
domains:
enabled: true
domainsList:
- main: "*.example.com"
challengeType: dns-01
dnsProvider:
name: cloudflare
cloudflare:
CLOUDFLARE_EMAIL: "my.email@gmail.com"
CLOUDFLARE_API_KEY: "32xxx****##########$$$$"安装 Home Assistant
# (可选) 安装 emqx 用于 mqtt 服务
helm install emqx stable/emqx
helm install hass stable/home-assistant --set ingress.enabled=true,ingress.hosts={'hass.example.com'},configurator.enabled=true,configurator.ingress.enabled=true,configurator.ingress.hosts={'hass-config.example.com'},hostNetwork=true待办事项
k3s 使用 containerd 作为默认容器运行时,所以在同一个节点上 Docker 和 Docker Compose (用于尚未迁移到 k8s 的应用) 可以共存。计划稍后将 Pi-hole 迁移到集群。