🚀 免費嘗試 Zilliz Cloud,完全托管的 Milvus,體驗速度提升 10 倍!立即嘗試

milvus-logo
LFAI
主頁
  • 管理指南
  • Home
  • Docs
  • 管理指南

  • 安全性

  • 傳輸中加密

傳輸中的加密

TLS(傳輸層安全)是一種加密協議,以確保通訊安全。Milvus 代理使用 TLS 單向和雙向認證。

本主題描述如何在 Milvus 代理中為 gRPC 和 RESTful 流量啟用 TLS。

TLS 和用戶認證是兩種不同的安全方法。如果你在 Milvus 系統中同時啟用了用戶認證和 TLS,你將需要提供用戶名、密碼和證書檔路徑。有關如何啟用使用者驗證的資訊,請參考驗證使用者存取

建立你自己的證書

先決條件

確定已安裝 OpenSSL。如果尚未安裝,請先建立並安裝OpenSSL。

openssl version

如果未安裝 OpenSSL。可在 Ubuntu 中使用下列指令安裝。

sudo apt install openssl

建立檔案

  1. 建立gen.sh 檔案。
mkdir cert && cd cert
touch gen.sh
  1. 將下列腳本複製到gen.sh

有必要在gen.sh 檔案中設定CommonNameCommonName 指的是客戶端在連線時應指定的伺服器名稱。

gen.sh

#!/usr/bin/env sh
# your variables
Country="US"
State="CA"
Location="Redwood City"
Organization="zilliz"
OrganizationUnit="devops"
CommonName="localhost"
ExpireDays=3650 # 10 years

# generate private key for ca, server and client
openssl genpkey -quiet -algorithm rsa:2048 -out ca.key
openssl genpkey -quiet -algorithm rsa:2048 -out server.key
openssl genpkey -quiet -algorithm rsa:2048 -out client.key

# create a new ca certificate
openssl req -x509 -new -nodes -key ca.key -sha256 -days 36500 -out ca.pem \
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"

# prepare extension config for signing certificates
echo '[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS = '$CommonName > openssl.cnf

# sign server certificate with ca
openssl req -new -key server.key\
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"\
  | openssl x509 -req -days $ExpireDays -out server.pem -CA ca.pem -CAkey ca.key -CAcreateserial \
    -extfile ./openssl.cnf -extensions v3_req

# sign client certificate with ca
openssl req -new -key client.key\
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"\
  | openssl x509 -req -days $ExpireDays -out client.pem -CA ca.pem -CAkey ca.key -CAcreateserial \
    -extfile ./openssl.cnf -extensions v3_req

gen.sh 檔案中的變數對建立證書簽章請求檔案的過程至關重要。前五個變數是基本的簽署資訊,包括國家、州、地點、組織、組織單位。配置CommonName 時必須謹慎,因為它會在客戶端與伺服器通訊期間進行驗證。

執行gen.sh 以產生憑證

執行gen.sh 檔案以建立憑證。

chmod +x gen.sh
./gen.sh

將會建立下列七個檔案:ca.key,ca.pem,ca.srl,server.key,server.pem,client.key,client.pem

請務必確保ca.key,ca.pem,ca.srl 的安全,以便稍後更新您的憑證。server.keyserver.pem 檔案供伺服器使用,而client.keyclient.pem 檔案供用戶端使用。

更新憑證 (選用)

如果您想在某些情況下更新證書,例如證書即將過期,您可以使用下列腳本。

您的工作目錄中需要ca.key,ca.pem,ca.srl

renew.sh

#!/usr/bin/env sh
# your variables
Country="US"
State="CA"
Location="Redwood City"
Organization="zilliz"
OrganizationUnit="devops"
CommonName="localhost"
ExpireDays=3650 # 10 years

# generate private key for ca, server and client
openssl genpkey -quiet -algorithm rsa:2048 -out server.key
openssl genpkey -quiet -algorithm rsa:2048 -out client.key

# prepare extension config for signing certificates
echo '[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS = '$CommonName > openssl.cnf

# sign server certificate with ca
openssl req -new -key server.key\
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"\
  | openssl x509 -req -days $ExpireDays -out server.pem -CA ca.pem -CAkey ca.key -CAcreateserial \
    -extfile ./openssl.cnf -extensions v3_req

# sign client certificate with ca
openssl req -new -key client.key\
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"\
  | openssl x509 -req -days $ExpireDays -out client.pem -CA ca.pem -CAkey ca.key -CAcreateserial \
    -extfile ./openssl.cnf -extensions v3_req

執行renew.sh 檔案來建立證書。

chmod +x renew.sh
./renew.sh

使用 TLS 設定 Milvus 伺服器

本節概述使用 TLS 加密設定 Milvus 伺服器的步驟。

為 Docker Compose 設定

1.修改 Milvus 伺服器設定

要啟用外部 TLS,請在milvus.yaml 檔案中加入下列配置:

proxy:
  http:
    # for now milvus do not support config restful on same port with grpc
    # so we set to 8080, grpc will still use 19530
    port: 8080 
tls:
  serverPemPath: /milvus/tls/server.pem
  serverKeyPath: /milvus/tls/server.key
  caPemPath: /milvus/tls/ca.pem

common:
  security:
    tlsMode: 1

參數:

  • serverPemPath:伺服器證書檔案的路徑。
  • serverKeyPath:伺服器金鑰檔案的路徑。
  • caPemPath:CA 憑證檔案的路徑。
  • tlsMode:外部服務的 TLS 模式。有效值:
    • 1:單向驗證,只有伺服器需要憑證,用戶端驗證憑證。此模式需要伺服器端提供server.pemserver.key ,用戶端提供server.pem
    • 2:雙向認證:伺服器和用戶端都需要憑證才能建立安全連線。此模式需要伺服器端的server.pem,server.key, 和ca.pem ,以及用戶端的client.pem,client.key, 和ca.pem

若要啟用內部 TLS,請在milvus.yaml 檔案中加入下列設定:

internaltls:
  serverPemPath: /milvus/tls/server.pem
  serverKeyPath: /milvus/tls/server.key
  caPemPath: /milvus/tls/ca.pem

common:
  security:
    internaltlsEnabled: true 

參數:

  • serverPemPath:伺服器證書檔案的路徑。
  • serverKeyPath:伺服器金鑰檔案的路徑。
  • caPemPath:CA 憑證檔案的路徑。
  • internaltlsEnabled:是否啟用內部 TLS。目前只支援單向 TLS。

2.將憑證檔案映射到容器

準備證書檔案

在與您的docker-compose.yaml 相同的目錄下建立一個名為tls 的新資料夾。將server.pemserver.keyca.pem 複製到tls 資料夾。將它們放置在如下的目錄結構中:

├── docker-compose.yml
├── milvus.yaml
└── tls
     ├── server.pem
     ├── server.key
     └── ca.pem

更新 Docker Compose 配置

編輯docker-compose.yaml 檔案以對應容器內的憑證檔案路徑,如下所示:

  standalone:
    container_name: milvus-standalone
    image: milvusdb/milvus:latest
    command: ["milvus", "run", "standalone"]
    security_opt:
    - seccomp:unconfined
    environment:
      ETCD_ENDPOINTS: etcd:2379
      MINIO_ADDRESS: minio:9000
    volumes:
      - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/milvus:/var/lib/milvus
      - ${DOCKER_VOLUME_DIRECTORY:-.}/tls:/milvus/tls
      - ${DOCKER_VOLUME_DIRECTORY:-.}/milvus.yaml:/milvus/configs/milvus.yaml
使用 Docker Compose 部署 Milvus

執行下列指令部署 Milvus:

sudo docker compose up -d

為 Milvus 操作員設定

將憑證檔案放到您的工作目錄中。目錄結構應如下所示:

├── milvus.yaml (to be created later)
├── server.pem
├── server.key
└── ca.pem

使用證書檔案建立一個秘密:

kubectl create secret generic certs --from-file=server.pem --from-file=server.key --from-file=ca.pem

若要啟用外部 TLS,請在milvus.yaml 檔案中加入下列設定:

apiVersion: milvus.io/v1beta1
kind: Milvus
metadata:
  name: my-release
spec:
  config:
    proxy:
      http:
        # for now not support config restful on same port with grpc
        # so we set to 8080, grpc will still use 19530
        port: 8080 
    common:
      security:
        tlsMode: 1 # tlsMode for external service 1 for one-way TLS, 2 for Mutual TLS, 0 for disable
    tls:
      serverPemPath: /certs/server.pem
      serverKeyPath: /certs/server.key
      caPemPath: /certs/ca.pem
  components:
    # mount the certs secret to the milvus container
    volumes:
      - name: certs
        secret:
          secretName: certs
    volumeMounts:
      - name: certs
        mountPath: /certs
        readOnly: true

要啟用內部 TLS,請在milvus.yaml 檔案中加入下列設定:

記住在證書中以 CommonName 取代internaltls.sni 欄位。

apiVersion: milvus.io/v1beta1
kind: Milvus
metadata:
  name: my-release
spec:
  config:
    proxy:
      http:
        # for now not support config restful on same port with grpc
        # so we set to 8080, grpc will still use 19530
        port: 8080 
    common:
      security:
        internaltlsEnabled: true # whether to enable internal tls
    # Configure tls certificates path for internal service
    internaltls:
      serverPemPath: /certs/server.pem
      serverKeyPath: /certs/server.key
      caPemPath: /certs/ca.pem
      sni: localhost # the CommonName in your certificates
  components:
    # mount the certs secret to the milvus container
    volumes:
      - name: certs
        secret:
          secretName: certs
    volumeMounts:
      - name: certs
        mountPath: /certs
        readOnly: true

建立 Milvus CR:

kubectl create -f milvus.yaml

設定為 Milvus Helm

把證書檔案放到你的工作目錄。目錄結構應該是這樣的

├── values.yaml (to be created later)
├── server.pem
├── server.key
└── ca.pem

使用證書檔案建立一個秘密:

kubectl create secret generic certs --from-file=server.pem --from-file=server.key --from-file=ca.pem

若要啟用外部 TLS,請在values.yaml 檔案中加入下列設定:

extraConfigFiles:
  user.yaml: |+
    proxy:
      http:
        # for now not support config restful on same port with grpc
        # so we set to 8080, grpc will still use 19530
        port: 8080 
    common:
      security:
        tlsMode: 1 # tlsMode for external service 1 means set to 2 to enable Mutual TLS
    # Configure tls certificates path for external service
    tls:
      serverPemPath: /certs/server.pem
      serverKeyPath: /certs/server.key
      caPemPath: /certs/ca.pem
# mount the certs secret to the milvus container
volumes:
  - name: certs
    secret:
      secretName: certs
volumeMounts:
  - name: certs
    mountPath: /certs
    readOnly: true

要啟用內部 TLS,請在values.yaml 檔案中加入下列設定:

切記在憑證中以 CommonName 取代internaltls.sni 欄位。

extraConfigFiles:
  user.yaml: |+
    common:
      security:
        internaltlsEnabled: true # whether to enable internal tls
    # Configure tls certificates path for internal service
    internaltls:
      serverPemPath: /certs/server.pem
      serverKeyPath: /certs/server.key
      caPemPath: /certs/ca.pem
      sni: localhost
# mount the certs secret to the milvus container
volumes:
  - name: certs
    secret:
      secretName: certs
volumeMounts:
  - name: certs
    mountPath: /certs
    readOnly: true

建立 milvus 版本:

helm repo add milvus https://zilliztech.github.io/milvus-helm/
helm repo update milvus
helm install my-release milvus/milvus -f values.yaml

驗證內部 TLS 已啟用

很難直接驗證內部 TLS。你可以檢查 Milvus 日誌,看看內部 TLS 是否啟用。

在 Milvus 日誌中,如果內部 TLS 已啟用,你應該會看到以下訊息:

[...date time...] [INFO] [utils/util.go:56] ["Internal TLS Enabled"] [value=true]

使用 TLS 連線到 Milvus 伺服器

對於 SDK 互動,根據 TLS 模式使用下列設定。

單向 TLS 連線

提供server.pem 的路徑,並確保server_name 與證書中設定的CommonName 吻合。

from pymilvus import MilvusClient

client = MilvusClient(
    uri="https://localhost:19530",
    secure=True,
    server_pem_path="path_to/server.pem",
    server_name="localhost"
)

雙向 TLS 連線

提供client.pem,client.key, 和ca.pem 的路徑,並確保server_name 與證書中設定的CommonName 吻合。

from pymilvus import MilvusClient

client = MilvusClient(
    uri="https://localhost:19530",
    secure=True,
    client_pem_path="path_to/client.pem",
    client_key_path="path_to/client.key",
    ca_pem_path="path_to/ca.pem",
    server_name="localhost"
)

更多資訊請參閱example_tls1.pyexample_tls2. py

使用 TLS 連線到 Milvus RESTful 伺服器

對於 RESTful API,您可以使用curl 指令檢查 tls。

單向 TLS 連線

curl --cacert path_to/ca.pem https://localhost:8080/v2/vectordb/collections/list

雙向 TLS 連線

curl --cert path_to/client.pem --key path_to/client.key --cacert path_to/ca.pem https://localhost:8080/v2/vectordb/collections/list

免費嘗試托管的 Milvus

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?