How to deploy a docker app in k8ssandra

In this tutorial I am going to show you how to deploy a dockerized application in your k8ssandra cluster. In addition to the deployment steps I am going to show you how to execute CQl and provide cassandra access credentials via ENV Variables so that the application is able to connect to cassandra and stargate. This particular application is a framework based on a PHP version of my MemeGen Application. For the purpose of this tutorial, stay focused on the deployment concepts and less on the actual application. These concepts exposed here can easily be used with a different docker container and associated application frameworks.

Requirements

  1. A running k3ds cluster and previous install of k8ssandra with all helm repos updated.
  2. A dockerized application/repo from k8ssandra-docker.
  3. A docker hub account (dsstevenmation).

Setup k8ssandra Environment

k3d cluster create
git clone https://github.com/ds-steven-matison/k8ssandra-docker.git
cd k8ssandra-docker
helm install -f k8ssandra.yaml k8ssandra k8ssandra/k8ssandra
watch kubectl get pods

** Wait about 10 minutes for stargate to finish rolling out

Get Cassandra Credentials

kubectl get secret k8ssandra-superuser -o jsonpath="{.data.username}" | base64 --decode ; echo
kubectl get secret k8ssandra-superuser -o jsonpath="{.data.password}" | base64 --decode ; echo

Execute CQL

kubectl exec -it k8ssandra-dc1-default-sts-0 -- bash -c "wget https://raw.githubusercontent.com/ds-steven-matison/k8ssandra-docker/master/demo.cql -P /tmp && /opt/cassandra/bin/cqlsh localhost 9042 -u k8ssandra-superuser -p [pass above] -f /tmp/demo.cql"

Build Docker Image

docker build . -t dsstevenmatison/k8ssandra-docker
docker push dsstevenmatison/k8ssandra-docker

Deploy Docker Application

kubectl apply -f deployment.k8s.yaml
kubectl port-forward deployments/k8ssandra-docker 1337:80

Now you can access your application:

http://localhost:1337/

Your k8ssandra cluster should now look like this:

NAME                                                READY   STATUS    RESTARTS   AGE
k8ssandra-kube-prometheus-operator-85695ffb-f42fv   1/1     Running   0          10m
k8ssandra-reaper-operator-b67dc8cdf-j5hnw           1/1     Running   0          10m
prometheus-k8ssandra-kube-prometheus-prometheus-0   2/2     Running   1          10m
k8ssandra-cass-operator-7c876d6d96-4pdjr            1/1     Running   0          10m
k8ssandra-grafana-5c6d5b8f5f-fj9r4                  2/2     Running   0          10m
k8ssandra-dc1-default-sts-0                         2/2     Running   0          9m32s
k8ssandra-reaper-655fc7dfc6-dct2n                   1/1     Running   0          5m39s
k8ssandra-dc1-stargate-77d4985d67-gfjwq             1/1     Running   0          10m
k8ssandra-docker-576fbdbd97-jc68b                   1/1     Running   0          30s

Update Docker Application

To update your docker application during development iterations, simply rebuild and push your image, then murder the previous application pod to re-roll the new image. Be sure to redo your port forward if working in a singe terminal.

docker build . -t dsstevenmatison/k8ssandra-docker
docker push dsstevenmatison/k8ssandra-docker
kubectl delete pod k8ssandra-docker-576fbdbd97-jc68b
kubectl port-forward deployments/k8ssandra-docker 1337:80

Lessons Learned

  1. K8ssandra is a great dev environment locally and easily moved to any Kubernetes Engine
  2. Docker images can easily be deployed as pods in k8ssandra clusters
  3. Schema processes can be fully automated
  4. Passing cassandra secrets to application pod is possible with ENV variables

What’s Next?

Check out my full blown MemeGen Application and stay tuned as I use this application to create multi-cloud, multi-region, and hybrid cloud deployments on Anthos, GCP, and AWS.

2 Likes

Love it! Thanks for sharing, @DsMatison . :beers:

1 Like