基本介绍
1、K3d 概念
K3d 是一个轻量级的 Kubernetes 安装器,支持在 Docker 中运行单节点或多节点的 K3s 集群,可以用于开发、测试和 CI/CD。
K3d 项目地址:https://github.com/k3d-io/k3d
K3d 官网文档:https://k3d.io/v5.7.3/
2、K3s 概念
K3s 是一个由 Rancher 发布的轻量级 Kubernetes 发行版,专为边缘计算、物联网(IoT)、CI/CD、开发测试环境以及在 ARM 设备上运行而设计。它具有以下特点:
K3s 项目地址:https://github.com/k3s-io/k3s/
K3s 官网文档:https://docs.rancher.cn/k3s/
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、部署集群
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
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.
书籍推荐
最后推荐一本笔者从 Docker 进阶到 Kubernetes 自学过程中,受益较深的书籍。笔者经常复读,并结合工作实践不断加深理解和体会,可谓常读常新。希望这本书可以帮助到更多对 Kubernetes 感兴趣或刚开始学习的读者。