前兩天的故障都很誠實,Pod 壞給你看。今天這個稍微不一樣,所有東西看起來正常的,但服務就是不通
弄壞的方式是改 service.yaml 的 selector,加一個錯字
spec:
type: ClusterIP
selector:
app: web-typo
就這樣,一個字。套用下去
kubectl apply -f manifests/base/service.yaml
kubectl get pods -n ithome-lab

Pod 正常,沒有異狀,流程的第一步在這裡看不出問題,但服務其實已經斷了
kubectl port-forward -n ithome-lab service/web 8080:80


port-forward 直接失敗,等一分鐘後吐 error: timed out waiting for the condition。因為它不是轉發到 ClusterIP,而是要先用 selector 挑一顆 Pod 出來,現在挑不到。錯誤訊息也沒告訴你原因
繼續查
kubectl get service -n ithome-lab
kubectl describe service web -n ithome-lab


get service 看不出問題,CLUSTER-IP 有、埠號對、type 也沒錯,物件本身沒有壞,它只是指向了不存在的東西
kubectl get endpointslices -n ithome-lab

前面說過 Service 不記 Pod 清單,是有個控制器一直拿 selector 去比對 Label,比對到才寫進 EndpointSlice,現在 selector 找的是 app=web-typo,而 Pod 身上掛的是 app=web,兩邊對不上,名單就變空的,describe service 的 Endpoints 那一行也會是空白
確認 Pod 掛什麼標籤
kubectl get pods -n ithome-lab --show-labels
kubectl get pods -n ithome-lab -l app=web-typo

Label 打錯一個字不會報錯,YAML 語法合法、物件建立成功,只有流量默默地不見
修復就是把字改回來,然後:
kubectl apply -f manifests/base/service.yaml
kubectl get endpointslices -n ithome-lab

明天見 :D
