apiVersion: apps/v1 kind: StatefulSet metadata: name: cockroachdb spec: serviceName: "cockroachdb" replicas: 3 selector: matchLabels: app: cockroachdb template: metadata: labels: app: cockroachdb spec: # Init containers are run only once in the lifetime of a pod, before # it's started up for the first time. It has to exit successfully # before the pod's main containers are allowed to start. # This particular init container does a DNS lookup for other pods in # the set to help determine whether or not a cluster already exists. # If any other pods exist, it creates a file in the cockroach-data # directory to pass that information along to the primary container that # has to decide what command-line flags to use when starting CockroachDB. # This only matters when a pod's persistent volume is empty - if it has # data from a previous execution, that data will always be used. initContainers: - name: bootstrap image: cockroachdb/cockroach-k8s-init:0.1 imagePullPolicy: IfNotPresent args: - "-on-start=/on-start.sh" - "-service=cockroachdb" env: - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace volumeMounts: - name: datadir mountPath: "/cockroach/cockroach-data" affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: app operator: In values: - cockroachdb topologyKey: kubernetes.io/hostname containers: - name: cockroachdb image: cockroachdb/cockroach:v1.0 imagePullPolicy: IfNotPresent ports: - containerPort: 26257 name: grpc - containerPort: 8080 name: http volumeMounts: - name: datadir mountPath: /cockroach/cockroach-data command: - "/bin/bash" - "-ecx" - | # The use of qualified `hostname -f` is crucial: # Other nodes aren't able to look up the unqualified hostname. CRARGS=("start" "--logtostderr" "--insecure" "--host" "$(hostname -f)" "--http-host" "0.0.0.0") # We only want to initialize a new cluster (by omitting the join flag) # if we're sure that we're the first node (i.e. index 0) and that # there aren't any other nodes running as part of the cluster that # this is supposed to be a part of (which indicates that a cluster # already exists and we should make sure not to create a new one). # It's fine to run without --join on a restart if there aren't any # other nodes. if [ ! "$(hostname)" == "cockroachdb-0" ] || \ [ -e "/cockroach/cockroach-data/cluster_exists_marker" ] then # We don't join cockroachdb in order to avoid a node attempting # to join itself, which currently doesn't work # (https://github.com/cockroachdb/cockroach/issues/9625). CRARGS+=("--join" "cockroachdb-0.cockroachdb,cockroachdb-1.cockroachdb,cockroachdb-2.cockroachdb") fi exec /cockroach/cockroach ${CRARGS[*]} # No pre-stop hook is required, a SIGTERM plus some time is all that's # needed for graceful shutdown of a node. terminationGracePeriodSeconds: 60 volumes: - name: datadir persistentVolumeClaim: claimName: datadir volumeClaimTemplates: - metadata: name: datadir spec: accessModes: - "ReadWriteOnce" resources: requests: storage: 1Gi