github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/cockroachdb/cockroachdb-statefulset.yaml (about)

     1  apiVersion: v1
     2  kind: Service
     3  metadata:
     4    # This service is meant to be used by clients of the database. It exposes a ClusterIP that will
     5    # automatically load balance connections to the different database pods.
     6    name: cockroachdb-public
     7    labels:
     8      app: cockroachdb
     9  spec:
    10    ports:
    11    # The main port, served by gRPC, serves Postgres-flavor SQL, internode
    12    # traffic and the cli.
    13    - port: 26257
    14      targetPort: 26257
    15      name: grpc
    16    # The secondary port serves the UI as well as health and debug endpoints.
    17    - port: 8080
    18      targetPort: 8080
    19      name: http
    20    selector:
    21      app: cockroachdb
    22  ---
    23  apiVersion: v1
    24  kind: Service
    25  metadata:
    26    # This service only exists to create DNS entries for each pod in the stateful
    27    # set such that they can resolve each other's IP addresses. It does not
    28    # create a load-balanced ClusterIP and should not be used directly by clients
    29    # in most circumstances.
    30    name: cockroachdb
    31    labels:
    32      app: cockroachdb
    33    annotations:
    34      # This is needed to make the peer-finder work properly and to help avoid
    35      # edge cases where instance 0 comes up after losing its data and needs to
    36      # decide whether it should create a new cluster or try to join an existing
    37      # one. If it creates a new cluster when it should have joined an existing
    38      # one, we'd end up with two separate clusters listening at the same service
    39      # endpoint, which would be very bad.
    40      service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
    41      # Enable automatic monitoring of all instances when Prometheus is running in the cluster.
    42      prometheus.io/scrape: "true"
    43      prometheus.io/path: "_status/vars"
    44      prometheus.io/port: "8080"
    45  spec:
    46    ports:
    47    - port: 26257
    48      targetPort: 26257
    49      name: grpc
    50    - port: 8080
    51      targetPort: 8080
    52      name: http
    53    clusterIP: None
    54    selector:
    55      app: cockroachdb
    56  ---
    57  apiVersion: policy/v1beta1
    58  kind: PodDisruptionBudget
    59  metadata:
    60    name: cockroachdb-budget
    61    labels:
    62      app: cockroachdb
    63  spec:
    64    selector:
    65      matchLabels:
    66        app: cockroachdb
    67    minAvailable: 67%
    68  ---
    69  apiVersion: apps/v1  #  for k8s versions before 1.9.0 use apps/v1beta2  and before 1.8.0 use extensions/v1beta1
    70  kind: StatefulSet
    71  metadata:
    72    name: cockroachdb
    73    labels:
    74      app: cockroachdb
    75  spec:
    76    serviceName: "cockroachdb"
    77    replicas: 3
    78    selector:
    79      matchLabels:
    80        app: cockroachdb
    81    template:
    82      metadata:
    83        labels:
    84          app: cockroachdb
    85      spec:
    86        # Init containers are run only once in the lifetime of a pod, before
    87        # it's started up for the first time. It has to exit successfully
    88        # before the pod's main containers are allowed to start.
    89        # This particular init container does a DNS lookup for other pods in
    90        # the set to help determine whether or not a cluster already exists.
    91        # If any other pods exist, it creates a file in the cockroach-data
    92        # directory to pass that information along to the primary container that
    93        # has to decide what command-line flags to use when starting CockroachDB.
    94        # This only matters when a pod's persistent volume is empty - if it has
    95        # data from a previous execution, that data will always be used.
    96        #
    97        # If your Kubernetes cluster uses a custom DNS domain, you will have
    98        # to add an additional arg to this pod: "-domain=<your-custom-domain>"
    99        initContainers:
   100        - name: bootstrap
   101          image: cockroachdb/cockroach-k8s-init:0.2
   102          imagePullPolicy: IfNotPresent
   103          args:
   104          - "-on-start=/on-start.sh"
   105          - "-service=cockroachdb"
   106          env:
   107          - name: POD_NAMESPACE
   108            valueFrom:
   109              fieldRef:
   110                fieldPath: metadata.namespace
   111          volumeMounts:
   112          - name: datadir
   113            mountPath: "/cockroach/cockroach-data"
   114        affinity:
   115          podAntiAffinity:
   116            preferredDuringSchedulingIgnoredDuringExecution:
   117            - weight: 100
   118              podAffinityTerm:
   119                labelSelector:
   120                  matchExpressions:
   121                  - key: app
   122                    operator: In
   123                    values:
   124                    - cockroachdb
   125                topologyKey: kubernetes.io/hostname
   126        containers:
   127        - name: cockroachdb
   128          image: cockroachdb/cockroach:v1.1.0
   129          imagePullPolicy: IfNotPresent
   130          ports:
   131          - containerPort: 26257
   132            name: grpc
   133          - containerPort: 8080
   134            name: http
   135          volumeMounts:
   136          - name: datadir
   137            mountPath: /cockroach/cockroach-data
   138          command:
   139            - "/bin/bash"
   140            - "-ecx"
   141            - |
   142              # The use of qualified `hostname -f` is crucial:
   143              # Other nodes aren't able to look up the unqualified hostname.
   144              CRARGS=("start" "--logtostderr" "--insecure" "--host" "$(hostname -f)" "--http-host" "0.0.0.0")
   145              # We only want to initialize a new cluster (by omitting the join flag)
   146              # if we're sure that we're the first node (i.e. index 0) and that
   147              # there aren't any other nodes running as part of the cluster that
   148              # this is supposed to be a part of (which indicates that a cluster
   149              # already exists and we should make sure not to create a new one).
   150              # It's fine to run without --join on a restart if there aren't any
   151              # other nodes.
   152              if [ ! "$(hostname)" == "cockroachdb-0" ] || \
   153                 [ -e "/cockroach/cockroach-data/cluster_exists_marker" ]
   154              then
   155                # We don't join cockroachdb in order to avoid a node attempting
   156                # to join itself, which currently doesn't work
   157                # (https://github.com/cockroachdb/cockroach/issues/9625).
   158                CRARGS+=("--join" "cockroachdb-public")
   159              fi
   160              exec /cockroach/cockroach ${CRARGS[*]}
   161        # No pre-stop hook is required, a SIGTERM plus some time is all that's
   162        # needed for graceful shutdown of a node.
   163        terminationGracePeriodSeconds: 60
   164        volumes:
   165        - name: datadir
   166          persistentVolumeClaim:
   167            claimName: datadir
   168    volumeClaimTemplates:
   169    - metadata:
   170        name: datadir
   171      spec:
   172        accessModes:
   173          - "ReadWriteOnce"
   174        resources:
   175          requests:
   176            storage: 1Gi