本文内容均来自个人笔记并重新梳理,如有错误欢迎指正!
如果对您有帮助,烦请点赞、关注、转发、订阅专栏!
专栏订阅入口
Linux 专栏 | Docker 专栏 | Kubernetes 专栏
往期精彩文章
【Docker】(全网首发)Kylin V10 下 MySQL 容器内存占用异常的解决方法
目录
一、基本介绍
1、K3d 概念
K3d 是一个轻量级的 Kubernetes 安装器,支持在 Docker 中运行单节点或多节点的 K3s 集群,可以用于开发、测试和 CI/CD。
K3d 项目地址:GitHub - k3d-io/k3d
K3d 官网文档:k3d | Document
2、K3s 概念
K3s 则是一个由 Rancher 发布的轻量级 Kubernetes 发行版,专为边缘计算、物联网(IoT)、CI/CD、开发测试环境以及在 ARM 设备上运行而设计。它具有以下特点:
- 轻量级:K3s 的二进制文件小于 100MB,占用的内存资源也相对较少,使得它能够在资源受限的环境中运行
- 易于安装:K3s 可以通过一个简单的命令快速安装,并且支持多种安装方式,包括在物理机、虚拟机或容器中运行
- 兼容性:K3s 与 Kubernetes 兼容,支持 Kubernetes 的 API,可以使用标准的 kubectl 工具进行管理和操作
- 内置功能:K3s 内置了一些常用的功能和服务,如 Traefik Ingress 控制器、Helm 控制器、本地存储提供程序和负载均衡器等
- 安全性:K3s 默认配置是安全的,并且支持自动更新证书,减少了安全风险
- 跨平台支持:K3s 支持多种架构,包括 x86_64、ARM64 和 ARMv7,使其可以灵活地在不同的硬件上运行
K3s 项目地址:GitHub - k3s-io/k3s
K3s 官网文档:K3S文档 | Rancher
二、K3d 下载、安装
# AMD架构
wget -O /usr/bin/k3d https://github.com/k3d-io/k3d/releases/download/v5.7.3/k3d-linux-amd64
chmod +x /usr/bin/k3d
# ARM架构
wget -O /usr/bin/k3d https://github.com/k3d-io/k3d/releases/download/v5.7.3/k3d-linux-arm64
chmod +x /usr/bin/k3d
# 查看版本信息
k3d version
三、K3d 部署 Kubernetes 集群
1、准备工作
# 关闭防火墙
systemctl disable --now firewalld
# 关闭 selinxu
setenforce 0
sed -i ’s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config
# 清空 iptables 规则
iptables -F && iptables -t nat -F
# 禁用 swap 分区
swapoff -a
sed -i '/swap / s/^\(.*\)$/#\1/g' /etc/fstab
# 安装 docker、kubectl(详细过程略)
🔔 docker 版本高于 v20.10.5,runc 版本高于 v1.0.0-rc93
2、部署集群
- 单节点集群(包含一个 Master 节点的集群)
k3d cluster create demo -s 1
🔔 -s:指定 Master 节点数量,默认为 1
# 输出信息
INFO[0000] Prep: Network
INFO[0000] Created network 'k3d-demo'
INFO[0000] Created image volume k3d-demo-images
INFO[0000] Starting new tools node...
INFO[0001] Creating node 'k3d-demo-server-0'
INFO[0001] Pulling image 'ghcr.io/k3d-io/k3d-tools:5.7.3'
INFO[0004] Starting node 'k3d-demo-tools'
INFO[0007] Pulling image 'docker.io/rancher/k3s:v1.30.3-k3s1'
INFO[0012] Creating LoadBalancer 'k3d-demo-serverlb'
INFO[0014] Pulling image 'ghcr.io/k3d-io/k3d-proxy:5.7.3'
INFO[0019] Using the k3d-tools node to gather environment information
INFO[0019] HostIP: using network gateway 172.18.0.1 address
INFO[0019] Starting cluster 'demo'
INFO[0019] Starting servers...
INFO[0019] Starting node 'k3d-demo-server-0'
INFO[0025] All agents already running.
INFO[0025] Starting helpers...
INFO[0025] Starting node 'k3d-demo-serverlb'
INFO[0032] Injecting records for hostAliases (incl. host.k3d.internal) and for 2 network members into CoreDNS configmap...
INFO[0034] Cluster 'demo' created successfully!
INFO[0034] You can now use it like this:
kubectl cluster-info
- 多节点集群(部署包含一个 Master 节点和一个 Node 节点的集群)
k3d cluster create demo -a 1
🔔 -a:指定 Node 节点数量,默认为 0
# 输出信息
INFO[0000] Prep: Network
INFO[0000] Created network 'k3d-demo'
INFO[0000] Created image volume k3d-demo-images
INFO[0000] Starting new tools node...
INFO[0000] Starting node 'k3d-demo-tools'
INFO[0001] Creating node 'k3d-demo-server-0'
INFO[0001] Creating node 'k3d-demo-agent-0'
INFO[0001] Creating LoadBalancer 'k3d-demo-serverlb'
INFO[0001] Using the k3d-tools node to gather environment information
INFO[0002] HostIP: using network gateway 172.19.0.1 address
INFO[0002] Starting cluster 'demo'
INFO[0002] Starting servers...
INFO[0002] Starting node 'k3d-demo-server-0'
INFO[0007] Starting agents...
INFO[0008] Starting node 'k3d-demo-agent-0'
INFO[0020] Starting helpers...
INFO[0020] Starting node 'k3d-demo-serverlb'
INFO[0026] Injecting records for hostAliases (incl. host.k3d.internal) and for 3 network members into CoreDNS configmap...
INFO[0029] Cluster 'demo' created successfully!
INFO[0029] You can now use it like this:
kubectl cluster-info
3、实测结果
Kubernetes 集群组件均以容器方式运行。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03daca5d7ad5 ghcr.io/k3d-io/k3d-proxy:5.7.3 "/bin/sh -c nginx-pr…" About a minute ago Up About a minute 80/tcp, 0.0.0.0:36823->6443/tcp k3d-demo-serverlb
08a14f0a247b rancher/k3s:v1.30.3-k3s1 "/bin/k3d-entrypoint…" About a minute ago Up About a minute k3d-demo-agent-0
b36d45027338 rancher/k3s:v1.30.3-k3s1 "/bin/k3d-entrypoint…" About a minute ago Up About a minute k3d-demo-server-0
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ghcr.io/k3d-io/k3d-proxy 5.7.3 0475c00b0478 3 weeks ago 61.2MB
ghcr.io/k3d-io/k3d-tools 5.7.3 91ba69c868e8 3 weeks ago 20.8MB
rancher/k3s v1.30.3-k3s1 86193a59fa57 3 weeks ago 209MB
四、K3d 常用命令
# 创建集群
k3d cluster create demo
# 删除集群
k3d cluster delete demo
# 查看集群
k3d cluster list
k3d -h
https://k3d.io/
k3d is a wrapper CLI that helps you to easily create k3s clusters inside docker.
Nodes of a k3d cluster are docker containers running a k3s image.
All Nodes of a k3d cluster are part of the same docker network.
Usage:
k3d [flags]
k3d [command]
Available Commands:
cluster Manage cluster(s)
completion Generate completion scripts for [bash, zsh, fish, powershell | psh]
config Work with config file(s)
help Help about any command
image Handle container images.
kubeconfig Manage kubeconfig(s)
node Manage node(s)
registry Manage registry/registries
version Show k3d and default k3s version
Flags:
-h, --help help for k3d
--timestamps Enable Log timestamps
--trace Enable super verbose output (trace logging)
--verbose Enable verbose output (debug logging)
--version Show k3d and default k3s version
Use "k3d [command] --help" for more information about a command.
========================================================================
# k3d cluster 命令汇总
k3d cluster -h
Manage cluster(s)
Usage:
k3d cluster [flags]
k3d cluster [command]
Available Commands:
create Create a new cluster
delete Delete cluster(s).
edit [EXPERIMENTAL] Edit cluster(s).
list List cluster(s)
start Start existing k3d cluster(s)
stop Stop existing k3d cluster(s)
Flags:
-h, --help help for cluster
Global Flags:
--timestamps Enable Log timestamps
--trace Enable super verbose output (trace logging)
--verbose Enable verbose output (debug logging)
Use "k3d cluster [command] --help" for more information about a command.