Kubernetes (k8s)
以下是一份 ?Kubernetes (k8s) 基礎使用教程,涵蓋從環境搭建到核心操作的完整流程,附詳細命令和示例:
🚀 ?一、環境準備(3種方式)??
?1. 本地開發環境(推薦)??
安裝 Minikube(單節點集群)
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
啟動集群(需提前安裝 Docker)
minikube start --driver=docker
驗證
kubectl get nodes # 應顯示一個節點狀態 Ready
?2. 生產環境?
- ?云服務?:使用托管K8s服務# AWS EKS
eksctl create cluster --name my-cluster --region us-east-1
Google GKE
gcloud container clusters create my-cluster
?3. 學習環境?
- Play with Kubernetes(免費在線沙盒)
?? ?二、核心概念與操作?
?1. 部署第一個應用?
運行Nginx容器
kubectl create deployment nginx-app --image=nginx:1.25
查看Pod(容器組)
kubectl get pods -w # -w 實時監控狀態變化
?輸出示例?:
NAME READY STATUS RESTARTS AGE
nginx-app-5d96f8d7b6-2k9jf 1/1 Running 0 25s
?2. 暴露服務?
創建Service(負載均衡)
kubectl expose deployment nginx-app --port=80 --type=NodePort
獲取訪問地址
minikube service nginx-app --url # 本地環境
輸出:http://192.168.49.2:32456
瀏覽器打開URL應看到Nginx歡迎頁
?3. 擴縮容?
擴容到3個副本
kubectl scale deployment nginx-app --replicas=3
查看Pod分布
kubectl get pods -o wide
?輸出?:
nginx-app-5d96f8b7b6-2k9jf 1/1 Running 0 2m node1
nginx-app-5d96f8b7b6-8x7pd 1/1 Running 0 10s node2
nginx-app-5d96f8b7b6-qw4k9 1/1 Running 0 10s node1
📁 ?三、YAML聲明式管理?
?1. 部署文件示例?
deploy.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
resources:
limits:
memory: “128Mi”
cpu: “500m”
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
?2. 應用配置?
創建資源
kubectl apply -f deploy.yaml
更新配置(修改文件后重新運行)
kubectl apply -f deploy.yaml
刪除資源
kubectl delete -f deploy.yaml
🔍 ?四、日常運維命令?
?1. 查看資源?
kubectl get pods # 查看Pod
kubectl get svc # 查看Service
kubectl get deployments # 查看部署
kubectl describe pod <pod名> # 查看Pod詳情
?2. 調試與日志?
進入Pod的Shell
kubectl exec -it <pod名> – /bin/bash
查看日志
kubectl logs <pod名> -f # -f 實時跟蹤日志
端口轉發(訪問內部服務)
kubectl port-forward svc/web-service 8080:80
瀏覽器訪問 http://localhost:8080
?3. 故障排查?
查看事件(定位啟動失敗原因)
kubectl get events --sort-by=.metadata.creationTimestamp
查看節點狀態
kubectl top nodes # 需安裝Metrics Server
🛠? ?五、進階操作?
?1. 配置管理?
創建ConfigMap
kubectl create configmap my-config
–from-literal=LOG_LEVEL=debug
–from-file=config.properties
Pod中掛載
spec:
containers:
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: my-config
key: LOG_LEVEL
volumeMounts:
- name: config-vol
mountPath: /etc/config
volumes:
- name: config-vol
configMap:
name: my-config
?2. 存儲卷?
持久卷聲明 (PVC)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
Pod掛載
spec:
containers:
volumeMounts:
- name: data
mountPath: /var/lib/mysql
volumes:
- name: data
persistentVolumeClaim:
claimName: mysql-pvc
🚨 ?六、常見問題解決?
?1. Pod卡在Pending狀態?
查看原因
kubectl describe pod <pod名> | grep Events -A20
常見原因:
- 資源不足(CPU/內存)
- 沒有可用節點(節點NotReady)
?2. 服務無法訪問?
檢查Service選擇器是否匹配Pod標簽
kubectl describe svc <service名>
驗證網絡策略
kubectl get networkpolicy
📚 ?七、學習資源?
1.?官方文檔?:kubernetes.io/docs
2.?交互式教程?:Katacoda Kubernetes
3.?命令備忘單?:kubectl Cheat Sheet
? ?快速總結表?
?提示?:生產環境務必配置:
- 資源限制(CPU/內存)
- 就緒探針(Readiness Probe)
- 存活探針(Liveness Probe)
- HPA(自動擴縮容)
掌握以上基礎操作后,可進一步學習Ingress、Helm、Operator等高級功能!