Data Grid on OpenShift
Install the Data Grid Operator and create an Infinispan cluster and a cache as custom resources.
Data Grid on OpenShift is two objects you own, and a set of objects the operator owns. You install the operator, apply an Infinispan resource, and apply a Cache resource. The operator creates the StatefulSet, the Services, the Secret with the developer credentials, and the TLS material. You do not hand-build those.
Prerequisites
- OpenShift 4.x and
oc, with permission to install an operator. On a shared cluster that permission belongs to an administrator. - A StorageClass. The pods store logs and, later, persistent caches. An emptyDir cluster works for a first afternoon and forgets everything when a pod is rescheduled.
- The
redhat-operatorscatalog, or the same operator mirrored into a disconnected cluster. If the catalog is not there, fix that before debugging the CR.
oc new-project datagrid
oc get operators datagrid.openshift-operators 2>/dev/null || true
Install the operator
From the console this is OperatorHub → Data Grid, installed for all namespaces or for datagrid. From the CLI the subscription is:
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
name: datagrid
namespace: openshift-operators
spec:
channel: "8.5.x"
installPlanApproval: Automatic
name: datagrid
source: redhat-operators
sourceNamespace: openshift-marketplace
Use the channel OperatorHub shows for the line you standardise on. 8.5.x matches the CRs below. Wait until the CSV is Succeeded:
oc get csv -n openshift-operators | grep datagrid
oc get crd infinispans.infinispan.org caches.infinispan.org
Both CRDs need to exist before the next apply. caches.infinispan.org is v2alpha1. infinispans.infinispan.org is v1.
A cluster
service.type: DataGrid is the one that exposes Hot Rod and REST with authentication. Cache (the other service type) is a more limited mode. Use DataGrid.
apiVersion: infinispan.org/v1
kind: Infinispan
metadata:
name: example-infinispan
namespace: datagrid
spec:
replicas: 3
service:
type: DataGrid
container:
memory: 1Gi
security:
endpointAuthentication: true
authorization:
enabled: true
oc apply -f infinispan.yaml
oc wait -n datagrid infinispan/example-infinispan \
--for=condition=WellFormed --timeout=600s
oc get pods -n datagrid -l app=infinispan-pod
oc get secret -n datagrid example-infinispan-generated-secret
WellFormed means the pods have formed a cluster view. Three pods for three replicas. The generated secret holds username, password and identities.yaml. developer is the application user the operator created. Treat the secret as a credential.
The Service is example-infinispan on port 11222, HTTPS. From another namespace the DNS name is example-infinispan.datagrid.svc. A Route is optional and usually wrong. Clients inside the cluster do not need one, and a Route would put the cache on the network edge. The security lab locks this down further.
A cache
Caches are their own resource. A cache created in the console is one the operator will adopt. Prefer Git.
apiVersion: infinispan.org/v2alpha1
kind: Cache
metadata:
name: catalog
namespace: datagrid
spec:
clusterName: example-infinispan
name: catalog
template: |
distributedCache:
mode: "SYNC"
owners: "2"
statistics: "true"
encoding:
mediaType: "application/json"
oc apply -f catalog-cache.yaml
oc get cache -n datagrid catalog
spec.name is the name clients use. The metadata.name has to be a legal Kubernetes name. statistics: "true" is what makes the monitoring lab possible later. application/json is convenient for REST experiments. Java clients that will use queries need Protobuf instead, which is the querying lab.
The cache modes lab explains owners and SYNC. For this page, two owners on three pods is the layout that survives oc delete pod.
Prove a pod can die
PASS=$(oc get secret -n datagrid example-infinispan-generated-secret \
-o jsonpath='{.data.password}' | base64 -d)
oc exec -n datagrid example-infinispan-0 -- \
curl -sk -u "developer:${PASS}" \
-X POST -H 'Content-Type: text/plain' \
https://example-infinispan:11222/rest/v2/caches/catalog/sku-42 \
-d '120'
oc delete pod -n datagrid example-infinispan-0
oc wait -n datagrid pod/example-infinispan-0 --for=condition=Ready --timeout=300s
oc exec -n datagrid example-infinispan-1 -- \
curl -sk -u "developer:${PASS}" \
https://example-infinispan:11222/rest/v2/caches/catalog/sku-42
The second curl prints 120. The entry was on two owners, so deleting one pod left the other. Wait until the new pod is in the cluster view before you call that a test of state transfer. oc get infinispan example-infinispan -o yaml and the WellFormed condition show the view.
If the second curl is empty, the cache was local, or the POST went to a different cluster, or authentication failed and you did not look at the status code. curl -sk hides the code. Add -w '%{http_code}\n'.
Upgrades and the namespace
The operator upgrades the operands when you move the subscription channel and the Infinispan image, following the version matrix for that operator. Do not change the image field to a random Infinispan tag. Data Grid support is the combination the operator ships.
One operator can run several Infinispan clusters, in different namespaces, which is how you keep a development cache away from production. Deleting the namespace deletes the caches. There is no undo. Backup and restore is the undo.
What's next
Caches and cache modes changes the template you just applied. Connecting applications replaces the curl with a Hot Rod client.