iT邦幫忙

2026 iThome 鐵人賽

DAY 18
0

前兩天的故障都很誠實,Pod 壞給你看。今天這個稍微不一樣,所有東西看起來正常的,但服務就是不通

弄壞的方式是改 service.yaml 的 selector,加一個錯字

spec:
  type: ClusterIP
  selector:
    app: web-typo

就這樣,一個字。套用下去

kubectl apply -f manifests/base/service.yaml
kubectl get pods -n ithome-lab

https://ithelp.ithome.com.tw/upload/images/20261002/201761548EpHetZOvw.png

Pod 正常,沒有異狀,流程的第一步在這裡看不出問題,但服務其實已經斷了

kubectl port-forward -n ithome-lab service/web 8080:80

https://ithelp.ithome.com.tw/upload/images/20261002/20176154CLQToFCwHA.png

https://ithelp.ithome.com.tw/upload/images/20261002/20176154zPfKUhFsHf.png

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

https://ithelp.ithome.com.tw/upload/images/20261002/20176154ftqL5kQjGr.png

https://ithelp.ithome.com.tw/upload/images/20261002/20176154kb4IshjuNC.png

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

kubectl get endpointslices -n ithome-lab

https://ithelp.ithome.com.tw/upload/images/20261002/20176154kgF7OFQAXC.png

前面說過 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

https://ithelp.ithome.com.tw/upload/images/20261002/20176154ASO4SubVUV.png

Label 打錯一個字不會報錯,YAML 語法合法、物件建立成功,只有流量默默地不見

修復就是把字改回來,然後:

kubectl apply -f manifests/base/service.yaml
kubectl get endpointslices -n ithome-lab

https://ithelp.ithome.com.tw/upload/images/20261002/20176154jNPyTVrsoi.png

明天見 :D


上一篇
Day 17 CrashLoopBackOff
下一篇
Day 19 Readiness probe
系列文
讓Claude Code當我的 K8s 助教:部署、故障排查與入門可觀測性 共 20 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言