AWS VPC상에서 인프라를 운영할때 웹서버와 같은 외부접속이 가능해야 하는 서버들은 EIP를 붙여 외부에서 접속을 할 수 있지만,
데이터베이스와 같은 자원은 인터넷 망에서 접근되지 않게하여 보안을 향상 시킬 수 있다.
하지만 관리자 또는 개발자는 여러가지 목적으로 접속을 해야 하지만 EIP가 없이는 접근이 쉽지 않다.
이런 부분을 해소하기 위해 AWS VPC에서는 VPN Connection을 제공하지만 Connection별 시간당 접속 요금과 network traffic 요금이 별도로 청구되기 때문에 관리 목적이라면 OpenVPN을 이용하여 간단하게 접속 할 수 있게 구성이 가능하다.
AWS VPC 구성
Public Subnet : 웹/API등 외부 접속이 불가피한 서버들이 사용하는 서브넷으로 각 인스턴스는 EIP를 부여해 외부 통신이 가능하게 설정
IP 대역 : 10.1.1.0/24
Private Subnet : 데이터베이스서버등 외부 접속이 불필요한 서버들이 사용하는 서브넷으로 EIP를 부여하지 않음. Outbount 통신은 NAT Gateway를 이용함.
IP 대역 : 10.1.2.0/24
관리용 Subnet : OpenVPN이 사용할 서브넷으로 외부 통신이 가능한 서브넷.
IP 대역 : 10.1.3.0/24
OpenVPN 서버 설치를 위한 환경 설정
관리용 Subnet에 VPN용으로 사용 할 우분투(버전은 14.04로 하겠다) 인스턴스를 생성하고 환경 설정을 한다.
패키지 업데이트
apt-get update && apt-get upgrade
openvpn, easy-rsa 패키지 설치
apt-get install openvpn easy-rsa
vpn 서버 설정 예제 파일을 /etc/openvpn 디렉토리로 복사
cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/ cd /etc/openvpn/ gunzip server.conf.gz
server.conf 파일 수정
# Diffie hellman parameters. # Generate your own with: # openssl dhparam -out dh1024.pem 1024 # Substitute 2048 for 1024 if you are using # 2048 bit keys. dh dh2048.pem
server, client 키를 만들때 RSA key의 길이를 두배로 늘려주기 위해 dh1024.pem을 dh2048.pem으로 수정.
# If enabled, this directive will configure # all clients to redirect their default # network gateway through the VPN, causing # all IP traffic such as web browsing and # and DNS lookups to go through the VPN # (The OpenVPN server machine may need to NAT # or bridge the TUN/TAP interface to the internet # in order for this to work properly). push "redirect-gateway def1 bypass-dhcp"
push "redirect-gateway def1 bypass-dhcp"주석해제
# Certain Windows-specific network settings # can be pushed to clients, such as DNS # or WINS server addresses. CAVEAT: # http://openvpn.net/faq.html#dhcpcaveats # The addresses below refer to the public # DNS servers provided by opendns.com. push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220"
push "dhcp-option ... 주석 해제
# It's a good idea to reduce the OpenVPN # daemon's privileges after initialization. # # You can uncomment this out on # non-Windows systems. user nobody group nogroup
user, group 주석 해제 후 server.conf 파일을 저장하고 닫는다.
패킷 포워딩 설정
/etc/sysctl.conf 파일에 패킷포워딩 설정값 추가.
net.ipv4.ip_forward=1
방화벽 설정
ssh와 VPN연결을 위한 방화벽 설정
# ufw allow ssh # ufw allow 1194/udp
# # rules.before # # Rules that should be run before the ufw command line added rules. Custom # rules should be added to one of these chains: # ufw-before-input # ufw-before-output # ufw-before-forward # # START OPENVPN RULES # NAT table rules *nat :POSTROUTING ACCEPT [0:0] # Allow traffic from OpenVPN client to eth0 -A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADE COMMIT # END OPENVPN RULES # Don't delete these required lines, otherwise there will be errors *filter
위와 같이 # START OPENVPN RULES 부터 # END OPENVPN RULES까지 추가한다.
방화벽 활성화
# ufw enable
방화벽 상태 확인
# ufw status Status: active To Action From -- ------ ---- 22 ALLOW Anywhere 1194/udp ALLOW Anywhere 22 (v6) ALLOW Anywhere (v6) 1194/udp (v6) ALLOW Anywhere (v6)
참고 사이트 :
http://aws-labs.com/set-openvpn-server-ubuntu-14-04/