Cara membuat CDN Sendiri dengan BIND dan GeoIP

Cara membuat CDN Sendiri dengan BIND dan GeoIP

Apa itu CDN CDN merupakan kependekan dari Content Delivery Network. Dengan CDN, kita bisa memperpendek routing karena setiap user akan mengambil cont

Cara test / check Kecepatan hardisk melalui Linux Command Line
Cara Install LAMP di Ubuntu 14.04
Mengenal system Partisi RAID

Apa itu CDN

CDN merupakan kependekan dari Content Delivery Network. Dengan CDN, kita bisa memperpendek routing karena setiap user akan mengambil content dari server terdekat.
Dalam tutorial ini, kita akan belajar membuat GeoDNS yang akan mengelompokkan pengunjung menjadi 3 dengan 3 regional yang berbeda:
1. Pengunjung dari Indonesia
Pengunjung dari Indonesia akan kami arahkan ke server IIX di jakarta, dan ini hanya diperuntukkan untuk pengunjung dari indonesia saja, mengingat kondisi saat ini port speed internasional untuk data center IIX masih terbilang sangat lambat.
2. Pengunjung dari Asia (Singapore, Malaysia, dll)
Pengunjung asia akan kita arahkan ke server singapore. Dengan demikian mereka akan mendapatkan speed loading yang optimal dibanding dengan server IIX.
3. Pengunjung Lainnya diluar Indonesia dan Asia
Pengunjung lain akan kita arahkan ke server USA.

Install BIND9

Siapkan server DNS dengan sebuah VPS kecil (misal VPS dengan RAM 512 atau 256MB) yang sudah diinstall OS Ubuntu 14.04. kemudian install BIND 9 dengan perintah berikut:

apt-get install -y bind9

Membuat GeoDNS

Point utama dalam artikel ini adalah GeoDNS, yaitu Domain name server yang bisa mendeteksi lokasi pengunjung web. Untuk bisa mengetahui dari mana asal pengunjung, kita perlu menggunakan GeoIP. Anda bisa mendownload GeoIP di http://geoip.site/download/MaxMind/GeoIP.acl dan menyimpannya di VPS. Namun karena IP ada kemungkinan berubah atau bertambah, maka ada baiknya kita membuat script sendiri yang bisa membuat file GeoIP.acl sendiri secara berkala.

Script GeoIP Creator

Script berikut ini berfungsi untuk membuat GeoIP. Masuk ke directory BIND di /etc/bind, kemudian buat script GeoIP.sh dengan perintah nano :

cd /etc/bind
nano GeoIP.sh

Jika perintah nano belum tersedia, anda bisa install dengan command apt-get install -y nano
setelah itu isi file GeoIP.sh dengan script berikut:

#!/bin/bash
[ -f GeoIPCountryCSV.zip ] || wget -T 5 -t 1 http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
echo -n "Creating initial CBE (Country,Begin,End) CSV file..."
unzip -p GeoIPCountryCSV.zip GeoIPCountryWhois.csv | awk -F \" '{print $10","$6","$8}' > cbe0.csv
echo -ne "DONE\nSplitting CBE CSV file..."
lc0=0; lc1=$(wc -l cbe0.csv | awk '{print $1}')
while [ $lc0 -lt $lc1 ]
do
  lc0=$lc1; echo -ne "\n$lc0\t"
  awk -F , '{m = 2^32-2^int(log($3-$2+1)/log(2)); n = and(m,$3); if (n == and(m,$2)) print; else printf "%s,%u,%u\n%s,%u,%u\n",$1,$2,n-1,$1,n,$3}' cbe0.csv > cbe1.csv
  mv -f cbe1.csv cbe0.csv; lc1=$(wc -l cbe0.csv | awk '{print $1}')
  echo -ne "+$[$lc1-$lc0]\t"; [ $lc0 -lt $lc1 ] && echo -n "OK"
done
echo -ne "DONE\nGenerating BIND GeoIP.acl file..."
(for c in $(awk -F , '{print $1}' cbe0.csv | sort -u)
do
  echo "acl \"$c\" {"
  grep "^$c," cbe0.csv | awk -F , '{printf "\t%u.%u.%u.%u/%u;\n",$2/2^24%256,$2/2^16%256,$2/2^8%256,$2%256,32-int(log($3-$2+1)/log(2))}'
  echo -e "};\n"
done) > GeoIP.acl
rm -f cbe0.csv
echo "DONE"
exit 0

jadikan file GeoIP.sh executable dengan chmod:

chmod +x GeoIP.sh

jalankan GeoIP.sh:

./GeoIP.sh

Hasilnya seperti pada gambar ini:
cara membuat cdn sendiri dengan bind, membuat cdn, geoIP DNS
Anda bisa lihat isi file GeoIP.acl sebagai berikut :

acl "A1" {
        64.46.32.0/23;
        64.46.35.0/24;
        64.46.40.64/26;
        64.46.42.0/23;
        64.46.47.0/24;
        66.38.243.0/24;
        67.15.183.0/25;
        69.10.130.128/26;
        69.10.139.0/25;
        69.10.140.192/26;
...
acl "GB" {
        2.6.190.56/29;
        9.20.0.0/17;
        12.129.72.32/29;
        23.0.0.0/9;
        25.0.0.0/8;
        32.58.57.0/29;
        32.58.58.0/28;
        32.58.59.0/29;
        32.60.34.96/27;
        51.0.0.0/8;
...

A1, GB adalah kode negara pemilik IP (Country Code). Untuk memudahkan Anda, berikut adalah Country code untuk pembagian regional sesuai dengan GeoIP standard:

A1,"Anonymous Proxy"
A2,"Satellite Provider"
O1,"Other Country"
AD,"Andorra"
AE,"United Arab Emirates"
AF,"Afghanistan"
AG,"Antigua and Barbuda"
AI,"Anguilla"
AL,"Albania"
AM,"Armenia"
AO,"Angola"
AP,"Asia/Pacific Region"
AQ,"Antarctica"
AR,"Argentina"
AS,"American Samoa"
AT,"Austria"
AU,"Australia"
AW,"Aruba"
AX,"Aland Islands"
AZ,"Azerbaijan"
BA,"Bosnia and Herzegovina"
BB,"Barbados"
BD,"Bangladesh"
BE,"Belgium"
BF,"Burkina Faso"
BG,"Bulgaria"
BH,"Bahrain"
BI,"Burundi"
BJ,"Benin"
BL,"Saint Bartelemey"
BM,"Bermuda"
BN,"Brunei Darussalam"
BO,"Bolivia"
BQ,"Bonaire, Saint Eustatius and Saba"
BR,"Brazil"
BS,"Bahamas"
BT,"Bhutan"
BV,"Bouvet Island"
BW,"Botswana"
BY,"Belarus"
BZ,"Belize"
CA,"Canada"
CC,"Cocos (Keeling) Islands"
CD,"Congo, The Democratic Republic of the"
CF,"Central African Republic"
CG,"Congo"
CH,"Switzerland"
CI,"Cote d'Ivoire"
CK,"Cook Islands"
CL,"Chile"
CM,"Cameroon"
CN,"China"
CO,"Colombia"
CR,"Costa Rica"
CU,"Cuba"
CV,"Cape Verde"
CW,"Curacao"
CX,"Christmas Island"
CY,"Cyprus"
CZ,"Czech Republic"
DE,"Germany"
DJ,"Djibouti"
DK,"Denmark"
DM,"Dominica"
DO,"Dominican Republic"
DZ,"Algeria"
EC,"Ecuador"
EE,"Estonia"
EG,"Egypt"
EH,"Western Sahara"
ER,"Eritrea"
ES,"Spain"
ET,"Ethiopia"
EU,"Europe"
FI,"Finland"
FJ,"Fiji"
FK,"Falkland Islands (Malvinas)"
FM,"Micronesia, Federated States of"
FO,"Faroe Islands"
FR,"France"
GA,"Gabon"
GB,"United Kingdom"
GD,"Grenada"
GE,"Georgia"
GF,"French Guiana"
GG,"Guernsey"
GH,"Ghana"
GI,"Gibraltar"
GL,"Greenland"
GM,"Gambia"
GN,"Guinea"
GP,"Guadeloupe"
GQ,"Equatorial Guinea"
GR,"Greece"
GS,"South Georgia and the South Sandwich Islands"
GT,"Guatemala"
GU,"Guam"
GW,"Guinea-Bissau"
GY,"Guyana"
HK,"Hong Kong"
HM,"Heard Island and McDonald Islands"
HN,"Honduras"
HR,"Croatia"
HT,"Haiti"
HU,"Hungary"
ID,"Indonesia"
IE,"Ireland"
IL,"Israel"
IM,"Isle of Man"
IN,"India"
IO,"British Indian Ocean Territory"
IQ,"Iraq"
IR,"Iran, Islamic Republic of"
IS,"Iceland"
IT,"Italy"
JE,"Jersey"
JM,"Jamaica"
JO,"Jordan"
JP,"Japan"
KE,"Kenya"
KG,"Kyrgyzstan"
KH,"Cambodia"
KI,"Kiribati"
KM,"Comoros"
KN,"Saint Kitts and Nevis"
KP,"Korea, Democratic People's Republic of"
KR,"Korea, Republic of"
KW,"Kuwait"
KY,"Cayman Islands"
KZ,"Kazakhstan"
LA,"Lao People's Democratic Republic"
LB,"Lebanon"
LC,"Saint Lucia"
LI,"Liechtenstein"
LK,"Sri Lanka"
LR,"Liberia"
LS,"Lesotho"
LT,"Lithuania"
LU,"Luxembourg"
LV,"Latvia"
LY,"Libyan Arab Jamahiriya"
MA,"Morocco"
MC,"Monaco"
MD,"Moldova, Republic of"
ME,"Montenegro"
MF,"Saint Martin"
MG,"Madagascar"
MH,"Marshall Islands"
MK,"Macedonia"
ML,"Mali"
MM,"Myanmar"
MN,"Mongolia"
MO,"Macao"
MP,"Northern Mariana Islands"
MQ,"Martinique"
MR,"Mauritania"
MS,"Montserrat"
MT,"Malta"
MU,"Mauritius"
MV,"Maldives"
MW,"Malawi"
MX,"Mexico"
MY,"Malaysia"
MZ,"Mozambique"
NA,"Namibia"
NC,"New Caledonia"
NE,"Niger"
NF,"Norfolk Island"
NG,"Nigeria"
NI,"Nicaragua"
NL,"Netherlands"
NO,"Norway"
NP,"Nepal"
NR,"Nauru"
NU,"Niue"
NZ,"New Zealand"
OM,"Oman"
PA,"Panama"
PE,"Peru"
PF,"French Polynesia"
PG,"Papua New Guinea"
PH,"Philippines"
PK,"Pakistan"
PL,"Poland"
PM,"Saint Pierre and Miquelon"
PN,"Pitcairn"
PR,"Puerto Rico"
PS,"Palestinian Territory"
PT,"Portugal"
PW,"Palau"
PY,"Paraguay"
QA,"Qatar"
RE,"Reunion"
RO,"Romania"
RS,"Serbia"
RU,"Russian Federation"
RW,"Rwanda"
SA,"Saudi Arabia"
SB,"Solomon Islands"
SC,"Seychelles"
SD,"Sudan"
SE,"Sweden"
SG,"Singapore"
SH,"Saint Helena"
SI,"Slovenia"
SJ,"Svalbard and Jan Mayen"
SK,"Slovakia"
SL,"Sierra Leone"
SM,"San Marino"
SN,"Senegal"
SO,"Somalia"
SR,"Suriname"
SS,"South Sudan"
ST,"Sao Tome and Principe"
SV,"El Salvador"
SX,"Sint Maarten"
SY,"Syrian Arab Republic"
SZ,"Swaziland"
TC,"Turks and Caicos Islands"
TD,"Chad"
TF,"French Southern Territories"
TG,"Togo"
TH,"Thailand"
TJ,"Tajikistan"
TK,"Tokelau"
TL,"Timor-Leste"
TM,"Turkmenistan"
TN,"Tunisia"
TO,"Tonga"
TR,"Turkey"
TT,"Trinidad and Tobago"
TV,"Tuvalu"
TW,"Taiwan"
TZ,"Tanzania, United Republic of"
UA,"Ukraine"
UG,"Uganda"
UM,"United States Minor Outlying Islands"
US,"United States"
UY,"Uruguay"
UZ,"Uzbekistan"
VA,"Holy See (Vatican City State)"
VC,"Saint Vincent and the Grenadines"
VE,"Venezuela"
VG,"Virgin Islands, British"
VI,"Virgin Islands, U.S."
VN,"Vietnam"
VU,"Vanuatu"
WF,"Wallis and Futuna"
WS,"Samoa"
YE,"Yemen"
YT,"Mayotte"
ZA,"South Africa"
ZM,"Zambia"
ZW,"Zimbabwe"

Konfigurasi BIND9 untuk GeoDNS

untuk bisa membuat GeoDNS dengan BIND9, tambahkan baris berikut di /etc/bind/named.conf :

include "/etc/bind/GeoIP.acl";

sehingga isi dari /etc/bind/named.conf menjadi:

// This is the primary configuration file for the BIND DNS server named.
//
// Please read /usr/share/doc/bind9/README.Debian.gz for information on the
// structure of BIND configuration files in Debian, *BEFORE* you customize
// this configuration file.
//
// If you are just adding zones, please do that in /etc/bind/named.conf.local
include "/etc/bind/GeoIP.acl";
include "/etc/bind/named.conf.options";
include "/etc/bind/named.conf.local";
include "/etc/bind/named.conf.default-zones";

setelah itu edit file /etc/bind/named.conf.local

//
// Do any local configuration here
//
// Consider adding the 1918 zones here, if they are not used in your
// organization
//include "/etc/bind/zones.rfc1918";
view "asia" {
              match-clients { SG; MY; BN; HK; KP; KR; PH; TH; CN; AU; JP;};
              recursion no;
              zone "bestariweb.com" {
                    type master;
                    file "/etc/bind/zones/asia-bestariweb.db";
              };
};
view "indonesia" {
              match-clients { ID; };
              recursion no;
              zone "bestariweb.com" {
                    type master;
                    file "/etc/bind/zones/id-bestariweb.db";
              };
};
view "negara-lain" {
              match-clients { any; };
              recursion no;
              zone "bestariweb.com" {
                    type master;
                    file "/etc/bind/zones/us-bestariweb.db";
              };
};

Setelah itu, edit pula named.conf.default-zones dan tambahkan di awal baris:

view "default" {
              match-clients { any; };
              recursion no;

dan di akhir baris tambahkan penutup };
Buat zone untuk masing-masing region. Untuk menambahkan zone, silahkan baca artikel Cara setting BIND9.
setelah itu restart BIND9 dengan perintah berikut:

service bin9 restart

check hasilnya di whatsmydns.net

Silahkan edit kembali file /etc/bind/named.conf.local untuk pembagian regional yang lebih presisi, dan jangan lupa untuk restart BIND9 setiap anda selesai melakukan editing regional.
Semoga bermanfaat

COMMENTS

WORDPRESS: 4
  • comment-avatar

    ASK: Apakah Harus ada 2 vps gan?
    1. VPS untuk WP
    2. Untuk CD

  • comment-avatar

    berarti ini semua data di vps1 di sync ke vps2 dong ya?
    saat ini hanya pakai dns cluster saja

    • comment-avatar
      Admin Bestariweb 3 tahun ago

      yup.. hanya satu arah dari vps1 ke vps2 semua data di sync (foder web dan database). tujuan penggunaan dns cluster di cpanel berbeda dgn yg saya jelaskan ini karena dns cluster hanya memisahkan dns server dengan web server. sedangkan dalam artikel ini, dns dilengkapi dengan geoIP untuk mengarahkan client ke server terdekat. jumlah VPS bisa lebih dari 2.
      Syncronisasi yang lebih bagus menggunakan shared folder (untuk sync folder static) dan mysql master-master sehingga sync bisa dilakukan 2 arah.
      contoh jika kita menggunakan wordpress, maka folder yang perlu di share adalah folder wp-content.
      Artikel tentang mysql master-master bisa dibaca di blog saya yang lainnya di https://linux.tutorials.id/database/cara-setting-dan-syncronize-mysql-master-master/

  • DISQUS: 0