为什么“Cassandra”对“Kubernetes”使用“StatefulSet”而不是“Deploymdnt”文件?

为什么“Cassandra”对“Kubernetes”使用“StatefulSet”而不是“Deploymdnt”文件?

我正在尝试部署Cassandra在我运行的本地Kind集群上Ubuntu 22.04。我找到的唯一指令是,使用 来StatefulSet表示。我只是想知道,Deployment文件不是更新的东西吗?为什么他们不使用Deployment文件而使用StatefulSet?如果使用文件更好Deployment,有人能帮我将其转换StatefulSetDeployment文件吗?

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cassandra
  labels:
    app: cassandra
spec:
  serviceName: cassandra
  replicas: 3
  selector:
    matchLabels:
      app: cassandra
  template:
    metadata:
      labels:
        app: cassandra
    spec:
      terminationGracePeriodSeconds: 1800
      containers:
      - name: cassandra
        image: gcr.io/google-samples/cassandra:v13
        imagePullPolicy: Always
        ports:
        - containerPort: 7000
          name: intra-node
        - containerPort: 7001
          name: tls-intra-node
        - containerPort: 7199
          name: jmx
        - containerPort: 9042
          name: cql
        resources:
          limits:
            cpu: "500m"
            memory: 1Gi
          requests:
            cpu: "500m"
            memory: 1Gi
        securityContext:
          capabilities:
            add:
              - IPC_LOCK
        lifecycle:
          preStop:
            exec:
              command: 
              - /bin/sh
              - -c
              - nodetool drain
        env:
          - name: MAX_HEAP_SIZE
            value: 512M
          - name: HEAP_NEWSIZE
            value: 100M
          - name: CASSANDRA_SEEDS
            value: "cassandra-0.cassandra.default.svc.cluster.local"
          - name: CASSANDRA_CLUSTER_NAME
            value: "K8Demo"
          - name: CASSANDRA_DC
            value: "DC1-K8Demo"
          - name: CASSANDRA_RACK
            value: "Rack1-K8Demo"
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
        readinessProbe:
          exec:
            command:
            - /bin/bash
            - -c
            - /ready-probe.sh
          initialDelaySeconds: 15
          timeoutSeconds: 5
        # 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-data
          mountPath: /cassandra_data

  # These are converted to volume claims by the controller
  # and mounted at the paths mentioned above.
  # do not use these in production until ssd GCEPersistentDisk or other
  # ssd pd
  volumeClaimTemplates:
  - metadata:
      name: cassandra-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: fast
      resources:
        requests:
          storage: 1Gi
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: fast
provisioner: k8s.io/minikube-hostpath
parameters:
  type: pd-ssd

答案1

AStatefulSet不同于 aDeployment。从文档

与 Deployment 类似,StatefulSet 管理基于相同容器规范的 Pod。与 Deployment 不同,StatefulSet 为每个 Pod 维护一个固定身份。这些 Pod 是根据相同规范创建的,但不可互换:每个 Pod 都有一个持久标识符,该标识符在任何重新调度后都会保留。

当你的 Pod 需要维护某种唯一状态时,可以使用 StatefulSet —— 例如,volumeClaimTemplates清单的部分意味着每个 Pod 都会获得一个唯一的PersistentVolumeClaim。使用 则无法实现这一点Deployment

一般来说,除非您只打算拥有一个副本,否则不能将 a 转换StatefulSet为 a 。Deployment

相关内容