First lets create a Load Balancer with the following yaml:
apiVersion: v1
kind: Service
metadata:
name: k8ssandra-lb-service
namespace: default
spec:
ports:
- protocol: TCP
port: 9042
selector:
cassandra.datastax.com/cluster: k8ssandra
cassandra.datastax.com/datacenter: dc1
type: LoadBalancer
loadBalancerIP:
Next lets apply this yaml to our k8ssandra cluster:
kubectl apply -f k8ssandra-lb.yaml
service/k8ssandra-lb-service created
Now we should see this service has been created with an external IP:
kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cass-operator-metrics ClusterIP 10.12.2.237 <none> 8383/TCP,8686/TCP 82d
k8ssandra-dc1-all-pods-service ClusterIP None <none> 9042/TCP,8080/TCP,9103/TCP 82d
k8ssandra-dc1-service ClusterIP None <none> 9042/TCP,9142/TCP,8080/TCP,9103/TCP,9160/TCP 82d
k8ssandra-dc1-stargate-service ClusterIP 10.12.14.148 <none> 8080/TCP,8081/TCP,8082/TCP,8084/TCP,8085/TCP,9042/TCP 82d
k8ssandra-grafana ClusterIP 10.12.2.224 <none> 80/TCP 82d
k8ssandra-kube-prometheus-operator ClusterIP 10.12.13.159 <none> 443/TCP 82d
k8ssandra-kube-prometheus-prometheus ClusterIP 10.12.15.21 <none> 9090/TCP 82d
k8ssandra-lb-service LoadBalancer 10.12.3.165 35.222.48.77 9042:31462/TCP 3m28s
k8ssandra-reaper-reaper-service ClusterIP 10.12.0.2 <none> 8080/TCP 82d
k8ssandra-seed-service ClusterIP None <none> <none> 82d
kubernetes ClusterIP 10.12.0.1 <none> 443/TCP 82d
memegen-v2-service LoadBalancer 10.12.0.112 35.222.90.197 80:32590/TCP 78d
prometheus-operated ClusterIP None <none> 9090/TCP 82d
To demonstrate connectivity, I exec into my cass pod as it has cqlsh installed:
kubectl exec -it k8ssandra-dc1-default-sts-0 /bin/bash
From the pod I execute cqlsh with the external IP with k8ssandra-superuser and password:
cassandra@k8ssandra-dc1-default-sts-0:/$ cqlsh 35.222.48.77 9042 -u k8ssandra-superuser -p H2mXvi6ftH8ZbW9i5k34
And viola, I am now connected to cassandra port 9042 with external IP address:
Connected to k8ssandra at 35.222.48.77:9042.
[cqlsh 5.0.1 | Cassandra 4.0-beta4 | CQL spec 3.4.5 | Native protocol v4]
Use HELP for help.
k8ssandra-superuser@cqlsh>
Last but not least, i delete this so you dont try to connect :
kubectl delete -f k8ssandra-lb.yaml
service "k8ssandra-lb-service" deleted