Author Topic: ha-proxy ทำการ redirect traffic จาก port 80 -> 8080  (Read 3319 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
ha-proxy ทำการ redirect traffic จาก port 80 -> 8080
« on: เมษายน 12, 2017, 11:04:22 AM »
วันนี้มีโจทย์ว่าต้องการทำ ha-proxy ให้สามารถ redirect traffic จาก port 80 -> port 8080
โดยที่ให้คนเรียกเข้ามาจาก URL www.pkl.com ด้วย port 80 แล้วให้ redirect ไปยัง
โหนดที่เป็น tomcat server port (8080) ที่รันอยู่เป็นการทำงานแบบ redundancy กัน 2 node ก็สามารถทำได้ง่ายๆ ดังนี้ครับผม

IP Global : 103.229.xxx.yyy  -> ทำการ mapping www record ใน zone file ให้เรียบร้อยครับผม เป็น www.pkl.com เป็นต้น
IP Global : ตัวนี้ทำการ listen เป็น ip interface ของเครื่อง ubuntu 14.04 ตัวนี้
IP webserver01 192.168.205.16:8080
IP webserver02 192.168.205.17:8080

เริ่มเลยโดยการทำการลง OS : Ubuntu 14.04 เรียบร้อย
 
ก็เริ่มการ import source กันเลย
# echo deb http://archive.ubuntu.com/ubuntu trusty-backports main universe | \
      tee /etc/apt/sources.list.d/backports.list

ทำการ update package และเริ่มลง haproxy
# apt-get update
# apt-get install haproxy -t trusty-backports

ทำการ enable haproxy ให้ทำงาน
#vi /etc/default/haproxy
ENABLED=1

ทำการ restart service 1 รอบ
#/etc/init.d/haproxy restart

ทำการปรับแก้ไข configure haproxy.cnf เพื่อทำการโหลดbalance ไปยัง node ลูกทั้งสองเครื่อง
haproxy.cnf

listen webfarm 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    balance roundrobin
    option httpclose
    option forwardfor
    server webserver01 192.168.205.16:8080 check
    server webserver02 192.168.205.17:8080 check

##Reload haproxy service
sudo service haproxy restart

แล้วทดสอบเรียกเว็บ http://www.pkl.com ก็จะสามารถเรียกเว็บได้ครับผม
โดยที่เบื้องหลังการทำงานก็จะ เป็นการ load balance ระหว่าง webserver01 กับ webserver02 ตามลำดับครับผม
โดย feature พิเศษถ้าเป็น haproxy 1.5.x เป็นต้นไปจะสามารถใช้งาน SSL off load ได้ด้วยครับ
คือสามารถติดตั้ง certificate ssl ไว้ที่เครื่อง haproxy ได้เลย โดยที่ไม่ต้องติดตั้ง SSL ไว้ที่ webserver node ให้ยุ่งยากครับ
และเวลาจัดการ ssl cert ก็จัดที่เครื่อง haproxy ได้เลย : )

ทีมงาน devstack Packetlove.com