天天看點

【Azure 應用服務】由Web App“無法連接配接資料庫”而逐漸分析到解析内網位址的辦法(SQL和Redis開啟private endpoint

問題描述

在Azure上建立的資料庫,單獨通過SQL的連接配接工具是可以通路,但在Web App卻無法通路,錯誤資訊為: 

{
    "timestamp": "2021-05-20T05:21:04.672+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "nested exception is org.apache.ibatis.exceptions.PersistenceException: \n### Error querying database.  
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; 
nested exception is com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 60000, active 0, maxActive 100, creating 0\n
### The error may exist in com/digital/dao/SumOrderMapper.java (best guess)\n
### The error may involve com.digital.dao.SumOrderMapper.selectOne\n
### The error occurred while executing a query\n
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; 
nested exception is com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 60000, active 0, maxActive 100, creating 0",
    "path": "/ba/getGapAndOffset"
}      

問題分析&解決

一:分析從Web App到資料庫的網絡是否相通:使用 PsPing & PaPing 進行 TCP 端口連通性測試

PsPing 是微軟 PSTools 工具套件中的其中一個指令。除了ICMP ping 測試,它主要用來測試 TCP 端口的連通性,還可以測試 TCP/UDP 網絡時延和帶寬。下載下傳位址為:​​https://technet.microsoft.com/zh-cn/sysinternals/jj729731.aspx​​

PsPing 進行 TCP 連接配接測試時所支援的參數說明:

-t 類似于 ICMP 的長 ping 測試,直到按下 Ctrl+C 停止測試,并顯示統計結果;
-n 指定測試次數。還可以指定測試的時間長度,以秒為機關,使用時在數字後加上 s,例如“10s”;
-i 每次測試的間隔,預設為 1 秒。還可以指定為 0 來進行快速 ping 測試;
-w 熱身次數,預設為 1 次;
-q 測試過程中不輸出結果,結束後顯示統計結果;
-h 将時延結果統計為直方圖列印(預設列印 20行),也可以指定結果行數,比如 -h 10,指定 10 行;另一種使用方法是統計自定義時延,比如 -h "65,70",結果将統計時延分别為 65 和 70 毫秒的次數;
-4 強制使用 IPv4;
-6 強制使用 IPv6;      

如果在 Linux 中發起 TCP 端口連通性和網路時延的測試,可以使用 PaPing 。PaPing 是一個跨平台的開源工具。它的功能相對 PsPing 而言更簡單,隻支援 TCP 端口的相關測試,不支援 UDP 端口的測試。下載下傳位址為:​​https://code.google.com/archive/p/paping/downloads​​

安裝方式(Linux):

#cd ~
#wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/paping/paping_1.5.5_x86-64_linux.tar.gz
#tar zxvf paping_1.5.5_x86-64_linux.tar.gz      

測試結果, 在App Service(Web App)上去paping資料庫可以連通的,由此可見網絡層面應該是可以通的。

【Azure 應用服務】由Web App“無法連接配接資料庫”而逐漸分析到解析内網位址的辦法(SQL和Redis開啟private endpoint

二:在網絡層是連通的情況下,進一步就需要檢視應用的異常資訊

在App Service(Web App)的應用日志中,發現了 “ ssl connection is required ” 的錯誤消息,這是因為資料庫(PostgreSQL) 配置SSL的連接配接要求,而應用程式時運作在Linux環境Docker中,也是需要開啟SSL的。而在Linux中為容器開啟SSL,需要以下幾步:

注:SSH 實作容器和用戶端之間的安全通信。 為了使自定義容器支援 SSH,必須将其添加到 Docker 映像本身。

第一步:将 sshd_config 檔案添加到項目檔案中,示例内容如下

Port            2222
ListenAddress       0.0.0.0
LoginGraceTime      180
X11Forwarding       yes
Ciphers aes128-cbc,3des-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
MACs hmac-sha1,hmac-sha1-96
StrictModes         yes
SyslogFacility      DAEMON
PasswordAuthentication  yes
PermitEmptyPasswords    no
PermitRootLogin     yes
Subsystem sftp internal-sftp      
此檔案配置 OpenSSH 并且必須包括以下項:
  • ​Port​

    ​ 必須設定為 2222。
  • ​Ciphers​

    ​​ 必須至少包含此清單中的一項:​

    ​aes128-cbc,3des-cbc,aes256-cbc​

    ​。
  • ​MACs​

    ​​ 必須至少包含此清單中的一項:​

    ​hmac-sha1,hmac-sha1-96​

    ​。

第二步:在 Dockerfile 中,添加以下指令

# Install OpenSSH and set the password for root to "Docker!". In this example, "apk add" is the install instruction for an Alpine Linux-based image.
RUN apk add openssh \
     && echo "root:Docker!" | chpasswd 

# Copy the sshd_config file to the /etc/ssh/ directory
COPY sshd_config /etc/ssh/

# Open port 2222 for SSH access
EXPOSE      
此配置不允許從外部建立到容器的連接配接。 容器的端口 2222 隻能在專用虛拟網絡的橋網絡中通路,Internet 上的攻擊者無法通路該端口。

第三步:在容器的啟動腳本中啟動 SSH 伺服器

/usr/sbin/sshd      

三:App Service(Web App)如何能夠解析内網中資源的Endpoint呢?

由于在App Service(Web App)中無法解析Redis的Private Endpoint IP,是以無法連接配接Redis,由于Redis在開啟Private Endpoint時也有建立Azure Private DNS Zone,是以需要在App Service配置使用Azure Private DNS Zone用于解析Redis Private E你的point。

在App Service的配置中添加兩項應用程式設定:

WEBSITE_DNS_SERVER= 168.63.129.16 #Azure Private DNS Server IP Address

WEBSITE_VNET_ROUTE_ALL=1 

這些設定會将所有出站調用從應用發送到 VNet,還允許應用通路 Azure DNS 專用區域。配置使用Azure Private DNS Zone的官方文檔:​​https://docs.azure.cn/zh-cn/app-service/web-sites-integrate-with-vnet#azure-dns-private-zones​​

參考資料

配置使用Azure Private DNS Zone: ​​https://docs.azure.cn/zh-cn/app-service/web-sites-integrate-with-vnet#azure-dns-private-zones​​

App service啟用應用程式日志: ​​https://docs.microsoft.com/zh-cn/azure/app-service/troubleshoot-diagnostic-logs#enable-application-logging-linuxcontainer​​

PostgreSQL配置ssl: ​​https://docs.azure.cn/zh-cn/postgresql/concepts-ssl-connection-security​​

Paping測試連通性: ​​https://docs.azure.cn/zh-cn/articles/azure-operations-guide/virtual-network/aog-virtual-network-tcp-psping-paping-connectivity#%E4%B8%8B%E8%BD%BD%E5%92%8C%E5%AE%89%E8%A3%85-1​​

【完】

繼續閱讀