Chef 를 사용하여 NGINX Plus 고가용성 배포

Chef 를 사용하여 전체 설정 프로세스가 자동화된 고가용성 NGINX Plus Active/Passive 클러스터를 설정하기 위해, 해당 포스트는 keepalived와 가상 라우터 장애 조치 프로토콜(VRRP)을 기반으로 한 NGINX에서 제공하는 HA 솔루션을 사용합니다. 이 솔루션은 keepalived 구성 파일을 생성하기 위한 대화형 스크립트와 함께 제공됩니다.

목차

1. Chef 파일 설정
 1-1. Chef 템플릿 생성
2. 설치를 위한 Chef 노드 준비
 2-1. 첫 번째 노드
 2-2. 두 번째 노드
 2-3. 노드 동기화
3. Host Name(호스트명)을 기반으로 Chef 기본 노드 설정
4. 결론

1. Chef 파일 설정

먼저, NGINX Plus HA 패키지 (nginx-ha-keepalived)를 설치하고 keepalived 구성 파일인 keepalive.conf를 생성하기 위해 Chef 파일을 설정합니다.

1. 기존의 plus_package를 수정하여 HA 솔루션을 위한 패키지와 구성 템플릿을 포함하도록 하기 위해 다음 코드를 plus_package.rb 파일의 맨 아래에 추가합니다.

각 NGINX 호스트에서 eth1 인터페이스를 사용하고 있으므로 코드가 eth0을 사용하는 경우보다 약간 복잡해집니다. eth0을 사용하는 경우, 관련 코드는 파일의 상단 부근에 주석 처리되어 있습니다.

이 코드는 세 가지 작업을 수행합니다.

  • NGINX Plus가 설치되는 노드의 eth1 인터페이스의 IP 주소를 조회하고, 값을 origip 변수에 할당하여 템플릿에 전달합니다.
  • Chef의 검색 기능을 사용하여 모든 노드를 반복하고, 해당 노드의 eth1 인터페이스의 IP 주소를 조회하여 ha_pair_ips 변수에 할당하여 같은 HA의 다른 노드를 찾습니다.
  • nginx-ha-keepalived 패키지를 설치하고, keepalived 서비스를 Chef에 등록하고, origip 및 ha_pair_ips 변수의 값을 전달하여 keepalived.conf 구성 파일을 템플릿으로 생성합니다.
if node['nginx']['enable_ha_mode'] == 'true'
  ha_pair_ips = Array.new
  origip = "#{node[:network][:interfaces][:eth1][:addresses].detect{|k,v| v[:family] == 'inet'}.first}"
  # The code for finding the IP address of the eth0 interface   
  # follows, commented out.
  #origip = "#{node[:ipaddress]}"
  #search(:node, "role:nginx_plus_ha") do |nodes|
  #  ha_pair_ips << nodes["ipaddress"]
  #end
  # This is a workaround for getting the IP address for the eth1
  # that VMs need
  search(:node, "role:nginx_plus_ha AND enable_ha_mode:true NOT name:#{node.name}") do |nodes|
    nodes["network"]["interfaces"]["eth1"]["addresses"].each_pair do |address,value|
      ha_pair_ips << address if value.has_key?("broadcast")
    end
  end

  package 'nginx-ha-keepalived' do
    action :install
  end

  service 'keepalived' do
    supports :status => true, :restart => true, :reload => true
    action   :enable
  end
  template '/etc/keepalived/keepalived.conf' do
    source 'nginx_plus_keepalived.conf.erb'
    owner 'root'
    group node['root_group']
    mode '0644'
    variables(
              :myip => origip,
              :ha_pair_ip => ha_pair_ips
             )
    notifies :reload, 'service[keepalived]', :delayed
  end
end

1-1. Chef 템플릿 생성

2. keepalive.conf를 생성하기 위한 템플릿을 다음 내용을 ~/chef-zero/playground/cookbooks/nginx/templates/default 디렉토리에 nginx_plus_keepalived.conf.erb라는 새로운 템플릿 파일로 복사하세요.

우리는 keepalived.conf에 필요한 정보를 전달하기 위해 변수와 속성의 조합을 사용하고 있습니다.
다음 단계에서는 속성을 설정할 것입니다. 여기서는 템플릿 파일의 두 변수를 plus_package.rb 레시피의 변수 지시문으로 설정된 호스트 IP 주소로 설정합니다.

  • myipkeepalived가 다른 고가용성 노드와 통신하는 데 사용하는 주요 IP 주소입니다. plus_package 레시피의 origip 변수와 대응됩니다.
  • ha_pair_ipnginx_plus_ha 역할을 가진 각 호스트의 IP 주소를 포함하는 배열입니다. 이는 keepalived.conf에서 동료(보조) 호스트의 IP 주소를 설정하는 데 사용됩니다. plus_package 레시피의 ha_pair_ips 배열과 대응됩니다.
vrrp_script chk_nginx_service {
     script "/usr/lib/keepalived/nginx-ha-check"
     interval 3
     weight 50
}

vrrp_instance VI_1 {
     interface eth1
     <% if node['nginx']['ha_primary'] == "true" %>
     state MASTER
     priority 151
     <% end %>
     <% if node['nginx']['ha_primary'] == "false" %>
     state BACKUP
     priority 150
     <% end %>
     virtual_router_id 51
     advert_int 1
     unicast_src_ip <%= @myip %>
     unicast_peer {
     <% @ha_pair_ip.each do |ip| %>
        <% if ip != @myip %>
           <%= ip %>
        <% end %>
     <% end %>
     }
     authentication {
          auth_type PASS
          auth_pass <%= node['nginx']['ha_keepalived_key'] %>
     }
     virtual_ipaddress {
          <%= node['nginx']['ha_vip'] %>
     }
     track_script {
          chk_nginx_service
     }
     notify "/usr/lib/keepalived/nginx-ha-notify"
}

3. 이전 단계에서 생성된 레시피 및 템플릿 파일에서 사용되는 속성을 설정하는 역할을 생성하려면 다음 내용을 ~/chef-zero/playground/roles 디렉토리에 있는 새로운 역할 파일인 nginx_plus_ha.rb에 복사하세요.

다음 세 가지 속성을 설정해야 합니다. 역할에서는 다음과 같이 세 가지를 설정합니다.

  • [nginx][ha_keepalived_key] – keepalived가 다른 고가용성 노드와의 통신을 암호화하는 데 사용하는 인증 키입니다. 템플릿 파일에서 사용됩니다.
  • [nginx][ha_vip] – NGINX Plus의 가상 IP 주소(VIP)로 클라이언트에게 알려지며, keepalived 프로세스가 주요 NGINX Plus 인스턴스에 할당하고 장애 조치 시 다른 인스턴스로 전달하는 역할을 합니다. 템플릿 파일에서 사용됩니다.
  • [nginx][enable_ha_mode] – true로 설정되면 plus_package 레시피의 HA 섹션을 트리거합니다. 레시피 파일에서 사용됩니다.

네 번째 속성인 ha_primary는 역할에서 설정하지 않습니다. 이는 호스트별로 설정해야 하기 때문입니다.
이 속성은 keepalived.conf에서 상태와 우선순위 값을 설정하는 데 사용되며, 이 값들은 다시 NGINX Plus 인스턴스 중 어느 것이 Primary로 표시되는지를 결정합니다. 노드의 ha_primary 값이 true인 경우 해당 노드는 HA 쌍에서 Primary NGINX Plus 인스턴스가 되며, 값이 false인 경우 보조 인스턴스가 됩니다.

또한, ha_primary 속성 대신 호스트명의 일치에 따라 Primary 인스턴스를 설정할 수도 있습니다. 호스트명 일치를 기반으로 Primary 노드를 설정하는 방법은 “호스트명 일치에 따라 Primary 노드 설정하기”에서 다룰 것입니다. 또한, 네 개의 속성을 호스트별로 설정할 수도 있지만, 공유 속성을 가능한 한 하나의 위치에 모아두는 것이 좋습니다.

역할의 run_list 지시문은 이전 블로그 포스트에서 생성한 nginx_plus 역할을 참조하여 이미 정의된 모든 내용을 중복으로 작성하지 않도록 합니다.

name "nginx_plus_ha"
description "An example role to install nginx plus in an HA cluster"
run_list "role[nginx_plus]"
default_attributes "nginx" => { "enable_ha_mode" => "true",
                                "ha_keepalived_key" => "a0cf476cf069ea3dfa8940ff6d6bd885",
                                "ha_vip" => "10.100.10.50"
                              }

2. 설치를 위한 Chef 노드 준비

이제 노드를 부트스트랩하고 설치를 준비합니다. 긴 출력은 잘라내 가장 중요한 출력만 남겨두었습니다.

2-1. 첫 번째 노드

1. 모든 파일을 업로드하고 첫 번째 노드를 부트스트랩 합니다.

root@chef-server:~# cd chef-zero/playground/cookbooks/
root@chef-server:~/chef-zero/playground/cookbooks# knife cookbook upload *
Uploading apache2      [1.0.0]
Uploading apt          [2.8.2]
...
Uploading nginx        [2.7.6]
...
Uploading yum          [3.8.1]
Uploading yum-epel     [0.6.3]
Uploaded 18 cookbooks.

root@chef-server:~/chef-zero/playground/cookbooks# cd ../

root@chef-server:~/chef-zero/playground# knife role from file roles/nginx_plus.rb
Updated Role nginx_plus!

root@chef-server:~/chef-zero/playground# knife role from file roles/nginx_plus_ha.rb
Updated Role nginx_plus_ha!

root@chef-server:~/chef-zero/playground# knife bootstrap -N chef-test-1 -x username --sudo 10.100.10.100
Creating new client for chef-test-1
Creating new node for chef-test-1
Connecting to 10.100.10.100
username@10.100.10.100's password:
10.100.10.100 knife sudo password:
Enter your password:
10.100.10.100
10.100.10.100 -----> Existing Chef installation detected
10.100.10.100 Starting first Chef Client run...
10.100.10.100 Starting Chef Client, version 12.6.0
10.100.10.100 resolving cookbooks for run list: []
10.100.10.100 Synchronizing Cookbooks:
10.100.10.100 Compiling Cookbooks...
10.100.10.100 [2016-02-07T06:17:13-08:00] WARN: Node chef-test-1 has an empty run list.
10.100.10.100 Converging 0 resources
10.100.10.100
10.100.10.100 Running handlers:
10.100.10.100 Running handlers complete
10.100.10.100 Chef Client finished, 0/0 resources updated in 01 seconds

2. 이전 단계에서 부트스트랩한 노드인 chef-test-1에 맞게 편집할 노드 정의 파일의 로컬 복사본을 만듭니다.

root@chef-server:~/chef-zero/playground# knife node show chef-test-1 --format json > nodes/chef-test-1.json

3. chef-test-1.json 파일을 다음 내용으로 편집합니다. 특히, HA 배포에 필요한 실행 목록(run list)과 ha_primary 속성을 설정합니다

{
  "name": "chef-test-1",
  "chef_environment": "_default",
  "run_list": [
             "role[nginx_plus_ha]"
  ]
,
  "normal": {
    "nginx": {
      "ha_primary": "true"
    },
    "tags": [

    ]
  }
}

4. 변경된 노드 정의를 Chef에 푸시(push)합니다.

root@chef-server:~/chef-zero/playground# knife node from file nodes/chef-test-1.json
Updated Node chef-test-1!

5. 노드에 로그인하여 chef-client 명령을 실행하여 모든 구성을 가져옵니다.

username@chef-test-1:~$ sudo chef-client
Starting Chef Client, version 12.6.0
resolving cookbooks for run list: ["nginx"]
Synchronizing Cookbooks:
  - ohai (2.0.1)
  - build-essential (2.2.4)
  - nginx (2.7.6)
  - yum-epel (0.6.3)
  - apt (2.8.2)
  - bluepill (2.4.0)
  - runit (1.7.2)
  - rsyslog (2.1.0)
  - packagecloud (0.1.0)
  - yum (3.8.1)
Compiling Cookbooks...
...
  * template[/etc/keepalived/keepalived.conf] action create
    - create new file /etc/keepalived/keepalived.conf
    - update content in file /etc/keepalived/keepalived.conf from none to 9816fd
    --- /etc/keepalived/keepalived.conf    2016-02-07 06:34:18.117013010 -0800
    +++ /etc/keepalived/.keepalived.conf20160207-1079-sjg8xn    2016-02-07 06:34:18.117013010 -0800
    @@ -1 +1,28 @@
    +vrrp_script chk_nginx_service {
    +    script "/usr/lib/keepalived/nginx-ha-check"
    +    interval 3
    +    weight 50
    +}
    +
    +vrrp_instance VI_1 {
    +    interface eth1
    +    state MASTER
    +    priority 151
    +    virtual_router_id 51
    +    advert_int 1
    +    unicast_src_ip 10.100.10.100
    +    unicast_peer {
    +    }
    +    authentication {
    +        auth_type PASS
    +        auth_pass a0cf476cf069ea3dfa8940ff6d6bd885
    +    }
    +    virtual_ipaddress {
    +        10.100.10.50
    +    }
    +    track_script {
    +        chk_nginx_service
    +    }
    +    notify "/usr/lib/keepalived/nginx-ha-notify"
    +}
    - change mode from '' to '0644'
    - change owner from '' to 'root'
    - change group from '' to 'root'
Recipe: nginx::default
  * service[nginx] action start (up to date)
  * service[nginx] action reload
    - reload service service[nginx]
Recipe: nginx::plus_package
  * service[keepalived] action reload (up to date)

Running handlers:
Running handlers complete
Chef Client finished, 18/50 resources updated in 07 seconds

6. chef-test-1 노드에 로그인하고 chef-client 명령을 실행하여 모든 구성을 가져옵니다.

username@chef-test-1:~$ sudo chef-client
Starting Chef Client, version 12.6.0
resolving cookbooks for run list: ["nginx"]
Synchronizing Cookbooks:
  - ohai (2.0.1)
  - build-essential (2.2.4)
  - nginx (2.7.6)
  - yum-epel (0.6.3)
  - apt (2.8.2)
  - bluepill (2.4.0)
  - runit (1.7.2)
  - rsyslog (2.1.0)
  - packagecloud (0.1.0)
  - yum (3.8.1)
Compiling Cookbooks...
...
  * template[/etc/keepalived/keepalived.conf] action create
    - create new file /etc/keepalived/keepalived.conf
    - update content in file /etc/keepalived/keepalived.conf from none to 9816fd
    --- /etc/keepalived/keepalived.conf    2016-02-07 06:34:18.117013010 -0800
    +++ /etc/keepalived/.keepalived.conf20160207-1079-sjg8xn    2016-02-07 06:34:18.117013010 -0800
    @@ -1 +1,28 @@
    +vrrp_script chk_nginx_service {
    +    script "/usr/lib/keepalived/nginx-ha-check"
    +    interval 3
    +    weight 50
    +}
    +
    +vrrp_instance VI_1 {
    +    interface eth1
    +    state MASTER
    +    priority 151
    +    virtual_router_id 51
    +    advert_int 1
    +    unicast_src_ip 10.100.10.100
    +    unicast_peer {
    +    }
    +    authentication {
    +        auth_type PASS
    +        auth_pass a0cf476cf069ea3dfa8940ff6d6bd885
    +    }
    +    virtual_ipaddress {
    +        10.100.10.50
    +    }
    +    track_script {
    +        chk_nginx_service
    +    }
    +    notify "/usr/lib/keepalived/nginx-ha-notify"
    +}
    - change mode from '' to '0644'
    - change owner from '' to 'root'
    - change group from '' to 'root'
Recipe: nginx::default
  * service[nginx] action start (up to date)
  * service[nginx] action reload
    - reload service service[nginx]
Recipe: nginx::plus_package
  * service[keepalived] action reload (up to date)

Running handlers:
Running handlers complete
Chef Client finished, 18/50 resources updated in 07 seconds

이 시점에서 keepalived.conf를 살펴보면, 템플릿이 unicast_peer 섹션에 어떤 값도 설정하지 않았음을 알 수 있습니다. 이는 Chef로 이 노드를 등록했기 때문입니다.
다음 명령은 역할 파일에서 지정된 VIP인 10.100.10.50이 이 노드의 eth1에 할당되어 Primary HA 노드로 설정되었음을 보여줍니다.

username@chef-test-1:~$ ip addr show eth1
3: eth1:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:c8:66:ee brd ff:ff:ff:ff:ff:ff
    inet 10.100.10.100/24 brd 10.100.10.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet 10.100.10.50/32 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fec8:66ee/64 scope link
       valid_lft forever preferred_lft forever

2-2. 두 번째 노드

이제 두 번째 노드를 부트스트랩하고 모든 값이 올바르게 전파되는지 확인해 봅시다.
첫 번째 노드에서 실행 목록이 예상대로 작동한다는 것을 알고 있으므로, 몇 가지 단계를 결합하여 run list와 ha_primary 속성을 직접 knife bootstrap 명령어로 설정할 수 있습니다. 이 명령은 Chef 서버에서 실행됩니다.

root@chef-server:~/chef-zero/playground# knife bootstrap -N chef-test-2 -x username --json-attributes "{"nginx": {"ha_primary": "false"}}" --sudo --run-list "role[nginx_plus_ha]" 10.100.10.102
Creating new client for chef-test-2
Creating new node for chef-test-2
Connecting to 10.100.10.102
username@10.100.10.102's password:
10.100.10.102 sudo: unable to resolve host chef-test
10.100.10.102 knife sudo password:
Enter your password:
10.100.10.102
10.100.10.102 -----> Existing Chef installation detected
10.100.10.102 Starting first Chef Client run...
10.100.10.102 Starting Chef Client, version 12.6.0
10.100.10.102 resolving cookbooks for run list: ["nginx"]
10.100.10.102 Synchronizing Cookbooks:
10.100.10.102   - bluepill (2.4.0)
10.100.10.102   - apt (2.8.2)
10.100.10.102   - build-essential (2.2.4)
10.100.10.102   - ohai (2.0.1)
10.100.10.102   - nginx (2.7.6)
10.100.10.102   - runit (1.7.2)
10.100.10.102   - yum-epel (0.6.3)
10.100.10.102   - rsyslog (2.1.0)
10.100.10.102   - packagecloud (0.1.0)
10.100.10.102   - yum (3.8.1)
10.100.10.102 Compiling Cookbooks...
...
10.100.10.102   * service[keepalived] action enable (up to date)
10.100.10.102   * template[/etc/keepalived/keepalived.conf] action create
10.100.10.102     - create new file /etc/keepalived/keepalived.conf
10.100.10.102     - update content in file /etc/keepalived/keepalived.conf from none to a9363c
10.100.10.102     --- /etc/keepalived/keepalived.conf    2016-02-07 06:45:10.529976825 -0800
10.100.10.102     +++ /etc/keepalived/.keepalived.conf20160207-11317-1env6hu    2016-02-07 06:45:10.529976825 -0800
10.100.10.102     @@ -1 +1,29 @@
10.100.10.102     +vrrp_script chk_nginx_service {
10.100.10.102     +    script "/usr/lib/keepalived/nginx-ha-check"
10.100.10.102     +    interval 3
10.100.10.102     +    weight 50
10.100.10.102     +}
10.100.10.102     +
10.100.10.102     +vrrp_instance VI_1 {
10.100.10.102     +    interface eth1
10.100.10.102     +    state BACKUP
10.100.10.102     +    priority 150
10.100.10.102     +    virtual_router_id 51
10.100.10.102     +    advert_int 1
10.100.10.102     +    unicast_src_ip 10.100.10.102
10.100.10.102     +    unicast_peer {
10.100.10.102     +          10.100.10.100
10.100.10.102     +    }
10.100.10.102     +    authentication {
10.100.10.102     +        auth_type PASS
10.100.10.102     +        auth_pass a0cf476cf069ea3dfa8940ff6d6bd885
10.100.10.102     +    }
10.100.10.102     +    virtual_ipaddress {
10.100.10.102     +        10.100.10.50
10.100.10.102     +    }
10.100.10.102     +    track_script {
10.100.10.102     +        chk_nginx_service
10.100.10.102     +    }
10.100.10.102     +    notify "/usr/lib/keepalived/nginx-ha-notify"
10.100.10.102     +}
10.100.10.102     - change mode from '' to '0644'
10.100.10.102     - change owner from '' to 'root'
10.100.10.102     - change group from '' to 'root'
10.100.10.102 Recipe: nginx::default
10.100.10.102   * service[nginx] action start (up to date)
10.100.10.102   * service[nginx] action reload
10.100.10.102     - reload service service[nginx]
10.100.10.102 Recipe: nginx::plus_package
10.100.10.102   * service[keepalived] action reload
10.100.10.102     - reload service service[keepalived]
10.100.10.102
10.100.10.102 Running handlers:
10.100.10.102 Running handlers complete
10.100.10.102 Chef Client finished, 18/50 resources updated in 10 seconds

이 시점에서 keepalived.conf를 살펴보면 unicast_peer 섹션에 피어가 설정되어 있는 것을 볼 수 있습니다.
그러나 다음 명령을 실행하면 chef-test-2가 보조 노드로 할당되어야 하는 VIP (10.100.10.50)도 할당받는 것을 확인할 수 있습니다. 이는 아직 chef-test-1의 구성을 업데이트하여 keepalived가 보조 노드를 인식할 수 있도록 하지 않았기 때문입니다.

username@chef-test-2:~$ ip addr show eth1 3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:6d:d9:64 brd ff:ff:ff:ff:ff:ff inet 10.100.10.102/24 brd 10.100.10.255 scope global eth1 valid_lft forever preferred_lft forever inet 10.100.10.50/32 scope global eth1 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe6d:d964/64 scope link valid_lft forever preferred_lft forever

2-3. 노드 동기화

chef-test-1에서 chef-test-2와 그 IP 주소를 keepalived가 인식하도록 하려면 chef-test-1에서 chef-client 명령을 다시 실행합니다.

username@chef-test-1:~$ sudo chef-client
Starting Chef Client, version 12.6.0
resolving cookbooks for run list: ["nginx"]
Synchronizing Cookbooks:
  - ohai (2.0.1)
  - build-essential (2.2.4)
  - nginx (2.7.6)
  - yum-epel (0.6.3)
  - apt (2.8.2)
  - bluepill (2.4.0)
  - runit (1.7.2)
  - rsyslog (2.1.0)
  - packagecloud (0.1.0)
  - yum (3.8.1)
Compiling Cookbooks...
...
  * template[/etc/keepalived/keepalived.conf] action create
    - update content in file /etc/keepalived/keepalived.conf from 9816fd to af7ce0
    --- /etc/keepalived/keepalived.conf    2016-02-07 06:34:18.117013010 -0800
    +++ /etc/keepalived/.keepalived.conf20160207-3369-73qgm3    2016-02-07 06:53:04.593013010 -0800
    @@ -12,6 +12,7 @@
         advert_int 1
         unicast_src_ip 10.100.10.100
         unicast_peer {
    +          10.100.10.102
         }
         authentication {
             auth_type PASS
Recipe: nginx::default
  * service[nginx] action start (up to date)
  * service[nginx] action reload
    - reload service service[nginx]
Recipe: nginx::plus_package
  * service[keepalived] action reload (up to date)

Running handlers:
Running handlers complete
Chef Client finished, 2/47 resources updated in 05 seconds

chef-test-1에는 여전히 VIP가 할당되어 있는 것을 확인합니다.

username@chef-test-1:~$ ip addr show eth1
3: eth1:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:c8:66:ee brd ff:ff:ff:ff:ff:ff
    inet 10.100.10.100/24 brd 10.100.10.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet 10.100.10.50/32 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fec8:66ee/64 scope link
       valid_lft forever preferred_lft forever

그리고 보조 노드인 chef-test-2에는 이제 물리적 IP 주소만 할당되어 있습니다.

username@chef-test-2:~$ ip addr show eth1
3: eth1:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:6d:d9:64 brd ff:ff:ff:ff:ff:ff
    inet 10.100.10.102/24 brd 10.100.10.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe6d:d964/64 scope link
       valid_lft forever preferred_lft forever

이 시점에서 keepalived 장애 조치를 테스트하려면 Primary 노드에서 NGINX Plus 서비스를 중지하세요.

3. Host Name(호스트명)을 기반으로 Chef 기본 노드 설정

호스트의 일반적인 역할 (Primary 또는 Secondary)을 호스트명에 표시하면 HA 설정을 더 자동화할 수 있습니다.
그런 다음 노드 정의 파일에서 ha_primary 속성을 설정하는 대신 (첫 번째 노드 설정의 3 단계에서처럼) Chef가 호스트명을 기반으로 속성을 설정하도록 할 수 있습니다.
이는 Chef가 레시피 내에서 변수를 설정할 수 있는 Chef의 유연성을 활용하는 것입니다.
이를 위해 Chef 서버가 인식하는 속성 및 멤버십 정보를 검사하기 위해 약간 수정된 루비 코드를 사용합니다.
예를 들어, NGINX Cookbook 수정의 1 단계에서는 plus_package 레시피에 코드를 추가하여 eth1의 IP 주소를 조회했습니다.

여기서는 호스트명을 검사하고 if 문을 추가하여 ha_primary를 적절하게 설정하는 코드를 더 확장합니다. 호스트명에 primary 또는 standby라는 단어가 있는지 여부에 의존합니다. origip 변수에 값을 할당하는 plus_package.rb의 코드 뒤에 놓으십시오.

  # ...

  origip = "#{node[:network][:interfaces][:eth1][:addresses].detect{|k,v| v[:family] == 'inet'}.first}"
  search(:node, "role:nginx_plus_ha AND enable_ha_mode:#{node.nginx.enable_ha_mode} NOT name:#{node.name}") do |nodes|
    nodes["network"]["interfaces"]["eth1"]["addresses"].each_pair do |address,value|
      ha_pair_ips << address if value.has_key?("broadcast")
    end
  end

  if node['name'].include? "primary"
    ha_primary = "true"
  elsif    node['name'].include? "standby"
    ha_primary = "false"
  end

  package 'nginx-ha-keepalived' do
    action :install
  end

  # ...

물론 primarystandby 외에도 lb1lb2와 같은 호스트명의 용어와 일치시킬 수 있습니다. 또한 이 논리를 nginx_plus_keepalived.conf.erb 템플릿 파일에 넣을 수도 있지만, 이는 이식성이 떨어집니다.

4. 요약

해당 포스트에서는 Chef 검색 기능과 Cookbook 레시피에서 Ruby 코드를 사용하여 가능한 몇 가지 더 복잡한 예제를 다루었습니다. 속성과 검색 기능을 사용하여 이식 가능하고 유연한 Chef 구성을 구축하는 방법은 다양한 방법으로 적용할 수 있습니다.

NGINX Plus HA 솔루션을 Chef와 함께 직접 사용해보세요. 오늘부터 무료 30일 체험을 시작하거나 사용 사례에 대해 논의하기 위해 NGINX STORE에게 연락하세요.

아래 뉴스레터를 구독하고 NGINX와 NGINX STORE의 최신 정보들을 빠르게 전달 받아보세요.