Add k3s cluster expansion documentation and scripts

- Complete expansion guide for 2/4/6 node scenarios
- Quick join scripts for worker and master nodes
- Health check and diagnostic scripts
- Quick reference card for common operations
This commit is contained in:
K3s Admin
2026-01-21 09:02:47 +00:00
parent b29c7aa904
commit a725f0f933
20 changed files with 2056 additions and 2 deletions

22
php-test/Database.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
require_once 'config.php';
class Database {
private static ?PDO $instance = null;
public static function getConnection(): PDO {
if (self::$instance === null) {
try {
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET;
self::$instance = new PDO($dsn, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
} catch (PDOException $e) {
die("数据库连接失败: " . $e->getMessage());
}
}
return self::$instance;
}
}

11
php-test/config.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
// 数据库配置
define('DB_HOST', 'localhost');
define('DB_NAME', 'test_db');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_CHARSET', 'utf8mb4');
// 错误报告设置(开发环境)
error_reporting(E_ALL);
ini_set('display_errors', 1);

210
php-test/index.php Normal file
View File

@@ -0,0 +1,210 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP MySQL 测试表单</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
padding: 40px;
border-radius: 10px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
max-width: 500px;
width: 100%;
}
h1 {
color: #333;
margin-bottom: 30px;
text-align: center;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 500;
}
input, textarea {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 5px;
font-size: 14px;
transition: border-color 0.3s;
}
input:focus, textarea:focus {
outline: none;
border-color: #667eea;
}
textarea {
resize: vertical;
min-height: 100px;
}
button {
width: 100%;
padding: 14px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s;
}
button:hover {
transform: translateY(-2px);
}
.message {
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
display: none;
}
.message.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
display: block;
}
.message.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
display: block;
}
.records {
margin-top: 30px;
padding-top: 30px;
border-top: 2px solid #e0e0e0;
}
.records h2 {
color: #333;
margin-bottom: 15px;
font-size: 20px;
}
.record-item {
background: #f8f9fa;
padding: 15px;
border-radius: 5px;
margin-bottom: 10px;
border-left: 4px solid #667eea;
}
.record-item strong {
color: #667eea;
}
.record-item p {
margin: 5px 0;
color: #555;
}
</style>
</head>
<body>
<div class="container">
<h1>📝 数据库测试表单</h1>
<?php
require_once 'Database.php';
// 显示提交结果消息
if (isset($_GET['status'])) {
if ($_GET['status'] === 'success') {
echo '<div class="message success">✅ 数据提交成功!</div>';
} elseif ($_GET['status'] === 'error') {
$error = htmlspecialchars($_GET['msg'] ?? '未知错误');
echo '<div class="message error">❌ 提交失败: ' . $error . '</div>';
}
}
?>
<form action="submit.php" method="POST">
<div class="form-group">
<label for="name">姓名 *</label>
<input type="text" id="name" name="name" required placeholder="请输入您的姓名">
</div>
<div class="form-group">
<label for="email">邮箱 *</label>
<input type="email" id="email" name="email" required placeholder="example@email.com">
</div>
<div class="form-group">
<label for="message">留言</label>
<textarea id="message" name="message" placeholder="请输入您的留言..."></textarea>
</div>
<button type="submit">提交数据</button>
</form>
<div class="records">
<h2>📊 最近提交的数据</h2>
<?php
try {
$db = Database::getConnection();
$stmt = $db->query("SELECT * FROM test_data ORDER BY created_at DESC LIMIT 5");
$records = $stmt->fetchAll();
if (count($records) > 0) {
foreach ($records as $record) {
echo '<div class="record-item">';
echo '<p><strong>姓名:</strong> ' . htmlspecialchars($record['name']) . '</p>';
echo '<p><strong>邮箱:</strong> ' . htmlspecialchars($record['email']) . '</p>';
if (!empty($record['message'])) {
echo '<p><strong>留言:</strong> ' . htmlspecialchars($record['message']) . '</p>';
}
echo '<p><strong>提交时间:</strong> ' . $record['created_at'] . '</p>';
echo '</div>';
}
} else {
echo '<p style="color: #999; text-align: center;">暂无数据</p>';
}
} catch (Exception $e) {
echo '<p style="color: #dc3545;">数据库查询失败: ' . htmlspecialchars($e->getMessage()) . '</p>';
}
?>
</div>
</div>
<!-- 热更新脚本 -->
<script>
let lastModified = null;
function checkForUpdates() {
fetch(window.location.href, {
method: 'HEAD',
cache: 'no-cache'
})
.then(response => {
const modified = response.headers.get('Last-Modified');
if (lastModified && modified && lastModified !== modified) {
console.log('检测到文件更新,正在刷新页面...');
window.location.reload();
}
lastModified = modified;
})
.catch(err => console.error('检查更新失败:', err));
}
// 每2秒检查一次更新
setInterval(checkForUpdates, 2000);
checkForUpdates();
</script>
</body>
</html>

13
php-test/schema.sql Normal file
View File

@@ -0,0 +1,13 @@
-- 创建数据库
CREATE DATABASE IF NOT EXISTS test_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE test_db;
-- 创建测试表
CREATE TABLE IF NOT EXISTS test_data (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
message TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

50
php-test/submit.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
require_once 'Database.php';
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: index.php');
exit;
}
try {
// 获取表单数据
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$message = trim($_POST['message'] ?? '');
// 验证必填字段
if (empty($name) || empty($email)) {
throw new Exception('姓名和邮箱为必填项');
}
// 验证邮箱格式
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('邮箱格式不正确');
}
// 获取数据库连接
$db = Database::getConnection();
// 准备SQL语句
$sql = "INSERT INTO test_data (name, email, message) VALUES (:name, :email, :message)";
$stmt = $db->prepare($sql);
// 绑定参数并执行
$stmt->execute([
':name' => $name,
':email' => $email,
':message' => $message
]);
// 重定向到首页并显示成功消息
header('Location: index.php?status=success');
exit;
} catch (Exception $e) {
// 重定向到首页并显示错误消息
$errorMsg = urlencode($e->getMessage());
header('Location: index.php?status=error&msg=' . $errorMsg);
exit;
}