かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その33 )( PC の IP アドレスが変更された時に修正するファイルを最小限にする )

概要

記事一覧はこちらです。

Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その32 )( Spring Boot を 2.0.6 → 2.0.8 へバージョンアップする ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • 別の記事を書いている途中に WindowsUpdate で PC を再起動したら IP アドレスが変更されたのですが、変更を反映するファイルが以下の5ファイルと多かったので最低限のファイルだけ修正すればよいようにします。
      • .env
      • docker/prometheus/prometheus.yml
      • src/main/resources/application-develop.properties
      • src/main/resources/application-product.properties
      • src/main/resources/application-unittest.properties
    • また HAProxy に stats page というものがあることに気づいたので、表示できるようにします。

参照したサイト・書籍

  1. Accessing host machine from within docker container
    https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248

    • docker container から PC で起動している Spring Boot の Web アプリケーションにアクセスする時に docker run --add-host="localhost:192.168.65.1" のように --add-host オプションを使うとホスト名でアクセスできると書かれていました。
    • https://docs.docker.com/compose/compose-file/ を見ると docker-compose の場合には、extra_hosts で定義できるようです。

目次

  1. docker-compose.yml、prometheus.yml を変更する
  2. application.properties を変更する
  3. RabbitMQ 用 HAProxy の stats page を表示する

手順

docker-compose.yml、prometheus.yml を変更する

まずは Prometheus と Grafana から。docker-compose.yml に extra_hosts を設定すると IP アドレスではなく定義したホスト名でアクセスできるようにできるので、docker-compose.yml を以下のように変更します。

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - ./docker/prometheus/storage:/prometheus
    extra_hosts:
      - "app:${HOST_IP_ADDRESS}"
  • prometheus コンテナに extra_hosts の定義を追加します。PC のホスト名は app にします。

docker/prometheus/prometheus.yml を以下のように変更します。

- job_name: 'spring-actuator'
  metrics_path: '/actuator/prometheus'
  scrape_interval: 15s
  basic_auth:
    username: actuator
    password: xxxxxxxx
  static_configs:
  # Docker で起動した Prometheus からローカルPCで起動している Spring Boot のアプリケーション
  # にアクセスする。docker-compose.yml の extra_hosts に定義した PC のホスト名 app を設定する。
  - targets: ['app:8080']
    labels:
      application: 'lending'
  • targets: ['172.31.16.1:8080']targets: ['app:8080'] に変更します。

Grafana の Data Source で URL に IP アドレスを記述していましたが、コンテナ名でアクセスできるので prometheus に変更します。

f:id:ksby:20190116072253p:plain

これで Grafana から app:8080 の Instance 名で Spring Boot の Web アプリケーションのメトリックスが見られるようになります。

f:id:ksby:20190116072848p:plain

application.properties を変更する

src/main/resources/application.properties を以下のように変更します。

host.ip.address=172.31.16.1
doma.dialect=org.seasar.doma.jdbc.dialect.PostgresDialect

management.endpoints.web.exposure.include=health,info,loggers,prometheus

#spring.autoconfigure.exclude=com.integralblue.log4jdbc.spring.Log4jdbcAutoConfiguration
spring.datasource.hikari.jdbc-url=jdbc:postgresql://localhost/ksbylending
spring.datasource.hikari.username=ksbylending_user
spring.datasource.hikari.password=xxxxxxxx
spring.datasource.hikari.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.leak-detection-threshold=60000
spring.datasource.hikari.register-mbeans=true

spring.mail.host=localhost
spring.mail.port=25

spring.redis.cluster.nodes[0]=${host.ip.address}:6379
spring.redis.cluster.nodes[1]=${host.ip.address}:6380
spring.redis.cluster.nodes[2]=${host.ip.address}:6381
spring.redis.cluster.nodes[3]=${host.ip.address}:6382
spring.redis.cluster.nodes[4]=${host.ip.address}:6383
spring.redis.cluster.nodes[5]=${host.ip.address}:6384

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=rabbitmq
spring.rabbitmq.password=12345678

spring.session.store-type=redis

spring.freemarker.cache=true
spring.freemarker.settings.number_format=computer
spring.freemarker.charset=UTF-8
spring.freemarker.enabled=false
spring.freemarker.prefer-file-system-access=false

spring.thymeleaf.mode=HTML

valueshelper.classpath.prefix=

logging.level.root=INFO
logging.level.org.seasar.doma=ERROR
  • host.ip.address=172.31.16.1 を追加します。
  • 以下の設定を追加し、application-develop.properties, application-unittest.properties, application-product.properties から削除します。
    • spring.mail. から始まる設定(2項目)
    • spring.redis. から始まる設定 (6項目)
    • spring.rabbitmq. から始まる設定 (4項目)
  • spring.redis.cluster.nodes の値の IP アドレスの部分を ${host.ip.address} に変更します。

これで IP アドレスが変更された時には .env の HOST_IP_ADDRESS と src/main/resources/application.properties の host.ip.address を変更するだけでよくなりました。

RabbitMQ 用 HAProxy の stats page を表示する

docker/rabbitmq/haproxy.cfg に listen stats の設定を記述していましたが、コピーして書いていただけで何の設定か理解していなかったので stats page を表示できるように設定していませんでした。表示すると RabbitMQ の起動状況が見えるようになるので、必要な設定をします。

まず docker/rabbitmq/haproxy.cfg の設定が少し足りなかったのと変えたい部分があったので以下のように変更します。

listen stats
    bind *:1936
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats refresh 5s
    stats uri /haproxy?stats
  • stats page を自動更新させたいので、stats refresh 5s の記述を追加し 5秒おきに自動更新されるようにします。
  • stats page であることが分かるよう URI を変更します。stats uri /stats uri /haproxy?stats にします。

次に docker-compose.yml を以下のように変更します。

  haproxy:
    image: haproxy:1.8.14-alpine
    container_name: haproxy
    volumes:
      - ./docker/rabbitmq/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
    depends_on:
      - rabbitmq1
      - rabbitmq2
      - rabbitmq3
    ports:
      - "1936:1936"
      - "5672:5672"
      - "15672:15672"
  • haproxy コンテナの設定の ports- "1936:1936" を追加します。

これで docker-compose up -d コマンドでコンテナを起動してから http://localhost:1936/haproxy?stats にアクセスすると HAProxy の stats page が表示されます。

起動直後は全ての rabbitmq が赤色ですが、

f:id:ksby:20190118003345p:plain

起動が認識されると黄色に変わって、

f:id:ksby:20190118003357p:plain

最後に緑色になります。全ての rabbitmq が緑色で表示されれば Spring Boot の Web アプリからアクセスしても問題ありません。

f:id:ksby:20190118003410p:plain

履歴

2019/01/18
初版発行。