一個容器,但是一整個k8s集羣

你可能需要一個快速啓動和銷燬的 k8s 集羣;你可能在資源受限的環境中運行 k8s 集羣;你可能是一個完全的初學者,覺得搭建完整的 k8s 套件太難。那麼這篇短文可能可以幫到你。

各種丐版 k8s 集羣

你可能見過各種丐版的 k8s 集羣部署方案,比如:K3S、K3d、Kind、MicroK8S、Minikube、Docker Desktop。而今天要寫的是其中之一:K3d。

爲什麼選擇 k3d 呢,因爲筆者在一個非常特殊的環境中使用 k8s:

  1. 這是一個 x86 的 openwrt 軟路由系統,已經內置了 docker。除了 k3d,其他的方案都因爲各種原因而失敗了。當然普通的 PC 以上方案都是可以的。而 k3d 幾乎也是最簡單的。
  2. 筆者打算在這個軟路由上安裝自己平時要用到的各種中間件,比如 nexus oss、jenkins 等等 。
  3. 考慮到這個環境可能需要做備份和重建,因此需要考慮一個快速啓動和銷燬的 k8s 集羣。後續在加上 argo-cd 等技術,可以實現一個完整的 k8s 集羣的備份和恢復。

使用 k3d 之前的準備

  1. 你需要一個 docker 環境。(必要)

開始安裝 k3d

方法 1,你可以選擇使用官方提供的腳本進行安裝:

wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash

方法 2,你也可以直接下載二進制文件,然後加入到 PATH 即可:https://github.com/k3d-io/k3d/releases

如果從 github 下載對你的網絡來說是一種困難,你可以選擇 k3d1 或者 FastGithub2

創建一個 k3d.yml 文件

k3d.yml 是用戶在創建 k3d 集羣時使用的配置文件。這是一個範例的配置文件:

apiVersion: k3d.io/v1alpha4
kind: Simple
metadata:
  name: k3s-default
servers: 1 # same as `--servers 1`
agents: 2 # same as `--agents 2`
image: docker.io/rancher/k3s:v1.25.6-k3s1
kubeAPI: # same as `--api-port myhost.my.domain:6445` (where the name would resolve to 127.0.0.1)
  host: '127.0.0.1' # important for the `server` setting in the kubeconfig
  # hostIP: "192.168.1.200" # where the Kubernetes API will be listening on
  hostPort: '6445' # where the Kubernetes API listening port will be mapped to on your host system
ports:
  - port: 80:80 # same as `--port '8080:80@loadbalancer'`
    nodeFilters:
      - loadbalancer
options:
  k3d: # k3d runtime settings
    wait: true # wait for cluster to be usable before returining; same as `--wait` (default: true)
    timeout: '60s' # wait timeout before aborting; same as `--timeout 60s`
    disableLoadbalancer: false # same as `--no-lb`
    disableImageVolume: false # same as `--no-image-volume`
    disableRollback: false # same as `--no-Rollback`
    loadbalancer:
      configOverrides:
        - settings.workerConnections=2048
  k3s: # options passed on to K3s itself
    extraArgs: # additional arguments passed to the `k3s server|agent` command; same as `--k3s-arg`
      - arg: '--tls-san=127.0.0.1 --tls-san=ks.newbe.io'
        nodeFilters:
          - server:*
  kubeconfig:
    updateDefaultKubeconfig: true # add new cluster to your default Kubeconfig; same as `--kubeconfig-update-default` (default: true)
    switchCurrentContext: true # also set current-context to the new cluster's context; same as `--kubeconfig-switch-context` (default: true)
registries: # define how registries should be created or used
  config:
    | # define contents of the `registries.yaml` file (or reference a file); same as `--registry-config /path/to/config.yaml`
    mirrors:
      "docker.io":
        endpoint:
          - "https://mirror.ccs.tencentyun.com"

創建一個 k3d 集羣

有了配置文件,現在就可以創建一個 k3d 集羣了:

k3d cluster create --config k3d.yml

可發帖可羣聊的技術交流方式已經上線,歡迎通過鏈接,加入我們一起討論。 https://www.newbe.pro/links/

運行結果大致如下:

root@OpenWrt:/mnt/sda1/workspace# ./k3d cluster create --config k3d.yml
INFO[0000] Using config file k3d.yml (k3d.io/v1alpha4#simple)
INFO[0000] portmapping '80:80' targets the loadbalancer: defaulting to [servers:*:proxy agents:*:proxy]
INFO[0000] Prep: Network
INFO[0000] Created network 'k3d-k3s-default'
INFO[0000] Created image volume k3d-k3s-default-images
INFO[0000] Starting new tools node...
INFO[0000] Starting Node 'k3d-k3s-default-tools'
INFO[0001] Creating node 'k3d-k3s-default-server-0'
INFO[0001] Creating node 'k3d-k3s-default-agent-0'
INFO[0001] Creating node 'k3d-k3s-default-agent-1'
INFO[0001] Creating LoadBalancer 'k3d-k3s-default-serverlb'
INFO[0001] Using the k3d-tools node to gather environment information
INFO[0001] HostIP: using network gateway 172.18.0.1 address
INFO[0001] Starting cluster 'k3s-default'
INFO[0001] Starting servers...
INFO[0001] Starting Node 'k3d-k3s-default-server-0'
INFO[0006] Starting agents...
INFO[0007] Starting Node 'k3d-k3s-default-agent-0'
INFO[0007] Starting Node 'k3d-k3s-default-agent-1'
INFO[0010] Starting helpers...
INFO[0010] Starting Node 'k3d-k3s-default-serverlb'
INFO[0017] Injecting records for hostAliases (incl. host.k3d.internal) and for 4 network members into CoreDNS configmap...
INFO[0019] Cluster 'k3s-default' created successfully!
INFO[0019] You can now use it like this:
kubectl cluster-info

這樣我們就得到了一個 k3d 集羣,其中包含了一個 master 節點和兩個 worker 節點。

獲取 kubeconfig

k3d 集羣創建成功後,我們可以通過 k3d 命令獲取 kubeconfig 文件:

k3d kubeconfig get --all

將 kubeconfig 配置好,就可以使用 kubectl 命令操作 k3d 集羣了。

kubectl get nodes
NAME                       STATUS   ROLES                  AGE   VERSION
k3d-k3s-default-server-0   Ready    control-plane,master   38m   v1.25.6+k3s1
k3d-k3s-default-agent-1    Ready    <none>                 38m   v1.25.6+k3s1
k3d-k3s-default-agent-0    Ready    <none>                 38m   v1.25.6+k3s1

可發帖可羣聊的技術交流方式已經上線,歡迎通過鏈接,加入我們一起討論。 https://www.newbe.pro/links/

部署一個應用

我們可以通過 kubectl 命令部署一個應用,比如 nginx:

kubectl create deployment nginx --image=nginx
kubectl create service clusterip nginx --tcp=80:80
kubectl apply -f thatfile.yaml

其中 thatfile.yaml 內容如下:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
  annotations:
    ingress.kubernetes.io/ssl-redirect: 'false'
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: nginx
                port:
                  number: 80

使用 curl 命令訪問 nginx 服務:

curl http://localhost

這樣我們就完成了一個 k3d 集羣的創建和應用部署。

總結

k3d 是一個非常好用的 k3s 集羣管理工具,它可以幫助我們快速創建一個 k3s 集羣,方便我們進行開發和測試。後續我們還會介紹如何使用通過其他的一些配套工具,使得我們的開發和測試更加方便。

參考資料

感謝您的閱讀,如果您覺得本文有用,請點贊、關注和轉發。


  1. https://newbe.pro/Mirrors/Mirrors-k3d/

  2. https://newbe.pro/Mirrors/Mirrors-FastGithub/

  3. https://k3d.io/

  4. https://k3d.io/v5.4.6/usage/exposing_services/

  5. https://thechief.io/c/editorial/k3d-vs-k3s-vs-kind-vs-microk8s-vs-minikube/

  6. https://www.cnblogs.com/haogj/p/16397876.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章