<p>I have started using Cass-Operator and the setup worked like a charm! <a href="https://github.com/datastax/cass-operator" rel="nofollow noreferrer">https://github.com/datastax/cass-operator</a>.</p>
<p>I have an issue though. My cluster is up and running on GCP. But how do I access it from my laptop (basically from outside)? Sorry, I'm new to Kubernetes so I do not know how to access the cluster from outside?</p>
<p>I can see the nodes are up on the GCP dashboard. I can ping the external IP of the nodes from my laptop but when I run <code>cqlsh external_ip 9042</code> then the connection fails.</p>
<p>How do I go about connecting the K8s/Cassandra cluster to outside work so that my web application can access it?</p>
<p>I would like to:</p>
<ol>
<li>have a url so that my web application uses that URL to connect to the cassandra/K8s cluster instead of IP address. Thus, I need a dns. Does it come by default in K8S? Would would be the url? Would K8s managing the dns mapping for me in some nodes get restarted?</li>
<li>My web application should be able to reach Cassandra on 9042. It seems load balancing is done for http/https. The Cassandra application is not a http/https request. So I don't need port 80 or 443</li>
</ol>
<p>I have read few tutorials which talk about Service, Loadbalancer and Ingress. But I am unable to make a start.</p>
<p>I created a service like this</p>
<pre><code>kind: Service
apiVersion: v1
metadata:
  name: cass-operator-service
spec:
  type: LoadBalancer
  ports:
    - port: 9042
  selector:
    name: cass-operator
</code></pre>
<p>Then created the service - <code>kubectl apply -f ./cass-operator-service.yaml</code></p>
<p>I checked if the service was created using <code>kubectl get svc</code> and got output</p>
<pre><code>NAME                    TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE 
cass-operator-service   LoadBalancer   10.51.249.224   34.91.214.233   9042:30136/TCP   4m17s 
kubernetes              ClusterIP      10.51.240.1     &lt;none&gt;          443/TCP          10h. 
</code></pre>
<p>But when I run <code>cqlsh 34.91.214.233 9042</code> then the connection fails</p>
<p>It seems that the requests to port 9042 would be forwarded to 30136. But They should be forwarded to 9042 as that is where the Cassandra image in the pods is listening for incoming requests</p>
<p>UPDATE</p>
<p>Tried targetPort but still no luck</p>
<pre><code>manuchadha25@cloudshell:~ (copper-frame-262317)$ cat cass-operator-service.yaml
kind: Service
apiVersion: v1
metadata:
  name: cass-operator-service
spec:
  type: LoadBalancer
  ports:
    - port: 9042
      targetPort: 9042
  selector:
    name: cass-operator
manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl get service
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.51.240.1   &lt;none&gt;        443/TCP   11h
manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl apply -f ./cass-operator-service.yaml
service/cass-operator-service created
manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl get service
NAME                    TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
cass-operator-service   LoadBalancer   10.51.255.184   &lt;pending&gt;     9042:30024/TCP   12s
kubernetes              ClusterIP      10.51.240.1     &lt;none&gt;        443/TCP          11h
manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl get service
NAME                    TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
cass-operator-service   LoadBalancer   10.51.255.184   &lt;pending&gt;     9042:30024/TCP   37s
kubernetes              ClusterIP      10.51.240.1     &lt;none&gt;        443/TCP          11h
manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl get service
NAME                    TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE
cass-operator-service   LoadBalancer   10.51.255.184   34.91.214.233   9042:30024/TCP   67s
kubernetes              ClusterIP      10.51.240.1     &lt;none&gt;          443/TCP          11h
manuchadha25@cloudshell:~ (copper-frame-262317)$ ping 34.91.214.233
PING 34.91.214.233 (34.91.214.233) 56(84) bytes of data.
64 bytes from 34.91.214.233: icmp_seq=1 ttl=109 time=7.89 ms
</code></pre>
<p><a href="https://i.stack.imgur.com/ab0X6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ab0X6.png" alt="enter image description here" /></a></p>
<p>Querying all names spaces reveal the following</p>
<p><a href="https://i.stack.imgur.com/jjH5E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jjH5E.png" alt="enter image description here" /></a></p>
<p>But querying pods with namespace cass-operator returns empty result</p>
<pre><code>manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl get pods -l name=cass-operator
No resources found in default namespace.
</code></pre>

Manu Chadha
<ul>
<li>Since you are new to Kubernetes, you probably are not familiar with <strong>StatefulSets</strong>:</li>
</ul>
<blockquote>
<p>StatefulSet is the workload API object used to manage stateful applications.</p>
<p>Manages the deployment and scaling of a set of  <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/" rel="noreferrer">Pods</a>,  <em>and provides guarantees about the ordering and uniqueness</em>  of these Pods.</p>
<p>Like a Deployment, a StatefulSet manages Pods that are based on an identical container spec. <strong>Unlike a Deployment</strong>, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.</p>
</blockquote>
<ul>
<li>I recommend you to read these articles to learn more about it's mechanisms:
<ul>
<li><a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="noreferrer">Kubernetes.io - Statefulsets</a></li>
<li><a href="https://www.magalix.com/blog/kubernetes-statefulsets-101-state-of-the-pods" rel="noreferrer">Megalix - Statefulsets 101</a></li>
<li><a href="https://itnext.io/exposing-statefulsets-in-kubernetes-698730fb92a1" rel="noreferrer">ITNext - Exposing Statefulsets in Kubernetes</a></li>
</ul>
</li>
</ul>
<hr />
<blockquote>
<p>How do I go about connecting the K8s/Cassandra cluster to outside work so that my web application can access it?</p>
</blockquote>
<ul>
<li>I found out that datastax/cass-operator is still developing their documentation, I found <a href="https://github.com/datastax/cass-operator/tree/f395fe44a36a9d4e7c26026618093ee134ebedf1/docs/proxy" rel="noreferrer">this document</a> that is not merged to master yet, but it explains very well about how to connect to Cassandra, I strongly recommend reading.</li>
<li>There are several <a href="https://github.com/datastax/cass-operator/issues" rel="noreferrer">open issues</a> for documenting methods for connection from outside the cluster.</li>
</ul>
<p>I followed the guide in <a href="https://github.com/datastax/cass-operator" rel="noreferrer">https://github.com/datastax/cass-operator</a> to deploy the cass-operator + Cassandra Datacenter Example as from your images I believe you followed as well:</p>
<pre><code>$ kubectl create -f https://raw.githubusercontent.com/datastax/cass-operator/v1.2.0/docs/user/cass-operator-manifests-v1.15.yaml
namespace/cass-operator created
serviceaccount/cass-operator created
secret/cass-operator-webhook-config created
customresourcedefinition.apiextensions.k8s.io/cassandradatacenters.cassandra.datastax.com created
clusterrole.rbac.authorization.k8s.io/cass-operator-cluster-role created
clusterrolebinding.rbac.authorization.k8s.io/cass-operator created
role.rbac.authorization.k8s.io/cass-operator created
rolebinding.rbac.authorization.k8s.io/cass-operator created
service/cassandradatacenter-webhook-service created
deployment.apps/cass-operator created
validatingwebhookconfiguration.admissionregistration.k8s.io/cassandradatacenter-webhook-registration created

$ kubectl create -f https://raw.githubusercontent.com/datastax/cass-operator/v1.2.0/operator/k8s-flavors/gke/storage.yaml
storageclass.storage.k8s.io/server-storage created

$ kubectl -n cass-operator create -f https://raw.githubusercontent.com/datastax/cass-operator/v1.2.0/operator/example-cassdc-yaml/cassandra-3.11.6/example-cassdc-minimal.yaml
cassandradatacenter.cassandra.datastax.com/dc1 created

$ kubectl get all -n cass-operator
NAME                                READY   STATUS    RESTARTS   AGE
pod/cass-operator-78c6469c6-6qhsb   1/1     Running   0          139m
pod/cluster1-dc1-default-sts-0      2/2     Running   0          138m
pod/cluster1-dc1-default-sts-1      2/2     Running   0          138m
pod/cluster1-dc1-default-sts-2      2/2     Running   0          138m

NAME                                          TYPE           CLUSTER-IP    EXTERNAL-IP    PORT(S)             AGE
service/cass-operator-metrics                 ClusterIP      10.21.5.65    &lt;none&gt;         8383/TCP,8686/TCP   138m
service/cassandradatacenter-webhook-service   ClusterIP      10.21.0.89    &lt;none&gt;         443/TCP             139m
service/cluster1-dc1-all-pods-service         ClusterIP      None          &lt;none&gt;         &lt;none&gt;              138m
service/cluster1-dc1-service                  ClusterIP      None          &lt;none&gt;         9042/TCP,8080/TCP   138m
service/cluster1-seed-service                 ClusterIP      None          &lt;none&gt;         &lt;none&gt;              138m

NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/cass-operator   1/1     1            1           139m

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/cass-operator-78c6469c6   1         1         1       139m

NAME                                        READY   AGE
statefulset.apps/cluster1-dc1-default-sts   3/3     138m

$ CASS_USER=$(kubectl -n cass-operator get secret cluster1-superuser -o json | jq -r '.data.username' | base64 --decode)
$ CASS_PASS=$(kubectl -n cass-operator get secret cluster1-superuser -o json | jq -r '.data.password' | base64 --decode)

$ echo $CASS_USER
cluster1-superuser

$ echo $CASS_PASS
_5ROwp851l0E_2CGuN_n753E-zvEmo5oy31i6C0DBcyIwH5vFjB8_g
</code></pre>
<ul>
<li>From the <code>kubectl get all</code> command above we can see there is an statefulset called <code>statefulset.apps/cluster1-dc1-default-sts</code> which controls the cassandra pods.</li>
<li>In order to create a LoadBalancer service that makes available all the pods managed by this <code>statefulset</code> we need to use the same labels assigned to them:</li>
</ul>
<pre><code>$ kubectl describe statefulset cluster1-dc1-default-sts -n cass-operator
Name:               cluster1-dc1-default-sts
Namespace:          cass-operator
CreationTimestamp:  Tue, 30 Jun 2020 12:24:34 +0200
Selector:           cassandra.datastax.com/cluster=cluster1,cassandra.datastax.com/datacenter=dc1,cassandra.datastax.com/rack=default
Labels:             app.kubernetes.io/managed-by=cass-operator
                    cassandra.datastax.com/cluster=cluster1
                    cassandra.datastax.com/datacenter=dc1
                    cassandra.datastax.com/rack=default
</code></pre>
<ul>
<li>Now let's create the LoadBalancer service yaml and use the labels as <code>selectors</code> for the service:</li>
</ul>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: cassandra-loadbalancer
  namespace: cass-operator
  labels:
    cassandra.datastax.com/cluster: cluster1
    cassandra.datastax.com/datacenter: dc1
    cassandra.datastax.com/rack: default
spec:
  type: LoadBalancer
  ports:
  - port: 9042
    protocol: TCP
  selector:
    cassandra.datastax.com/cluster: cluster1
    cassandra.datastax.com/datacenter: dc1
    cassandra.datastax.com/rack: default
</code></pre>
<blockquote>
<p><em>&quot;My web application should be able to reach Cassandra on 9042. It seems load balancing is done for http/https. The Cassandra application is not a http/https request. So I don't need port 80 or 443.&quot;</em></p>
</blockquote>
<ul>
<li><p>When you create a Service of type  <code>LoadBalancer</code>, a Google Cloud controller wakes up and configures a  <a href="https://cloud.google.com/load-balancing/docs/network" rel="noreferrer">network load balancer</a>  in your project. The load balancer has a stable IP address that is accessible from outside of your project.</p>
</li>
<li><p><strong>The network load balancer supports any and all ports</strong>. You can use Network Load Balancing to load balance TCP and UDP traffic. Because the load balancer is a pass-through load balancer, your backends terminate the load-balanced TCP connection or UDP packets themselves.</p>
</li>
<li><p>Now let's apply the yaml and note the Endpoint IPs of the pods being listed:</p>
</li>
</ul>
<pre><code>$ kubectl apply -f cassandra-loadbalancer.yaml 
service/cassandra-loadbalancer created

$ kubectl get service cassandra-loadbalancer -n cass-operator 
NAME                     TYPE           CLUSTER-IP    EXTERNAL-IP    PORT(S)          AGE
cassandra-loadbalancer   LoadBalancer   10.21.4.253   146.148.89.7   9042:30786/TCP   5m13s

$ kubectl describe svc cassandra-loadbalancer -n cass-operator
Name:                     cassandra-loadbalancer
Namespace:                cass-operator
Labels:                   cassandra.datastax.com/cluster=cluster1
                          cassandra.datastax.com/datacenter=dc1
                          cassandra.datastax.com/rack=default
Annotations:              Selector:  cassandra.datastax.com/cluster=cluster1,cassandra.datastax.com/datacenter=dc1,cassandra.datastax.com/rack=default
Type:                     LoadBalancer
IP:                       10.21.4.253
LoadBalancer Ingress:     146.148.89.7
Port:                     &lt;unset&gt;  9042/TCP
TargetPort:               9042/TCP
NodePort:                 &lt;unset&gt;  30786/TCP
Endpoints:                10.24.0.7:9042,10.24.2.7:9042,10.24.3.9:9042
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   &lt;none&gt;
</code></pre>
<ul>
<li>To test it, I'll use my cloud shell with a cassandra container to emulate your notebook using the <code>LoadBalancer</code> IP provided above:</li>
</ul>
<pre class="lang-sh prettyprint-override"><code>$ docker run -it cassandra /bin/sh

# cqlsh -u cluster1-superuser -p _5ROwp851l0E_2CGuN_n753E-zvEmo5oy31i6C0DBcyIwH5vFjB8_g 146.148.89.7 9042                

Connected to cluster1 at 146.148.89.7:9042.
[cqlsh 5.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cluster1-superuser@cqlsh&gt; select * from system.peers;

 peer      | data_center | host_id                              | preferred_ip | rack    | release_version | rpc_address | schema_version                       | tokens
-----------+-------------+--------------------------------------+--------------+---------+-----------------+-------------+--------------------------------------+--------------------------
 10.24.3.9 |         dc1 | bcec6c12-49a1-41d5-be58-5150e99f5dfb |         null | default |          3.11.6 |   10.24.3.9 | e84b6a60-24cf-30ca-9b58-452d92911703 |  {'2248175870989649036'}
 10.24.0.7 |         dc1 | 68409f08-9d6e-4e40-91ff-f43581c8b6f3 |         null | default |          3.11.6 |   10.24.0.7 | e84b6a60-24cf-30ca-9b58-452d92911703 | {'-1105923522927946373'}

(2 rows)
</code></pre>
<hr />
<blockquote>
<p><em>&quot;have a url so that my web application uses that URL to connect to the cassandra/K8s cluster instead of IP address. So I need a dns. Does it come by default in K8S? Would would be the url? Would K8s managing the dns mapping for me in some nodes get restarted?&quot;</em></p>
</blockquote>
<ul>
<li>That documentation on cassandra-operator also has a section about <a href="https://github.com/datastax/cass-operator/tree/f395fe44a36a9d4e7c26026618093ee134ebedf1/docs/proxy#ingresses" rel="noreferrer">Ingress</a>, I recommend reading as well.</li>
<li>Kubernetes does not come with a default DNS name.</li>
<li>You will have to register a domain, point the DNS to the IP of the load balancer this way it will resolve the IP of the Network LoadBalancer.</li>
<li>The Network LoadBalancer is bound to a Static Public IP,  any changes in Kubernetes nodes will not cause service unavailability.</li>
</ul>
<p>If you have any question, let me know in the comments.</p>
