자습서: Ansible을 사용하여 Azure Application Gateway를 사용하여 웹 트래픽 관리

중요합니다

이 문서의 샘플 플레이북을 실행하려면 Ansible 2.7 이상이 필요합니다.

Azure Application Gateway는 웹 애플리케이션에 대한 트래픽을 관리할 수 있는 웹 트래픽 부하 분산 장치입니다. 원본 IP 주소 및 포트에 따라 기존 부하 분산 장치는 트래픽을 대상 IP 주소 및 포트로 라우팅합니다. Application Gateway는 URL에 따라 트래픽을 라우팅할 수 있는 더 세부적인 제어 수준을 제공합니다. 예를 들어 URL 경로인 경우 images 트래픽이 이미지에 대해 구성된 특정 서버 집합(풀이라고 함)으로 라우팅된다는 것을 정의할 수 있습니다.

이 문서에서는 다음 방법을 알아봅니다.

  • 네트워크 설정
  • HTTPD 이미지를 사용하여 두 개의 Azure 컨테이너 인스턴스 만들기
  • 서버 풀에서 Azure 컨테이너 인스턴스와 함께 작동하는 애플리케이션 게이트웨이 만들기

필수 조건

  • Azure 구독: Azure 구독이 아직 없는 경우 시작하기 전에 체험 계정을 만듭니다.

리소스 그룹 만들기

이 섹션의 플레이북 코드는 Azure 리소스 그룹을 만듭니다. 리소스 그룹은 Azure 리소스가 구성된 논리적 컨테이너입니다.

다음 플레이북을 rg.yml로 저장합니다.

- hosts: localhost
  vars:
    resource_group: myResourceGroup
    location: eastus 
  tasks:
    - name: Create a resource group
      azure_rm_resourcegroup:
        name: "{{ resource_group }}"
        location: "{{ location }}"

플레이북을 실행하기 전에 다음 참고 사항을 참조하세요.

  • 리소스 그룹 이름은 .입니다 myResourceGroup. 이 값은 자습서 전체에서 사용됩니다.
  • 리소스 그룹이 eastus 위치에 생성되었습니다.

ansible-playbook을 사용하여 플레이북 실행

ansible-playbook rg.yml

네트워크 리소스 만들기

이 섹션의 플레이북 코드는 애플리케이션 게이트웨이가 다른 리소스와 통신할 수 있도록 가상 네트워크를 만듭니다.

다음 플레이북을 vnet_create.yml에 저장하세요.

- hosts: localhost
  vars:
    resource_group: myResourceGroup
    location: eastus 
    vnet_name: myVNet 
    subnet_name: myAGSubnet 
    publicip_name: myAGPublicIPAddress
    publicip_domain: mydomain
  tasks:
    - name: Create a virtual network
      azure_rm_virtualnetwork:
        name: "{{ vnet_name }}"
        resource_group: "{{ resource_group }}"
        address_prefixes_cidr:
            - 10.1.0.0/16
            - 172.100.0.0/16
        dns_servers:
            - 127.0.0.1
            - 127.0.0.2

    - name: Create a subnet
      azure_rm_subnet:
        name: "{{ subnet_name }}"
        virtual_network_name: "{{ vnet_name }}"
        resource_group: "{{ resource_group }}"
        address_prefix_cidr: 10.1.0.0/24

    - name: Create a public IP address
      azure_rm_publicipaddress:
        resource_group: "{{ resource_group }}" 
        allocation_method: Dynamic
        name: "{{ publicip_name }}"
        domain_name_label: "{{ publicip_domain }}"

플레이북을 실행하기 전에 다음 참고 사항을 참조하세요.

  • 이 vars 섹션에는 네트워크 리소스를 만드는 데 사용되는 값이 포함되어 있습니다.
  • 특정 환경에 대해 이러한 값을 변경해야 합니다.

ansible-playbook을 사용하여 플레이북 실행

ansible-playbook vnet_create.yml

서버 만들기

이 섹션의 플레이북 코드는 애플리케이션 게이트웨이에 대한 웹 서버로 사용할 HTTPD 이미지가 있는 두 개의 Azure 컨테이너 인스턴스를 만듭니다.

다음 플레이북을 aci_create.yml로 저장하십시오.

- hosts: localhost
  vars:
    resource_group: myResourceGroup
    location: eastus 
    aci_1_name: myACI1
    aci_2_name: myACI2
  tasks:
    - name: Create a container with httpd image 
      azure_rm_containerinstance:
        resource_group: "{{ resource_group }}"
        name: "{{ aci_1_name }}"
        os_type: linux
        ip_address: public
        location: "{{ location }}"
        ports:
          - 80
        containers:
          - name: mycontainer
            image: httpd
            memory: 1.5
            ports:
              - 80

    - name: Create another container with httpd image 
      azure_rm_containerinstance:
        resource_group: "{{ resource_group }}"
        name: "{{ aci_2_name }}"
        os_type: linux
        ip_address: public
        location: "{{ location }}"
        ports:
          - 80
        containers:
          - name: mycontainer
            image: httpd
            memory: 1.5
            ports:
              - 80

ansible-playbook을 사용하여 플레이북 실행

ansible-playbook aci_create.yml

Application Gateway 만들기

이 섹션의 플레이북 코드는 라는 myAppGateway애플리케이션 게이트웨이를 만듭니다.

다음 플레이북을 다음과 같이 appgw_create.yml저장합니다.

- hosts: localhost
  connection: local
  vars:
    resource_group: myResourceGroup
    vnet_name: myVNet
    subnet_name: myAGSubnet
    location: eastus
    publicip_name: myAGPublicIPAddress
    appgw_name: myAppGateway
    aci_1_name: myACI1
    aci_2_name: myACI2
  tasks:
    - name: Get info of Subnet
      azure_rm_resource_facts:
        api_version: '2018-08-01'
        resource_group: "{{ resource_group }}"
        provider: network
        resource_type: virtualnetworks
        resource_name: "{{ vnet_name }}"
        subresource:
          - type: subnets
            name: "{{ subnet_name }}"
      register: subnet

    - name: Get info of backend server 1
      azure_rm_resource_facts:
        api_version: '2018-04-01'
        resource_group: "{{ resource_group }}"
        provider: containerinstance
        resource_type: containergroups
        resource_name: "{{ aci_1_name }}"
      register: aci_1_output
    - name: Get info of backend server 2
      azure_rm_resource_facts:
        api_version: '2018-04-01'
        resource_group: "{{ resource_group }}"
        provider: containerinstance
        resource_type: containergroups
        resource_name: "{{ aci_2_name }}"
      register: aci_2_output

    - name: Create instance of Application Gateway
      azure_rm_appgateway:
        resource_group: "{{ resource_group }}"
        name: "{{ appgw_name }}"
        sku:
          name: standard_v2
          tier: standard_v2
          capacity: 2
        gateway_ip_configurations:
          - subnet:
              id: "{{ subnet.response[0].id }}"
            name: appGatewayIP
        frontend_ip_configurations:
          - public_ip_address: "{{ publicip_name }}"
            name: appGatewayFrontendIP
        frontend_ports:
          - port: 80
            name: appGatewayFrontendPort
        backend_address_pools:
          - backend_addresses:
              - ip_address: "{{ aci_1_output.response[0].properties.ipAddress.ip }}"
              - ip_address: "{{ aci_2_output.response[0].properties.ipAddress.ip }}"
            name: appGatewayBackendPool
        backend_http_settings_collection:
          - port: 80
            protocol: http
            cookie_based_affinity: enabled
            name: appGatewayBackendHttpSettings
        http_listeners:
          - frontend_ip_configuration: appGatewayFrontendIP
            frontend_port: appGatewayFrontendPort
            name: appGatewayHttpListener
        request_routing_rules:
          - rule_type: Basic
            backend_address_pool: appGatewayBackendPool
            backend_http_settings: appGatewayBackendHttpSettings
            http_listener: appGatewayHttpListener
            name: rule1

플레이북을 실행하기 전에 다음 참고 사항을 참조하세요.

  • appGatewayIP 는 블록에 정의되어 있습니다 gateway_ip_configurations . 게이트웨이의 IP 구성에는 서브넷 참조가 필요합니다.
  • appGatewayBackendPool 는 블록에 정의되어 있습니다 backend_address_pools . 애플리케이션 게이트웨이에는 하나 이상의 백 엔드 주소 풀이 있어야 합니다.
  • appGatewayBackendHttpSettings 는 블록에 정의되어 있습니다 backend_http_settings_collection . 포트 80 및 HTTP 프로토콜이 통신에 사용되도록 지정합니다.
  • appGatewayHttpListener 는 블록에 정의되어 있습니다 backend_http_settings_collection . appGatewayBackendPool과 연결된 기본 수신기입니다.
  • appGatewayFrontendIP 는 블록에 정의되어 있습니다 frontend_ip_configurations . appGatewayHttpListener에 myAGPublicIPAddress를 할당합니다.
  • rule1 는 블록에 정의되어 있습니다 request_routing_rules . appGatewayHttpListener와 연결된 기본 라우팅 규칙입니다.

ansible-playbook을 사용하여 플레이북 실행

ansible-playbook appgw_create.yml

애플리케이션 게이트웨이를 만드는 데 몇 분 정도 걸릴 수 있습니다.

애플리케이션 게이트웨이 테스트

  1. 리소스 그룹 만들기 섹션에서 위치를 지정합니다. 해당 값을 확인합니다.

  2. 네트워크 리소스 만들기 섹션에서 도메인을 지정합니다. 해당 값을 확인합니다.

  3. 테스트 URL의 경우, 다음 패턴을 위치 및 도메인으로 바꿉니다. http://<domain>.<location>.cloudapp.azure.com

  4. 테스트 URL로 이동합니다.

  5. 다음 페이지가 표시되면 애플리케이션 게이트웨이가 예상대로 작동합니다.

    작업 중인 애플리케이션 게이트웨이의 성공적인 테스트

자원을 정리하세요

  1. 다음 코드를 .로 delete_rg.yml저장합니다.

    ---
    - hosts: localhost
      tasks:
        - name: Deleting resource group - "{{ name }}"
          azure_rm_resourcegroup:
            name: "{{ name }}"
            state: absent
          register: rg
        - debug:
            var: rg
    
  2. ansible-playbook 명령을 사용하여 플레이북을 실행합니다. 자리 표시자를 삭제할 리소스 그룹의 이름으로 바꿉니다. 리소스 그룹 내의 모든 리소스가 삭제됩니다.

    ansible-playbook delete_rg.yml --extra-vars "name=<resource_group>"
    

    핵심 사항:

    • 플레이북의 register 변수와 debug 섹션 때문에 명령이 완료되면 결과가 표시됩니다.

다음 단계