如何在Kubernetes中从外部公开StatefulSet cassandra集群的无头服务

时间:2019-03-26 12:16:51

标签: apache-spark cassandra kubernetes

我要在kubernetes(Azure AKS)中设置多节点cassandra集群,因为这是一个具有全状态pod的无头服务,而没有外部IP。 如何将我的spark应用程序与kubernetes集群中的cassandra连接

我们已经尝试使用集群ip和入口ip,但是只有一个Pod正在起床休息。

我有3个清单:

  1. 服务
  2. PersistentVolumeClaim
  3. StatefulSet
apiVersion: v1
kind: Service
metadata:
  labels:
    app: cassandra
  name: cassandra
spec:
  clusterIP: None
  ports:
    - port: 9042
  selector:
    app: cassandra


kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: myvolume-disk-claim
spec:
  storageClassName: default
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi


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:
      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
          env:
            - name: CASSANDRA_SEEDS
              value: cassandra-0.cassandra.default.svc.cluster.local
            - name: MAX_HEAP_SIZE
              value: 256M
            - name: HEAP_NEWSIZE
              value: 100M
            - name: CASSANDRA_CLUSTER_NAME
              value: "Cassandra"
            - name: CASSANDRA_DC
              value: "DC1"
            - name: CASSANDRA_RACK
              value: "Rack1"
            - name: CASSANDRA_ENDPOINT_SNITCH
              value: GossipingPropertyFileSnitch
            - name: POD_IP
              valueFrom:
                fieldRef:
                   fieldPath: status.podIP
          readinessProbe:
            exec:
              command:
              - /bin/bash
              - -c
              - /ready-probe.sh
            initialDelaySeconds: 15
            timeoutSeconds: 5        
          volumeMounts:
          - mountPath: /var/lib/cassandra/data
            name: myvolume-disk-claim
  volumeClaimTemplates:
  - metadata:
      name: myvolume-disk-claim
    spec:
      storageClassName: default
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 10Gi

预期结果:(公共ip作为外部IP)

dspg@Digiteds28:$ kubectl get svc
NAME                      TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
kubernetes                ClusterIP   10.0.0.1     <none>        443/TCP    1h
cassandra                 ClusterIP   None        154.167.90.98  9042/TCP   1h



dspg@Digiteds28:$ kubectl get pod
NAME                READY   STATUS    RESTARTS   AGE
cassandra-0         1/1     Running   0          59m
cassandra-1         1/1     Running   0          58m
cassandra-2         1/1     Running   0          56m

实际输出:

dspg@Digiteds28:$ kubectl get svc
NAME                      TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
kubernetes                ClusterIP   10.0.0.1     <none>        443/TCP    1h
cassandra                 ClusterIP   None         <none>        9042/TCP   1h



dspg@Digiteds28:$ kubectl get pod
NAME                READY   STATUS    RESTARTS   AGE
cassandra-0         1/1     Running   0          59m
cassandra-1         1/1     Running   0          58m
cassandra-2         1/1     Running   0          56m

现在,这不包括用于连接到应用程序的外部IP。

2 个答案:

答案 0 :(得分:4)

这取决于您要尝试执行的操作。如果需要外部IP,则通常需要创建一个附加的Service对象(可能为type: LoadBalancer),如下所示:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: cassandra
  name: cassandra-ext
spec:
  type: LoadBalancer
  ports:
    - port: 9042
  selector:
    app: cassandra

如果您需要从群集中访问它,则使用另一个Pod中的DNS名称cassandra-0.cassandra.default(如果StatefulSet部署在默认名称空间中)

答案 1 :(得分:0)

apiVersion: v1
kind: Service
metadata:
  labels:
    app: cassandra
  name: cassandra-ext
spec:
  type: LoadBalancer
  ports:
    - port: 9042
  selector:
    app: cassandra

使用附加服务为我提供了一个外部IP,并且因为如果我开始将我的spark或devcenter应用程序连接到cassandra集群,