iptables で NAT 環境を構築した時の備忘録です。
■検証環境
■説明
クライアントからWebサーバへNATして繋げてみます。
クライアントはWebサーバへ接続するのに、まず「192.168.1.1」に行きそこからIPが変換されて「192.168.0.30」で繋げれるか検証してみます。
■NATルーター情報
OS : CentOS 6.0 64bit
iptables : 1.4.7-3.el6.x86_64
(1)NATルーターでMASQUERADE設定
[root@example ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -d 192.168.0.0/24 -j MASQUERADE※上記設定では、「192.168.1.1/24」からきたものは「192.168.0.0/24」宛てにNATする設定です。
(2)NATの設定を確認
[root@example ~]# iptables -t nat -nL -v Chain PREROUTING (policy ACCEPT 1 packets, 60 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 1 60 MASQUERADE all -- * * 192.168.1.0/24 192.168.0.0/24 Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
(3)IP転送の許可
[root@example ~]# vi /etc/sysctl.conf # 最終行にでも追加 net.ipv4.ip_forward = 1 [root@example ~]# sysctl -p※「sysctl.conf」に設定をしておけば、再起動でも転送許可が有効になっている。
(4)クライアントからWebサーバへアクセスし動作確認
[root@web ~]# tail -f /var/log/httpd/access_log 192.168.0.30 - - [18/Dec/2011:21:49:32 +0900] "GET /index.html HTTP/1.0" 200 78 "-" "Wget/1.12 (linux-gnu)"※クライアントでは「wget http://192.168.0.45/index.html」を実行している。
結果から、問題無くNATされて接続されています:-)
次に、例えば NAT前 と NAT後 の IP を対にしたい場合は以下のようにします。
[root@example ~]# iptables -t nat -A POSTROUTING -s 192.168.1.2/32 -d 192.168.0.0/24 -j SNAT --to 192.168.0.30
次に、逆のパターンを考えてみます。
Webサーバから逆にクライアントに NAT で接続してみます。
※ここで、役割は入れ替わります。(クライアント から Webサーバ、Webサーバ から クライアント)
■Webサーバ(元クライアント)の環境
待ち受けポートは、8080とします。
[root@example ~]# iptables -t nat -A POSTROUTING -s 0.0.0.0/0.0.0.0 -d 192.168.1.0/24 -j MASQUERADE [root@example ~]# iptables -t nat -A PREROUTING -s 0.0.0.0/0.0.0.0 -d 192.168.1.0/24 -p tcp --dport 80 -j DNAT --to 192.168.1.2:8080
こうすることで、クライアントから80ポートで接続してきても8080接続にNATるーがーが変換してWebサーバに接続できます。
[…] iptables で NAT 環境を構築してみた kubernetes 公式ページ access control Kubernetes/Web UI (Dashboard)の追加 KubernetesにDashboardをインストールして、認証なしでアクセスする […]