2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Proxmox VEに敷設したプライベートネットワーク上のVMにリバプロでアクセスしてみる

Posted at

こんにちは。
株式会社クラスアクト インフラストラクチャ事業部の大塚と申します。
前回、Proxmox VEでOVSを使ってインターネットにつながっていないプライベートネットワークを敷設し、ついでにVMを立ち上げてみました。

今回はこの続きでnginx VMを立ち上げて、プライベートネットワーク上のVMにリバプロ経由でアクセスしてみようと思います。

構築ゴール

以前挙げた記事にも掲載しておりますが、以下がイメージとなります。
nginx VMには2つのNICを搭載し、片方をインターネットにつながっているネットワークに、もう一方をプライベートネットワークに繋げています。

PCからnginx VMに対してWebブラウザで8000ポートを指定してアクセスをすると、プライベートネットワーク上に存在するapache VMにリダイレクトされるようにしたいと思います。
VMのOSは双方ともubuntu22.04を使用しております。

68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f333231393338352f64653962363632302d386264352d336661322d636238352d31376530306635396235613.png

構築

Proxmox VE上でVMを立ち上げる手順は、いつも通りのため省略。
nginx VMからデフォルトゲートウェイ及びapache VMに対して疎通をとれることを確認します。
問題なさそうですね。

root@nginx:~# ping 192.168.2.254 
PING 192.168.2.254 (192.168.2.254) 56(84) bytes of data. 
64 bytes from 192.168.2.254: icmp_seq=1 ttl=64 time=0.967 ms 
64 bytes from 192.168.2.254: icmp_seq=2 ttl=64 time=0.741 ms 
64 bytes from 192.168.2.254: icmp_seq=3 ttl=64 time=0.625 ms 
64 bytes from 192.168.2.254: icmp_seq=4 ttl=64 time=0.728 ms 
64 bytes from 192.168.2.254: icmp_seq=5 ttl=64 time=0.728 ms 
^C 
--- 192.168.2.254 ping statistics --- 
5 packets transmitted, 5 received, 0% packet loss, time 4056ms 
rtt min/avg/max/mdev = 0.625/0.757/0.967/0.112 ms

root@nginx:~# ping 192.168.200.10 
PING 192.168.200.10 (192.168.200.10) 56(84) bytes of data. 
64 bytes from 192.168.200.10: icmp_seq=1 ttl=64 time=0.501 ms 
64 bytes from 192.168.200.10: icmp_seq=2 ttl=64 time=1.33 ms 
64 bytes from 192.168.200.10: icmp_seq=3 ttl=64 time=0.433 ms 
64 bytes from 192.168.200.10: icmp_seq=4 ttl=64 time=0.602 ms 
64 bytes from 192.168.200.10: icmp_seq=5 ttl=64 time=0.423 ms 
^C 
--- 192.168.200.10 ping statistics --- 
5 packets transmitted, 5 received, 0% packet loss, time 4071ms 
rtt min/avg/max/mdev = 0.423/0.658/1.332/0.342 ms

nginx VMからapache VMに対してssh(所謂apache VMへの踏み台アクセス)も問題なくできました。
※/etc/hostsに名前解決用の設定を入れているのでホスト名を指定してアクセスできていますが、IPアドレスでもできます。

root@nginx:~# ssh test@ovs-apache2.com 
The authenticity of host 'ovs-apache2.com (192.168.200.10)' can't be established. 
ED25519 key fingerprint is SHA256:n7QLZYMhpMZ3DEo2IYxcQIDMZALl9VVi5MmuG44c8/I. 
This key is not known by any other names 
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes 
Warning: Permanently added 'ovs-apache2.com' (ED25519) to the list of known hosts. 
test@ovs-apache2.com's password: 
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-60-generic x86_64) 
 * Documentation:  https://help.ubuntu.com 
 * Management:     https://landscape.canonical.com 
 * Support:        https://ubuntu.com/advantage 
  System information as of Sat Apr  8 11:56:35 PM UTC 2023 
  System load:  0.76220703125      Processes:              102 
  Usage of /:   39.3% of 14.66GB   Users logged in:        1 
  Memory usage: 5%                 IPv4 address for ens18: 192.168.200.10 
  Swap usage:   0% 
 * Introducing Expanded Security Maintenance for Applications. 
   Receive updates to over 25,000 software packages with your 
   Ubuntu Pro subscription. Free for personal use. 
     https://ubuntu.com/pro 
Expanded Security Maintenance for Applications is not enabled. 
0 updates can be applied immediately. 
Enable ESM Apps to receive additional future security updates. 
See https://ubuntu.com/esm or run: sudo pro status 
Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings 
Last login: Sat Apr  8 03:20:57 2023 
To run a command as administrator (user "root"), use "sudo <command>". 
See "man sudo_root" for details.

test@ovs-vm:~$ ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
    inet 127.0.0.1/8 scope host lo 
       valid_lft forever preferred_lft forever 
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever 
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 9e:7d:83:04:21:fc brd ff:ff:ff:ff:ff:ff 
    altname enp0s18 
    inet 192.168.200.10/24 brd 192.168.200.255 scope global ens18 
       valid_lft forever preferred_lft forever 
    inet6 fe80::9c7d:83ff:fe04:21fc/64 scope link 
       valid_lft forever preferred_lft forever 
test@ovs-vm:~$

nginx VMにnginxをインストールしていきます。
以下のWeb記事を参考にしました。

root@nginx:~# apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \ 
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
root@nginx:~# gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg 
gpg: keyblock resource '/root/.gnupg/pubring.kbx': No such file or directory 
pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14] 
      573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 
uid                      nginx signing key <signing-key@nginx.com>
root@nginx:~# echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ 
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \ 
    | sudo tee /etc/apt/sources.list.d/nginx.list
root@nginx:~# apt update
root@nginx:~# apt install nginx

nginxインストール後、versionを確認。
startとenableを実行します

root@nginx:~# nginx -v 
nginx version: nginx/1.22.1 
root@nginx:~# systemctl start nginx 
root@nginx:~# systemctl enable nginx 
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install. 
Executing: /lib/systemd/systemd-sysv-install enable nginx 
root@nginx:~# systemctl status nginx 
● nginx.service - nginx - high performance web server 
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) 
     Active: active (running) since Sat 2023-04-08 23:07:04 UTC; 14s ago 
       Docs: https://nginx.org/en/docs/ 
   Main PID: 2554 (nginx) 
      Tasks: 5 (limit: 5773) 
     Memory: 4.0M 
        CPU: 14ms 
     CGroup: /system.slice/nginx.service 
             tq2554 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf" 
             tq2555 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" 
             tq2556 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" 
             tq2557 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" 
             mq2558 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" 
Apr 08 23:07:04 nginx systemd[1]: Starting nginx - high performance web server... 
Apr 08 23:07:04 nginx systemd[1]: Started nginx - high performance web server.

nginx VMのIPアドレスを指定してWebブラウザで接続。
問題なさそうですね。
image (2).png

リバースプロキシの設定を入れていきます。
以下を参考にしております。

/etc/nginx/conf.d/配下にあるdefault.confからserver.confを複製。
以下のように設定しました。
serverディレクティブ?のlistenにnginxサーバが受け付けるポート番号を、server_nameに自身のIPアドレスを入力。
locationディレクティブにはリダイレクト先のサーバのIPアドレスとポートを指定しました。

root@nginx:/etc/nginx/conf.d# cat server.conf 
server { 
    listen       8000; 
    server_name  192.168.2.190; 
    location / { 
        proxy_pass http://192.168.200.10:8000/; 
    } 
}

nginxを再起動します。

root@nginx:/etc/nginx/conf.d# systemctl restart nginx

apache VMにapache2をインストールしていきます。
nginxにフォワードプロキシを設定してもよかったのですが、面倒だったので、一時的に本VMにインターネットに繋がるネットワークのNICを追加してインストールしていきます。

NICを追加する前のProxmox VE上のVMのHW構成は以下となっていました。
image (3).png

NICを追加した後は以下となっております。
image (4).png

VM上で追加したNICにIPアドレスを設定していきます。
併せてipコマンドで設定が反映されているかを確認していきます。
IPアドレスを反映させるためにnetplanを使っています。

root@ovs-vm:~# cd /etc/netplan 
root@ovs-vm:/etc/netplan# ls -ltr 
total 4 
-rw-r--r-- 1 root root 279 Apr  8 02:53 00-installer-config.yaml 
root@ovs-vm:/etc/netplan# vi 00-installer-config.yaml
root@ovs-vm:/etc/netplan# cat 00-installer-config.yaml 
# This is the network config written by 'subiquity' 
network: 
  ethernets: 
    ens18: 
      addresses: 
      - 192.168.200.10/24 
      nameservers: 
        addresses: 
        - 8.8.8.8 
        search: [] 
      routes: 
      - to: default 
        via: 192.168.200.254 
    ens19: 
      addresses: 
      - 192.168.2.192/24 
  version: 2
root@ovs-vm:/etc/netplan# netplan apply 
root@ovs-vm:/etc/netplan# ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
    inet 127.0.0.1/8 scope host lo 
       valid_lft forever preferred_lft forever 
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever 
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 9e:7d:83:04:21:fc brd ff:ff:ff:ff:ff:ff 
    altname enp0s18 
    inet 192.168.200.10/24 brd 192.168.200.255 scope global ens18 
       valid_lft forever preferred_lft forever 
    inet6 fe80::9c7d:83ff:fe04:21fc/64 scope link 
       valid_lft forever preferred_lft forever 
3: ens19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 3a:40:fa:91:39:67 brd ff:ff:ff:ff:ff:ff 
    altname enp0s19 
    inet 192.168.2.192/24 brd 192.168.2.255 scope global ens19 
       valid_lft forever preferred_lft forever 
    inet6 fe80::3840:faff:fe91:3967/64 scope link 
       valid_lft forever preferred_lft forever

デフォルトゲートウェイを修正し、名前解決できることを確認します。

root@ovs-vm:~# ip route show 
default via 192.168.200.254 dev ens18 proto static 
192.168.2.0/24 dev ens19 proto kernel scope link src 192.168.2.192 
192.168.200.0/24 dev ens18 proto kernel scope link src 192.168.200.10
root@ovs-vm:~# ip route del default 
root@ovs-vm:~# ip route show 
192.168.2.0/24 dev ens19 proto kernel scope link src 192.168.2.192 
192.168.200.0/24 dev ens18 proto kernel scope link src 192.168.200.10 
root@ovs-vm:~# ip route add default via 192.168.2.254 dev ens19 
root@ovs-vm:~# ip route show 
default via 192.168.2.254 dev ens19 
192.168.2.0/24 dev ens19 proto kernel scope link src 192.168.2.192 
192.168.200.0/24 dev ens18 proto kernel scope link src 192.168.200.10 

root@ovs-vm:~# ping google.com 
PING google.com (142.251.42.206) 56(84) bytes of data. 
64 bytes from nrt12s47-in-f14.1e100.net (142.251.42.206): icmp_seq=1 ttl=117 time=4.01 ms 
64 bytes from nrt12s47-in-f14.1e100.net (142.251.42.206): icmp_seq=2 ttl=117 time=3.32 ms 
64 bytes from nrt12s47-in-f14.1e100.net (142.251.42.206): icmp_seq=3 ttl=117 time=3.41 ms 
64 bytes from nrt12s47-in-f14.1e100.net (142.251.42.206): icmp_seq=4 ttl=117 time=3.68 ms 
64 bytes from nrt12s47-in-f14.1e100.net (142.251.42.206): icmp_seq=5 ttl=117 time=3.11 ms 
^C 
--- google.com ping statistics --- 
5 packets transmitted, 5 received, 0% packet loss, time 3996ms 
rtt min/avg/max/mdev = 3.114/3.506/4.013/0.311 ms

apache2のインストールしていきます。

root@ovs-vm:~# apt update
root@ovs-vm:~# apt install -y apache2
root@ovs-vm:~# systemctl start apache2 
root@ovs-vm:~# systemctl enable apache2 
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install. 
Executing: /lib/systemd/systemd-sysv-install enable apache2 
root@ovs-vm:~# systemctl status apache2 
● apache2.service - The Apache HTTP Server 
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) 
     Active: active (running) since Sun 2023-04-09 00:19:55 UTC; 31s ago 
       Docs: https://httpd.apache.org/docs/2.4/ 
   Main PID: 2527 (apache2) 
      Tasks: 55 (limit: 5777) 
     Memory: 5.1M 
        CPU: 42ms 
     CGroup: /system.slice/apache2.service 
             tq2527 /usr/sbin/apache2 -k start 
             tq2528 /usr/sbin/apache2 -k start 
             mq2529 /usr/sbin/apache2 -k start 
Apr 09 00:19:55 ovs-vm systemd[1]: Starting The Apache HTTP Server... 
Apr 09 00:19:55 ovs-vm apachectl[2526]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppres> 
Apr 09 00:19:55 ovs-vm systemd[1]: Started The Apache HTTP Server.

apache2の待ち受けポートを80から8000ポートに変更していきます。

root@ovs-vm:~# cd /etc/apache2 
root@ovs-vm:/etc/apache2# cp -p ports.conf ports.conf.org 
root@ovs-vm:/etc/apache2# vi ports.conf 
root@ovs-vm:/etc/apache2# diff ports.conf ports.conf.org 
5,6c5 
< #Listen 80 
< Listen 8000 
--- 
> Listen 80 
root@ovs-vm:/etc/apache2# systemctl restart apache2

apache2のインストールが終わったため、追加したNICを削除して外部ネットワークから遮断します。
image (5).png

ipコマンドの出力結果を確認します。

root@ovs-vm:/etc/apache2# ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
    inet 127.0.0.1/8 scope host lo 
       valid_lft forever preferred_lft forever 
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever 
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 9e:7d:83:04:21:fc brd ff:ff:ff:ff:ff:ff 
    altname enp0s18 
    inet 192.168.200.10/24 brd 192.168.200.255 scope global ens18 
       valid_lft forever preferred_lft forever 
    inet6 fe80::9c7d:83ff:fe04:21fc/64 scope link 
       valid_lft forever preferred_lft forever

この状態でnginx VMに対して8000ポートを指定してアクセスします。
するとapache2 VMの8000ポートにリダイレクトされ、apacheのWebページが表示されると思います。

image (6).png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?