Share via


快速入門:完成在 Azure 操作員服務管理員中部署容器化網路函式的必要條件

在本快速入門中,您會在使用 Azure 操作員服務管理員 (AOSM) 之前完成必要的工作。

必要條件

請連絡您的 Microsoft 帳戶小組以註冊您的 Azure 訂用帳戶,以存取 Azure 操作員服務管理員(AOSM),或透過 合作夥伴註冊表單 表達您的興趣。

下載並安裝 Azure CLI

在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱 在 Azure Cloud Shell 中啟動 Cloud Shell 以使用 Bash 環境。

對於偏好在本機執行 CLI 參考命令的使用者,請參閱 如何安裝 Azure CLI

如果您是在 Window 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。

如果您使用本機安裝,請使用 az login 命令登入 Azure CLI,並完成終端機中顯示的提示以完成驗證。 如需更多登入選項,請參閱 使用 Azure CLI 登入。

安裝 Azure 操作員服務管理員 (AOSM) CLI 擴充功能

使用此命令安裝 Azure 操作員服務管理員 (AOSM) CLI 擴充功能:

az extension add --name aosm
  1. 執行 az version 以查看已安裝的版本和相依程式庫。
  2. 執行 az upgrade 以升級至目前版本的 Azure CLI。

註冊並驗證必要的資源提供者

開始使用 Azure 操作員服務管理員之前,請執行下列命令來註冊必要的資源提供者。 此註冊程式最多可能需要 5 分鐘的時間。

# Register Resource Provider
az provider register --namespace Microsoft.HybridNetwork
az provider register --namespace Microsoft.ContainerRegistry

確認資源提供者的註冊狀態。 執行下列命令。

# Query the Resource Provider
az provider show -n Microsoft.HybridNetwork --query "{RegistrationState: registrationState, ProviderName: namespace}"
az provider show -n Microsoft.ContainerRegistry --query "{RegistrationState: registrationState, ProviderName: namespace}"

注意

資源提供者註冊可能需要幾分鐘的時間才能完成。 註冊成功之後,您就可以繼續使用 Azure 操作員服務管理員 (AOSM)。

容器化網路功能的需求 (CNF)

針對使用容器化網路函式的使用者,請務必確定下列套件會安裝在您執行 CLI 的電腦上:

設定容器化網路功能 (CNF) 部署

針對容器化網路函式 (CNFs) 的部署,請務必將下列內容儲存在您要執行 CLI 的電腦上:

  • 具有架構 的 Helm 套件 - 這些套件應該存在於本機儲存體上,並在組態檔內 input.json 參考。 遵循本快速入門時,您會下載必要的 helm 套件。

  • 建立範例組態檔 - 產生定義 CNF 部署的範例組態檔。 發出此命令,以產生 input.json 您需要填入特定組態的檔案。

    az aosm nfd generate-config --definition-type cnf
    
  • CNF 的影像 - 以下是選項:

    • 現有 Azure Container Registry 的參考,其中包含 CNF 的映射。 目前,每個 CNF 只支援一個 ACR 和命名空間。 要從這個 ACR 複製的映射會根據 helm 套件架構自動填入。 您必須擁有此 ACR 的讀者/AcrPull 許可權。 若要使用此選項,請填入 source_registry input.json 檔案,並選擇性地 source_registry_namespace 填入 。
    • 來自本機電腦的來源 Docker 映射名稱。 此映射名稱適用于有限的使用案例,其中 CNF 只需要存在於本機 Docker 存放庫中的單一 Docker 映射。 若要使用此選項,請填入 source_local_docker_image input.json 檔案。 需要安裝 Docker。 本快速入門會引導您下載要用於此選項的 nginx Docker 映射。
  • 選擇性:對應檔案 (path_to_mappings) :您可以選擇性地提供名為 path_to_mappings 的檔案(磁片上)。 此檔案應該會鏡像 values.yaml ,並將您選取的值取代為部署參數。 這樣做會將它們公開為 CNF 的參數。 或者,您可以在 中 input.json 保留此空白,而 CLI 會產生檔案。 在此案例中,中的每個值 values.yaml 預設都會公開為部署參數。 或者,使用 --interactive CLI 引數以互動方式做出選擇。 本快速入門會引導您建立此檔案。

設定 input.json 檔案時,請確定您依應部署的順序列出 Helm 套件。 例如,如果套件 「A」 必須部署在封裝 「B」 之前,您的 input.json 結構應該類似下列結構:

"helm_packages": [
    {
        "name": "A",
        "path_to_chart": "Path to package A",
        "path_to_mappings": "Path to package A mappings",
        "depends_on": [
            "Names of the Helm packages this package depends on"
        ]
    },
    {
        "name": "B",
        "path_to_chart": "Path to package B",
        "path_to_mappings": "Path to package B mappings",
        "depends_on": [
            "Names of the Helm packages this package depends on"
        ]
    }
]

遵循這些指導方針可確保使用 Helm 套件和相關聯的組態來部署容器化網路函式 (CNFs) 的有組織且結構化的方法。

將 nginx 映射下載至本機 Docker 存放庫

在本快速入門中,您會將 nginx docker 映射下載到本機存放庫。 Azure 操作員服務管理員 (AOSM) Azure CLI 擴充功能會將映射從該處推送至 Azure 操作員服務管理員 (AOSM) 成品存放區 ACR。 CLI 延伸模組也支援從現有的 ACR 複製映射。 複製映射是預期的預設使用案例,但快速入門建立要複製的 ACR 的速度較慢,因此此處不會使用此方法。

發出下列命令: docker pull nginx:stable

下載範例 Helm 圖表

從這裡 下載範例 Helm 圖表 ,以搭配本快速入門使用。

深入瞭解 Helm 圖表

本節將介紹基本的 Helm 圖表,該圖表會設定 nginx,並將其設定為接聽指定的埠。 本節中提供 Helm 圖表已納入檔案 values.schema.json

範例 values.schema.json 檔案

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "additionalProperties": true,
    "properties": {
        "affinity": {
            "additionalProperties": false,
            "properties": {},
            "type": "object"
        },
        "fullnameOverride": {
            "type": "string"
        },
        "image": {
            "additionalProperties": false,
            "properties": {
                "pullPolicy": {
                    "type": "string"
                },
                "repository": {
                    "type": "string"
                },
                "tag": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        "imagePullSecrets": {
            "items": {
                "anyOf": []
            },
            "type": "array"
        },
        "ingress": {
            "additionalProperties": false,
            "properties": {
                "annotations": {
                    "additionalProperties": false,
                    "properties": {},
                    "type": "object"
                },
                "enabled": {
                    "type": "boolean"
                },
                "hosts": {
                    "items": {
                        "anyOf": [
                            {
                                "additionalProperties": false,
                                "properties": {
                                    "host": {
                                        "type": "string"
                                    },
                                    "paths": {
                                        "items": {
                                            "anyOf": []
                                        },
                                        "type": "array"
                                    }
                                },
                                "type": "object"
                            }
                        ]
                    },
                    "type": "array"
                },
                "tls": {
                    "items": {
                        "anyOf": []
                    },
                    "type": "array"
                }
            },
            "type": "object"
        },
        "nameOverride": {
            "type": "string"
        },
        "nodeSelector": {
            "additionalProperties": false,
            "properties": {},
            "type": "object"
        },
        "podSecurityContext": {
            "additionalProperties": false,
            "properties": {},
            "type": "object"
        },
        "replicaCount": {
            "type": "integer"
        },
        "resources": {
            "additionalProperties": false,
            "properties": {},
            "type": "object"
        },
        "securityContext": {
            "additionalProperties": false,
            "properties": {},
            "type": "object"
        },
        "service": {
            "additionalProperties": false,
            "properties": {
                "port": {
                    "type": "integer"
                },
                "type": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        "serviceAccount": {
            "additionalProperties": false,
            "properties": {
                "create": {
                    "type": "boolean"
                },
                "name": {
                    "type": "null"
                }
            },
            "type": "object"
        },
        "tolerations": {
            "items": {
                "anyOf": []
            },
            "type": "array"
        }
    },
    "type": "object"
}

雖然本文並未深入探討 Helm 的複雜問題,但值得強調的幾個元素包括:

  • 服務埠組態: values.yaml 具有 80 服務埠的預設。

範例 values.yaml 檔案

# Default values for nginxdemo. 
# This is a YAML-formatted file. 
# Declare variables to be passed into your templates. 

 
replicaCount: 1 
 

image: 

  # Repository gets overwritten by AOSM to the Artifact Store ACR, however we've hard-coded the image name and tag in deployment.yaml 

  repository: overwriteme 
  tag: stable 
  pullPolicy: IfNotPresent 


imagePullSecrets: [] 
nameOverride: "" 
fullnameOverride: "" 

 
serviceAccount: 

  # Specifies whether a service account should be created 

  create: false 

  # The name of the service account to use. 
  # If not set and create is true, a name is generated using the fullname template 

  name: 
 

podSecurityContext: 

  {} 

  # fsGroup: 2000 

 
securityContext: 

  {} 
  # capabilities: 
  #   drop: 
  #   - ALL 
  # readOnlyRootFilesystem: true 
  # runAsNonRoot: true 
  # runAsUser: 1000 

 
service: 

  type: ClusterIP 
  port: 80 

  
ingress: 

  enabled: false 
  annotations: 

    {} 

    # kubernetes.io/ingress.class: nginx 
    # kubernetes.io/tls-acme: "true" 

  hosts: 

    - host: chart-example.local 
      paths: [] 

  
  tls: [] 

  #  - secretName: chart-example-tls 
  #    hosts: 
  #      - chart-example.local 

 
resources: 

  {} 

  # We usually recommend not to specify default resources and to leave this as a conscious 
  # choice for the user. This also increases chances charts run on environments with little 
  # resources, such as Minikube. If you do want to specify resources, uncomment the following 
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'. 
  # limits: 
  #   cpu: 100m 
  #   memory: 128Mi 
  # requests: 
  #   cpu: 100m 
  #   memory: 128Mi 
 

nodeSelector: {} 

 
tolerations: [] 

 
affinity: {}
  • 埠參考: 此埠會在多個位置尋找其用法:

    • 在 內 service.yaml{{ Values.service.port }}

範例 service.yaml 檔案

apiVersion: v1 
kind: Service 

metadata: 
  name: {{ include "nginxdemo.fullname" . }} 
  labels: 

{{ include "nginxdemo.labels" . | indent 4 }} 

spec: 
  type: {{ .Values.service.type }} 
  ports: 
    - port: {{ .Values.service.port }} 
      targetPort: http 
      protocol: TCP 
      name: http 

  selector: 
    app.kubernetes.io/name: {{ include "nginxdemo.name" . }} 
    app.kubernetes.io/instance: {{ .Release.Name }}
  • nginx_config_map.yaml 中,以 {{ Values.service.port }} 表示。 此檔案會對應至 /etc/nginx/conf.d/default.conf ,且對應是使用 中的 deployment.yaml 組態對應所建立的對應。

範例 nginx_config_map.yaml 檔案

apiVersion: v1 
kind: ConfigMap 
metadata: 
  name: nginx-config 
# This writes the nginx config file to the ConfigMap and deployment.yaml mounts it as a volume  
# to the right place. 

data: 
  default.conf: | 
    log_format client '$remote_addr - $remote_user $request_time $upstream_response_time ' 
                    '[$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" ' 
                    '"$http_user_agent" "$http_x_forwarded_for"'; 

    server { 
        listen       80; 
        listen       {{ .Values.service.port }}; 
        listen  [::]:80; 
        server_name  localhost; 

        access_log  /var/log/nginx/host.access.log  client; 

        location / { 
            root   /usr/share/nginx/html; 
            index  index.html index.htm; 
            error_page 405 =200 $uri; 
        } 


        #error_page  404              /404.html; 
        # redirect server error pages to the static page /50x.html 
        # 
        error_page   500 502 503 504  /50x.html; 
        location = /50x.html { 
            root   /usr/share/nginx/html; 
        } 


        location = /cnf/test {   
            error_page 405 =200 $uri; 
        } 
   

        location = /post_thing { 
            # turn off logging here to avoid double logging 
            access_log off; 
            error_page 405 =200 $uri; 
        } 
    }

部署組態: 檔案 deployment.yaml 會展示與 和 image 相關的 imagePullSecrets 特定行。 請務必觀察其結構化格式,因為 Azure 操作員服務管理員 (AOSM) 會在部署期間為這些欄位提供必要的值。 如需詳細資訊,請參閱 Helm 套件需求

範例 deployment.yaml 檔案

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: {{ include "nginxdemo.fullname" . }} 
  labels: 
{{ include "nginxdemo.labels" . | indent 4 }} 

spec: 
  replicas: {{ .Values.replicaCount }} 
  selector: 
    matchLabels: 
      app.kubernetes.io/name: {{ include "nginxdemo.name" . }} 
      app.kubernetes.io/instance: {{ .Release.Name }} 

  template: 
    metadata: 
      labels: 
        app.kubernetes.io/name: {{ include "nginxdemo.name" . }} 
        app.kubernetes.io/instance: {{ .Release.Name }} 

    spec: 
      # Copied from sas 
      imagePullSecrets: {{ mustToPrettyJson (ternary (list ) .Values.imagePullSecrets (kindIs "invalid" .Values.imagePullSecrets)) }} 
      serviceAccountName: {{ template "nginxdemo.serviceAccountName" . }} 
      securityContext: 
        {{- toYaml .Values.podSecurityContext | nindent 8 }} 
      containers: 
        - name: {{ .Chart.Name }} 
          securityContext: 
            {{- toYaml .Values.securityContext | nindent 12 }} 
          # Want this to evaluate to acr-name.azurecr.io/nginx:stable (or specific version) 
          # docker tag nginx:stable acr-name.azurecr.io/nginx:stable 
          # docker push acr-name.azurecr.io/nginx:stable 
          # Image hard coded to that put in the Artifact Store ACR for this CNF POC 
          image: "{{ .Values.image.repository }}/nginx:stable" 
          imagePullPolicy: {{ .Values.image.pullPolicy }} 
          ports: 
            - name: http 
              containerPort: 80 
              protocol: TCP 
          livenessProbe: 
            httpGet: 
              path: / 
              port: http 
          readinessProbe: 
            httpGet: 
              path: / 
              port: http 
          resources: 
            {{- toYaml .Values.resources | nindent 12 }} 
          # Gets the nginx config from the configMap - see nginx_config_map.yaml 
          volumeMounts: 
            - name: nginx-config-volume 
              mountPath: /etc/nginx/conf.d/default.conf 
              subPath: default.conf 
      volumes: 
        - name: nginx-config-volume 
          configMap: 
            name: nginx-config 
      {{- with .Values.nodeSelector }} 
      nodeSelector: 
        {{- toYaml . | nindent 8 }} 
      {{- end }} 
    {{- with .Values.affinity }} 
      affinity: 
        {{- toYaml . | nindent 8 }} 
    {{- end }} 
    {{- with .Values.tolerations }} 
      tolerations: 
        {{- toYaml . | nindent 8 }} 
    {{- end }}

下一步