前言

前篇 已經介紹了 Talos 是什麼,以及要安裝的環境簡介,接下來要撰寫安裝用的設定檔。

這是一個系列文,目錄 在此。

產生安裝用設定檔 (Machine Config)

talos 與普通安裝作業系統不太一樣,不是裝好系統後才下指令設定環境,而是安裝前就撰寫好設定檔 (Machine Config),系統安裝時會照著那份 spec 設置機器,內建可以做到俗稱的 IaC (基礎設施即程式碼,Infrastructure as Code)。

首先,我們會先純使用官方的 talosctl,再來介紹第三方工具 talhelper(已棄用)。

官方作法

  1. 先使用 talosctl gen secrets -o secrets.yaml,產生 secrets.yaml,包含了 kubernetes 叢集的溝通用密鑰
$ talosctl gen secrets -o secrets.yaml
$ cat secrets.yaml
# cluster:
#     id: buUIh....
#     secret: khlN....
# secrets:
#     bootstraptoken: qwt....
#     secretboxencryptionsecret: 3kGCa....
# trustdinfo:
#     token: 5kyafg.ce2cod17...
# certs:
# ...
  1. 同資料夾下,再建立一個檔案 talos-template.yaml,作為叢集的共用設定。全部的設定可以參考官方文件:Config | Talos Linux
talos-template.yaml
machine:
  time:
    servers:
      - 10.2.6.1 # NTP server on OPNsense
      - 211.22.103.157 # tock.stdtime.gov.tw
      - 118.163.81.63 # watch.stdtime.gov.tw
      - 118.163.81.61 # time.stdtime.gov.tw
      - 211.22.103.158 # clock.stdtime.gov.tw
      - 118.163.81.62 # tick.stdtime.gov.tw
      - /dev/ptp0 # PTP device
  network:
    nameservers:
      - 10.2.6.5
  install:
    wipe: true
 
cluster:
  network:
    dnsDomain: efficient.k8s.lab.yee
    podSubnets:
      - 10.244.0.0/16
    serviceSubnets:
      - 10.96.0.0/12
	cni:
      name: none
  proxy:
    disabled: true
  allowSchedulingOnControlPlanes: true
  • 24-27:因想使用 cilium 當作 CNI,所以設定為 none
  1. 產生帶有 VM Guest Tool 和 UPS NUT Client 的 Extension ID,詳情介紹可參考 Talos - 安裝 Extension
talos-extension-proxmox.yaml
customization:
  systemExtensions:
    officialExtensions:
      - siderolabs/qemu-guest-agent
      - siderolabs/nut-client
talos-extension-vmware.yaml
customization:
  systemExtensions:
    officialExtensions:
      - siderolabs/vmtoolsd-guest-agent
      - siderolabs/nut-client
$ curl -X POST --data-binary @talos-extension-proxmox.yaml https://factory.talos.dev/schematics
# {"id":"54524b221db0eeee2d8bb3d21379e04c662c386ecef3745c24a0aa6f51ab31dd"}
$ curl -X POST --data-binary @talos-extension-vmware.yaml https://factory.talos.dev/schematics
# {"id":"091c828af58eea73d872919931153ffda36cf8ebcdc5b027889412028e7531d3"}
  1. 建立每一台機器的專屬設定,這裡分別示範一台 controlplane 和 worker
controlplane.11.patch.yaml
machine:
  network:
    hostname: control-e01
    interfaces:
    - deviceSelector:
        busPath: "0*"
      dhcp: false
      addresses:
        - 10.2.7.11/23
      vip:
        ip: 10.2.7.15
      routes:
        - network: 0.0.0.0/0
          gateway: 10.2.6.1
      mtu: 8192
  install:
      image: factory.talos.dev/installer/54524b221db0eeee2d8bb3d21379e04c662c386ecef3745c24a0aa6f51ab31dd:v1.7.3
worker.16.patch.yaml
machine:
  network:
    hostname: worker-e01
    interfaces:
    - deviceSelector:
        busPath: "0*"
      dhcp: false
      addresses:
        - 10.2.7.16/23
      routes:
        - network: 0.0.0.0/0
          gateway: 10.2.6.1
      mtu: 8192
  install:
      image: factory.talos.dev/installer/54524b221db0eeee2d8bb3d21379e04c662c386ecef3745c24a0aa6f51ab31dd:v1.7.3
  1. 使用 talosctl gen config 產生一台 controlplane 和 worker node 的 Machine Configuration
$ talosctl gen config efficient-cluster01 https://10.2.7.15:6443 --config-patch @talos-template.yaml --with-secrets secrets.yaml --output efficient-cluster01
 
# generating PKI and tokens
# Created efficient-cluster01/controlplane.yaml
# Created efficient-cluster01/worker.yaml
# Created efficient-cluster01/talosconfig
  1. 依序使用 talosctl machineconfig patch 為每台機器產生專屬 Machine Config
$ talosctl machineconfig patch efficient-cluster01/controlplane.yaml --patch @controlplane.11.patch.yaml --output efficient-cluster01/controlplane.11.yaml
$ talosctl machineconfig patch efficient-cluster01/controlplane.yaml --patch @controlplane.12.patch.yaml --output efficient-cluster01/controlplane.12.yaml
$ talosctl machineconfig patch efficient-cluster01/controlplane.yaml --patch @controlplane.13.patch.yaml --output efficient-cluster01/controlplane.13.yaml
$ talosctl machineconfig patch efficient-cluster01/worker.yaml --patch @worker.16.patch.yaml --output efficient-cluster01/worker.16.yaml
$ talosctl machineconfig patch efficient-cluster01/worker.yaml --patch @worker.17.patch.yaml --output efficient-cluster01/worker.17.yaml
  1. (Optional) 驗證修改後的設定檔
$ talosctl validate --config efficient-cluster01/controlplane.11.yaml --mode metal
# efficient-cluster01/controlplane.11.yaml is valid for metal mode
$ talosctl validate --config efficient-cluster01/controlplane.12.yaml --mode metal
# efficient-cluster01/controlplane.12.yaml is valid for metal mode
$ talosctl validate --config efficient-cluster01/controlplane.13.yaml --mode metal
# efficient-cluster01/controlplane.13.yaml is valid for metal mode
$ talosctl validate --config efficient-cluster01/worker.16.yaml --mode metal
# efficient-cluster01/worker.16.yaml is valid for metal mode
$ talosctl validate --config efficient-cluster01/worker.17.yaml --mode metal
# efficient-cluster01/worker.17.yaml is valid for metal mode

下一步,我們來安裝系統: 我的 Talos 安裝紀錄 (3) - 安裝系統

補充:使用 talhelper

(筆者已棄用此方式,以下流程不再更新,僅供參考)

從官方作法可以發現,talosctl 沒辦法一次為每一台機器產生專屬的 machine config,導致過程比較繁瑣。

使用 talhelper 可以幫我們解決這個困擾。

  1. 首先,我們先安裝 talhelper
$ brew install talhelper
  1. 使用 gensecret 產生 secret file
$ talhelper gensecret > talsecret.yaml

(Optional) 可以使用 sopsage 來加密此 yaml file,就可以安全的讓設定檔上版控

# 先安裝 sops 和 age
$ brew install sops age
 
# 產生 key
$ age-keygen -o age.key
# Public key: age1nxlyszwmfuxmplkjkwq3tw459pfpcy6gg6zddj333exetgw5lf5q7f9mzv
 
$ talhelper gensecret > talsecret.sops.yaml
$ sops --encrypt -i --age $(cat age.key | sed -n 's/^# public key: \(.*\)/\1/p') talsecret.sops.yaml
  1. 撰寫 talconfig.yaml,詳細說明可參考官網:Configuration - Talhelper
talconfig.yaml
clusterName: efficient-cluster01
endpoint: https://10.2.7.15:6443
allowSchedulingOnMasters: true
domain: efficient.k8s.lab.yee
cniConfig:
  name: none
nodes:
  - hostname: control-e01
    ipAddress: 10.2.7.11
    controlPlane: true
    installDisk: /dev/sda
    nameservers:
      - 10.2.6.5
    networkInterfaces:
      - deviceSelector:
          busPath: "0*"
        addresses:
          - 10.2.7.11/23
        routes:
          - network: 0.0.0.0/0
            gateway: 10.2.6.1
        vip:
          ip: 10.2.7.15
  - hostname: control-e02
    ipAddress: 10.2.7.12
    controlPlane: true
    installDisk: /dev/sda
    nameservers:
      - 10.2.6.5
    networkInterfaces:
      - deviceSelector:
          busPath: "0*"
        addresses:
          - 10.2.7.12/23
        routes:
          - network: 0.0.0.0/0
            gateway: 10.2.6.1
        vip:
          ip: 10.2.7.15
  - hostname: control-e03
    ipAddress: 10.2.7.13
    controlPlane: true
    installDisk: /dev/sda
    nameservers:
      - 10.2.6.5
    networkInterfaces:
      - deviceSelector:
          busPath: "0*"
        addresses:
          - 10.2.7.13/23
        routes:
          - network: 0.0.0.0/0
            gateway: 10.2.6.1
        vip:
          ip: 10.2.7.15
  - hostname: worker-e01
    ipAddress: 10.2.7.16
    controlPlane: false
    installDisk: /dev/sda
    nameservers:
      - 10.2.6.5
    networkInterfaces:
      - deviceSelector:
          busPath: "0*"
        addresses:
          - 10.2.7.16/23
        routes:
          - network: 0.0.0.0/0
            gateway: 10.2.6.1
  - hostname: worker-e02
    ipAddress: 10.2.7.17
    controlPlane: false
    installDisk: /dev/sda
    nameservers:
      - 10.2.6.5
    networkInterfaces:
      - deviceSelector:
          busPath: "0*"
        addresses:
          - 10.2.7.17/23
        routes:
          - network: 0.0.0.0/0
            gateway: 10.2.6.1
 
controlPlane:
  schematic:
    customization:
      systemExtensions:
        officialExtensions:
          - siderolabs/qemu-guest-agent
worker:
  schematic:
    customization:
      systemExtensions:
       officialExtensions:
         - siderolabs/qemu-guest-agent
  1. 產生機器的 Machine Config
# 產生 Machine Config
$ talhelper genconfig -c talconfig.yaml -s talsecret.yaml -o efficient-cluster01