apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cassandra
  labels:
    app: cassandra
    group: db
    version: v3.11.4
spec:
  serviceName: cassandra
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: OrderedReady
  selector:
    matchLabels:
      app: cassandra
      group: db
      version: v3.11.4
  template:
    metadata:
      labels:
        app: cassandra
        group: db
        version: v3.11.4
    spec:
      containers:
      - name: cassandra
        image: cassandra:3.11.4
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            cpu: 1
            memory: 4Gi
          requests:
            cpu: 1
            memory: 4Gi
        env:
        - name: MAX_HEAP_SIZE
          value: 2048M
        - name: HEAP_NEWSIZE
          value: 512M
        - name: CASSANDRA_CLUSTER_NAME
          value: cassandra-cluster
        # - name: CASSANDRA_SEEDS
        #   value: "cassandra-0.cassandra.default.svc.cluster.local,cassandra-1.cassandra.default.svc.cluster.local"
        # - name: CASSANDRA_ENDPOINT_SNITCH
        #   value: "SimpleSnitch"
        - name: CASSANDRA_DC
          value: DC1
        - name: CASSANDRA_RACK
          value: RACK1
        # - name: CASSANDRA_START_RPC
        #   value: "false"
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        ports:
        - name: intra-node
          containerPort: 7000
        - name: tls-intra-node
          containerPort: 7001
        - name: jmx
          containerPort: 7199
        - name: cql
          containerPort: 9042
        - name: thrift
          containerPort: 9160
        # These volume mounts are persistent. They are like inline claims,
        # but not exactly because the names need to match exactly one of
        # the stateful pod volumes.
        volumeMounts:
        - name: cassandra-volume
          mountPath: /var/lib/cassandra
        # lifecycle:
        #   preStop:
        #     exec:
        #       command: [ "/bin/sh", "-c", "nodetool drain" ]
        livenessProbe:
          exec:
            command: [ "/bin/sh", "-c", "nodetool status" ]
          initialDelaySeconds: 240
          periodSeconds: 10
          timeoutSeconds: 3
          successThreshold: 1
          failureThreshold: 3
        readinessProbe:
          exec:
            command: [ "/bin/sh", "-c", "nodetool status | grep -E \"^UN\\s+${POD_IP}\"" ]
          initialDelaySeconds: 90
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
        securityContext:
          capabilities:
            add:
              - IPC_LOCK
              - SYS_RESOURCE
        #   procMount: Default
      - name: cassandra-exporter
        # v2.2.2
        image: criteord/cassandra_exporter@sha256:3f42fcd8f75e82b581417f655a8f46aa07a0ff6aca32cf7c8549f51aade8d7f2
        command: ['/sbin/dumb-init', '/bin/sh', '-c', '/usr/local/apache-cassandra/scripts/exporter_run_override.sh']
        resources:
          limits:
            cpu: 300m
            memory: 500Mi
          requests:
            cpu: 300m
            memory: 500Mi
        env:
        - name: CASSANDRA_EXPORTER_CONFIG_host
          value: "127.0.0.1:7199"
        - name: CASSANDRA_EXPORTER_CONFIG_listenPort
          value: "8080"
        - name: JVM_OPTS
          value: "-XX:MaxRAMFraction=2
                  -XX:+AlwaysPreTouch
                  -Dorg.slf4j.simpleLogger.showDateTime=true
                  -Dorg.slf4j.simpleLogger.dateTimeFormat=\"yyyy-MM-dd'T'HH:mm:ss\"
                  -Dcom.sun.management.jmxremote.ssl=false
                  -Dcom.sun.management.jmxremote.authenticate=false
                  -Dcom.sun.management.jmxremote.port=5555
                  -Dcom.sun.management.jmxremote.local.only=false
                  -Dnetworkaddress.cache.ttl=15"
        ports:
        - name: jmx-exporter
          containerPort: 5555
        - name: metrics
          containerPort: 8080
          protocol: TCP
        volumeMounts:
        - name: scripts
          mountPath: /usr/local/apache-cassandra/scripts
        # - name: exporter-config
        #   mountPath: /etc/cassandra_exporter/
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 180
        readinessProbe:
          httpGet:
            path: /metrics
            port: 8080
          initialDelaySeconds: 180
          timeoutSeconds: 45
      # initContainers:
      # - command: [ "sysctl", "-w", "vm.max_map_count=1048575" ]
      #   # this digest corresponds to version 1.30.1
      #   image: busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d
      #   imagePullPolicy: IfNotPresent
      #   name: init-sysctl
      #   resources: {}
      #   securityContext:
      #     privileged: true
      #     procMount: Default
      #   terminationMessagePath: /dev/termination-log
      #   terminationMessagePolicy: File
      terminationGracePeriodSeconds: 180
      # dnsPolicy: ClusterFirst
      # hostNetwork: false
      # affinity:
      #   # use nodes dedicated for dbs
      #   nodeAffinity:
      #     requiredDuringSchedulingIgnoredDuringExecution:
      #       nodeSelectorTerms:
      #       - matchExpressions:
      #         - key: dedicated
      #           operator: In
      #           values:
      #           - "yes"
      #         - key: purpose
      #           operator: In
      #           values:
      #           - db
      #   # avoid nodes that already has a cassandra instance on it
      #   podAntiAffinity:
      #     preferredDuringSchedulingIgnoredDuringExecution:
      #     - podAffinityTerm:
      #         labelSelector:
      #           matchExpressions:
      #           - key: app
      #             operator: In
      #             values: 
      #             - cassandra
      #         # anti affinity is on hostname level
      #         topologyKey: kubernetes.io/hostname
      #       weight: 100
      # tolerations:
      # - key: purpose
      #   operator: Equal
      #   value: db
      #   effect: NoExecute
      volumes:
      - name: scripts
        configMap:
          name: cassandra-scripts
          defaultMode: 0775
      # - name: exporter-config
      #   configMap:
      #     name: cassandra-exporter
      #     defaultMode: 0775
  volumeClaimTemplates:
  - metadata:
      name: cassandra-volume
      labels:
        app: cassandra
        group: db
        version: v3.11.4
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 5Gi
