###### tags: `Workshop` `Azure Kubernetes` `MTC 2021` # AKS 補充資料 [TOC] ## AAD issue ### 建立 ACR secret - 啟用 ACR admin ![](https://i.imgur.com/jVifpew.png) - 在 K8S 中建立 ACR secret ``` kubectl create secret docker-registry acr --docker-server=<ACR_NAME>.azurecr.io --docker-username=<ACR_USERNAME> --docker-password=<ACR_PASSWORD> -n ratingsapp ``` ### API deployment ```yaml=1 apiVersion: apps/v1 kind: Deployment metadata: name: ratings-api spec: selector: matchLabels: app: ratings-api template: metadata: labels: app: ratings-api # the label for the pods and the deployments spec: containers: - name: ratings-api image: <acrname>.azurecr.io/ratings-api:v1 # IMPORTANT: update with your own repository imagePullPolicy: Always ports: - containerPort: 3000 # the application listens to this port env: - name: MONGODB_URI # the application expects to find the MongoDB connection details in this environment variable valueFrom: secretKeyRef: name: mongosecret # the name of the Kubernetes secret containing the data key: MONGOCONNECTION # the key inside the Kubernetes secret containing the data resources: requests: # minimum resources required cpu: 150m memory: 64Mi limits: # maximum resources allocated cpu: 300m memory: 256Mi readinessProbe: # is the container ready to receive traffic? httpGet: port: 3000 path: /healthz livenessProbe: # is the container healthy? httpGet: port: 3000 path: /healthz imagePullSecrets: - name: acr ``` ### WEB deployment ```yaml=1 apiVersion: apps/v1 kind: Deployment metadata: name: ratings-web spec: selector: matchLabels: app: ratings-web template: metadata: labels: app: ratings-web # the label for the pods and the deployments spec: containers: - name: ratings-web image: <acrname>.azurecr.io/ratings-web:v1 # IMPORTANT: update with your own repository imagePullPolicy: Always ports: - containerPort: 8080 # the application listens to this port env: - name: API # the application expects to connect to the API at this endpoint value: http://ratings-api.ratingsapp.svc.cluster.local resources: requests: # minimum resources required cpu: 250m memory: 64Mi limits: # maximum resources allocated cpu: 500m memory: 512Mi imagePullSecrets: - name: acr ``` ### INGRESS ```yaml=1 apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ratings-web-ingress spec: ingressClassName: nginx rules: - host: frontend.<ingress ip>.nip.io # IMPORTANT: update <ingress ip> with the dashed public IP of your ingress, for example frontend.13-68-177-68.nip.io http: paths: - path: / pathType: Prefix backend: service: name: ratings-web port: number: 80 ``` ## Deployment ### API - image:您將會建立部署,其中包含執行您推送至先前所建立 Azure Container Registry 執行個體 (例如 acr4229.azurecr.io/ratings-api:v1) 中映像的複本。 容器會接聽連接埠 3000。 部署和 Pod 都會加上 app=ratings-api 標籤。 - secretKeyRef:評等 API 預期在名為 MONGODB_URI 的環境變數中找到 MongoDB 資料庫連線詳細資料。 透過使用 valueFrom 和 secretKeyRef,您可以參考儲存在部署 MongoDB 時所建立 Kubernetes 祕密 mongosecret 中的值。 - resources:每個容器執行個體都會提供至少 0.25 個核心及 64 MB 的記憶體。 Kubernetes 排程器會尋找具備可用容量的節點來排程這類 Pod。 容器不一定可以長時間超過其 CPU 限制。 但是,容器不會因為過多的 CPU 使用量而遭到終止。 如果容器超過其記憶體限制,則可能會遭到終止。 - readinessProbe 和 livenessProbe:應用程式會在 /healthz 公開健康情況檢查端點。 如果 API 無法連線到 MongoDB,則健康情況檢查端點會傳回錯誤。 您可以使用這些探查來設定 Kubernetes,並檢查容器的健康情況是否良好且已準備好接收流量。 ### Web - image:您將會建立執行推送至稍早所建立 Container Registry 執行個體 (例如 acr4229.azurecr.io/ratings-web:v1) 中映像的部署。 容器會接聽連接埠 8080。 部署和 Pod 都會加上 app=ratings-web 標籤。 - env:評等前端預期連線到在 API 環境變數中設定的 API 端點。 如果您使用預設並在 ratingsapp 命名空間中部署評等 API 服務,則其值應該會是 http://ratings-api.ratingsapp.svc.cluster.local。 - resources:每個容器執行個體都會提供至少 0.25 個核心及 64 MB 的記憶體。 Kubernetes 排程器會尋找具備可用容量的節點來排程這類 Pod。 容器不一定可以長時間超過其 CPU 限制。 但是,容器不會因為過多的 CPU 使用量而遭到終止。 如果容器超過其記憶體限制,則可能會遭到終止。 ## Service ### API - selector:選取器會判斷服務的目標 Pod 集合。 在下列範例中,Kubernetes 會將流量負載平衡至具有 app: ratings-api 標籤的 Pod。 此標籤是在您建立部署時所定義的。 服務控制器會持續掃描符合該標籤的 Pod,以將這些 Pod 新增到負載平衡器。 - ports:服務可以將傳入的 port 對應至 targetPort。 傳入連接埠是服務所回應的連接埠。 目標連接埠是 Pod 設定要接聽的連接埠。 例如,服務會在叢集中於 ratings-api.ratingsapp.svc.cluster.local:80 向內部公開,且會將流量負載平衡至在連接埠 3000 上接聽的 ratings-api Pod。 - type:ClusterIP 類型的服務會建立內部 IP 位址,以在叢集內使用。 選擇這個值可以讓服務只能從叢集內部連接。 叢集 IP 是預設的服務類型。 ### Web - selector:選取器會判斷服務的目標 Pod 集合。 在下列範例中,Kubernetes 會將流量負載平衡至具有 app: ratings-web 標籤的 Pod。 此標籤是在您建立部署時所定義的。 服務控制器會持續掃描符合該標籤的 Pod,以將這些 Pod 新增到負載平衡器。 - ports:服務可以將傳入的 port 對應至 targetPort。 傳入連接埠是服務所回應的連接埠。 目標連接埠是 Pod 設定要接聽的連接埠。 例如,服務會在連接埠 80 向外部公開,且會將流量負載平衡至接聽連接埠 8080 的 ratings-web Pod。 - type:LoadBalancer 類型的服務會在 Azure 中建立公用 IP 位址,並將其指派給 Azure Load Balancer。 選擇這個值可以讓服務從叢集外部連接