서버 네트워크 대역폭 제한하기
서버에서 큰 용량을 가져가거나 보낼 때 대역폭 제한이 필요합니다. 제한을 안하면 네트워크 과부화 문제나 IDC 과금 문제가 발생합니다. 이번 시간에는 서버 네트워크 대역폭 제한하는 방법을 알아보도록 하겠습니다.
대역폭을 제한하는 ethtool과 tc 설정 두 가지가 많이 사용되어 설정 방법을 알아봅시다. 각 방식은 장/단점이 존재하기 때문에 장/단점을 확인하여 두 방식 중 선택해서 사용해야 합니다.
1. 대역폭
대역폭이란 데이터를 전송하기 위한 통로라고 생각하면 됩니다. 대역폭은 전송되는 데이터를 허용할 수 있는 동시접속자 수와 같은 의미입니다. 예를 들면 도로라고 생각하시면 됩니다. 고속도로의 차선이 4차선보다 8차선일때 더욱 원할하게 교통이 이루어지듯이 대역폭이 높을 수록 빠르게 서비스를 제공할 수 있습니다. 다시 말하면 대역폭은 초당 전송될 수 있는 최대량을 의미합니다.
2. ethtool/tc 차이점
ethtool, tc 설정 전에 차이점에 대해서 파악하고 원하는 방식을 선택해서 세팅합니다.
TOOL | 장점 | 단점 |
ethtool | 설정이 tc 보단 쉬움 | 트래픽 제한을 10^n으로만 설정해야한다. (10, 100, 1000 ..) |
tc | 트래픽 제한을 10^n 설정이 아닌 세밀하게 설정할 수 있다. | ethtool 보다는 설정이 약간 복잡하다. |
3. iperf3
iperf3는 네트워크 처리량 측정을 위한 툴입니다. 대역폭을 제한하기 전에 iperf3를 설치하여 대역폭 속도가 제한되는지 테스트할 것입니다. iperf3 명령어는 아래 그림과 같은 구조로 이루어 집니다. 한 서버에서 -s 옵션으로 서버를 설정하고, 다른 서버에서 -c 옵션으로 클라이언트를 설정합니다. 서버는 트래픽을 처리하기 위한 용도, 클라이언트는 트래픽을 생성하기 위한 용도로 사용됩니다.
설치하기
-
centos
# yum install iperf3
-
우분투
# sudo apt-get install iperf3
속도 테스트하기
2개의 서버가 통신을 하면서 테스트를 할 것이다. 한 서버는 server, 다른 서버는 client 로 만들어 테스트를 합니다. ethtool과 tc 세팅 전에 미리 얼마나 속도가 나오는지 확인해 봅시다.
아래 확인해보면 sender/receiver 모두 대략 930 Mbits/sec 정도가 나오는 것을 확인할 수 있습니다.
# 서버 1. server
# iperf3 -s
# 서버 2. client
# iperf3 -c <server 용 서버 1 ip>
[ ID] Interval Transfer Bandwidth
[ 5] 0.00-1.00 sec 107 MBytes 895 Mbits/sec
[ 5] 1.00-2.00 sec 111 MBytes 934 Mbits/sec
[ 5] 2.00-3.00 sec 111 MBytes 935 Mbits/sec
[ 5] 3.00-4.00 sec 111 MBytes 934 Mbits/sec
[ 5] 4.00-5.00 sec 111 MBytes 934 Mbits/sec
[ 5] 5.00-6.00 sec 111 MBytes 934 Mbits/sec
[ 5] 6.00-7.00 sec 111 MBytes 934 Mbits/sec
[ 5] 7.00-8.00 sec 111 MBytes 934 Mbits/sec
[ 5] 8.00-9.00 sec 111 MBytes 934 Mbits/sec
[ 5] 9.00-10.00 sec 111 MBytes 934 Mbits/sec
[ 5] 10.00-10.04 sec 4.42 MBytes 935 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Retr
[ 5] 0.00-10.04 sec 1.09 GBytes 932 Mbits/sec 0 sender
[ 5] 0.00-10.04 sec 1.09 GBytes 930 Mbits/sec receiver
4. 방법 1. ethtool
첫 번째 방법은 "ethtool" 명령어로 네트워크 대역폭을 변경하겠습니다. 이 방법은 매우 간단하게 설정이 가능합니다.
ifconfig - 네트워크 장치명 확인
네트워크 대역폭을 제한하려는 네트워크 장치명을 확인합니다.
# ifconfig
eno1:
inet <공인 ip> netmask ***.***.***.*** broadcast ***.***.***.***
-------------------------- 생략 ----------------------------------
ethtool [네트워크 장치명] : 현재 속도를 확인해준다.
[root@localhost www]# ethtool eno1
Settings for eno1:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: off (auto)
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
ethtool 로 원하는 속도로 변경
아래 속도는 10 배수 단위로만 설정할 수 있다. 10, 100, 1000 등 으로 원하는 속도를 입력합니다.
# ethtool -s [네트워크 장치명] speed [속도] duplex full autoneg off
아래는 10Mbs로 변경하였습니다.
# ethtool -s eno1 speed 10 duplex full autoneg off
[root@localhost www]# ethtool eno1
Settings for eno1:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: Not reported
Advertised pause frame use: No
Advertised auto-negotiation: No
Advertised FEC modes: Not reported
Speed: 10Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: off
MDI-X: off (auto)
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
iperf3 명령어로 속도 테스트
iperf3 명령어로 아래와 같이 확인할 수 있습니다.
# 서버 1. server
# iperf3 -s
# 서버 2. client
# iperf3 -c <server 용 서버 1 ip>
---------------- 생략 ----------------------
[ ID] Interval Transfer Bandwidth
[ 5] 0.00-1.00 sec 741 KBytes 6.07 Mbits/sec
[ 5] 1.00-2.00 sec 755 KBytes 6.19 Mbits/sec
[ 5] 2.00-3.00 sec 742 KBytes 6.08 Mbits/sec
[ 5] 3.00-4.00 sec 745 KBytes 6.10 Mbits/sec
[ 5] 4.00-5.00 sec 819 KBytes 6.71 Mbits/sec
[ 5] 5.00-6.00 sec 839 KBytes 6.87 Mbits/sec
[ 5] 6.00-7.00 sec 802 KBytes 6.57 Mbits/sec
[ 5] 7.00-8.00 sec 802 KBytes 6.57 Mbits/sec
[ 5] 8.00-9.00 sec 875 KBytes 7.17 Mbits/sec
[ 5] 9.00-10.00 sec 894 KBytes 7.32 Mbits/sec
[ 5] 10.00-10.05 sec 48.1 KBytes 8.54 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Retr
[ 5] 0.00-10.05 sec 9.93 MBytes 8.30 Mbits/sec 0 sender
[ 5] 0.00-10.05 sec 7.87 MBytes 6.57 Mbits/sec receiver
5. 방법 2. tc
두 번째 방법은 tc 입니다. 이는 ethtool 설정 보다는 약간 복잡합니다.
ifconfig - 네트워크 장치명 확인
네트워크 대역폭을 제한하려는 네트워크 장치명을 확인합니다.
# ifconfig
eno1:
inet <공인 ip> netmask ***.***.***.*** broadcast ***.***.***.***
-------------------------- 생략 ----------------------------------
tc 쉘 스크립트 생성하기
"네트워크_장치명_tc.sh" sh 파일을 생성해 줍니다.
# cd /
# touch eno1_tc.sh
# chmod 700 eno1_tc.sh
그 후, eno1_tc.sh 파일에 아래 내용을 입력해 줍니다.
# vi eno1_tc.sh
#!/bin/bash
#
# tc uses the following units when passed as a parameter.
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: Kilobits per second
# mbit: Megabits per second
# bps: Bytes per second
# Amounts of data can be specified in:
# kb or k: Kilobytes
# mb or m: Megabytes
# mbit: Megabits
# kbit: Kilobits
# To get the byte figure from bits, divide the number by 8 bit
#
#
# Name of the traffic control command.
TC=/sbin/tc
# The network interface we're planning on limiting bandwidth.
IF=<서버 네트워크 인터페이스명> # Interface
# Download limit (in mega bits)
DNLD=1000mbit # DOWNLOAD Limit # IN 트래픽 제한
# Upload limit (in mega bits)
UPLD=10mbit # UPLOAD Limit # OUT 트래픽 제한
# IP address of the machine we are controlling
IP=<서버 ip> # Host IP
# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
start() {
# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.
$TC qdisc add dev $IF root handle 1: htb default 30
$TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD
$TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD
$U32 match ip dst $IP/32 flowid 1:1
$U32 match ip src $IP/32 flowid 1:2
# The first line creates the root qdisc, and the next two lines
# create two child qdisc that are to be used to shape download
# and upload bandwidth.
#
# The 4th and 5th line creates the filter to match the interface.
# The 'dst' IP address is used to limit download speed, and the
# 'src' IP address is used to limit upload speed.
}
stop() {
# Stop the bandwidth shaping.
$TC qdisc del dev $IF root
}
restart() {
# Self-explanatory.
stop
sleep 1
start
}
show() {
# Display status of traffic control status.
$TC -s qdisc ls dev $IF
}
case "$1" in
start)
echo -n "Starting bandwidth shaping: "
start
echo "done"
;;
stop)
echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;
restart)
echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;
show)
echo "Bandwidth shaping status for $IF:"
show
echo ""
;;
*)
pwd=$(pwd)
echo "Usage: tc.bash {start|stop|restart|show}"
;;
esac
exit 0
tc 스크립트 실행
아래 명령어로 스크립트 파일을 실행해 줍니다.
# sh eno1_tc.sh start
Starting bandwidth shaping: done
iperf 명령어로 속도 테스트
# 서버 1. server
# iperf3 -s
# 서버 2. client
# iperf3 -c <server 용 서버 1 ip>
[ ID] Interval Transfer Bandwidth
[ 5] 0.00-1.00 sec 1.10 MBytes 9.26 Mbits/sec
[ 5] 1.00-2.00 sec 1.14 MBytes 9.56 Mbits/sec
[ 5] 2.00-3.00 sec 1.13 MBytes 9.48 Mbits/sec
[ 5] 3.00-4.00 sec 1.14 MBytes 9.56 Mbits/sec
[ 5] 4.00-5.00 sec 1.13 MBytes 9.46 Mbits/sec
[ 5] 5.00-6.00 sec 1.14 MBytes 9.56 Mbits/sec
[ 5] 6.00-7.00 sec 1.09 MBytes 9.13 Mbits/sec
[ 5] 7.00-8.00 sec 1.08 MBytes 9.02 Mbits/sec
[ 5] 8.00-9.00 sec 1.07 MBytes 8.94 Mbits/sec
[ 5] 9.00-10.00 sec 1.10 MBytes 9.21 Mbits/sec
[ 5] 10.00-10.06 sec 65.0 KBytes 9.12 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Retr
[ 5] 0.00-10.06 sec 11.8 MBytes 9.81 Mbits/sec 0 sender
[ 5] 0.00-10.06 sec 11.2 MBytes 9.32 Mbits/sec receiver