你可能须要一个疾速启动和销毁的 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...

如果从 github 下载对你的网络来说是一种艰难,你能够抉择 k3d1 或者 FastGithub2

创立一个 k3d.yml 文件

k3d.yml 是用户在创立 k3d 集群时应用的配置文件。这是一个范例的配置文件:

apiVersion: k3d.io/v1alpha4kind: Simplemetadata:  name: k3s-defaultservers: 1 # same as `--servers 1`agents: 2 # same as `--agents 2`image: docker.io/rancher/k3s:v1.25.6-k3s1kubeAPI: # 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 systemports:  - port: 80:80 # same as `--port '8080:80@loadbalancer'`    nodeFilters:      - loadbalanceroptions:  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.ymlINFO[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: NetworkINFO[0000] Created network 'k3d-k3s-default'INFO[0000] Created image volume k3d-k3s-default-imagesINFO[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 informationINFO[0001] HostIP: using network gateway 172.18.0.1 addressINFO[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   VERSIONk3d-k3s-default-server-0   Ready    control-plane,master   38m   v1.25.6+k3s1k3d-k3s-default-agent-1    Ready    <none>                 38m   v1.25.6+k3s1k3d-k3s-default-agent-0    Ready    <none>                 38m   v1.25.6+k3s1
可发帖可群聊的技术交换形式曾经上线,欢送通过链接,退出咱们一起探讨。 https://www.newbe.pro/links/

部署一个利用

咱们能够通过 kubectl 命令部署一个利用,比方 nginx:

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

其中 thatfile.yaml 内容如下:

apiVersion: networking.k8s.io/v1kind: Ingressmetadata:  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 集群,不便咱们进行开发和测试。后续咱们还会介绍如何应用通过其余的一些配套工具,使得咱们的开发和测试更加不便。

参考资料

  • k3d3
  • exposing_services4
  • K3d vs k3s vs Kind vs Microk8s vs Minikube5
  • k3d 入门:在 Docker 中运行 k3s6

感谢您的浏览,如果您感觉本文有用,请点赞、关注和转发。

  • 本文作者: newbe36524
  • 本文链接: https://www.newbe.pro/Others/0x01A-one-container-but-an-entire-k8s-cluster/
  • 版权申明: 本博客所有文章除特地申明外,均采纳 BY-NC-SA 许可协定。转载请注明出处!

  1. https://newbe.pro/Mirrors/Mir... ↩
  2. https://newbe.pro/Mirrors/Mir... ↩
  3. https://k3d.io/ ↩
  4. https://k3d.io/v5.4.6/usage/e... ↩
  5. https://thechief.io/c/editori... ↩
  6. https://www.cnblogs.com/haogj... ↩