【问题标题】:How to issue a CLI command (or make use of Azure SDK) from an Azure Docker Container at startup如何在启动时从 Azure Docker 容器发出 CLI 命令(或使用 Azure SDK)
【发布时间】:2022-02-25 02:03:57
【问题描述】:

我需要发出以下命令或通过 Azure SDK 和 C# 完成等效命令。

az network private-dns record-set a update --name <name> -g <resource-group> -z <zone-name> --set aRecords[0].ipv4Address=$(hostname -i)

每次 ACI 启动时我都需要这样做(无论出于何种原因),以便私有 DNS 区域具有 ACI 的当前 IP 地址(因为私有 IP 可以更改),它将在同一个 VNet 中运行。

我看到了几种可能的解决方案,但我都不太熟悉,无法实施。

  1. YAML 文件设置。
  2. 从容器内调用 Azure SDK c# 方法。
  3. Shell 从容器中运行命令。

了解有关如何完成这些解决方案之一的任何详细信息。

【问题讨论】:

  • 这里是 Docker CLI 命令的link,它将帮助您解决问题

标签: azure docker dns


【解决方案1】:

这是我的问题的解决方案,包括指向 Microsoft 建议的链接。

https://docs.microsoft.com/en-us/learn/modules/secure-apps-azure-container-instances-sidecar/6-deploy-with-init-container

此 YAML 文件使用 init 容器来运行一些 Azure 命令,包括使用服务主体登录,以及创建和更新私有 DNS 区域条目,以便容器可以使用 DNS 而不是 IP 地址相互进行 HTTP 调用(可以改变)。

YAML 文件:

location: centralus
name: dns-zone-test-a
properties:
  initContainers:
  - name: inita
    properties:
      image: mcr.microsoft.com/azure-cli:latest
      # redirection of output to a file for these commands is optional...a nice to have to confirm what's working
      command: ['/bin/sh', '-c', 'az login --service-principal -u $SP_APPID -p $SP_PASSWORD --tenant $SP_TENANT > /scripts/outsp_a.txt; 
      az container show -n $ACI_NAME -g $RG --query ''ipAddress.ip'' -o tsv > /scripts/swac_a.txt;
      my_private_ip=$(az container show -n $ACI_NAME -g $RG --query ''ipAddress.ip'' -o tsv);
      az network private-dns record-set a create -n $HOSTNAME -z $DNS_ZONE_NAME -g $RG  > /scripts/crzone_a.txt;
      az network private-dns record-set a add-record --record-set-name $HOSTNAME -z $DNS_ZONE_NAME -g $RG -a $my_private_ip > /scripts/addzone_a.txt;']      
      environmentVariables:
      - name: RG
        value: myResourceGroup
      - name: SP_APPID  # service principal with the permissions to update private DNS zone
        value: 5xxxxxxxxxxxxxx
      - name: SP_PASSWORD   # service principal password
        secureValue: byyyyyyyyyyyyyyy
      - name: SP_TENANT
        value: bzzzzzzzzzzzzzzzzzz
      - name: DNS_ZONE_NAME
        value: dns-zone-mine.com
      - name: HOSTNAME
        value: dns-zone-test-a      
      - name: ACI_NAME
        value: dns-zone-test-a      
      volumeMounts: # needed only if redirecting the output from the commands above to a file
      - name: initscript
        mountPath: /scripts/
  containers:   # any docker container you want
    - name: cab-a
      properties:
        image: MyRegistry.azurecr.io/contest1:latest
        ports:
          - port: 80
            protocol: TCP
        resources:
          requests:
            cpu: 1.0
            memoryInGB: 1.5
  imageRegistryCredentials: # Credentials to pull a private image
    - server: MyRegistry.azurecr.io
      username: MyUserRegistry
      password: 5xxxxxxxxxxxxx
  volumes:  # only needed if redirecting the output from the commands above to a file
  - name: initscript
    azureFile:
      readOnly: false
      shareName: initscript
      storageAccountName: myStorage
      storageAccountKey: zzzzzzzzzzzzzzzzzzzzzzzzzzz
  ipAddress:
    ports:
      - port: 80
        protocol: TCP
    type: private
  osType: Linux
  subnetIds:
    - id: /subscriptions/xxxxyyyyyzzzzz/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/MyVNet/subnets/MyTestSubnet
      name: default
  # dnsConfig: # DNS configuration for container group: not needed likely for this test, and may interfere with private DNS zone usage
  #  nameServers:
  # - 192.168.1.44
tags: null
type: Microsoft.ContainerInstance/containerGroups

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2020-09-13
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多