Spring Boot アプリケーションをAWS EKSにデプロイ
Spring BootアプリケーションをAWS EKSにデプロイして、
LoadBalancer タイプのサービスで外部公開するまでの手順、備忘録
spring boot アプリケーションの作成
Docker で Spring Bootを参考にspring bootアプリケーションの実行可能jarファイルを作成
Docker image構築
事前にDocker実行環境を作成しておくこと
Install Docker Desktop on Windows | Docker Documentation
Dockerfileを作成し、イメージをbuild
$ cat Dockerfile FROM openjdk:8-jdk-alpine AS builder WORKDIR target/dependency ARG APPJAR=build/libs/*.jar COPY ${APPJAR} app.jar RUN jar -xf ./app.jar FROM openjdk:8-jre-alpine VOLUME /tmp ARG DEPENDENCY=target/dependency COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.demo.DemoApplication"] $ docker build -t test .
ImageをECRにPush
事前にAWS CLIで以下コマンドやeksctlが利用できる環境を作成しておくこと
eksctl の開始方法 - Amazon EKS
リポジトリを作成
aws ecr create-repository \ --repository-name test \ --image-scanning-configuration scanOnPush=true \ --region ap-northeast-1
ログイン
aws ecr get-login-password | docker login --username AWS --password-stdin xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com
イメージのタグ付け
docker tag test:latest xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/test:latest
push
docker push xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/test:latest
EKS clusterの作成
eksctlコマンドで実施
今回は、t3.microインスタンスタイプのlinuxのみのワークロードクラスターで作成
eksctl create cluster \ --name prod \ --version 1.16 \ --region ap-northeast-1 \ --nodegroup-name standard-workers \ --node-type t3.micro \ --nodes 2 --nodes-min 2 \ --nodes-max 2 \ --ssh-access \ --ssh-public-key <SSH接続時の公開鍵> \ --managed
spring boot アプリケーションのデプロイメント定義を作成
kubectl create deployment test --image= xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/test --dry-run -o=yaml > deployment.yaml
デプロイメントの適用
kubectl apply -f deployment.yaml
作成されていることを確認
$ kubectl get all NAME READY STATUS RESTARTS AGE pod/test-55cfbd7855-frrx8 1/1 Running 0 5s
外部接続のためのロードバランサー サービス定義を作成
$ cat loadbalancer.yaml apiVersion: v1 kind: Service metadata: name: nginx-service spec: type: LoadBalancer selector: app: test ports: - protocol: TCP port: 8080 targetPort: 8080
serviceを適用
kubectl create -f loadbalancer.yaml
作成されていることを確認
$ kubectl get service/nginx-service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-service LoadBalancer yy.yyy.yyy.yyy xxxxxxxxx.ap-northeast-1.elb.amazonaws.com 8080:31880/TCP 24s
アクセス!
参考
Docker で Spring Boot
Spring Boot Kubernetes
AWS CLI を使用した Amazon ECR の開始方法