0
0

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.

alpineLinuxを用いたk8s構築(web/ap/dbサーバ)~Tomcatのイメージビルド・DeploymentPodの作成編~

Last updated at Posted at 2023-05-05

目的

Apache(httpd)のPodでは、Javaのアプリケーションに転送した。
Apacheから転送したhttp通信を受け付け、
MySQLへのDB接続を行うため、TomcatPodを構築するため。

構築準備・事前作業

イメージの作成

AlpineLinux3.17のイメージから、以下のDockerfileを用いて、構築します。

  • Dockerfile
    ※EXPOSEするポートは秘密とさせてください。
    (【★ポート番号は秘密】という記載にしています。)
FROM alpine:3.17

ENV TOMCAT_MAJOR=9 \
    TOMCAT_VERSION=9.0.24 \
    TOMCAT_HOME=/opt/tomcat \
    CATALINA_HOME=/opt/tomcat \
    CATALINA_OUT=/dev/null \
    MYSQL_JDBC_VERSION=8.0.28
 #   MYSQL_JDBC_VERSION=5.1.39 \
 #   POSTGRESQL_JDBC_VERSION=9.4.1207

RUN mkdir -p /opt && \
    apk update  --update-cache && \
    apk upgrade --update-cache && \
    apk add --update openjdk8-jre curl tar
    #curl -jksSL -o /tmp/apache-tomcat.tar.gz http://archive.apache.org/dist/tomcat/tomcat-${TOMCAT_MAJOR}/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz && \
    #tar -C /opt -xvzf /tmp/apache-tomcat.tar.gz && \

COPY ./tomcat_opt_modules /opt/

RUN ln -s /opt/apache-tomcat-${TOMCAT_VERSION} ${TOMCAT_HOME} && \
    rm -rf ${TOMCAT_HOME}/webapps/*

COPY ./libmodules/mysql-connector-java-${MYSQL_JDBC_VERSION}.jar /tmp/mysql-connector-java-${MYSQL_JDBC_VERSION}-bin.jar

    #curl -jksSL -o /tmp/mysql-connector-java.tar.gz http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-${MYSQL_JDBC_VERSION}.tar.gz && \
    #tar -C /tmp -xvzf /tmp/mysql-connector-java.tar.gz && \

RUN cp /tmp/mysql-connector-java-${MYSQL_JDBC_VERSION}-bin.jar /opt/tomcat/lib/

# RUN cp /tmp/mysql-connector-java-${MYSQL_JDBC_VERSION}/mysql-connector-java-${MYSQL_JDBC_VERSION}-bin.jar /opt/tomcat/lib/ && \
 #   curl -jksSL -o /opt/tomcat/lib/postgresql-${POSTGRESQL_JDBC_VERSION}.jar http://jdbc.postgresql.org/download/postgresql-${POSTGRESQL_JDBC_VERSION}.jar && \

COPY ./tomcat_opt_conf/* /opt/tomcat/conf/
COPY ./tomcat_opt_webapps/* /opt/tomcat/webapps/

RUN apk del curl && \
    rm -rf /tmp/* /var/cache/apk/*



EXPOSE 【★ポート番号は秘密】
ENTRYPOINT [ "/opt/tomcat/bin/catalina.sh", "run" ]
  • ここで、以下のCOPYですが、それぞれ、tomcatの設定ファイルとwarファイルをそれぞれコンテナホスト上に配置しています。
    (Dockerfileと同じカレントディレクトリに配置します。)

./tomcat_opt_modules(Tomcatモジュールの配置)

展開された/opt/tomcat配下のモジュールです。
apache-tomcat-9.0.24を公式サイトから事前ダウンロード(apache-tomcat-9.0.24.tar.gz)しています。

tomcat_opt_conf(Tomcat設定カスタマイズ)

tomcat_opt_modulesをコンテナ上の/opt/tomcat配下にコピー後、
tomcat_opt_confにあるファイルで上書きします。

  • 事前にTomcatの設定ファイル(context.xml )に関しては、設定を変更します。
[root@t_kyn029 tomcatdocker]# diff -r ./tomcat_opt_modules/apache-tomcat-9.0.24/conf/ ./tomcat_opt_conf/
./tomcat_opt_conf/ のみに存在: Catalina
diff -r ./tomcat_opt_modules/apache-tomcat-9.0.24/conf/context.xml ./tomcat_opt_conf/context.xml
26c26,29
<
---
>     <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
>             maxTotal="100" maxIdle="30" maxWaitMillis="10000"
>             username="mysql" password="【★MySQLのパスワードは秘密★】" driverClassName="com.mysql.jdbc.Driver"
>             url="jdbc:mysql://mysql-service:【★MySQLのServiceのポートは秘密★】/mysql"/>
[root@t_kyn029 tomcatdocker]#

tomcat_opt_webapps

warファイルを配置します。
ここでは、sample.warを用意しますが、必要に応じて、
maven.3.6.3-jdk-11

[root@t_kyn029 tomcatdocker]# ls -lrt tomcat_opt_webapps/*war
-rw-r--r--. 1 root root 4606  4月 15 17:16 tomcat_opt_webapps/sample.war
[root@t_kyn029 tomcatdocker]#

ファイルの構成は以下の通りです。

[root@t_kyn029 tomcatdocker]# ls -ltr ./tomcat_opt_*
./tomcat_opt_modules:
合計 0
drwxr-xr-x. 9 root root 220  4月  9 19:24 apache-tomcat-9.0.24

./tomcat_opt_conf:
合計 236
-rw-r--r--. 1 root root   2313  4月  9 20:22 jaspic-providers.xsd
-rw-r--r--. 1 root root   1149  4月  9 20:22 jaspic-providers.xml
-rw-r--r--. 1 root root   7243  4月  9 20:22 catalina.properties
-rw-r--r--. 1 root root  12873  4月  9 20:22 catalina.policy
-rw-r--r--. 1 root root   2633  4月  9 20:22 tomcat-users.xsd
-rw-r--r--. 1 root root   2164  4月  9 20:22 tomcat-users.xml
-rw-r--r--. 1 root root   7511  4月  9 20:22 server.xml
-rw-r--r--. 1 root root   4144  4月  9 20:22 logging.properties
-rw-r--r--. 1 root root 172362  4月  9 20:22 web.xml
drwxr-xr-x. 3 root root     23  4月  9 20:22 Catalina
-rw-r--r--. 1 root root   1693  4月  9 20:37 context.xml

./tomcat_opt_webapps:
合計 12
-rw-r--r--. 1 root root  659  4月  9 20:51 connection.java
-rw-r--r--. 1 root root 4606  4月 15 17:16 sample.war
[root@t_kyn029 tomcatdocker]#

実行内容

デプロイ方法を記載予定。

dockerをビルドし、イメージファイル作成

[root@t_kyn029 tomcatdocker]#  docker image build -t t_kyn_tomcat:v1.1.0 .
Sending build context to Docker daemon  557.1kBSending build context to Docker daemon  2.785MBSending build context to Docker daemon  11.14MBSending build context to Docker daemon  12.26MBSending build context to Docker daemon  13.93MBSending build context to Docker daemon  14.48MBSending build context to Docker daemon  15.04MBSending build context to Docker daemon   15.6MBSending build context to Docker daemon  17.27MBSending build context to Docker daemon  21.17MBSending build context to Docker daemon  23.95MBSending build context to Docker daemon  25.07MBSending build context to Docker daemon  26.74MBSending build context to Docker daemon  27.85MBSending build context to Docker daemon  28.41MBSending build context to Docker daemon  28.97MBSending build context to Docker daemon  29.52MBSending build context to Docker daemon  29.62MB
Step 1/12 : FROM alpine:3.17
 ---> 9ed4aefc74f6
Step 2/12 : ENV TOMCAT_MAJOR=9     TOMCAT_VERSION=9.0.24     TOMCAT_HOME=/opt/tomcat     CATALINA_HOME=/opt/tomcat     CATALINA_OUT=/dev/null     MYSQL_JDBC_VERSION=8.0.28
 ---> Running in 07233493880d
Removing intermediate container 07233493880d
 ---> 52e2b5c6929d
Step 3/12 : RUN mkdir -p /opt &&     apk update  --update-cache &&     apk upgrade --update-cache &&     apk add --update openjdk8-jre curl tar
 ---> Running in e73d5ed2f7fe
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz
v3.17.3-94-g62f9529517f [https://dl-cdn.alpinelinux.org/alpine/v3.17/main]
v3.17.3-97-g0d8fbc8bcc9 [https://dl-cdn.alpinelinux.org/alpine/v3.17/community]
OK: 17818 distinct packages available
OK: 7 MiB in 15 packages
(1/52) Installing ca-certificates (20220614-r4)
(2/52) Installing brotli-libs (1.0.9-r9)
(3/52) Installing nghttp2-libs (1.51.0-r0)
(4/52) Installing libcurl (8.0.1-r0)
(5/52) Installing curl (8.0.1-r0)
(6/52) Installing libexpat (2.5.0-r0)
(7/52) Installing libbz2 (1.0.8-r4)
(8/52) Installing libpng (1.6.38-r0)
(9/52) Installing freetype (2.12.1-r0)
(10/52) Installing fontconfig (2.14.1-r0)
(11/52) Installing encodings (1.0.6-r0)
(12/52) Installing libfontenc (1.1.6-r0)
(13/52) Installing mkfontscale (1.2.2-r1)
(14/52) Installing font-dejavu (2.37-r3)
(15/52) Installing libxau (1.0.10-r0)
(16/52) Installing libmd (1.0.4-r0)
(17/52) Installing libbsd (0.11.7-r0)
(18/52) Installing libxdmcp (1.1.4-r0)
(19/52) Installing libxcb (1.15-r0)
(20/52) Installing libx11 (1.8.4-r0)
(21/52) Installing libxcomposite (0.4.5-r1)
(22/52) Installing libxext (1.3.5-r0)
(23/52) Installing libxi (1.8-r0)
(24/52) Installing libxrender (0.9.11-r0)
(25/52) Installing libxtst (1.2.4-r0)
(26/52) Installing alsa-lib (1.2.8-r0)
(27/52) Installing libgcc (12.2.1_git20220924-r4)
(28/52) Installing giflib (5.2.1-r2)
(29/52) Installing libjpeg-turbo (2.1.4-r0)
(30/52) Installing libstdc++ (12.2.1_git20220924-r4)
(31/52) Installing openjdk8-jre-lib (8.362.09-r1)
(32/52) Installing java-common (0.5-r0)
(33/52) Installing libffi (3.4.4-r0)
(34/52) Installing p11-kit (0.24.1-r1)
(35/52) Installing libtasn1 (4.19.0-r0)
(36/52) Installing p11-kit-trust (0.24.1-r1)
(37/52) Installing java-cacerts (1.0-r1)
(38/52) Installing nspr (4.35-r0)
(39/52) Installing sqlite-libs (3.40.1-r0)
(40/52) Installing nss (3.85-r1)
(41/52) Installing krb5-conf (1.0-r2)
(42/52) Installing libcom_err (1.46.6-r0)
(43/52) Installing keyutils-libs (1.6.3-r1)
(44/52) Installing libverto (0.3.2-r1)
(45/52) Installing krb5-libs (1.20.1-r0)
(46/52) Installing lcms2 (2.14-r0)
(47/52) Installing pcsc-lite-libs (1.9.9-r0)
(48/52) Installing liblksctp (1.0.19-r1)
(49/52) Installing openjdk8-jre-base (8.362.09-r1)
(50/52) Installing openjdk8-jre (8.362.09-r1)
(51/52) Installing libacl (2.3.1-r1)
(52/52) Installing tar (1.34-r2)
Executing busybox-1.35.0-r29.trigger
Executing ca-certificates-20220614-r4.trigger
Executing fontconfig-2.14.1-r0.trigger
Executing mkfontscale-1.2.2-r1.trigger
Executing java-common-0.5-r0.trigger
OK: 109 MiB in 67 packages
Removing intermediate container e73d5ed2f7fe
 ---> 589fb64f6f33
Step 4/12 : COPY ./tomcat_opt_modules /opt/
 ---> 3c37311fe544
Step 5/12 : RUN ln -s /opt/apache-tomcat-${TOMCAT_VERSION} ${TOMCAT_HOME} &&     rm -rf ${TOMCAT_HOME}/webapps/*
 ---> Running in 5a3df47a555e
Removing intermediate container 5a3df47a555e
 ---> 7d4013cb9d84
Step 6/12 : COPY ./libmodules/mysql-connector-java-${MYSQL_JDBC_VERSION}.jar /tmp/mysql-connector-java-${MYSQL_JDBC_VERSION}-bin.jar
 ---> a74466d3631d
Step 7/12 : RUN cp /tmp/mysql-connector-java-${MYSQL_JDBC_VERSION}-bin.jar /opt/tomcat/lib/
 ---> Running in 12e21dee77ef
Removing intermediate container 12e21dee77ef
 ---> 18a92d4b0a52
Step 8/12 : COPY ./tomcat_opt_conf/* /opt/tomcat/conf/
 ---> 17863c503322
Step 9/12 : COPY ./tomcat_opt_webapps/* /opt/tomcat/webapps/
 ---> 10a76900f6ca
Step 10/12 : RUN apk del curl &&     rm -rf /tmp/* /var/cache/apk/*
 ---> Running in 9e24d8a1cb63
(1/3) Purging curl (8.0.1-r0)
(2/3) Purging libcurl (8.0.1-r0)
(3/3) Purging nghttp2-libs (1.51.0-r0)
Executing busybox-1.35.0-r29.trigger
OK: 108 MiB in 64 packages
Removing intermediate container 9e24d8a1cb63
 ---> f9dc20e08f69
Step 11/12 : EXPOSE 8080
 ---> Running in 2777653fd4b6
Removing intermediate container 2777653fd4b6
 ---> e339916cf7aa
Step 12/12 : ENTRYPOINT [ "/opt/tomcat/bin/catalina.sh", "run" ]
 ---> Running in bb45fb28bc59
Removing intermediate container bb45fb28bc59
 ---> 04440b44fa73
Successfully built 04440b44fa73
Successfully tagged t_kyn_tomcat:v1.1.0
[root@t_kyn029 tomcatdocker]# ls -lrt 

Kubectlでapplyする。

[root@t_kyn029 apachedocker]# kubectl apply -f apache-deployment.yml
service/apache-service created
deployment.apps/apache-deployment created
[root@t_kyn029 apachedocker]# 

疎通確認

Tomcat-MySQL間の疎通はToBeContinue…

事後作業

特になしですが、不要であれば削除しましよう。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?