# k8s **Repository Path**: randomk/k8s ## Basic Information - **Project Name**: k8s - **Description**: learn k8s. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-08 - **Last Updated**: 2026-07-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # k8s Kubernetes 网络层实践:在自建裸金属集群上,从零搭建完整的流量入口链路。 ``` 外部流量 → MetalLB (LoadBalancer IP) → Traefik (Gateway API) → Service → Pod ``` ## 项目代码 ``` git clone https://gitee.com/randomk/k8s.git ``` ## 前置条件 - Kubernetes 集群(已配置 `kubectl`) - Helm 3 - yq v4 ## 目录结构 ``` k8s/ ├── service/ # Demo 应用 simple-app(whoami 镜像):ClusterIP + LoadBalancer Service ├── gateway/ │ ├── traefik/ # Traefik 安装、升级、卸载及 Helm values │ ├── http/ # HTTP 路由(HTTPRoute) │ └── tcp/ # TCP 路由(TCPRoute + Gateway) └── loadbalancer/ # MetalLB 安装及 IP 池配置 ``` ## 部署步骤 ### 1. 部署 Demo 应用 ```bash ./service/deploy_svc.sh ``` 在 `demo` 命名空间创建 `simple-app` Deployment(2 副本,whoami 镜像)和 ClusterIP Service。 ### 2. 安装 Traefik 网关(NodePort 模式) ```bash ./gateway/traefik/install_traefik.sh ./gateway/tcp/deploy_tcp_gateway.sh ./gateway/http/deploy_http_gateway.sh ``` - 安装 Kubernetes Gateway API CRD 和 Traefik Helm Chart - 创建 TCP Gateway,监听 `node-01:31080` - 创建 HTTP 路由,`demo.test.com` → `simple-app`,`traefik.test.com` → Traefik Dashboard ### 3. 安装 MetalLB ```bash ./loadbalancer/install_metallb.sh ./loadbalancer/deploy_lb.sh ``` 安装 MetalLB 并配置 IP 池 `172.16.0.30–172.16.0.40`(`metallb-config.yaml`)。 ### 4. 为 Demo 应用创建 LoadBalancer Service ```bash ./service/deploy_lb_svc.sh ``` 为 `simple-app` 创建 LoadBalancer 类型 Service,由 MetalLB 分配 IP。 ### 5. 将 Traefik 切换为 LoadBalancer 模式 ```bash ./gateway/traefik/upgrade_traefik.sh ``` 将 Traefik Service 从 NodePort 升级为 LoadBalancer,并指定 IP `172.16.0.35`。 ## 使用 just 项目提供 [just](https://github.com/casey/just) 快捷命令: ```bash just deploy-svc # 步骤 1 just deploy-gateway # 步骤 2 just deploy-metallb # 步骤 3 just deploy-lb-svc # 步骤 4 just modify-gateway # 步骤 5 ``` ## 验证 ```bash # NodePort 模式(步骤 2 完成后) curl -H "Host: demo.test.com" http://node-01:30080 curl -H "Host: traefik.test.com" http://node-01:30080/dashboard/ # LoadBalancer 模式(步骤 4 完成后) curl -H "Host: demo.test.com" http://172.16.0.35 ``` ## 卸载 ```bash ./gateway/traefik/uninstall_traefik.sh ./loadbalancer/uninstall_metallb.sh ./service/uninstall_svc.sh ``` ## License [MIT License](https://opensource.org/licenses/MIT)