Oracle Fusion Middleware/Coherence

10장. Coherence Demo with K8s, Coherence Operator (From / Coherence CE Community)

수상한 김토끼 2023. 8. 24. 23:40

안녕하세요.

'수상한 김토끼' 입니다.

9장까지의 내용을 통해 Coherence Demo 적용 및 사용 방법을 확인해 보았습니다.

10장에서는 Coherence의 컨테이너 운영을 도와주는 오픈소스 도구인 Coherence Operator를 활용해 K8s(쿠버네티스)에 배포하고 운영하는 방법을 알아가는 시간을 갖도록 하겠습니다.

 

9장과 10장에서 진행에 참고한 Coherence CE Examples 문서입니다.

https://coherence.community/examples.html

 

Coherence Community: Examples

Sock Shop Our flagship example application shows you not only how easy it is to develop Coherence-based microservices with Spring, Micronaut, or Helidon, but also how to deploy them to Kubernetes using Coherence Operator, and monitor them using Prometheus,

coherence.community

 

진행에 문제가 있거나 추가적인 내용은 위 문서를 참조해 주세요.

 

이 블로그 글은 미들웨어 경험이 없으신 분들도 쉽게 따라 하실 수 있도록 쉽게 작성하는 것이 목표입니다.

설명을 보고 진행하시다가 궁금하신 내용은 댓글로 문의하시면 가능한 범위 내에서 알려 드리도록 하겠습니다.


1. Coherence Operator는?

우선 9장에서 진행한 Coherence Demo와 동일한 어플리케이션을 가지고 10장은 진행되며 어플리케이션을 docker image로 빌드하고 Coherence Operator가 설치된 K8s에 배포하는 과정을 진행하게 됨을 참고해 주세요.

 

Coherence Operator는 Coherence를 K8s에서 손쉽게 사용할 수 있도록 Cluster 구성을 지원하고 기타 모니터링 API 등을 가능하도록 구현해 주는 오픈소스 프로젝트입니다.

https://github.com/oracle/coherence-operator

 

GitHub - oracle/coherence-operator: Oracle Coherence Operator

Oracle Coherence Operator. Contribute to oracle/coherence-operator development by creating an account on GitHub.

github.com

Coherence K8s환경에 Cluster 구성으로 배포하기 위해 필요한 도구로 K8s에 단순히 배포해주는 것만으로 Coherece Cluster 구성을 가능하게 해 주는 역할을 하고 있습니다.

 

구성은 미리 정의 되어 배포되고 있는 yaml 파일을 사용하거나 helm 차트를 통해 가능하며 이번 예제에서는 Oracle helm repository를 통해 제공되고 있는 helm 차트를 활용하였습니다.

 

Coherence Operator에 대한 상세한 내용은 다음 포스팅에서 다루기로 하고 이번장에서는 9장에서 구현해 보았던 Coherence Demo Application을 K8s 환경에서 Coherence Operator를 활용하여 동일하게 구현하는 내용으로 진행해 보겠습니다.

 

※ 이번 실습은 Oracle Cloud의 K8s 지원도구인 OKE(Oracle Kubernetes Engine) 환경에서 진행되었으나 클라우드 벤더만이 제공하고 있는 일부기능(ex. LoadBalancer)이 사용되지 않았으므로 일반적인 K8s 환경에서도 적용이 가능합니다.


2. Demo 필요 환경구성

기본적으로 9장의 2번째 챕터(https://with-kami.tistory.com/1534858)에서 진행한 부분은 동일하게 진행이 필요합니다.

같은 어플리케이션을 K8s에서 클러스터로 구성하는 부분만 차이점이기 때문에 mvn 명령으로 소스 메이븐 빌드 전 과정까지 진행은 동일하게 진행 후 다음 내용을 참조해 주세요.

 

Maven 빌드를 통해 docker image를 생성해야 하기 때문에 mvn 명령은 9장과 차이가 있으며 Docker 실행이 가능한 환경이 필요합니다.

 

docker 구성은 인터넷 검색을 통해 본인에게 맞는 docker 런타임 구성 후 다음 내용을 진행해 주세요.

 

빌드를 위해 GitHub에서 프로젝트를 내려 받아 줍니다.

 Code Tab을 클릭해 Clone --> HTTPS 아래의 URL을 복사하여 Clone을 진행해 줍니다.

[opc@oke-kubectl ~]$ git clone https://github.com/coherence-community/coherence-demo.git
Cloning into 'coherence-demo'...
remote: Enumerating objects: 1119, done.
remote: Counting objects: 100% (142/142), done.
remote: Compressing objects: 100% (46/46), done.
remote: Total 1119 (delta 116), reused 99 (delta 89), pack-reused 977
Receiving objects: 100% (1119/1119), 158.12 MiB | 20.35 MiB/s, done.
Resolving deltas: 100% (580/580), done.

 

 

docker 런타임 구성이 완료되었다면 다음 명령으로 빌드를 수행해 docker image를 생성해 줍니다.

$ mvn clean install -P docker

빌드 수행결과 다음과 같이 'coherence-demo:7.0.1-SNAPSHOT' 라는 이름으로 docker image가 생성된 것을 확인할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[opc@oke-kubectl coherence-demo]$ pwd
/home/opc/coherence-demo
[opc@oke-kubectl coherence-demo]$ mvn clean install -P docker
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.oracle.coherence:coherence-demo >-----------------
[INFO] Building coherence-demo 7.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ coherence-demo ---
[INFO] Deleting /home/opc/coherence/coherence-demo/coherence-demo/target
[INFO]
[INFO] --- enforcer:1.3.1:enforce (enforce) @ coherence-demo ---
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ coherence-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 92 resources from src/main/resources to target/classes
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ coherence-demo ---
[INFO] Changes detected - recompiling the module! :source
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 26 source files with javac [debug release 17] to target/classes
[INFO] /home/opc/coherence/coherence-demo/coherence-demo/src/main/java/com/oracle/coherence/demo/application/StartMemberResource.java: /home/opc/coherence/coherence-demo/coherence-demo/src/main/java/com/oracle/coherence/demo/application/StartMemberResource.java uses or overrides a deprecated API.
[INFO] /home/opc/coherence/coherence-demo/coherence-demo/src/main/java/com/oracle/coherence/demo/application/StartMemberResource.java: Recompile with -Xlint:deprecation for details.
[INFO] /home/opc/coherence/coherence-demo/coherence-demo/src/main/java/com/oracle/coherence/demo/application/ManagementResource.java: Some input files use unchecked or unsafe operations.
[INFO] /home/opc/coherence/coherence-demo/coherence-demo/src/main/java/com/oracle/coherence/demo/application/ManagementResource.java: Recompile with -Xlint:unchecked for details.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ coherence-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/opc/coherence/coherence-demo/coherence-demo/src/test/resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ coherence-demo ---
[INFO] No sources to compile
[INFO]
[INFO] --- surefire:3.1.2:test (default-test) @ coherence-demo ---
[INFO] No tests to run.
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ coherence-demo ---
[INFO] Building jar: /home/opc/coherence/coherence-demo/coherence-demo/target/coherence-demo-7.0.1-SNAPSHOT.jar
[INFO]
[INFO] >>> source:2.1.2:jar (attach-sources) > generate-sources @ coherence-demo >>>
[INFO]
[INFO] --- enforcer:1.3.1:enforce (enforce) @ coherence-demo ---
[INFO]
[INFO] <<< source:2.1.2:jar (attach-sources) < generate-sources @ coherence-demo <<<
[INFO]
[INFO]
[INFO] --- source:2.1.2:jar (attach-sources) @ coherence-demo ---
[INFO] Building jar: /home/opc/coherence/coherence-demo/coherence-demo/target/coherence-demo-7.0.1-SNAPSHOT-sources.jar
[INFO]
[INFO] --- jib:2.3.0:dockerBuild (default) @ coherence-demo ---
[WARNING] Setting image creation time to current time; your image may not be reproducible.
[INFO]
[INFO] Containerizing application to Docker daemon as coherence-demo, coherence-demo:7.0.1-SNAPSHOT...
[WARNING] Base image 'gcr.io/distroless/java17' does not use a specific image digest - build may not be reproducible
[INFO] Using base image with digest: sha256:052076466984fd56979c15a9c3b7433262b0ad9aae55bc0c53d1da8ffdd829c3
[INFO]
[INFO] Container entrypoint set to [java, -server, -Djava.awt.headless=true-XX:+UnlockExperimentalVMOptions, -XX:+UseG1GC, -cp, /app/classpath/*:/app/libs/*, com.tangosol.net.DefaultCacheServer]
[INFO]
[INFO] Built image to Docker daemon as coherence-demo, coherence-demo:7.0.1-SNAPSHOT
[INFO] Executing tasks:
[INFO] [==============================100.0complete
[INFO]
[INFO]
[INFO] --- install:3.1.1:install (default-install) @ coherence-demo ---
[INFO] Installing /home/opc/coherence/coherence-demo/coherence-demo/pom.xml to /home/opc/.m2/repository/com/oracle/coherence/coherence-demo/7.0.1-SNAPSHOT/coherence-demo-7.0.1-SNAPSHOT.pom
[INFO] Installing /home/opc/coherence/coherence-demo/coherence-demo/target/coherence-demo-7.0.1-SNAPSHOT.jar to /home/opc/.m2/repository/com/oracle/coherence/coherence-demo/7.0.1-SNAPSHOT/coherence-demo-7.0.1-SNAPSHOT.jar
[INFO] Installing /home/opc/coherence/coherence-demo/coherence-demo/target/coherence-demo-7.0.1-SNAPSHOT-sources.jar to /home/opc/.m2/repository/com/oracle/coherence/coherence-demo/7.0.1-SNAPSHOT/coherence-demo-7.0.1-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  14.371 s
[INFO] Finished at: 2023-08-24T11:49:47Z
[INFO] ------------------------------------------------------------------------
cs

생성된 docker image는 K8s 노드에서 image에 접근할 수 있도록 container registry에 push후 배포가 가능합니다.

여기서는 docker hub에 이미지 push 하고 다음 단계를 진행해 보겠습니다.

 

우선 docker hub에 로그인을 해 줍니다.

1
2
3
4
5
6
7
8
9
[opc@oke-kubectl ~]$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: <docker-hub-계정>
Password: <docker-hub-암호>
WARNING! Your password will be stored unencrypted in /home/opc/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
 
Login Succeeded
cs

 

다음 명령으로 docker image tag를 변경 후 push를 진행 해 줍니다.

$ docker image tag coherence-demo:7.0.1-SNAPSHOT <docker-hub-계정>/coherence-demo:7.0.1-SNAPSHOT
$ git push <docker-hub-계정>/coherence-demo:7.0.1-SNAPSHOT

여기까지 Coherence Demo Application의 준비과정은 마무리되었습니다.


3. K8s Helm 환경 구성

K8s 환경에서 Coherence Operator를 실행하기 위해서 우선적으로 'Oracle Coherence Operator Helm Chart'가 필요합니다.

K8s 환경에서 helm 사용이 가능하도록 구성 후 다음 명령으로 Helm repository를 추가해 줍니다.

$ helm repo add stable https://charts.helm.sh/stable
$ helm repo add coherence https://oracle.github.io/coherence-operator/charts
$ helm repo update

정상적으로 등록되면 'helm repo update' 실행 후 다음과 같이 표시됩니다.

1
2
3
4
5
[opc@oke-kubectl ~]$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "coherence" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈Happy Helming!
cs

 

이제 Coherence Operator를 구성할 K8s 네임스페이스를 생성합니다.

예제에서는 'coherence-example'을 네임스페이스로 사용하였으며 다음 명령으로 생성할 수 있습니다.

1
2
[opc@oke-kubectl coherence-demo]$ kubectl create namespace coherence-example
namespace/coherence-example created
cs

4. Coherence Operator 설치

다음 helm 명령으로 Coherence Operator 설치가 가능합니다.

$ helm install --namespace coherence-example coherence-operator coherence/coherence-operator
1
2
3
4
5
6
7
8
9
[opc@oke-kubectl coherence-demo]$ helm install --namespace coherence-example coherence-operator coherence/coherence-operator
NAME: coherence-operator
LAST DEPLOYED: Thu Aug 24 10:25:01 2023
NAMESPACE: coherence-example
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Coherence Operator successfully installed.
cs

helm ls 명령으로 차트 생성을 확인해 볼 수 있습니다.

$ helm ls --namespace coherence-example
1
2
3
4
5
6
7
8
[opc@oke-kubectl coherence-demo]$ helm ls --namespace coherence-example
NAME                    NAMESPACE               REVISION        UPDATED                                 STATUS          CHART                        APP VERSION
coherence-operator      coherence-example       1               2023-08-24 10:25:01.095744296 +0000 UTC deployed        coherence-operator-3.2.11    3.2.11
[opc@oke-kubectl coherence-demo]$ kubectl get pods --namespace coherence-example
NAME                                  READY   STATUS    RESTARTS      AGE
coherence-operator-79fbdbf5d7-2jws6   1/1     Running   0             19s
coherence-operator-79fbdbf5d7-4xxpr   1/1     Running   1 (16s ago)   19s
coherence-operator-79fbdbf5d7-vvtds   1/1     Running   0             19s
cs

5. Coherence Cluster 설치

K8s에 Coherence Operator까지 구성이 완료되었으므로 Coherence Cluster (Demo Application) 구성이 가능합니다.

 

이 데모에서 Coherence Cluster는 2가지 역할로 구성되며 소스 내부에 'yaml/demo-cluster.yaml' 파일에 다음과 같이 설정되어 있습니다.

1. storage - 애플리케이션 데이터를 저장하는 스토리지 지원 계층

2. http - 애플리케이션을 제공하는 저장소가 비활성화된 http 서버
[opc@oke-kubectl yaml]$ cat demo-cluster.yaml
apiVersion: coherence.oracle.com/v1
kind: Coherence
metadata:
  name: primary-cluster-storage
spec:
  cluster: primary-cluster
  role: storage
  jvm:
    memory:
      heapSize: 512m
    args:
      - "-Dwith.http=false"
      - "-Dprimary.cluster=primary-cluster"
      - "-Dcoherence.distributed.threads.min=10"
  ports:
    - name: metrics
      port: 9612
      serviceMonitor:
        enabled: true
  coherence:
    cacheConfig: cache-config.xml
    metrics:
      enabled: true
  image: <docker-hub-계정>/coherence-demo:7.0.1-SNAPSHOT
  imagePullPolicy: IfNotPresent
  replicas: 2
---
apiVersion: coherence.oracle.com/v1
kind: Coherence
metadata:
  name: primary-cluster-http
spec:
  cluster: primary-cluster
  role: http
  jvm:
    memory:
      heapSize: 512m
    args:
      - "-Dprimary.cluster=primary-cluster"
      - "-Dcoherence.management.http=inherit"
      - "-Dcoherence.management.http.port=30000"
      - "-Dcoherence.distributed.threads.min=10"
  ports:
    - name: http
      port: 8080
    - name: metrics
      port: 9612
      serviceMonitor:
        enabled: true
    - name: management
      port: 30000
  coherence:
    cacheConfig: cache-config.xml
    storageEnabled: false
    metrics:
      enabled: true
      port: 9612
  image: <docker-hub-계정>/coherence-demo:7.0.1-SNAPSHOT
  imagePullPolicy: IfNotPresent
  replicas: 1

※ yaml 파일 내 image 항목을 docker hub에 업로드한 이름으로 변경해 주세요.

 

위의 yaml 파일을 사용하여 Coherence Cluster를 실행하려면 다음 명령을 사용해 줍니다.

$ kubectl create --namespace coherence-example -f yaml/demo-cluster.yaml
1
2
3
4
5
6
7
8
9
10
11
[opc@oke-kubectl coherence-demo]$ kubectl create --namespace coherence-example -f yaml/demo-cluster.yaml
coherence.coherence.oracle.com/primary-cluster-storage created
coherence.coherence.oracle.com/primary-cluster-http created
[opc@oke-kubectl coherence-demo]$ kubectl get pods --namespace coherence-example -o wide
NAME                                  READY   STATUS    RESTARTS        AGE     IP            NODE          NOMINATED NODE   READINESS GATES
coherence-operator-79fbdbf5d7-2jws6   1/1     Running   0               3m38s   10.0.10.249   10.0.10.114   <none>           <none>
coherence-operator-79fbdbf5d7-4xxpr   1/1     Running   1 (3m35s ago)   3m38s   10.0.10.157   10.0.10.184   <none>           <none>
coherence-operator-79fbdbf5d7-vvtds   1/1     Running   0               3m38s   10.0.10.227   10.0.10.139   <none>           <none>
primary-cluster-http-0                1/1     Running   0               68s     10.0.10.147   10.0.10.184   <none>           <none>
primary-cluster-storage-0             1/1     Running   0               68s     10.0.10.208   10.0.10.216   <none>           <none>
primary-cluster-storage-1             1/1     Running   0               68s     10.0.10.41    10.0.10.114   <none>           <none>
cs

Coherence Cluster에 접속하기 위해 다음 명령으로 포트 포워딩을 설정해 줍니다.

$ kubectl port-forward --namespace coherence-example primary-cluster-http-0 8080:8080 --address='0.0.0.0'

마지막에 --address='0.0.0.0' 옵션을 주지 않으면 127.0.0.1로 포트 포워딩 되어 외부에서는 접근할 수 없습니다.

저처럼 외부 서버에 kubectl 환경을 구성하신 분들은 --address='0.0.0.0' 옵션을 포함해야 서버에 접근하실 수 있습니다.

 

접속 URL - http://<서버IP>:8080/application/index.html

브라우저를 사용해서 접속 URL로 접근하시면 Coherence Demo를 확인하실 수 있습니다.

9장에서와 마찬가지로 정상적으로 demo application이 실행되는 것을 확인할 수 있지만 차이점이 있습니다.

Add Servers 항목을 선택하여 Coherence Cluster 개수를 조정하고자 하면 다음과 같은 메시지를 확인할 수 있는데요

K8s에서 구성된 Cluster 임으로 K8s에서 조정이 필요하다는 안내입니다.

'demo-cluster.yaml'파일내부의 replicas 항목 수정 혹은 다음의 kubectl scale 명령으로 서버를 추가할 수 있습니다.

$ kubectl scale --namespace coherence-example coherence/primary-cluster-storage --replicas=6
1
2
[opc@oke-kubectl yaobank]$ kubectl scale --namespace coherence-example coherence/primary-cluster-storage --replicas=6
coherence.coherence.oracle.com/primary-cluster-storage scaled
cs

서버가 정상적으로 추가된 것을 확인할 수 있습니다.

실습 종료 후 Coherence Cluster 제거는 아래 명령으로 가능합니다.

$ kubectl delete --namespace coherence-example -f yaml/demo-cluster.yaml

 

Coherence-Demo에 구현되어 확인가능한 자세한 기능들은 GitHub 문서를 참조하여 확인해 주시기 바라며

Coherence Community에서 제공되는 Demo 구성을 Coherence Operator를 활용하여 K8s에서 진행해 보았습니다.

 

어렵게 받아들여질 수도 있는 K8s 환경에서의 Coherence Data Grid를 손쉽게 구현해 보고 확인해 볼 수 있는 좋은 자료인 것 같아 소개하게 되었습니다. 관련 내용에 관심 있는 분들께 많은 도움이 되었으면 좋겠습니다.


블로그 진행내용을 따라해 보실 분들을 위해 영상으로 만들어보았습니다.

 


Coherence Operator는 이와 별개로 Prometheus, Grafana 등 외부 모니터링 도구들을 통해 Coherence Cluster를 모니터링하는 기능들도 제공하고 있습니다.

 

다음 장에서는 Coherene Operator가 제공하는 추가 기능들은 어떠한 것들이 있는지 조금 더 자세하게 알아보는 과정을 진행해 보도록 하겠습니다.