[JAVA] A story that failed when connecting to CloudSQL by running Sprint-boot with kubernetes (GKE)

TL;DR

  1. I'm using gke + CloudSQL Proxy and tried to specify the configuration file from configmap with spring-boot. cf. Connect from GKE to CloudSQL Proxy
  2. I prepared a Java API that returns the query result to the screen and hit it, but I could not connect to CloudSQL.
  3. I changed the mountPath and it worked fine.
  4. It is better to specify mountPath in a place that has nothing to do with the workdir of the container.
ex.
/deployment/config
/daemonset/config
/pod/config
etc...

Prerequisite settings

Dockerfile

FROM frolvlad/alpine-glibc:alpine-3.9

###########################################################################
# variables:
###########################################################################

ENV JAVA_VERSION=8 \
    JAVA_UPDATE=201 \
    JAVA_BUILD=09 \
    JAVA_PATH=42970487e3af4f5aa5bca3f542482c60 \
    JAVA_HOME="/usr/lib/jvm/default-jvm" \
    MYSQL_CONNECTOR_JAVA_VERSION=5.1.40

###########################################################################
# default:
###########################################################################

RUN apk add --no-cache tzdata bash dumb-init

###########################################################################
# jdk:
###########################################################################

RUN apk add --no-cache --virtual=build-dependencies wget ca-certificates unzip && \
    cd "/tmp" && \
    wget --header "Cookie: oraclelicense=accept-securebackup-cookie;" \
        "http://download.oracle.com/otn-pub/java/jdk/${JAVA_VERSION}u${JAVA_UPDATE}-b${JAVA_BUILD}/${JAVA_PATH}/jdk-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64.tar.gz" && \
    tar -xzf "jdk-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64.tar.gz" && \
    mkdir -p "/usr/lib/jvm" && \
    mv "/tmp/jdk1.${JAVA_VERSION}.0_${JAVA_UPDATE}" "/usr/lib/jvm/java-${JAVA_VERSION}-oracle" && \
    ln -s "java-${JAVA_VERSION}-oracle" "$JAVA_HOME" && \
    ln -s "$JAVA_HOME/bin/"* "/usr/bin/" && \
    rm -rf "$JAVA_HOME/"*src.zip && \
    wget --header "Cookie: oraclelicense=accept-securebackup-cookie;" \
        "http://download.oracle.com/otn-pub/java/jce/${JAVA_VERSION}/jce_policy-${JAVA_VERSION}.zip" && \
    unzip -jo -d "${JAVA_HOME}/jre/lib/security" "jce_policy-${JAVA_VERSION}.zip" && \
    rm "${JAVA_HOME}/jre/lib/security/README.txt" && \
    apk del build-dependencies && \
    rm "/tmp/"*

###########################################################################
# Install mysql client & mysql-connector-java for hivemetastore
###########################################################################

RUN apk add --no-cache --no-progress mysql-client && \
    apk add --no-cache --virtual=build-dependencies wget ca-certificates tar && \
    wget -q -O - http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-${MYSQL_CONNECTOR_JAVA_VERSION}.tar.gz | tar -xzf - -C /tmp && \
    mv /tmp/mysql-connector-java-${MYSQL_CONNECTOR_JAVA_VERSION}/mysql-connector-java-${MYSQL_CONNECTOR_JAVA_VERSION}-bin.jar ${JAVA_HOME}/lib && \
    apk del build-dependencies && \
    rm -rf /tmp/mysql-connector-java-${MYSQL_CONNECTOR_JAVA_VERSION}

ENV app_dir /opt/apps
WORKDIR ${app_dir}
COPY ./build/libs/test.jar .
COPY ./src/main/resources/log4j2.xml .
COPY ./src/main/resources/application.yml .

EXPOSE ####

COPY docker-entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["java", "-jar", "-Dserver.port=####", "-Dlog4j.configurationFile=log4j2.xml", "test.jar"]

docker-entrypoint.sh

#!/usr/bin/dumb-init /bin/bash
set -e

if [ "${1:0:1}" = '-' ]; then
    set -- java "@"
fi

exec "$@"

configmap.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: application-yml
data:
  application.yml: |+
    server:
      port: ####

    datasource:
      testCommonDb:
        protocol: jdbc:mysql
        host: 127.0.0.1
        port: 3306
        driverClassName: com.mysql.jdbc.Driver
        database: ${ENV:dev}_test
        username: ${MYSQL_USERNAME}
        password: ${MYSQL_PASSWORD}

inspection result

## NG!!!

・ ・ ・
command: ['java', '-jar', '-Dserver.port=####', '-Dlog4j.configurationFile=log4j2.xml', '-Dspring.config.location="file:/opt/apps/config/application.yml"', 'test.jar']
・ ・ ・
volumeMounts:
- name: application-yml-volume
  mountPath: "/opt/apps/config"
  readOnly: true
・ ・ ・
- name: application-yml-volume
  configMap:
    name: application-yml

## OK!
・ ・ ・
command: ['java', '-jar', '-Dserver.port=####', '-Dlog4j.configurationFile=log4j2.xml', '-Dspring.config.location="file:/deployments/config/application.yml"', 'test.jar']
・ ・ ・
volumeMounts:
- name: application-yml-volume
  mountPath: "/deployments/config"
  readOnly: true
・ ・ ・
- name: application-yml-volume
  configMap:
    name: application-yml

Ref. --I referred to the settings here. https://dzone.com/articles/configuring-spring-boot-on-kubernetes-with-configm --Spring Boot on Kubernetes: Yahoo! Zubatoku Case Study #jjug_ccc https://www.slideshare.net/techblogyahoo/spring-boot-on-kubernetes-yahoo-jjugccc

Recommended Posts

A story that failed when connecting to CloudSQL by running Sprint-boot with kubernetes (GKE)
A story that stumbled when deploying a web application created with Spring Boot to EC2
A story of connecting to a CentOS 8 server with an old Ansible
When introducing JOOQ to Spring boot, a story that was dealt with because an error occurred around Liquibase
A story when I tried to make a video by linking Processing and Resolume
A story that I was really into when I did triple DES with ruby
A story that took time to establish a connection
A story that failed using "bundle exec rubocop -a"
A memo that I was addicted to when making batch processing with Spring Boot
A story that did not work when trying to handle events in Notification Center
A story I was addicted to when getting a key that was automatically tried on MyBatis
Java: A story that made me feel uncomfortable when I was taught to compare strings with equals for no reason.
A story about trying to get along with Mockito
A story about reducing memory consumption to 1/100 with find_in_batches
A note that I had trouble when trying to use nginx with Remote-Containers of vscode
When I personally developed with Rails, it was a painful story that Rails was hit very much
A story that ended up taking a break when using the Linked List with a light feeling
A story that made it as easy as possible to check the operation when automatically creating a library update PR with Github Dependabot