Java를 이용한 도커라이징 - TheOpenCloudEngine/uEngine-cloud GitHub Wiki

Java

1. 예제 프로젝트 다운로드

 > git clone https://github.com/jinyoung/2018-public-education.git

2. 예제 프로젝트 빌드

 > cd 2018-public-education/
 > ls
 > mvn package -B -Dmaven.test.skip=true
 > cd course/

3. Docker 이미지 생성

 > docker build -t gcr.io/[Project ID]/class-course:v1 .
 # Sending build context to Docker daemon  64.76MB
 # Step 1/5 : FROM openjdk:8u111-jdk-alpine
 # 8u111-jdk-alpine: Pulling from library/openjdk
 # 709515475419: Pull complete
 # ...
 # Successfully built 02da06a08a1b
 # Successfully tagged gcr.io/[Project ID]/class-course:v1

 > docker push gcr.io/[Project ID]/class-course:v1
 # The push refers to repository [gcr.io/[Project ID]/class-course]
 # ...
 * Container Registry 를 확인하면 등록된 것을 알 수 있다.

 > kubectl run class-course --image=gcr.io/[Project ID]/class-course:v1
 # deployment.apps "class-course" created

 > kubectl get po -l run=class-course
 # NAME                            READY     STATUS    RESTARTS   AGE
 # class-course-7bc586bbb8-7fq2q   1/1       Running   0          20s
 
 > kubectl expose deploy class-course --type="LoadBalancer" --port=8080
 # NAME           TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE
 # class-course   LoadBalancer   10.11.244.188   <pending>       8080:30729/TCP   7s

 * Pending이 완료되면 해당하는 External Ip로 접속을 해본다.

4. 오류

위의 과정을 거치면 정상적으로 접속이 되지않는다.

오류로그를 확인하여 본다.

 > kubectl get po -l run=class-course
 # NAME                            READY     STATUS    RESTARTS   AGE
 # class-course-7bc586bbb8-7fq2q   0/1       Error     5          7m
 * 상태가 Error임을 알 수 있다.

 > kubectl logs class-course-6d5fbdcdb5-nmtdg -f
 # Unable to connect to education-kafka-zookeeper:2181
 * 에러 로그를 확인하여보면, kafka가 없음을 확인 할 수 있다.

5. Helm 설치

Helm은 Kafka 설치를 위하여 진행되는 과정이다.
 > curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get | bash
 # Downloading https://kubernetes-helm.storage.googleapis.com/helm-v2.12.1-linux-amd64.tar.gz
 # Preparing to install helm and tiller into /usr/local/bin
 # helm installed into /usr/local/bin/helm
 # tiller installed into /usr/local/bin/tiller
 # Run 'helm init' to configure helm.

 > kubectl --namespace kube-system create sa tiller
 # serviceaccount "tiller" created
 
 > kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller
 # clusterrolebinding.rbac.authorization.k8s.io "tiller" created

 > helm init --service-account tiller
 # Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.
 # Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
 # To prevent this, run `helm init` with the --tiller-tls-verify flag.
 # For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm- installation
 # Happy Helming!

 > helm repo update
 # Hang tight while we grab the latest from your chart repositories...
 # ...Skip local chart repository
 # ...Successfully got an update from the "incubator" chart repository
 # ...Successfully got an update from the "stable" chart repository
 # Update Complete. ⎈ Happy Helming!⎈

6. kafka 설치

Helm을 설치하였으면, Helm을 이용하여 Kafka를 설치하는 과정이다.
 > helm repo add incubator http://storage.googleapis.com/kubernetes-charts-incubator
 
 > helm install --name education-kafka incubator/kafka
 # NAME:   education-kafka
 # LAST DEPLOYED: Thu Dec 20 15:03:51 2018
 # NAMESPACE: default
 # STATUS: DEPLOYED
 # ...
 # To create a message in the above session, simply type the message and press "enter"
 # To end the producer session try: Ctrl+C
 * Kafka 설치가 완료.

 > kubectl get svc
 # NAME                                 TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)                 AGE
 # class-course                         LoadBalancer   10.11.244.188   <External IP>   8080:30729/TCP               18m
 # education-kafka                      ClusterIP      10.11.241.112   <none>          9092/TCP                     1m
 # education-kafka-headless             ClusterIP      None            <none>          9092/TCP                     1m
 # education-kafka-zookeeper            ClusterIP      10.11.255.102   <none>          2181/TCP                     1m
 # education-kafka-zookeeper-headless   ClusterIP      None            <none>          2181/TCP,3888/TCP,2888/TCP   1m

7. 테스트

Kafka의 설치가 완료되었다면 class-course 가 정상적으로 작동하는지 확인을 해본다.
 > http <external ip>:8080/courses title="MSA 과정"  # 성공
 > http <external ip>:8080/clazzes startDate="2019-1-1"  # 성공

8. 미션

class-course가 정상적으로 작동되면 나머지 서비스를 도커라이징하여 쿠버네티스로 구동시켜본다.

  1. class-calendar 서비스 구동
  2. class-marketing 서비스 구동
해답
# class-calendar service

> cd calendar/
> docker build -t gcr.io/<Project-ID>/class-calendar:v1 .
> docker push gcr.io/<Project-ID>/class-calendar:v1
> kubectl run class-calendar --image=gcr.io/<Project-ID>/class-calendar:v1
> kubectl expose deploy class-calendar --type="LoadBalancer" --port="8088"
# class-marketing service

> cd marketing/
> docker build -t gcr.io/<Project-ID>/class-marketing:v1 .
> docker push gcr.io/<Project-ID>/class-marketing:v1
> kubectl run class-marketing --image=gcr.io/<Project-ID>/class-marketing:v1

9. 최종 테스트

나머지 서비스를 모두 정상적으로 구동하였는지 확인하는 테스트이다.
 > http 23.236.57.186:8080/courses title="SOA"

 > http 23.236.57.186:8080/clazzes course="http://23.236.57.186:8080/courses/1" startDate="2019-2-1" instructorName="장진영"
 
 * (달력 서비스가 Core domain 의 이벤트를 받았는지 확인)

 > http 35.238.50.250:8088/schedules     # 장진영의 일정이 확인되면 됨.

 * (마케팅 서비스가 Core domain 의 이벤트를 받았는지 확인)

 > kubectl logs (marketing 의 pod name) -f    # 광고메일 발송 내역이 확인됨.
⚠️ **GitHub.com Fallback** ⚠️