This guide provides step-by-step instructions for exposing Elasticsearch services managed by KubeBlocks, both externally and internally. You'll learn to configure external access using cloud provider LoadBalancer services, manage internal services, and properly disable external exposure when no longer needed.
Before proceeding, ensure the following:
kubectl create ns demo
namespace/demo created
KubeBlocks uses a declarative approach for managing Elasticsearch Clusters. Below is an example configuration for deploying a Elasticsearch Cluster with create a cluster with replicas for different roles.
Apply the following YAML configuration to deploy the cluster:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: es-multinode
namespace: demo
spec:
terminationPolicy: Delete
componentSpecs:
- name: dit
componentDef: elasticsearch-8
serviceVersion: 8.8.2
configs:
- name: es-cm
variables:
# use key `roles` to specify roles this component assume
roles: data,ingest,transform
replicas: 3
disableExporter: false
resources:
limits:
cpu: "1"
memory: "2Gi"
requests:
cpu: "1"
memory: "2Gi"
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
- name: master
componentDef: elasticsearch-8
serviceVersion: 8.8.2
configs:
- name: es-cm
variables:
# use key `roles` to specify roles this component assume
roles: master
replicas: 3
disableExporter: false
resources:
limits:
cpu: "1"
memory: "2Gi"
requests:
cpu: "1"
memory: "2Gi"
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
Monitor the cluster status until it transitions to the Running state:
kubectl get cluster es-multinode -n demo -w
Expected Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
es-multinode Delete Creating 10s
es-multinode Delete Updating 41s
es-multinode Delete Running 42s
Check the pod status and roles:
kubectl get pods -l app.kubernetes.io/instance=es-multinode -n demo
Expected Output:
NAME READY STATUS RESTARTS AGE
es-multinode-dit-0 3/3 Running 0 6m21s
es-multinode-dit-1 3/3 Running 0 6m21s
es-multinode-dit-2 3/3 Running 0 6m21s
es-multinode-master-0 3/3 Running 0 6m21s
es-multinode-master-1 3/3 Running 0 6m21s
es-multinode-master-2 3/3 Running 0 6m21s
Once the cluster status becomes Running, your Elasticsearch cluster is ready for use.
If you are creating the cluster for the very first time, it may take some time to pull images before running.
List the Services created for the Elasticsearch cluster:
kubectl get service -l app.kubernetes.io/instance=es-multinode -n demo
Example Services:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
es-multinode-dit-http ClusterIP 10.96.224.72 <none> 9200/TCP 56m
es-multinode-master-http ClusterIP 10.96.153.35 <none> 9200/TCP 56m
External service addresses enable public internet access to Elasticsearch, while internal service addresses restrict access to the user's VPC.
Type | Use Case | Cloud Cost | Security |
---|---|---|---|
ClusterIP | Internal service communication | Free | Highest |
NodePort | Development/testing | Low | Moderate |
LoadBalancer | Production external access | High | Managed via security groups |
Option 1: Using OpsRequest
To expose the Elasticsearch service externally using a LoadBalancer, create an OpsRequest resource:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: es-multinode-expose-enable-ops
namespace: demo
spec:
type: Expose
clusterName: es-multinode
expose:
- componentName: master
services:
- name: internet
# Determines how the Service is exposed. Defaults to 'ClusterIP'.
# Valid options are 'ClusterIP', 'NodePort', and 'LoadBalancer'.
serviceType: LoadBalancer
ports:
- name: es-http
port: 9200
protocol: TCP
targetPort: es-http
# Contains cloud provider related parameters if ServiceType is LoadBalancer.
# Following is an example for AWS EKS
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: nlb
service.beta.kubernetes.io/aws-load-balancer-internal: "false" # or "true" for an internal VPC IP
switch: Enable
Wait for the OpsRequest to complete:
kubectl get ops es-multinode-expose-enable-ops -n demo
Example Output:
NAME TYPE CLUSTER STATUS PROGRESS AGE
es-multinode-expose-enable-ops Expose es-multinode Succeed 1/1 31s
Option 2: Using Cluster API
Alternatively, update the spec.services
section in the Cluster resource to include a LoadBalancer service:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: es-multinode
namespace: demo
spec:
terminationPolicy: Delete
clusterDef: elasticsearch
# expose a external service
services:
- annotations:
service.beta.kubernetes.io/aws-load-balancer-type: nlb # Use Network Load Balancer
service.beta.kubernetes.io/aws-load-balancer-internal: "false" # or "true" for an internal VPC IP
componentSelector: master
name: master-internet
serviceName: master-internet
spec:
ports:
- name: es-http
nodePort: 32751
port: 9200
protocol: TCP
targetPort: es-http
type: LoadBalancer
componentSpecs:
...
The YAML configuration above adds a new external service under the services section. This LoadBalancer service includes annotations for AWS Network Load Balancer (NLB).
Cloud Provider Annotations
When using a LoadBalancer service, you must include the appropriate annotations specific to your cloud provider. Below is a list of commonly used annotations for different cloud providers:
service.beta.kubernetes.io/aws-load-balancer-type: nlb # Use Network Load Balancer
service.beta.kubernetes.io/aws-load-balancer-internal: "true" # Use "false" for internet-facing LoadBalancer
service.beta.kubernetes.io/azure-load-balancer-internal: "true" # Use "false" for internet-facing LoadBalancer
networking.gke.io/load-balancer-type: "Internal" # Restricts the LoadBalancer to internal VPC access only. Defaults to internet-facing if not specified.
cloud.google.com/l4-rbs: "enabled" # Optimization for internet-facing LoadBalancer
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type: "internet" # Use "intranet" for internal-facing LoadBalancer
The service.beta.kubernetes.io/aws-load-balancer-internal
annotation controls whether the LoadBalancer is internal or internet-facing. Note that this annotation cannot be modified dynamically after service creation.
service.beta.kubernetes.io/aws-load-balancer-internal: "false" # Use "true" for internal VPC IPs
If you change this annotation from "false" to "true" after the Service is created, the annotation may update in the Service object, but the LoadBalancer will still retain its public IP.
To properly modify this behavior:
service.beta.kubernetes.io/aws-load-balancer-internal
: "true").Wait for the Cluster status to transition to Running using the following command:
kubectl get cluster es-multinode -n demo -w
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
es-multinode Delete Running 18m
Check the service details to confirm the LoadBalancer service is created:
kubectl get service -l app.kubernetes.io/instance=es-multinode -n demo
Example Output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
es-multinode-dit-http ClusterIP 10.96.224.72 <none> 9200/TCP 59m
es-multinode-master-http ClusterIP 10.96.153.35 <none> 9200/TCP 59m
es-multinode-master-internet LoadBalancer 10.96.38.72 <EXTERNAL_IP> 9200:30998/TCP 19s
Option 1: Using OpsRequest
To disable external access, create an OpsRequest:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: es-multinode-expose-disable-ops
namespace: demo
spec:
clusterName: es-multinode
expose:
- componentName: master
services:
- name: internet
serviceType: LoadBalancer
switch: Disable
preConditionDeadlineSeconds: 0
type: Expose
Wait for the OpsRequest to complete:
kubectl get ops es-multinode-expose-disable-ops -n demo
Example Output:
NAME TYPE CLUSTER STATUS PROGRESS AGE
es-multinode-expose-disable-ops Expose es-multinode Succeed 1/1 16s
Option 2: Using Cluster API
Alternatively, remove the spec.services
field from the Cluster resource:
kubectl patch cluster es-multinode -n demo --type=json -p='[
{
"op": "remove",
"path": "/spec/services"
}
]'
Monitor the cluster status until it is Running:
kubectl get cluster es-multinode -n demo -w
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
es-multinode Delete Running 26m
Ensure that the 'es-multinode-elasticsearch-internet' Service is removed:
kubectl get service -l app.kubernetes.io/instance=es-multinode -n demo
Expected Result: The 'es-multinode-elasticsearch-internet' Service should be removed.
To remove all created resources, delete the Elasticsearch cluster along with its namespace:
kubectl delete cluster es-multinode -n demo
kubectl delete ns demo
This guide demonstrated how to:
KubeBlocks provides flexibility and simplicity for managing MySQL services in Kubernetes environments. simplicity for managing Elasticsearch services in Kubernetes environments.