k3s,autok3s的一些坑

设置coredns后还原

中心集群和边缘集群通过好几个前置机,边缘云要通过域名连通到中心云

image-20211210100932751

这里主要涉及到边缘云主节点的coredns的configmap的hosts插件域名解析。

但是后来发现,重启k3s或者添加新节点时候,coredns的hosts部分会还原回去More extensibility to CoreDNS configmap 这里有比较详细的讨论。

主要就是我们用的k3s version 1.19.5,试了最新的stable1.22.7确实支持最新的自定义coredns(主要是使用import插件)

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns-custom
  namespace: kube-system
data:
    xxx.server: | # 必须.server结尾
          example1.org example3.org  {
              hosts {
                   127.0.0.1 example1.org example3.org
                   fallthrough
              }
          }

但是升级k3s成本有点难以预料

  • 升级k3s本身就有潜在的问题(软件本身,数据迁移之类)
  • 使用rancher接管k3s,对k3s适用版本范围有限制

image-20220614192047780

后面应急方法

  • 修改纳管时候部署的agent,使用添加hostAliases,只能解决云间连通的问题,存在域名解析必须配coredns(例如镜像仓库地址)
1
kubectl patch  deploy cattle-cluster-agent -n cattle-system --type='merge' -p '{"spec":{"template":{"spec":{"hostAliases":[{"ip":"xxx.xxx.xx.205","hostnames":["域名"]}]}}}}
  • 通过crd+controller解决

AutoK3S存在问题

最后决定从0.4.6开始维护自己的分支,对autok3s的维护者水平有点失望

rancher添加集群or节点

x509: certificate has expired or is not yet valid

节点和中心集群节点的时间相差大

K3s 配置 containerd的配置

containerd 实现了 kubernetes 的 Container Runtime Interface (CRI) 接口,提供容器运行时核心功能,如镜像管理、容器管理等,相比 dockerd 更加简单、健壮和可移植。

K3s 为了降低资源消耗,将默认的 runtime 修改为 containerd,同时也内置了 Kubernetes CLI 工具 crictl 和 ctr。

K3s 默认的 containerd 配置文件目录为/var/lib/rancher/k3s/agent/etc/containerd/config.toml,但直接操作 containerd 的配置文件去设置镜像仓库或加速器相比于操作 docker 要复杂许多。K3s 为了简化配置 containerd 镜像仓库的复杂度,K3s 会在启动时检查中是否存在/etc/rancher/k3s/registries.yaml 文件,如果存在该文件,就会根据 registries.yaml 的内容转换为 containerd 的配置并存储到/var/lib/rancher/k3s/agent/etc/containerd/config.toml,从而降低了配置 containerd 镜像仓库的复杂度。

K3s 镜像仓库配置文件由两大部分组成:mirrors 和 configs

  • Mirrors 是一个用于定义专用镜像仓库的名称和 endpoint 的指令
  • Configs 部分定义了每个 mirror 的 TLS 和证书配置。对于每个 mirror,你可以定义 auth 和/或 tls

可以理解 mirror 配置就是一个反向代理,它把客户端的请求代理到 endpoint 配置的后端镜像仓库。可以配置多个 endpoint,默认解析到第一个 endpoint,如果第一个 endpoint 没有返回数据,则自动切换到第二个 endpoint,以此类推。

registries.yaml 替代默认仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
mirrors:
  "docker.io":
    endpoint:
      - "http://xxx.xxx.xx.203:8080"
configs:
  "docker.io":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
   tls:
      ca_file: /opt/certs/ca.crt # path to the ca file used in the registry
      cert_file: /opt/certs/harbor-ksd.kingsd.top.cert # path to the cert file used in the registry
      key_file: /opt/certs/harbor-ksd.kingsd.top.key # path to the key file used in the registry

重启

1
systemctl restart k3s

使用测试

1
crictl pull docker.io/library/alpine
1
2
3
4
5
6
7
8
9
10
11
[plugins."io.containerd.grpc.v1.cri".registry]
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
    [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
      endpoint = ["https://harbor.creditgogogo.com"]
  [plugins."io.containerd.grpc.v1.cri".registry.configs]
    [plugins."io.containerd.grpc.v1.cri".registry.configs."docker.io".tls]
      insecure_skip_verify = true
    [plugins."io.containerd.grpc.v1.cri".registry.configs."docker.io".auth]
      username = "admin"
      password = "Harbor12345"

Containerd 与 docker 都有默认仓库,均为 docker.io。 如果配置中未指定 mirror 为 docker.io,containerd 后会自动加载 docker.io 配置。与 docker 不同的是,containerd 可以修改 docker.io 对应的 endpoint(默认为 https://registry-1.docker.io),而 docker 无法修改。

安装rancher2.5.6 docker 单机版

准备镜像仓库

必须使用https,而且ssl证书必须有SAN(Subject Alternative Names),不然会报错x509: certificate relies on legacy Common Name field,和go version > 1.15有关,高版本弃用CN(CommonName) 字段

需要创建一个新的有效证书以包含subjectAltName属性,并且应该在使用 openssl 命令创建 SSL 自签名证书时通过指定-addext标志直接添加

1
openssl req -x509 -sha256 -nodes -days 36500 -newkey rsa:2048 -keyout harbor.key -out harbor.crt -subj "/CN=xxxxx.xxxx.cn" -addext "subjectAltName = DNS:xxxxx.xxxx.cn"

需要到的镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
rancher/shell:v0.1.16
rancher/rancher-webhook:v0.2.5
rancher/fleet:v0.3.9
rancher/gitjob:v0.1.26
rancher/fleet-agent:v0.3.9
rancher/rke-tools:v0.1.80
rancher/hyperkube:v1.23.6-rancher1
rancher/mirrored-coreos-etcd:v3.5.3
rancher/mirrored-pause:3.6
rancher/mirrored-calico-cni:v3.22.0
rancher/mirrored-calico-pod2daemon-flexvol:v3.22.0
rancher/kube-api-auth:v0.1.8
rancher/mirrored-calico-node:v3.22.0
rancher/mirrored-flannelcni-flannel:v0.17.0
rancher/mirrored-cluster-proportional-autoscaler:1.8.5
rancher/mirrored-metrics-server:v0.6.1
rancher/mirrored-ingress-nginx-kube-webhook-certgen:v1.1.1
rancher/mirrored-coredns-coredns:1.9.0
rancher/mirrored-calico-kube-controllers:v3.22.0
rancher/nginx-ingress-controller:nginx-1.2.0-rancher1

docker 命令

生成的证书要放到/root/harbor/cert下面

1
docker run -d --name rancher2.6.5 --restart=unless-stopped -e CATTLE_SYSTEM_CATALOG=bundled -e SSL_CERT_DIR="/container/certs" -v /root/harbor/cert:/container/certs -p 3280:80 -p 3443:443 --privileged xxx.xxx.xx.203:8080/rancher/rancher:v2.6.5

主要命令

1
2
3
4
docker run -d --restart=unless-stopped \
  -p 80:80 -p 443:443 \
  --privileged \
  rancher/rancher:latest

参数详解

加载system-charts,其实默认已经在rancher镜像里面,这个变量告诉 Rancher 使用本地的,而不是尝试从 GitHub 获取它们。

1
-e CATTLE_SYSTEM_CATALOG=bundled

Custom CA Root Certificates,参考这里面的 docker配置,这里配置Rancher 需要访问的服务需要用的自签名证书,不然会报错x509: certificate signed by unknown authority

1
-e SSL_CERT_DIR="/container/certs" -v /root/harbor/cert:/container/certs

配置私有仓库

根据这个Private Registry Configuration, 进到容器里面配置

1
2
3
docker exec -it rancher2.6.5 bash

vim /etc/rancher/k3s/registries.yaml

registries.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mirrors:
  "docker.io":
    endpoint:
      - "https://xxxxx.xxxx.cn:4443"
configs:
  "docker.io":
    auth:
      username: admin
      password: Harbor12345
    tls:
      key_file: /container/certs/harbor.key
      cert_file: /container/certs/harbor.crt
      #ca_file: /container/certs/ca.crt
      insecure_skip_verify: true

重启容器

1
docker restart rancher2.6.5

重新进入容器,然后配置hosts,不然使用域名解析不了,不是配置coredns

1
echo "xxx.xxx.xx.205 xxxxx.xxxx.cn" >> /etc/hosts

重启后,要等containd启动,检查containd更新的配置

1
cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml

测试拉镜像

1
crictl pull rancher/shell:v0.1.16