前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【K8s】Kubernetes 安装方法之 K3d

【K8s】Kubernetes 安装方法之 K3d

作者头像
行者Sun
发布2024-09-12 16:54:30
1350
发布2024-09-12 16:54:30
举报
文章被收录于专栏:Kubernetes

基本介绍

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 的二进制文件小于 100MB,占用的内存资源也相对较少,使得它能够在资源受限的环境中运行
  • 易于安装:K3s 可以通过一个简单的命令快速安装,并且支持多种安装方式,包括在物理机、虚拟机或容器中运行
  • 兼容性:K3s 与 Kubernetes 兼容,支持 Kubernetes 的 API,可以使用标准的 kubectl 工具进行管理和操作
  • 内置功能:K3s 内置了一些常用的功能和服务,如 Traefik Ingress 控制器、Helm 控制器、本地存储提供程序和负载均衡器等
  • 安全性:K3s 默认配置是安全的,并且支持自动更新证书,减少了安全风险
  • 跨平台支持:K3s 支持多种架构,包括 x86_64、ARM64 和 ARMv7,使其可以灵活地在不同的硬件上运行

K3s 项目地址:https://github.com/k3s-io/k3s/

K3s 官网文档:https://docs.rancher.cn/k3s/


K3d 下载、安装

代码语言:javascript
复制
# 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、准备工作

代码语言:javascript
复制
# 关闭防火墙
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 节点的集群)
代码语言:javascript
复制
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 节点的集群)
代码语言:javascript
复制
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 集群组件均以容器方式运行。

代码语言:javascript
复制
# 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 常用命令

代码语言:javascript
复制
# 创建集群
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 感兴趣或刚开始学习的读者。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-09-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 实施运维知识交流 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档