GW中に自宅のラボ環境を作っていたのですがOSはubuntu22.04LTSをインストールしました。
インストール時に一緒に入れるパッケージを選択するのですがmicrok8s, dockerなど今時のコンテナパッケージもプリインストールできるようになっていました。
docker環境を作ろうと思っていたのでこれは楽だと思ってdockerを選択してOSインストールを完了したのですがswarmではまりました。
プリインストールだと20.10.17でした。
masashi@docker1:~$ sudo docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION hpveqaw4f73b67m62m0xqanb3 * docker1 Ready Active Leader 20.10.17 nrbktgzusr2vg6r4d70m4l88a docker2 Ready Active 20.10.17
swarmのrouting meshはポートを公開するとシングル時と同じようにホストインターフェースに紐付いてListenするはずです。
テストとしてnginxのコンテナを上げたのですが見た感じListenしてなくて当然アクセスもNGです。
masashi@docker1:~$ sudo docker service create -p 80:80 nginx zjggriv0mn5eo1db1j7jf3nzf overall progress: 1 out of 1 tasks 1/1: running [==================================================>] verify: Service converged masashi@docker1:~$ sudo docker service ls ID NAME MODE REPLICAS IMAGE PORTS zjggriv0mn5e jovial_bose replicated 1/1 nginx:latest *:80->80/tcp 7fh4ob0007lc portainer_agent global 0/0 portainer/agent:2.18.2 yjgfmqgj8aa4 portainer_portainer replicated 0/1 portainer/portainer-ce:2.18.2 *:8000->8000/tcp, *:9000->9000/tcp, *:9443->9443/tcp masashi@docker1:~$ ss -lnt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 4096 *:2377 *:* LISTEN 0 4096 *:7946 *:*
masashi@docker1:~$ curl -I http://localhost curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused
うーむ。これはいったい。
ホストモードで公開
swarmはデフォルトだとrouting meshというインターフェース経由でswarm内のコンテナに振り分ける機能があるのですがこれが失敗してるのでhostモードで起動。
これだとうまくいきます。しかしこれはdockerの上にリバースプロキシを立てる構成の時に使うようです。
せっかくならrouting mesh使ってみたいですよね。
※ログがなかったのでvagrantで検証してた時のやつです。
vagrant@vagrant-server1:~$ sudo docker service create --name=web --mode=global --publish mode=host,target=80,published=80 nginx lte55tmex8smkb2ioiihs7lcg overall progress: 1 out of 2 tasks overall progress: 2 out of 2 tasks l4wdcpjisrtn: running [==================================================>] ncnv9sn1j9zn: running [==================================================>] verify: Service converged vagrant@vagrant-server1:~$ ss -lnt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 4096 0.0.0.0:80 0.0.0.0:* LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 4096 [::]:80 [::]:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 4096 *:2377 *:* LISTEN 0 4096 *:7946 *:* vagrant@vagrant-server1:~$ curl localhost <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> (略)
シングルでの公開
swarmじゃなくてシングル時のpublishだとうまくいきます。 こうなるとswarmが怪しいですね。
masashi@docker1:~$ sudo docker run -d -p 8080:80 --name=web2 nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx Digest: sha256:63b44e8ddb83d5dd8020327c1f40436e37a6fffd3ef2498a6204df23be6e7e94 Status: Downloaded newer image for nginx:latest 5b3877648fb9ef8ce92be13e7ea6f6e389559bf8f81bbe46f07d14bbf7383a20 masashi@docker1:~$ masashi@docker1:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5b3877648fb9 nginx "/docker-entrypoint.窶ヲ" 9 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp web2 72d51b97fbb3 nginx:latest "/docker-entrypoint.窶ヲ" 26 minutes ago Up 26 minutes 80/tcp web.2.qr7p8bi9oc2yz2zu8991er946 masashi@docker1:~$ masashi@docker1:~$ curl localhost:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
別OSで試す
swarm公式とかいろんな方のブログ等ではうまく動いているので切り分けのためにdebianではどうなるか試してみます。
vagrantは助かりますね。
vagrant@bullseye:~$ sudo docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION yhg12e1o86543uzfdttytldhu * bullseye Ready Active Leader 20.10.5+dfsg1 vagrant@bullseye:~$ sudo docker service create --name=web -p 80:80 nginx Error response from daemon: rpc error: code = InvalidArgument desc = port '80' is already in use by service 'web' (v5fgjiie5ybd6qwkf6o2qwc4a) as an ingress port vagrant@bullseye:~$ sudo docker service rm web web vagrant@bullseye:~$ sudo docker service create --name=web -p 80:80 nginx xqiuv0s6rz9q6lrhd61jm0jle overall progress: 1 out of 1 tasks 1/1: running [==================================================>] verify: Service converged vagrant@bullseye:~$ vagrant@bullseye:~$ sudo docker service ls ID NAME MODE REPLICAS IMAGE PORTS xqiuv0s6rz9q web replicated 1/1 nginx:latest *:80->80/tcp vagrant@bullseye:~$ vagrant@bullseye:~$ ss -lnt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 4096 127.0.0.1:32971 0.0.0.0:* LISTEN 0 4096 *:80 *:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 4096 *:2377 *:* LISTEN 0 4096 *:7946 *:* vagrant@bullseye:~$ vagrant@bullseye:~$ nc -vz 172.17.0.1 80 172.17.0.1: inverse host lookup failed: Unknown host (UNKNOWN) [172.17.0.1] 80 (http) open vagrant@bullseye:~$ wget --method=head --spider http://172.17.0.1 Spider mode enabled. Check if remote file exists. --2023-05-03 10:46:59-- http://172.17.0.1/ Connecting to 172.17.0.1:80... connected. HTTP request sent, awaiting response... 200 OK Length: 615 [text/html] Remote file exists and could contain further links, but recursion is disabled -- not retrieving.```
バージョンが少し違いますがdebianではうまく動いてますね。
フォーラムを探せ
同じような人いないかなと思って探したら出てきました。
networking - Ubuntu 22.04: docker: containers not accessible from outside - Ask Ubuntu
対策としては関連パッケージを削除して最新のものを入れろとありました。
考えたら当然のことなのですが最初にインストール選択出来たらそのまま使っちゃいますよね。
いろいろあって再インストール
もう一度最初からきれいなものを作ろうと思い際インストールしました。 今回は何も追加パッケージを選択せず、OSインストール後は公式に沿ってdockerもインストールしました。
Install Docker Engine on Ubuntu | Docker Documentation
そして前回の記事につながります。
これが運用しているバージョンです。23.0.5まで上がってました。
masashi@lab-docker1:~$ sudo docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION i06s83zzw3xyfjpvp8fj4jybk * lab-docker1 Ready Active Leader 23.0.5 0crn1z5vpy219bkt513ccn5mh lab-docker2 Ready Active 23.0.5
解決してよかった。
バージョン管理、確認、大事です。