kubernetes（GKE）でSprint-bootを動かして、CloudSQLへ接続するときに失敗した話

# TL;DR

1. gke + CloudSQL Proxyを使っていて、spring-bootで、設定ファイルをconfigmapから指定しようとした。  
cf. [GKEからCloudSQL Proxyへ接続する](https://cloud.google.com/sql/docs/container-engine-connect?hl=ja)
2. query結果を画面に返すJava API用意して、叩いたが、CloudSQLへの接続ができなかった。
3. mountPathを変更したら、うまく動作した。
4. containerのworkdirとは全然関係ないところにmountPathを指定した方が良さげな雰囲気。

```markdown
ex.
/deployment/config
/daemonset/config
/pod/config
etc...
```

# 前提となる設定
## Dockerfile
```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
```shell
#!/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}
```

# 検証結果
```yaml
## 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.
- ここの設定を参考にした。  
https://dzone.com/articles/configuring-spring-boot-on-kubernetes-with-configm
- Spring Boot on Kubernetes : Yahoo!ズバトク事例 #jjug_ccc  
https://www.slideshare.net/techblogyahoo/spring-boot-on-kubernetes-yahoo-jjugccc