共用方式為


在 Sidecar 容器中啟用 TLS 端點

本文說明如何透過應用程式容器,和執行 TLS/SSL 提供者的 sidecar 容器來建立容器群組。 藉由設定具有個別 TLS 端點的容器群組,您可以為應用程式啟用 TLS 連線,而不需變更應用程式程式碼。

您可設定由兩個容器所組成的範例容器群組:

  • 使用公用 Microsoft aci-helloworld 映像,來執行簡單 Web 應用程式的應用程式容器。
  • 執行公用 Nginx 映像的 sidecar 容器,會設定為使用 TLS。

在此範例中,容器群組只會公開 Nginx 與其公用 IP 位址的連接埠 443。 Nginx 會將 HTTPS 要求路由傳送至隨附 Web 應用程式,以在內部接聽連接埠 80。 您可以調整在其他連接埠上接聽的容器應用程式範例。

如需在容器群組中啟用 TLS 的其他方法,請參閱後續步驟

必要條件

  • 本文需要 2.0.55 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

建立自我簽署憑證

若要將 Nginx 設定為 TLS 提供者,您需要 TLS/SSL 憑證。 本文說明如何建立和設定自我簽署 TLS/SSL 憑證。 針對實際執行環境的案例,您應該從憑證授權單位取得憑證。

若要建立自我簽署 TLS/SSL 憑證,請使用 Azure Cloud Shell 和許多 Linux 發行版本中提供的 OpenSSL 工具,或在作業系統中使用可比較的用戶端工具。

首先,在本機工作目錄中建立憑證要求 (.csr 檔案):

openssl req -new -newkey rsa:2048 -nodes -keyout ssl.key -out ssl.csr

遵循提示來新增識別資訊。 針對一般名稱,輸入與憑證相關聯的主機名稱。 當系統提示您輸入密碼時,請按 Enter 而不要進行輸入,以略過新增密碼的步驟。

執行下列命令,從憑證要求中建立自我簽署憑證 (.crt 檔案)。 例如:

openssl x509 -req -days 365 -in ssl.csr -signkey ssl.key -out ssl.crt

您現在應該會在目錄中看到三個檔案:憑證要求 (ssl.csr)、私密金鑰 (ssl.key),以及自我簽署憑證 (ssl.crt)。 您在稍後的步驟中會使用 ssl.keyssl.crt

將 Nginx 設定為使用 TLS

建立 Nginx 組態檔

在本章節中,您會建立 Nginx 的組態檔以使用 TLS。 請先將下列文字複製到名為 nginx.conf 的新檔案中。 在 Azure Cloud Shell 中,您可以使用 Visual Studio Code,在工作目錄中建立檔案:

code nginx.conf

location 中,請務必為您的應用程式設定 proxy_pass 和正確的連接埠。 在此範例中,我們會為容器設定 aci-helloworld 連接埠 80。

# nginx Configuration File
# https://wiki.nginx.org/Configuration

# Run as a less privileged user for security reasons.
user nginx;

worker_processes auto;

events {
  worker_connections 1024;
}

pid        /var/run/nginx.pid;

http {

    #Redirect to https, using 307 instead of 301 to preserve post data

    server {
        listen [::]:443 ssl;
        listen 443 ssl;

        server_name localhost;

        # Protect against the BEAST attack by not using SSLv3 at all. If you need to support older browsers (IE6) you may need to add
        # SSLv3 to the list of protocols below.
        ssl_protocols              TLSv1.2;

        # Ciphers set to best allow protection from Beast, while providing forwarding secrecy, as defined by Mozilla - https://wiki.mozilla.org/Security/Server_Side_TLS#Nginx
        ssl_ciphers                ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:AES128:AES256:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK;
        ssl_prefer_server_ciphers  on;

        # Optimize TLS/SSL by caching session parameters for 10 minutes. This cuts down on the number of expensive TLS/SSL handshakes.
        # The handshake is the most CPU-intensive operation, and by default it is re-negotiated on every new/parallel connection.
        # By enabling a cache (of type "shared between all Nginx workers"), we tell the client to re-use the already negotiated state.
        # Further optimization can be achieved by raising keepalive_timeout, but that shouldn't be done unless you serve primarily HTTPS.
        ssl_session_cache    shared:SSL:10m; # a 1mb cache can hold about 4000 sessions, so we can hold 40000 sessions
        ssl_session_timeout  24h;


        # Use a higher keepalive timeout to reduce the need for repeated handshakes
        keepalive_timeout 300; # up from 75 secs default

        # remember the certificate for a year and automatically connect to HTTPS
        add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains';

        ssl_certificate      /etc/nginx/ssl.crt;
        ssl_certificate_key  /etc/nginx/ssl.key;

        location / {
            proxy_pass http://localhost:80; # TODO: replace port if app listens on port other than 80

            proxy_set_header Connection "";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}

Base64 編碼秘密和組態檔

Base64 編碼 Nginx 組態檔、TLS/SSL 憑證和 TLS 金鑰。 在下一章節中,您會在用來部署容器群組的 YAML 檔案中輸入編碼的內容。

cat nginx.conf | base64 > base64-nginx.conf
cat ssl.crt | base64 > base64-ssl.crt
cat ssl.key | base64 > base64-ssl.key

部署容器群組

請在 YAML 檔案中指定容器組態,以部署容器群組。

建立 YAML 檔案

將下列 YAML 複製到名為 deploy-aci.yaml 的新檔案中。 在 Azure Cloud Shell 中,您可以使用 Visual Studio Code,在工作目錄中建立檔案:

code deploy-aci.yaml

輸入在 secret 底下表示的 base64 編碼檔案內容。 例如,cat 每個 base64 編碼的檔案以查看其內容。 在部署期間,這些檔案會新增至容器群組中的秘密磁碟區。 在此範例中,秘密磁碟區會掛接至 Nginx 容器。

api-version: 2019-12-01
location: westus
name: app-with-ssl
properties:
  containers:
  - name: nginx-with-ssl
    properties:
      image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
      ports:
      - port: 443
        protocol: TCP
      resources:
        requests:
          cpu: 1.0
          memoryInGB: 1.5
      volumeMounts:
      - name: nginx-config
        mountPath: /etc/nginx
  - name: my-app
    properties:
      image: mcr.microsoft.com/azuredocs/aci-helloworld
      ports:
      - port: 80
        protocol: TCP
      resources:
        requests:
          cpu: 1.0
          memoryInGB: 1.5
  volumes:
  - secret:
      ssl.crt: <Enter contents of base64-ssl.crt here>
      ssl.key: <Enter contents of base64-ssl.key here>
      nginx.conf: <Enter contents of base64-nginx.conf here>
    name: nginx-config
  ipAddress:
    ports:
    - port: 443
      protocol: TCP
    type: Public
  osType: Linux
tags: null
type: Microsoft.ContainerInstance/containerGroups

部署容器群組

使用 az group create 命令,建立資源群組:

az group create --name myResourceGroup --location westus

使用 az container create 命令部署容器群組,並將 YAML 檔案作為引數進行傳遞。

az container create --resource-group <myResourceGroup> --file deploy-aci.yaml

檢視部署狀態

若要檢視部署狀態,請使用下列 az container show 命令:

az container show --resource-group <myResourceGroup> --name app-with-ssl --output table

針對成功的部署,其輸出會類似下列內容:

Name          ResourceGroup    Status    Image                                                    IP:ports             Network    CPU/Memory       OsType    Location
------------  ---------------  --------  -------------------------------------------------------  -------------------  ---------  ---------------  --------  ----------
app-with-ssl  myresourcegroup  Running   nginx, mcr.microsoft.com/azuredocs/aci-helloworld        52.157.22.76:443     Public     1.0 core/1.5 gb  Linux     westus

驗證 TLS 連線

使用瀏覽器瀏覽至容器群組的公用 IP 位址。 此範例中顯示的 IP 位址為 52.157.22.76,因此 URL 為 https://52.157.22.76。 由於使用 Nginx 伺服器組態,因此您必須使用 HTTPS 來查看執行中的應用程式。 嘗試透過 HTTP 連線失敗。

Browser screenshot showing application running in an Azure container instance

注意

由於此範例使用自我簽署憑證,而不是憑證授權單位中的憑證,因此瀏覽器會在透過 HTTPS 連線到網站時,顯示安全性警告。 您可能需要接受警告,或調整瀏覽器或憑證設定,才能繼續瀏覽頁面。 此行為是預期的。

下一步

本文說明設定 Nginx 容器的方式,針對在容器群組中執行的 Web 應用程式啟用 TLS 連線。 您可以調整應用程式的此範例,這些應用程式會接聽連接埠 80 以外的連接埠。 您也可以更新 Nginx 組態檔,以在連接埠 80 (HTTP) 上自動重新導向伺服器連線,讓您可使用 HTTPS。

雖然本文在 sidecar 中使用 Nginx,您也可以使用另一個 TLS 提供者,例如 Caddy

如果您在 Azure 虛擬網路中部署容器群組,則可以考慮其他選項來啟用後端容器執行個體的 TLS 端點,包括: