วันอาทิตย์ที่ 4 สิงหาคม พ.ศ. 2562

[Linux Container] - Container Device / Proxy Device

Proxy Device ! ใช้สำหรับการ Bind Port เพื่อให้เข้าไปยัง Container ได้ โดยเริ่มแรกที่เราใช้ Container นั้น
ตอนนั้นเรายังไม่รู้จัก เราก็ใช้ iptables ไปก่อน ก็ใช้งานได้นะ แต่ยังติดที่ว่า Port 21 กับ Port 22 ใช้ไม่ได้
และ iptables ตอนนั้นเราก็ยังไม่ได้ Save Config พอเครื่อง Server Restart ก็เจอปัญหาว่ามันลืมค่า

ตอนนี้เราจะมาดูกันว่า เวลาที่เราจะเปิดให้ข้างนอกเข้ามา Container ได้โดยใช้ Proxy Device นั้นทำยังไง
และจะลองทดสอบ Port ที่ใช้ทำ SSH ด้วยว่าใช้งานผ่าน Proxy Device ได้ไหม ? ปกติ Container เข้าไม่เปิด
การใช้งาน SSH เข้าไปใน Container

การเพิ่ม Proxy Device

ข้อมูลอ้างอิงจาก : https://lxd.readthedocs.io/en/latest/containers/
# listen ให้รอรับข้อมูลทางไหน ส่วนใหญ่ก็จะเป็น 0.0.0.0:80
# connect ให้ต่อเข้ากับช่องทางไหน ก็จะเป็น 127.0.0.1:80 
# type คือ Connection Type ก็พวก TCP, UDP

lxc config device add <container-name> <device-name> proxy \
  listen=<type>:<address>:&ltport> connect=<type>:<address>:<port>

# listen จะเป็น Port ของ Host
# connect จะเป็น Port ของ Container

# คำสั่งที่ใช้ในการลบ Config ออก
lxc config device remove <container-name> <device-name>

# ตัวอย่างการใช้
lxc config device add ubuntu-80 ubuntu-device-80 proxy \
  listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80

เราจะลองสร้าง Container 2 อันโดย อันแรกจะใช้ Port 80 อีกอันใช้ Port 82 เปิดเป็นเว็บดู
# สร้าง Container
lxc launch images:ubuntu/18.04 ubuntu-80

เข้าไปใน Container
lxc exec ubuntu-80 -- bash

# ติดตั้ง Apache2 และ Curl 
apt install -y apache2 curl

# ทดสอบมันทุกอัน เสร็จแล้วออกจาก Container
curl 0.0.0.0
curl 127.0.0.1
curl localhost
exit

# ทำการเพิ่ม Proxy Device
lxc config device add ubuntu-80 ubuntu-device-80 proxy \
  listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80

เสร็จแล้วทำอีกรอบแต่เป็น ubuntu-82 แล้วลองเข้า IP ของ Host ดูทั้ง 2 Port (80, 82) ว่าเว็บขึ้นไหม
ผลที่ได้คือขึ้นทั้ง 2 ตัว

ก่อนทำการเพิ่ม Proxy Device

หลังทำการเพิ่ม Proxy Device 80 และ 82

# ตรวจสอบ Port ที่เปิดฟังอยู่ จะเห็นว่ามี :::80 และ :::82
netstat -plnt

# ลองลบ Proxy Device แล้วใช้ netstat -plnt เช็ค Port อีกรอบ 80 ต้องหายไปแล้ว
lxc config device remove ubuntu-80 ubuntu-device-80

ไม่มีความคิดเห็น:

แสดงความคิดเห็น