升級容器工作負載,提升執行階段效能

如果您使用 Migrate to Containers 1.7.x 和 1.8.x 版本建立現有的容器工作負載,可以將這些工作負載轉換為使用簡化的 Linux 服務管理工具。這樣一來,您就能在 GKE Autopilot 叢集中執行這些容器。

如要執行轉換作業,請編輯 Dockerfile 和執行原始遷移作業時建立的 deployment_spec.yaml 檔案。編輯完成後,您就可以在 Autopilot 叢集上部署容器工作負載。

關於轉換容器工作負載

轉換現有工作負載的程序取決於您要轉換的為無狀態工作負載或有狀態工作負載。

有狀態工作負載是指維持或儲存狀態資訊的工作負載。對於有狀態的工作負載,您通常會使用 spec.containers.volumeMounts 中的 StatefulSet 掛載其他磁區。請務必保留 volumeMounts 定義,同時移除 /sys/fs/cgroup 的定義。詳情請參閱「掛接外部磁碟區」。

轉換現有工作負載的一般程序需要編輯以下項目:

  • Dockerfile

    • 將 Migrate to Containers 版本設為 1.15.0。
    • 插入兩個 ADD 指令,將 logs.yaml 檔案複製到容器映像檔。
    • servicemanager_generate_config 公用程式插入 RUN 指令。
  • deployment_spec.yaml 檔案:

    • 刪除 /sys/fs/cgrouphostPathvolumeMounts 定義。
    • 刪除 securityContext 定義。
    • 刪除 readinessProbe 定義。
    • 您可以為 logs-config 保留 mountPathconfigMap 定義,但記錄目前無法與簡化的 Linux 服務管理員搭配使用。

如要瞭解具體轉換程序,請參閱以下各節:

轉換無狀態工作負載

以下範例說明如何轉換無狀態容器工作負載:

  1. 找出包含現有遷移構件 (包括 deployment_spec.yaml 檔案) 的目錄。

  2. 編輯 Dockerfile 來設定產品版本、複製 logs.yaml 檔案,以及執行 servicemanager_generate_config 公用程式:

    ...
    # Set the product version to 1.15.0:
    FROM anthos-migrate.gcr.io/v2k-run-embedded:v1.15.0 as migrate-for-anthos-runtime
    
    ...
    
    ADD blocklist.yaml /.m4a/blocklist.yaml
    # Insert the ADD commands to copy the `logs.yaml` file to the container image:
    ADD logs.yaml /code/config/logs/logsArtifact.yaml
    ADD logs.yaml /code/config/logs/logs.yaml
    
    # Insert the RUN command for servicemanager_generate_config:
    RUN /servicemanager_generate_config build-all -o /.m4a/
    
    # Migrate to Containers image includes entrypoint
    ENTRYPOINT [ "/.v2k.go" ]
  3. 在編輯器中開啟 deployment_spec.yaml 檔案。例如:

    vi  deployment_spec.yaml
  4. 在檔案中找出下列部分,然後刪除標示的程式碼行:

    apiVersion: apps/v1 
    kind: Deployment
    metadata: 
      creationTimestamp: null 
      name: IMAGE_NAME  
          
        spec:
          containers:
          - image: gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL
            name: IMAGE_NAME
    # Delete the following lines:
            readinessProbe:
              exec:
                command:
                - /code/ready.sh
            resources: {}
            securityContext:
              privileged: true
            volumeMounts:
            - mountPath: /sys/fs/cgroup
              name: cgroups
            - mountPath: /code/config/logs
              name: logs-config
          volumes:
          - hostPath:
              path: /sys/fs/cgroup
              type: Directory
            name: cgroups
          - configMap:
              name: suitecrm-crddefault-logs
            name: logs-config
    # Stop the delete here.
  5. 加入以下指令行來設定 HC_V2K_SERVICE_MANAGER 環境變數。

    spec:
      containers:
      - image: gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL
        name: IMAGE_NAME
    # Add the following lines:
        env:
          - name: HC_V2K_SERVICE_MANAGER
            value: "true"
  6. 儲存檔案。

  7. 請確認目標叢集具有 Docker 映像檔登錄檔的讀取權限,如「確保目標叢集具有 Docker 映像檔登錄檔的讀取權限」一節所述。

  8. 請建構更新後的映像檔,並使用更新後的版本標記推送至 Container Registry,確保有足夠的時間完成建構作業。在以下範例中,圖片位於目前目錄中:

    gcloud builds submit --timeout 4h --tag gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL .
  9. 部署容器:

    kubectl apply -f deployment_spec.yaml

    如果您將部署規格套用至 Autopilot 叢集,但未在 deployment_spec.yaml 中進行必要變更,您會在表單中看到錯誤訊息:

    "Trying to run without root privileges is not possible. Did you try to use the new runtime? In that case please pass the environment variable HC_V2K_SERVICE_MANAGER=true to the pod"

  10. 查看要部署至叢集的 Pod。

    kubectl get pods

轉換有狀態的工作負載

以下範例說明如何轉換有狀態容器工作負載:

  1. 找出包含現有遷移構件的目錄,包括 deployment_spec.yaml 檔案。

  2. 編輯 Dockerfile 以設定產品版本,並執行 servicemanager_generate_config 公用程式:

    ...
    # Set the product version to 1.15.0:
    FROM anthos-migrate.gcr.io/v2k-run-embedded:v1.15.0 as migrate-for-anthos-runtime
    
    ...
    
    ADD blocklist.yaml /.m4a/blocklist.yaml
    # Insert the ADD commands to copy the `logs.yaml` file to the container image:
    ADD logs.yaml /code/config/logs/logsArtifact.yaml
    ADD logs.yaml /code/config/logs/logs.yaml
    
    # Insert the RUN command for servicemanager_generate_config:
    RUN /servicemanager_generate_config build-all -o /.m4a/
    
    # Migrate to Containers image includes entrypoint
    ENTRYPOINT [ "/.v2k.go" ]
  3. 在編輯器中開啟 deployment_spec.yaml 檔案。例如:

    vi  deployment_spec.yaml
  4. 在檔案中找出下列三個部分,然後刪除標示的程式碼行:

    apiVersion: apps/v1 
    kind: StatefulSet  
    ... 
    spec: 
      containers: 
      - image: gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL 
        name: IMAGE_NAME 
    # Delete the following lines:
        readinessProbe: 
          exec: 
            command: 
            - /code/ready.sh 
        resources: {} 
        securityContext: 
          privileged: true 
    # Stop the delete here.
        volumeMounts: 
    # Delete the following lines:
        - mountPath: /sys/fs/cgroup 
          name: cgroups 
    # Stop the delete here.
        - mountPath: /opt/suitecrm-7.10.5-0/mysql/data 
          name: data-pvc-0-1b12-d0af-48b3-9f5e-6c25fa5 
          subPath: opt/suitecrm-7.10.5-0/mysql/data 
      volumes:
    # Delete the following lines:
      - hostPath:
          path: /sys/fs/cgroup 
          type: Directory 
        name: cgroups 
    # Stop the delete here.
      - name: data-pvc-2-d0af-48b3-9f5e09c25fa5 
        persistentVolumeClaim: 
          claimName: data-pvc-0-1a2-d0af-48b3-9f5e-605fa5

    請注意,您只需移除 cgroupsvolumeMountsvolumes 定義,其餘定義則保留不變。

  5. 新增下列幾行內容,設定 HC_V2K_SERVICE_MANAGER 環境變數:

    spec: 
      containers: 
      - image: gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL
        name: IMAGE_NAME 
    # Add the following lines:
        env:
        - name: HC_V2K_SERVICE_MANAGER 
          value: "true" 
    # Stop the add here.
        volumeMounts: 
        - mountPath: /opt/suitecrm-7.10.5-0/mysql/data 
          name: data-pvc-0-1b12-d0af-48b3-9f5e-6c25fa5 
          subPath: opt/suitecrm-7.10.5-0/mysql/data 
      volumes:
      - name: data-pvc-2-d0af-48b3-9f5e09c25fa5 
        persistentVolumeClaim: 
          claimName: data-pvc-0-1a2-d0af-48b3-9f5e-605fa5
  6. 儲存檔案。

  7. 請按照「確保目標叢集具有 Docker 映像檔登錄檔的讀取權」一節所述,確認目標叢集具有 Docker 映像檔登錄檔的讀取權限。

  8. 請建構更新後的映像檔,並使用更新後的版本標記推送至 Container Registry,確保有足夠的時間完成建構作業。在以下範例中,圖片位於目前目錄中:

    gcloud builds submit --timeout 4h --tag gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL .
  9. 部署容器:

    kubectl apply -f deployment_spec.yaml

    如果您在 deployment_spec.yaml 中未進行必要變更,就將部署規格套用至 Autopilot 叢集,系統會顯示以下錯誤訊息:

    "Trying to run without root privileges is not possible. Did you try to use the new runtime? In that case please pass the environment variable HC_V2K_SERVICE_MANAGER=true to the pod"

  10. 查看要部署至叢集的 Pod。

    kubectl get pods

轉換後工作

將現有遷移作業轉換為使用簡易版 Linux 服務管理工具後,您可能需要修改為:

  • 更新遷移工作負載所使用的服務。
  • 新增服務。

無論是哪種情況,您都必須編輯 Dockerfile,然後重新建構容器映像檔。

更新服務

在本節中,您將編輯 Dockerfile,根據已遷移工作負載的 /etc/systemd 所做的變更,更新容器中的 services-config.yaml 檔案。

如要更新容器映像檔,以便變更現有服務,請按照下列步驟操作:

  1. 在 Dockerfile 中新增 servicemanager_generate_config 指令:

    ...
    FROM anthos-migrate.gcr.io/v2k-run-embedded:v1.15.0 as migrate-for-anthos-runtime
    
    ...
    
    ADD blocklist.yaml /.m4a/blocklist.yaml
    
    # Use the update command for servicemanager_generate_config to update the configuration:
    RUN /servicemanager_generate_config update -u /.m4a/
    
    # Migrate to Containers image includes entrypoint
    ENTRYPOINT [ "/.v2k.go" ]
  2. 請建構更新後的映像檔,並使用更新後的版本標記推送至 Container Registry,確保有足夠的時間完成建構作業。在以下範例中,圖片位於目前目錄中:

    gcloud builds submit --timeout 4h --tag gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL .
  3. 部署新建的映像檔:

    kubectl set image deployment/myWorkload my-app=gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL --record

新增服務

如何將服務新增至容器映像檔:

  1. 在 Dockerfile 中新增 servicemanager_generate_config 指令:

    ...
    FROM anthos-migrate.gcr.io/v2k-run-embedded:v1.15.0 as migrate-for-anthos-runtime
    
    ...
    
    ADD blocklist.yaml /.m4a/blocklist.yaml
    
    # This example adds the redis-server service.
    # Add the following lines to install redis-server.
    RUN apt-get update && apt-get -y install redis-server
    
    # Use the servicemanager_generate_config add command to add
    # redis-server to the configuration:
    RUN /servicemanager_generate_config add redis-server -u /.m4a/
    
    # Migrate to Containers image includes entrypoint
    ENTRYPOINT [ "/.v2k.go" ]
  2. 請建構更新後的映像檔,並使用更新後的版本標記推送至 Container Registry,確保有足夠的時間完成建構作業。在以下範例中,圖片位於目前目錄中:

    gcloud builds submit --timeout 4h --tag gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL .
  3. 部署新建的映像檔:

    kubectl set image deployment/myWorkload my-app=gcr.io/PROJECT_NAME/IMAGE_NAME:LABEL --record

servicemanager_generate_config 語法

servicemanager_generate_config 公用程式會採用下列選項:

  • build-all -o /.m4a/:重建遷移作業,並將設定寫入 m4a 目錄。請勿變更目錄名稱。

    第一次將遷移作業轉換為使用簡化版 Linux 服務管理工具時,請使用這個指令。

  • update -u /.m4a/:更新 m4a 目錄中現有服務的清單。請勿變更目錄名稱。

  • add SERVICE_NAME -u /.m4a/:將服務名稱新增至遷移作業,並將設定寫入 m4a 目錄。請勿變更目錄名稱。

    如要新增多項服務,請新增多個 RUN /servicemanager_generate_config 指令,每項服務一個。

後續步驟