Notice
Recent Posts
Recent Comments
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

ITFragile

[Ansible] 08. DB 로드밸런싱 본문

Project

[Ansible] 08. DB 로드밸런싱

경요 2023. 3. 16. 17:49

 

앞서 작성한 haproxy.cfg.j2에는 lb서버가 web을 로드밸런싱 할 수 있도록

frontend, backend 정보가 작성되어 있었다. DB도 lb 서버를 통해 백엔드로 전달될 수 있도록 설정한다.

 

1. haproxy.cfg.j2 수정

# haproxy.cfg.j2

frontend web-lb
    bind *:80
    default_backend webserver
    option forwardfor

backend webserver
    balance roundrobin
    {% for host in groups['web'] %}
    server {{ hostvars[host].inventory_hostname }} {{ hostvars[host]['ansible_facts']['enp0s8']['ipv4']['address'] }}:80 check
    {% endfor %}

frontend db-lb # db 추가
    bind *:3306
    mode tcp
    default_backend dbserver
    option forwardfor

backend dbserver
    balance roundrobin
    {% for host in groups['db'] %}
    server {{ hostvars[host].inventory_hostname }} {{ hostvars[host]['ansible_facts']['enp0s8']['ipv4']['address'] }}:3306 check
    {% endfor %}

 

2. index.php 수정

lb 서버를 통해 로드밸런싱되도록 index.php 파일 내 server_addr을 lb 서버 ip로 변경한다.

# index.php

<?php
$server_addr = "192.168.56.106"; #lb ip 주소
$user_name = "web_user";
$password = "12345";
$db_name = "webdb";
$connection = mysqli_connect($server_addr, $user_name, $password, $db_name);
$query = "SELECT * FROM web_tb";
$rst = mysqli_query($connection, $query);

if (mysqli_num_rows($rst) > 0) {
  while($i = mysqli_fetch_assoc($rst)) {
    echo "id : "  .  $i["id"]  .  " | name : "  .  $i["name"]  .   "<br>" ;
  }
}
mysqli_close($connection);
?>

 

3. 플레이북 실행

이때 플레이북은 정상적으로 실행되나, 페이지 접속시 db-s에서 db 정보를 못받아옴. curl시에도 두번 중 한번은 아무것도 출력되지 않음

 

db 초기 구성시 slave의 구성파일에 innodb-read-only 옵션을 추가해주었었음. 해당 옵션 제거한 뒤 다시 실행

[변경 전]

[변경 후]

 

4. 접속 확인

# db-s01
vim /etc/mysql/mariadb.conf.d/50-server.cnf

[변경 전]

[변경 후]

 

[lb 확인]

페이지 접속시 DB 정상적으로 로드밸런싱됨

 

 


♣ 최종 main.yaml

---
- name: Gathering Facts TASK First
  hosts: all
  tasks:
  - setup:

- name: LB Playbook
  hosts: lb
  vars_files:
    vars/lb.yaml
  tasks:
    - import_tasks: update.yaml
    - import_tasks: apt.yaml
    - import_tasks: service.yaml
    - import_tasks: service.yaml
      vars:
        - svc_name: ufw
    - import_tasks: ufw.yaml
    - import_tasks: haproxy.yaml

- name: WEB Playbook
  hosts: web
  vars_files:
    vars/web.yaml
  tasks:
    - import_tasks: update.yaml
    - import_tasks: apt.yaml
    - import_tasks: service.yaml
    - import_tasks: service.yaml
      vars:
         - svc_name: ufw
    - import_tasks: ufw.yaml
    - import_tasks: index.yaml

- name: DB Playbook
  hosts: db
  vars_files:
    vars/db.yaml
  tasks:
    - import_tasks: update.yaml
    - import_tasks: apt.yaml
    - import_tasks: service.yaml
    - import_tasks: service.yaml
      vars:
        - svc_name: ufw
    - import_tasks: ufw.yaml
    - import_tasks: dbsetting.yaml
    - import_tasks: db.yaml

- name: DB WEB Connection
  hosts: web
  tasks:
    - import_tasks: php.yaml

ansible 끝~~~~

 

'Project' 카테고리의 다른 글

[Kubernetes] 02. DB StatefulSet으로 이중화 구성  (0) 2023.03.21
[Kubernetes] 01. 작업 환경 구성  (0) 2023.03.20
[Ansible] 07. php 활성화  (0) 2023.03.16
[Ansible] 06. mysql 구성하기  (0) 2023.03.16
[Ansible] 05. DB 이중화  (0) 2023.03.16