透過 VPC 對等從 GCP Kubernetes 叢集連接到 MongoDB Atlas 時出錯

透過 VPC 對等從 GCP Kubernetes 叢集連接到 MongoDB Atlas 時出錯

我正在連接在 GCP Kubernetes 叢集上運行的 Nodejs 應用程式。之前,當我啟用 VPC 對等互連並且使用連接字串(私有)時,它運作得很好。 Atlas儀表板上推薦:

mongodb+srv://(使用者名稱):(密碼)@<叢集名稱>-pri.m6tkv.gcp.mongodb.net/<資料庫名稱>?retryWrites=true&w=majority

我按照本教學進行設定:https://docs.atlas.mongodb.com/security-vpc-peering/

我無法理解,因為我的應用程式已經使用此配置運行了很長一段時間。但有一天,在我的 GCP 叢集中的 Pod 上運行的應用程式無法連接。

這就是錯誤訊息:

Connected mongodb failed MongoNetworkError: failed to connect to server [<cluster_name>-00-00-pri.m6tkv.gcp.mongodb.net:27017] on first connect [MongoNetworkError: connection timed out  
at connectionFailureError (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:406:14)                                                                  
at TLSSocket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:294:16)                                                                   
at Object.onceWrapper (events.js:299:28)                                                                                                                                           
at TLSSocket.emit (events.js:210:5)                                                                                                                                                
at TLSSocket.Socket._onTimeout (net.js:469:8)                                                                                                                                      
at listOnTimeout (internal/timers.js:531:17)                                                                                                                                       
at processTimers (internal/timers.js:475:7) {name: 'MongoNetworkError'                                                                                                                                                           }]                                                                                                                                                                                    at Pool.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:438:11)                                                                         
at Pool.emit (events.js:210:5)                                                                                                                                                     
at /app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:562:14                                                                                              
at /app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:1009:9                                                                                              
at /app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:31:7                                                                                             
at callback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:264:5)                                                                                 
at TLSSocket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:294:7)                                                                    
at Object.onceWrapper (events.js:299:28)                                                                                                                                           
at TLSSocket.emit (events.js:210:5)                                                                                                                                                
at TLSSocket.Socket._onTimeout (net.js:469:8)                                                                                                                                      
at listOnTimeout (internal/timers.js:531:17)                                                                                                                                       
at processTimers (internal/timers.js:475:7)    

我切換回舊連接(標準),我的應用程式再次運行。連接字串:

mongodb+srv://(使用者名稱):(密碼)@<叢集名稱>.m6tkv.gcp.mongodb.net/<資料庫名稱>?retryWrites=true&w=majority

因此透過 VPC 對等連接的連接出現了一些問題,我找不到任何線索。

答案1

將此答案發佈為社區維基以獲得更好的可見性。

我透過將叢集中的 pod 位址範圍新增至 Atlas 上的 IP 白名單中解決了這個問題。

解決這個問題的方法是將Pod位址範圍加入Atlas伺服器的IP白名單。

官方文件中也提到了:

您必須將與對等 VPC 關聯的 VPC CIDR 區塊位址(或子集)新增至白名單在您的新 VPC 對等方可以連接到您的 Atlas 叢集之前。若要了解 GCP 使用的自動模式 IP 範圍,請參閱自動模式 IP 範圍

Docs.atlas.mongodb.com:安全 VPC 對等互連

您需要點擊以下鏈接,其中包含指南:

相關內容