Skip to content

Commit 7106b04

Browse files
committed
Init
1 parent 428dea2 commit 7106b04

File tree

10 files changed

+2790
-1
lines changed

10 files changed

+2790
-1
lines changed

.github/workflows/validate.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Shellcheck & Validate
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
shellcheck:
11+
name: ShellCheck Lint
12+
runs-on: ubuntu-22.04
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Install shellcheck
18+
run: |
19+
sudo apt-get install -y shellcheck
20+
21+
- name: Run ShellCheck on all scripts
22+
run: |
23+
find . -name "*.sh" -not -path "./.git/*" | \
24+
xargs shellcheck \
25+
--severity=warning \
26+
--shell=bash \
27+
--exclude=SC2034,SC1091,SC2155 \
28+
|| true
29+
30+
- name: Validate script syntax
31+
run: |
32+
for script in \
33+
kubevirtinstall.sh \
34+
kubevirtuninstall.sh \
35+
scripts/onevm.sh \
36+
scripts/create_vm.sh \
37+
scripts/deletevm.sh \
38+
scripts/listvms.sh \
39+
scripts/update-port-rules.sh; do
40+
echo "Checking syntax: $script"
41+
bash -n "$script" && echo " OK: $script" || echo " FAIL: $script"
42+
done
43+
44+
readme-check:
45+
name: README Check
46+
runs-on: ubuntu-22.04
47+
steps:
48+
- name: Checkout repository
49+
uses: actions/checkout@v4
50+
51+
- name: Check README exists and has content
52+
run: |
53+
if [ ! -f README.md ] || [ ! -s README.md ]; then
54+
echo "README.md is missing or empty"
55+
exit 1
56+
fi
57+
echo "README.md OK ($(wc -l < README.md) lines)"

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vmlog
2+
*.log
3+
.DS_Store
4+
*.swp
5+
*.swo
6+
*~

README.md

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,163 @@
1-
# kubevirt
1+
# kubevirt
2+
3+
[![Hits](https://hits.spiritlhl.net/kubevirt.svg)](https://hits.spiritlhl.net/kubevirt)
4+
5+
基于 KubeVirt + K3s 的虚拟机环境一键安装与管理脚本
6+
7+
支持一键安装 KubeVirt 运行时,并开设各种 Linux 虚拟机(提供 SSH 访问),支持 CPU、内存、磁盘资源限制,端口映射,cloud-init 初始化等。
8+
9+
## 说明
10+
11+
- 使用 K3s 作为轻量级 Kubernetes,自动安装单节点集群
12+
- 使用 KubeVirt 提供虚拟机能力(基于 KVM/QEMU)
13+
- 使用 CDI(Containerized Data Importer)导入云镜像
14+
- 通过 NodePort Service + iptables DNAT 实现端口映射
15+
- 支持系统:Ubuntu 22.04, Debian 12, AlmaLinux 9, RockyLinux 9, CentOS Stream 9, openSUSE Leap 15.5
16+
- 支持架构:amd64 (x86_64)
17+
- 宿主机系统支持:Ubuntu 20.04/22.04/24.04,Debian 11/12
18+
19+
## 环境要求
20+
21+
- 宿主机需支持 KVM 虚拟化(`/dev/kvm` 存在且可用)
22+
- 最低配置:2 核 CPU,4GB RAM,20GB 可用磁盘
23+
- root 权限运行
24+
- 需要公网访问以下载 K3s、KubeVirt 组件和虚拟机镜像
25+
26+
## 安装 KubeVirt 环境
27+
28+
```bash
29+
bash <(wget -qO- https://raw.githubusercontent.com/oneclickvirt/kubevirt/main/kubevirtinstall.sh)
30+
```
31+
32+
## 开设单个虚拟机
33+
34+
```bash
35+
wget -q https://raw.githubusercontent.com/oneclickvirt/kubevirt/main/scripts/onevm.sh
36+
chmod +x onevm.sh
37+
./onevm.sh <name> <cpu> <memory_gb> <disk_gb> <password> <sshport> <startport> <endport> [system]
38+
```
39+
40+
**参数说明:**
41+
42+
| 参数 | 说明 | 默认值 |
43+
|------|------|--------|
44+
| `name` | 虚拟机名称(小写字母数字和连字符) | `test` |
45+
| `cpu` | CPU 核心数 | `1` |
46+
| `memory_gb` | 内存(GB) | `1` |
47+
| `disk_gb` | 磁盘大小(GB) | `10` |
48+
| `password` | root 密码 | `123456` |
49+
| `sshport` | 宿主机 SSH 映射端口 | `25000` |
50+
| `startport` | 公网端口范围起始 | `34975` |
51+
| `endport` | 公网端口范围结束 | `35000` |
52+
| `system` | 操作系统 | `ubuntu` |
53+
54+
**支持的系统:**
55+
56+
| 系统标识 | 说明 | 镜像来源 |
57+
|----------|------|----------|
58+
| `ubuntu` | Ubuntu 22.04 LTS | pve_kvm_images → kvm_images(ubuntu22) → 官方 |
59+
| `debian` | Debian 12 | pve_kvm_images → kvm_images(debian12) → 官方 |
60+
| `debian11` | Debian 11 | pve_kvm_images → kvm_images(debian11) → 官方 |
61+
| `almalinux` | AlmaLinux 9 | pve_kvm_images → kvm_images(almalinux8) → 官方 |
62+
| `rockylinux` | RockyLinux 9 | pve_kvm_images → kvm_images(rockylinux8) → 官方 |
63+
| `centos` | CentOS 7 | pve_kvm_images → kvm_images(centos7) → 官方 |
64+
| `centos8-stream` | CentOS Stream 8 | pve_kvm_images → kvm_images → 官方 |
65+
| `centos-stream` | CentOS Stream 9 | pve_kvm_images → 官方 |
66+
| `opensuse` | openSUSE Leap 15.5 | pve_kvm_images → kvm_images(opensuse-leap-15) → 官方 |
67+
68+
> 镜像来源优先级:
69+
> 1. `oneclickvirt/pve_kvm_images` releases(最新编译版)
70+
> 2. `idc.wiki` 镜像站
71+
> 3. `oneclickvirt/kvm_images` releases(稳定版)
72+
> 4. 官方上游地址(最终兜底)
73+
>
74+
> 下载均支持 CDN 加速(`cdn0.spiritlhl.top` 等),网络不佳时自动检测并启用。
75+
76+
**示例:**
77+
78+
```bash
79+
./onevm.sh vm1 2 2 20 MyPassword 25000 34975 35000 debian
80+
```
81+
82+
## 批量开设虚拟机
83+
84+
```bash
85+
wget -q https://raw.githubusercontent.com/oneclickvirt/kubevirt/main/scripts/create_vm.sh
86+
chmod +x create_vm.sh
87+
./create_vm.sh
88+
```
89+
90+
## 查看所有虚拟机
91+
92+
```bash
93+
wget -q https://raw.githubusercontent.com/oneclickvirt/kubevirt/main/scripts/listvms.sh
94+
chmod +x listvms.sh
95+
./listvms.sh
96+
```
97+
98+
## 删除单个虚拟机
99+
100+
```bash
101+
wget -q https://raw.githubusercontent.com/oneclickvirt/kubevirt/main/scripts/deletevm.sh
102+
chmod +x deletevm.sh
103+
./deletevm.sh <name>
104+
```
105+
106+
## 卸载(完整清理)
107+
108+
```bash
109+
bash <(wget -qO- https://raw.githubusercontent.com/oneclickvirt/kubevirt/main/kubevirtuninstall.sh)
110+
```
111+
112+
## 日志文件
113+
114+
批量开设时,连接信息会记录在当前目录的 `vmlog` 文件中,格式如下:
115+
116+
```
117+
vm1 root@<宿主机IP>:25000 密码: MyPassword 端口范围: 34975-35000
118+
vm2 root@<宿主机IP>:25001 密码: MyPassword 端口范围: 35001-35026
119+
```
120+
121+
## 工作原理
122+
123+
```
124+
宿主机(K3s + KubeVirt)
125+
├── K3s Kubernetes(单节点)
126+
│ ├── KubeVirt Operator(管理虚拟机生命周期)
127+
│ └── CDI(导入云镜像到 PVC)
128+
├── 虚拟机 Pod(virt-launcher)
129+
│ ├── vm1(QEMU/KVM)
130+
│ │ ├── SSH :22 ←→ iptables DNAT → 宿主机:25000
131+
│ │ └── 额外端口 ←→ iptables DNAT → 宿主机:34975-35000
132+
│ └── vm2(QEMU/KVM)
133+
│ ├── SSH :22 ←→ iptables DNAT → 宿主机:25001
134+
│ └── 额外端口 ←→ iptables DNAT → 宿主机:35001-35026
135+
```
136+
137+
## 常用管理命令
138+
139+
```bash
140+
# 查看所有虚拟机状态
141+
kubectl get vm -n kubevirt-vms
142+
143+
# 查看虚拟机实例(运行中的)
144+
kubectl get vmi -n kubevirt-vms
145+
146+
# 查看数据卷导入进度
147+
kubectl get dv -n kubevirt-vms
148+
149+
# 进入虚拟机串口控制台(Ctrl+] 退出)
150+
virtctl console <vmname> -n kubevirt-vms
151+
152+
# 启动/停止/重启虚拟机
153+
virtctl start <vmname> -n kubevirt-vms
154+
virtctl stop <vmname> -n kubevirt-vms
155+
virtctl restart <vmname> -n kubevirt-vms
156+
```
157+
158+
## 注意事项
159+
160+
1. 虚拟机首次启动需要等待镜像下载导入(根据网速可能需要 5-20 分钟)
161+
2. 宿主机需要开启 KVM 嵌套虚拟化或直接使用裸金属服务器
162+
3. 端口转发通过 iptables 实现,重启后自动通过 systemd 服务恢复
163+
4. 如需重置密码,通过 `virtctl console` 进入控制台手动修改

0 commit comments

Comments
 (0)