如果你觉得这篇文章对你有帮助,请不要吝惜你的“关注”、“点赞”、“评价”,我们可以进一步讨论实现方案和细节。你的支持永远是我前进的动力~~~
K3s 是一个轻量级的 Kubernetes 发行版,专为在资源受限的环境、边缘计算场景以及物联网设备上运行 Kubernetes 而设计。它是由 Rancher Labs 开发的,旨在提供一个完全符合 Kubernetes API 的简化版本,同时减少运行 Kubernetes 所需的资源。
virtualbox安装
官网下载 Oracle VirtualBox
设置环境变量,PATH环境变量添加:C:\Program Files\Oracle\VirtualBox
vagrant安装
sql
代码解读
复制代码vagrant -h
Usage: vagrant [options] <command> [<args>]
-h, --help Print this help.
Common commands:
autocomplete manages autocomplete installation on host
box manages boxes: installation, removal, etc.
cloud manages everything related to Vagrant Cloud
destroy stops and deletes all traces of the vagrant machine
global-status outputs status Vagrant environments for this user
halt stops the vagrant machine
help shows the help for a subcommand
init initializes a new Vagrant environment by creating a Vagrantfile
login
package packages a running vagrant environment into a box
plugin manages plugins: install, uninstall, update, etc.
port displays information about guest port mappings
powershell connects to machine via powershell remoting
provision provisions the vagrant machine
push deploys code in this environment to a configured destination
rdp connects to machine via RDP
reload restarts vagrant machine, loads new Vagrantfile configuration
resume resume a suspended vagrant machine
serve start Vagrant server
snapshot manages snapshots: saving, restoring, etc.
ssh connects to machine via SSH
ssh-config outputs OpenSSH valid configuration to connect to the machine
status outputs status of the vagrant machine
suspend suspends the machine
up starts and provisions the vagrant environment
upload upload to machine via communicator
validate validates the Vagrantfile
version prints current and latest Vagrant version
winrm executes commands on a machine via WinRM
winrm-config outputs WinRM configuration to connect to the machine
For help on any individual command run `vagrant COMMAND -h`
Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.
--[no-]color Enable or disable color output
--machine-readable Enable machine readable output
-v, --version Display Vagrant version
--debug Enable debug output
--timestamp Enable timestamps on log output
--debug-timestamp Enable debug output with timestamps
--no-tty Enable non-interactive output
查找需要的box:
app.vagrantup.com/boxes/searc…
添加box(ubuntu2204): vagrant box add ubuntu/jammy64
查看box列表: vagrant box list
创建虚拟机box:
- 创建工作目录: mkdir ubuntu2204
- 进入工作目录: cd ubuntu2204
- 初始化虚拟机box: vagrant init ubuntu/jammy64 , 生成Vagrantfile 文件,做相应的修改
- 启动box: vagrant up
- SSH进虚拟机: vagrant ssh
- 关机: vagrant halt
- 暂停: vagrant suspend
- 销毁: vagrant destroy
- 重新加载配置: vagrant reload
- 移除: vagrant box remove ubuntu/jammy64
- 目录共享: 虚拟机中 /vagrant 默认共享当前工作目录
ini
代码解读
复制代码# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.hostname = "k3s-work1"
config.vm.network "private_network", ip: "192.168.33.20"
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = false
vb.name = "ubuntu2204-k3s-work1"
# Customize the amount of memory on the VM:
vb.memory = "2048"
vb.cpus = 2
end
end
k3s部署
一键在线安装主节点:
curl -sfL rancher-mirror.rancher.cn/k3s/k3s-ins… | INSTALL_K3S_MIRROR=cn INSTALL_K3S_VERSION=v1.29.0+k3s1 K3S_KUBECONFIG_MODE=644 sh -s - --flannel-iface enp0s3 --cluster-cidr 10.100.0.0/16 --service-cidr 10.101.0.0/16 --cluster-dns 10.101.0.1
K3S_KUBECONFIG_MODE=644 是为了免sudo执行k8s命令
curl -sfL rancher-mirror.rancher.cn/k3s/k3s-ins… | INSTALL_K3S_MIRROR=cn INSTALL_K3S_VERSION=v1.29.0+k3s1 K3S_KUBECONFIG_MODE=644 INSTALL_K3S_EXEC="--flannel-iface enp0s3 --cluster-cidr 10.100.0.0/16 --service-cidr 10.101.0.0/16 --cluster-dns 10.101.0.1" sh -s -
运行此安装后:
- K3s 服务将被配置为在节点重启后或进程崩溃或被杀死时自动重启。
- 将安装其他实用程序,包括kubectl、crictl、ctr、k3s-killall.sh 和 k3s-uninstall.sh。
- 将kubeconfig文件写入到/etc/rancher/k3s/k3s.yaml,由 K3s 安装的 kubectl 将自动使用该文件
- 命令补全: source <(kubectl completion bash)
注意
每台计算机必须具有唯一的主机名。如果您的计算机没有唯一的主机名,请传递K3S_NODE_NAME环境变量,并为每个节点提供一个有效且唯一的主机名。
添加工作节点
找出token: sudo cat /var/lib/rancher/k3s/server/node-token
要在工作节点上安装并将它们添加到集群,请使用K3S_URL和K3S_TOKEN环境变量运行安装脚本。以下示例演示了如何加入 worker 节点:
curl -sfL rancher-mirror.rancher.cn/k3s/k3s-ins… |INSTALL_K3S_MIRROR=cn K3S_URL=https://192.168.33.10:6443 K3S_TOKEN=639e72fd883c57a906987956ef0755e6 sh -s - --flannel-iface enp0s8
设置K3S_URL参数会使 K3s 以 worker 模式运行。K3s agent 将在所提供的 URL 上向监听的 K3s 服务器注册
路由转发,连接办公室网络和 k8s 集群 pod、svc
以192.168.33.10为中转机,允许固定网段的请求进来
pod id段
iptables -t nat -A POSTROUTING -s 192.168.33.0/24 -d 10.42.0.0/16 -j MASQUERADE
svc ip段
iptables -t nat -A POSTROUTING -s 192.168.33.0/24 -d 10.43.0.0/16 -j MASQUERADE
linux静态路由
pod和svc网段的请求都路由至中转机
sudo route add -net 10.42.0.0/16 gw 192.168.33.10
sudo route add -net 10.43.0.0/16 gw 192.168.33.10
windows 静态路由:虚拟机外访问pod ip
输出当前的路由表: route print -4
在为pod的ip地址以10.42开头,所以执行命令
route add 10.42.0.0 mask 255.255.0.0 192.168.33.10
删除路由:route delete 10.42.0.0
卸载 K3s
如果你使用install.sh脚本安装了 K3s,那么在安装过程中会生成一个卸载脚本。该脚本在您的节点上的/usr/local/bin/k3s-uninstall.sh上创建(或者是k3s-agent-uninstall.sh)。
打开命令行工具,运行该脚本即可卸载 K3s:
./k3s-uninstall.sh #或是以下命令
./k3s-agent-uninstall.sh