簡體   English   中英

部署 Pod 無法連接 ClusterIP 服務

[英]Deployment pod cannot connect ClusterIP service

我嘗試使用 Ingress 公開我的服務器 IP。

服務器是一個 Express.js 應用程序。 當沒有 docker 時,它在本地偵聽 http://localhost:5000。

這是我的 Kubernetes 配置文件:

服務器部署.yaml

apiVersion: v1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: server
  template:
    metadata:
      labels:
        component: server
    spec:
      containers:
        - name: server
          image: hongbomiao/hongbomiao-server:latest
          ports:
            - containerPort: 5000
          env:
            - name: NODE_ENV
              value: development

服務器-集群-ip-service.yaml

kind: Service
metadata:
  name: server-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: server
  ports:
    - port: 5000
      targetPort: 5000

入口服務.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: server-cluster-ip-service
                port:
                  number: 5000

我得到了我的 minikube IP

➜ minikube ip
192.168.64.12

當我在瀏覽器中打開 192.168.64.12 時,我得到502 Bad Gateway

在閱讀https://cloud.google.com/kubernetes-engine/docs/how-to/exposing-apps#kubectl-apply后,我有了一些調試想法。 這是我嘗試過的:

➜ kubectl get service
NAME                        TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes                  ClusterIP   10.96.0.1      <none>        443/TCP    5h34m
server-cluster-ip-service   ClusterIP   10.102.5.161   <none>        5000/TCP   4h39m

➜ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
server-deployment-bc6777445-pj59f   1/1     Running   0          4h39m

➜ kubectl exec -it server-deployment-bc6777445-pj59f -- sh
/app # apk add --no-cache curl
...
/app # curl 10.102.5.161:5000
curl: (28) Failed to connect to 10.102.5.161 port 5000: Operation timed out

看來我的部署 pod 現在連接 ClusterIP 服務有問題。 任何幫助都會很好!

原來問題是由我的VPN引起的。

在我的問題中,我沒有為 Kubernetes 配置更改任何內容。

此外,也不需要讓 Express.js 服務器顯式偵聽0.0.0.0

(注意@David Maze 在關於0.0.0.0的問題下的評論仍然很有價值)

const app = express()
  .use(bodyParser.json())
  .use(express.static(path.join(__dirname, '../dist')))

app.listen(5000); // This just works. No need explicitly change to app.listen(5000, '0.0.0.0');

在撰寫本文時,我在中國。 為了在仍然使用 Kubenetes / minikube 的同時擺脫 VPN,我找到了一種方法並將其發布在 GitHub 此處

使用此解決方法關閉 VPN 后,一切正常。

在中國使用 minikube 復制我的解決方案:


Step 1 - 下載阿里雲版minikube

curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.14.2/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

注意:上面的命令可以在https://github.com/AliyunContainerService/minikube/wiki#%E5%AE%89%E8%A3%85minikube查看是否有新版本替換v1.14.2

第 2 步 - 啟動 minikube

minikube start --image-mirror-country cn \
 --driver=hyperkit \
 --iso-url=https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.15.0.iso \
 --registry-mirror=https://xxxxxxxx.mirror.aliyuncs.com

注意 1 :您可以在https://github.com/kubernetes/minikube/blob/master/CHANGELOG.md找到最新的 minikube 版本,然后將上面命令中的v1.15.0替換為更新的版本。

不過,阿里雲的 minikube 版本有點落后。 驗證是否存在新版本,可以將https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.15.0.iso的URL中的版本替換為不同的新版本,如作為v1.15.1 ,然后在瀏覽器中打開它。

注2 :上面命令中的xxxxxxxx ,可以在https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors找到你的(需要先注冊阿里雲賬號)

注3 :可以給這個阿里雲版本minikube start傳遞更多參數,查看https://github.com/AliyunContainerService/minikube/wiki#%E5%90%AF%E5%8A%A8

就我而言,我使用的是 macOS 上的driver hyperkit,以及阿里雲的iso-urlregistry-mirror來加速。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM