當您需要對外提供加密的HTTPS訪問時,可以通過ASM網關啟用HTTPS安全服務。ASM網關支援認證的動態載入,您可以即時動態地配置私密金鑰、伺服器憑證和根憑證,無需重啟網關或依賴Secret卷掛載,降低操作複雜性並消除因重啟造成的服務中斷風險。ASM網關能夠監控並管理多個認證和私密金鑰對,為不同的主機提供靈活且安全的通訊能力,加強資料轉送的安全性。
前提條件
背景資訊
Istio支援動態載入認證。TLS(Transport Layer Security)所需的私密金鑰、伺服器憑證以及根憑證,都可以在網關不重啟的條件下動態配置。ASM網關會監視所在命名空間中的Secret,通過Gateway CR實現動態載入。ASM網關的HTTPS協議支援有以下優勢:
入口網關可以在不需要重啟的情況下,動態添加、刪除或更新所需要的認證、私密金鑰或者對應的根憑證。
不需要Secret卷掛載。建立Kubernetes Secret後,ASM網關會捕獲該Secret,並將其包含的認證、私密金鑰或根憑證發送到入口網關。
ASM網關可以監視多個認證、私密金鑰對,只需要為多個主機建立Secret並更新網關規則。
步驟一:為多個主機準備伺服器憑證和私密金鑰
使用網域名稱時需要備案才能正常訪問。本樣本中使用aliyun.com產生認證和私密金鑰,並儲存為Secret。
如果您已經擁有針對aliyun.com可用的認證和私密金鑰,需要將密鑰命名為aliyun.com.key,認證命名為aliyun.com.crt。如果沒有,可以通過openssl,執行以下步驟來產生認證和密鑰。
執行以下命令,建立根憑證和私密金鑰。
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=myexample Inc./CN=aliyun.com' -keyout aliyun.root.key -out aliyun.root.crt執行以下命令,為aliyun.com伺服器產生認證和私密金鑰。
openssl req -out aliyun.com.csr -newkey rsa:2048 -nodes -keyout aliyun.com.key -subj "/CN=aliyun.com/O=myexample organization" openssl x509 -req -days 365 -CA aliyun.root.crt -CAkey aliyun.root.key -set_serial 0 -in aliyun.com.csr -out aliyun.com.crt按照ASM執行個體版本,建立Secret或認證。
ASM執行個體為1.17以下
在入口網關Pod所在的叢集對應的KubeConfig環境下,執行以下命令,在istio-system命名空間中建立包含認證和私密金鑰的Secret。
kubectl create -n istio-system secret tls myexample-credential --key=aliyun.com.key --cert=aliyun.com.crt重要Secret名稱不能以istio或prometheus開頭,且不能包含token欄位。
ASM執行個體為1.17及以上
步驟二:為a.aliyun.com定義內部服務
樣本中的內部服務是基於Nginx實現的,首先為Nginx伺服器建立設定檔。以網域名稱a.aliyun.com的內部服務為例,定義請求根路徑直接返回字樣Welcome to a.aliyun.com!及狀態代碼200。myexample-nginx.conf的具體內容如下。
events {
}
http {
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
server {
listen 80;
location /hello {
return 200 'Welcome to a.aliyun.com!';
add_header Content-Type text/plain;
}
}
}在入口網關Pod所在的叢集對應的KubeConfig環境下,執行以下命令,建立Kubernetes ConfigMap(儲存Nginx伺服器的配置)。
kubectl create configmap myexample-nginx-configmap --from-file=nginx.conf=./myexample-nginx.conf為default命名空間啟用Sidecar網格代理自動注入。具體操作,請參見啟用自動注入。
建立並拷貝以下內容到myexampleapp.yaml檔案,執行
kubectl apply -f myexampleapp.yaml命令,建立網域名稱a.aliyun.com的內部服務。
步驟三:為b.aliyun.com定義內部服務
本樣本中的內部服務基於httpbin實現。
使用以下內容,建立httpbin.example.yaml檔案。
在入口網關Pod所在的叢集對應的KubeConfig環境下,執行以下命令,建立網域名稱為b.aliyun.com的內部服務。
kubectl apply -f httpbin.example.yaml
步驟四:建立網關規則
登入ASM控制台,在左側導覽列,選擇。
在網格管理頁面,單擊目標執行個體名稱,然後在左側導覽列,選擇,然後單擊使用YAML建立。
在建立頁面,選擇目標命名空間(本文以default為例)和任意情境模版,配置如下YAML,然後單擊建立。
建立完成後,您可以在網關規則頁面,看到建立的網關。
步驟五:建立虛擬服務
登入ASM控制台,在左側導覽列,選擇。
在網格管理頁面,單擊目標執行個體名稱,然後在左側導覽列,選擇,然後單擊使用YAML建立。
在建立頁面,選擇目標命名空間和任意情境模版,配置以下YAML,然後單擊建立。
重複此步驟,為b.aliyun.com定義相應的虛擬服務。
建立完成後,在虛擬服務頁面,可以看到建立的虛擬服務。
執行結果
擷取入口網關服務的地址
方式一:通過ASM控制台查看。登入ASM控制台,選中對應的Service Mesh執行個體,在左側導覽列選擇。在入口網關頁面中查看對應的資訊。
方式二:通過Container Service管理主控台查看。具體操作,請參見查看入口網關的服務資訊。
通過命令列訪問入口網關服務
執行以下命令,通過HTTPS協議訪問a.aliyun.com服務。
curl -k -H Host:a.aliyun.com --resolve a.aliyun.com:443:{替換成真實的入口網關IP地址} https://a.aliyun.com/hello預期輸出:
Welcome to aliyun.com!執行以下命令,通過HTTPS協議訪問b.aliyun.com服務。
curl -k -H Host:b.aliyun.com --resolve b.aliyun.com:443:{替換成真實的入口網關IP地址} https://b.aliyun.com/status/418預期輸出:
-=[ teapot ]=- _...._ .' _ _ `. | ."` ^ `". _, \_;`"---"`|// | ;/ \_ _/ `"""`
相關操作
更新網關認證
如果您要更新網關掛載的認證,需要在資料面建立一個Secret,然後修改Gateway資源的credentialName欄位值為Secret的名稱,網關將自動掛載新的Secret。本文以為example.com伺服器建立名為new-istio-ingressgateway-certs的Secret為例。
建立頒發者為myexample的認證。
在OpenSSL工具中執行以下命令,建立根憑證和私密金鑰。
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=myexample Inc./CN=example.com' -keyout example.root.key -out example.root.crt執行以下命令,為example.com伺服器產生認證和私密金鑰。
openssl req -out example.com.csr -newkey rsa:2048 -nodes -keyout example.com.key -subj "/CN=example.com/O=myexample organization" openssl x509 -req -days 365 -CA example.root.crt -CAkey example.root.key -set_serial 0 -in example.com.csr -out example.com.crt執行以下命令,建立名為new-istio-ingressgateway-certs的Secret。
kubectl create -n istio-system secret tls new-istio-ingressgateway-certs --key example.com.key --cert example.com.crt
在叢集中執行以下命令,刪除舊的認證Secret。
kubectl delete secret istio-ingressgateway-certs -n istio-system更新網關規則。
登入ASM控制台,在左側導覽列,選擇。
在網格管理頁面,單擊目標執行個體名稱,然後在左側導覽列,選擇。
在網關規則頁面,單擊目標規則右側操作列下的查看YAML。
在編輯對話方塊,修改credentialName參數值為新的認證Secret名稱new-istio-ingressgateway-certs,然後單擊確定。
驗證更新網關認證是否成功。
執行以下命令,在叢集中查看當前認證資訊。
kubectl exec istio-ingressgateway-xxxx -n istio-system -- curl localhost:15000/config_dump > ingressgateway_dump.yaml執行以下命令,列印並搜尋new-istio-ingressgateway-certs認證。
cat ingressgateway_dump.yaml | grep new-istio-ingressgateway-certs -A 3預期輸出:

複製以上inline_bytes參數後的內容,擷取Base64編碼後的認證。
在本地命令列工具中執行以下命令,將認證進行Base64解碼。
echo <Base64編碼後的認證內容> | base64 --decode將Base64解碼內容儲存為test.com.crt檔案。
在OpenSSL工具中執行以下命令,查看認證的組織。
openssl x509 -in test.com.crt -text -noout預期輸出:

可以看到組織成功更換為
myexample,說明網關認證更新成功。