Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapted templates and documentation to work with hazelcast 5.3.0 #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ Temporary Items
.apdisk

/.idea/

# Python environment
/.venv
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM ubuntu:18.04
FROM ubuntu:22.04
RUN apt-get update && \
apt-get install -y python python-pip && \
pip install Flask hazelcast-python-client
apt-get install -y python3 python3-pip && \
pip install Flask hazelcast-python-client pyasyncore
EXPOSE 5000
COPY app.py .
ENTRYPOINT ["python", "app.py"]
ENTRYPOINT ["python3", "app.py"]
11 changes: 7 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import hazelcast
from hazelcast import HazelcastClient
from flask import Flask
from flask import request

import logging
logging.basicConfig(level=logging.INFO)

app = Flask(__name__)

config = hazelcast.ClientConfig()
config.network_config.addresses.append("127.0.0.1:5701")
hazelcastClient = hazelcast.HazelcastClient(config)

hazelcastClient = HazelcastClient(
cluster_name="dev"
)

@app.route("/put")
def put():
Expand Down
4 changes: 3 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ metadata:
data:
hazelcast.yaml: |-
hazelcast:
cluster-name: dev
network:
join:
multicast:
enabled: false
kubernetes:
enabled: true
enabled: true
service-name: hazelcast-headless
37 changes: 23 additions & 14 deletions docs/modules/ROOT/pages/kubernetes-sidecar.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
== Before you Begin

- https://docs.docker.com/install/[Docker] (https://www.docker.com/products/docker-desktop[Docker for Desktop] is good enough)
- https://kubernetes.io/[Kubernetes] cluster (https://www.docker.com/products/docker-desktop[Docker for Desktop] or https://minikube.sigs.k8s.io/docs/[Minikube] is good enough)
- https://kubernetes.io/[Kubernetes] cluster (https://www.docker.com/products/docker-desktop[Docker for Desktop], https://minikube.sigs.k8s.io/docs/[Minikube] or https://microk8s.io/docs/install-alternatives[MicroK8s] is good enough)
- https://git-scm.com/[Git]
- Python
- `kubectl`
Expand Down Expand Up @@ -47,17 +47,24 @@ Hazelcast sidecar members from all Pods will form a Hazelcast cluster.
== Step 1. Create the Python Application

You can use any programming language for which Hazelcast Client is provided (https://hazelcast.org/clients-languages/[Java, .NET, C++, Node.js, Python, Go, Scala]) for this guide.
As a sample we will use the Python application from https://github.com/hazelcast-guides/kubernetes-sidecar[Hazelcast as Sidecar container]
As a sample we will use the Python application from https://github.com/hazelcast-guides/kubernetes-sidecar[Hazelcast as Sidecar container].
First, setup a Python environment:

[source, shell]
----
$ python -m venv .venv
$ source .venv/bin/activate
$ pip install -r requirements.txt
----
You can find the Python web service application (written with the Flask framework) in the `app.py` file. The most interesting part is the connection to the Hazelcast member.

[source, python]
----
config = hazelcast.ClientConfig()
config.network_config.addresses.append("127.0.0.1:5701")
hazelcastClient = hazelcast.HazelcastClient(config)
hazelcastClient = HazelcastClient(
cluster_name="dev"
)
----

We connect to `127.0.0.1`, because in Kubernetes Pod all containers share the same network layer. Thanks to that, we can always depend on the fact that the Hazelcast member is running at localhost.
In the example, we used `dev` for the cluster name. Adding members is not necessary due to the auto-discovery via the headless service.

Then, in the endpoint controller, we simply use the `hazelcastClient` as we always do.

Expand Down Expand Up @@ -85,17 +92,18 @@ NOTE: If you don't have a Docker Hub account, or you don't want to use it, you c
* Build image with your Kubernetes-related Docker host (then you don't need to push it):
- If you use Docker Desktop, then your local image is already accessible to Kubernetes
- If you use Minikube, then you need to execute eval $(minikube docker-env) before building the image
- If you use MicroK8s, follow the instructions on https://microk8s.io/docs/registry-built-in
- If you use Kubernetes from a Cloud platform, then you need to upload the image to their registry

== Step 3. Use Hazelcast as a Sidecar Deployment

The next step is to configure Python application container and Hazelcast member container to exist in the same Kubernetes Pod. We do in deployment.yaml.
The next step is to configure Python application container and Hazelcast member container to exist in the same Kubernetes Pod. We do in `statefulset.yaml`.

[source, yaml]
----
containers:
- name: hazelcast
image: hazelcast/hazelcast:3.12
image: hazelcast/hazelcast:5.3.0
ports:
- name: hazelcast
containerPort: 5701
Expand Down Expand Up @@ -123,7 +131,7 @@ Finally, we can deploy our application with the sidecar Hazelcast member.

[source, shell script]
----
kubectl apply -f deployment.yaml
kubectl apply -f statefulset.yaml
----

== Step 5. Verify the Sidecar Application
Expand All @@ -132,7 +140,7 @@ You should see 2 Pods, each having 2 containers (`hazelcast` ans `app`).

[source, shell script]
----
$ kubectl get pods
$ kubectl get pods -l app=hazelcast-sidecar
NAME READY STATUS RESTARTS AGE
hazelcast-sidecar-0 2/2 Running 2 1m
hazelcast-sidecar-1 2/2 Running 2 1m
Expand Down Expand Up @@ -177,9 +185,10 @@ In our case `<NODE-PORT>` is `32470`.

Checking `<NODE-IP>` depends on your Kubernetes:

In case of Docker Desktop, it's `localhost`
In case of Minikube, check it with `minikube ip`
In case of Cloud platforms (and on-premise), check it with: `kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="ExternalIP")].address }'`
- In case of Docker Desktop, it's `localhost`
- In case of Minikube, check it with `minikube ip`
- In case of MicroK8s, check it with `kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="InternalIP")].address}'`
- In case of Cloud platforms (and on-premise), check it with: `kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="ExternalIP")].address}'`

Let's insert some data and then read it.

Expand Down
22 changes: 19 additions & 3 deletions rbac.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: hazelcast-cluster-role
rules:
- apiGroups:
- ""
resources:
- endpoints
- pods
- nodes
- services
verbs:
- get
- list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: default-cluster
name: hazelcast-cluster-role-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: view
name: hazelcast-cluster-role
subjects:
- kind: ServiceAccount
name: default
namespace: default
namespace: default
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==3.0.2
hazelcast-python-client==5.3.0
pyasyncore==1.0.4
19 changes: 15 additions & 4 deletions deployment.yaml → statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
spec:
containers:
- name: hazelcast
image: hazelcast/hazelcast:3.12
image: hazelcast/hazelcast:5.3.0
ports:
- name: hazelcast
containerPort: 5701
Expand All @@ -36,9 +36,20 @@ spec:
- name: hazelcast-storage
configMap:
name: hazelcast-configuration

---

apiVersion: v1
kind: Service
metadata:
name: hazelcast-headless
spec:
type: ClusterIP
clusterIP: None
selector:
app: hazelcast-sidecar
ports:
- name: tcp-hazelcast
port: 5701
---
apiVersion: v1
kind: Service
metadata:
Expand All @@ -49,4 +60,4 @@ spec:
app: hazelcast-sidecar
ports:
- name: app
port: 5000
port: 5000