Kubernetes 入门教程(基于 OrbStack)
Kubernetes + OrbStack 入门教程
本教程基于 OrbStack 本地虚拟化环境,演示从集群查看、Deployment 创建、Service 暴露到 Ingress 配置和流量访问的完整流程。
1. 查看集群状态
orb versiondocker versionkubectl version --clientkubectl get nodeskubectl get pods -A注意:OrbStack 内部的 Kubernetes 节点是单节点的,所有系统 Pod 初次可能处于
ContainerCreating状态。
2. 创建 Deployment
kubectl create deployment nginx --image=nginxkubectl get deploymentkubectl get pods -w滚动更新
kubectl set image deployment/nginx nginx=nginx:alpinekubectl get pods -w可以看到 Pod 被逐步替换为新镜像,Deployment 保证可用副本数。
3. 暴露 Service
kubectl expose deployment nginx --port=80 --type=NodePortkubectl get svc内部与外部访问示例
kubectl run curlpod --rm -i --tty --image=radial/busyboxplus:curl --restart=Never -- curl http://192.168.194.173curl http://localhost:310214. 流量示意图
flowchart TD
Browser["浏览器 / curl"]
NodePort["NodePort / LoadBalancer"]
Ingress["Ingress Controller"]
Service["Service: nginx"]
subgraph Deployment[Deployment: nginx]
pod1[nginx-8596cb5464-xm4wl]
pod2[nginx-8596cb5464-82k2q]
pod3[nginx-8596cb5464-cjn4d]
end
PodClient["集群内部 Pod 客户端"]
Browser --> NodePort
NodePort --> Ingress
Ingress --> Service
Service --> pod1
Service --> pod2
Service --> pod3
PodClient --> Service
Service --> pod1
Service --> pod2
Service --> pod3
上图展示了外部访问和集群内部访问的流量路径,Service 自动负载均衡请求到 Pod。
5. 配置 Ingress
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.9.1/deploy/static/provider/cloud/deploy.yamlkubectl get pods -n ingress-nginxkubectl apply -f nginx-ingress.yamlkubectl get ingress外部访问示例
curl -H "Host: nginx.local" http://localhost:32683输出:
bashHello from nginx-8596cb5464-82k2q
6. 多标签命令与输出展示
kubectl get pods -Akubectl get svccurl -H "Host: nginx.local" http://localhost:32683nginx-8596cb5464-xm4wlnginx-8596cb5464-82k2qnginx-8596cb5464-cjn4d总结
- 使用 OrbStack 可以快速启动本地 Kubernetes 单节点环境
- Deployment + Service + NodePort + Ingress 可以完整模拟生产环境流量
- Service 自动负载均衡请求到 Pod
- Deployment 支持滚动更新,实现平滑升级