连接到Cassandra从工作到不同的吊舱

ghhkc1vu  于 2021-06-13  发布在  Cassandra
关注(0)|答案(3)|浏览(269)

当我试图执行以下命令时

["/bin/sh", "-c", "cqlsh cassandra.my-namespace.svc.cluster.local -f /path/to/schema.cql"]

从我的工作中,我收到以下错误:

Traceback (most recent call last):
  File "/usr/bin/cqlsh.py", line 2443, in <module>
    main(*read_options(sys.argv[1:], os.environ))
  File "/usr/bin/cqlsh.py", line 2421, in main
    encoding=options.encoding)
  File "/usr/bin/cqlsh.py", line 485, in __init__
    load_balancing_policy=WhiteListRoundRobinPolicy([self.hostname]),
  File "/usr/share/cassandra/lib/cassandra-driver-internal-only-3.11.0-bb96859b.zip/cassandra-driver-3.11.0-bb96859b/cassandra/policies.py", line 417, in __init__
socket.gaierror: [Errno -2] Name or service not known

我的工作被定义为与 post-install 注解。我的cassandra pod是用statefulset定义的。

kind: StatefulSet
metadata:
  name: cassandra
spec:
  serviceName: cassandra
  replicas: 1
  template:
    metadata:
      labels:
        app: cassandra
    spec:
      containers:
        - name: cassandra
          image: cassandra:3
          imagePullPolicy: IfNotPresent
          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
          volumeMounts:
            - name: cassandra-data
              mountPath: /var/lib/cassandra/data
  volumeClaimTemplates:
    - metadata:
        name: cassandra-data
        annotations:  # comment line if you want to use a StorageClass
          # or specify which StorageClass
          volume.beta.kubernetes.io/storage-class: ""   # comment line if you
          # want to use a StorageClass or specify which StorageClass
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi

这是我的服务:

kind: Service
metadata:
  labels:
    app: cassandra
  name: cassandra
spec:
  clusterIP: None
  ports:
    - port: 9042
  selector
    app: cassandra

当我运行 cqlsh 从容器中手动命令,一切正常。不幸的是,自动化解决方案抛出了上述错误。
我在服务配置中遗漏了什么吗?我想既然我从job创建的pod连接到服务,它就应该工作了。
编辑:作业如下所示:

apiVersion: batch/v1
kind: Job
metadata:
  name: init-db
spec:
  template:
    metadata: 
      name: init-db
      annotations: 
        "helm.sh/hooks": postn-install
    spec:
      containers:
      - name: cqlsh
        image: <cassandra-image>
        command: ["/bin/sh", "-c", "cqlsh cassandra.my-namespace.svc.cluster.local -f /path/to/schema.cql"]
        volumeMounts:
        - name: cass-init
          mountPath: /etc/config
    volumes:
      ...

这是 etc/resolv.conf :

nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndtos:5
ncgqoxb0

ncgqoxb01#

您发布的错误表明,无论您在哪里运行cqlsh命令,它都无法解析服务名称。
根据k8s集群的配置方式和作业的运行位置(在同一k8s集群内部或外部),您需要使用 Ingress 或者 NodePort .
啤酒š nosek在他的博客文章中很好地解释了如何访问pods。干杯!

tkclm6bt

tkclm6bt2#

由于您将insto作为服务mesh运行,因此您的设置可能存在一些问题。
首先,我建议为两个pod启用特使日志: kubectl exec -it <pod-name> -c istio-proxy -- curl -X POST http://localhost:15000/logging?level=trace 不,按照istio侧车日志 kubectl logs <pod-name> -c isiot-proxy -f 这可能已经为您提供了一些信息,说明连接失败的原因。
一些可能存在问题的想法:
Cassandra的吊舱没有侧车
如果cassandra pod没有侧车,而您正在强制执行mtls,它将接收加密的通信量,但无法对其进行解密。这与文档相矛盾,似乎是一个bug。您可以使用destinationrule为cassandra服务的流量禁用MTL:

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: cassandra-disable-mtls
spec:
  host: "cassandra-host" #insert correct cassandra name here
  trafficPolicy:
    tls:
      mode: DISABLE

工作舱没有侧车
如果cassandras pod有一个sidecar,但是作业的pod没有,并且您强制使用mtls,那么cassandras sidecar将拒绝访问,因为流量没有加密。您可以将侧车插入作业的pod(推荐)或启用 PERMISSIVE 模式,因此sidecar将同时接受ssl加密和纯文本通信。

apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: cassandra-allow-plain-traffic
spec:
  selector:
    matchLabels:
      app: cassandra
  mtls:
    mode: PERMISSIVE

如果你需要进一步的帮助,请告诉我。

carvr3hs

carvr3hs3#

statefulset没有选择器,因此所讨论的yaml无法工作。如果您使用下面的yaml,它也可以工作并连接到作业。所有内容都将部署在默认命名空间中。您需要使用适当的存储类(下面的示例使用标准),您可以从这里开始遵循cassandra部署

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cassandra
spec:
  serviceName: cassandra
  replicas: 1
  selector:
    matchLabels:
      app: cassandra
  template:
    metadata:
      labels:
        app: cassandra
    spec:
      containers:
        - name: cassandra
          image: cassandra:3
          imagePullPolicy: IfNotPresent
          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
          volumeMounts:
            - name: cassandra-data
              mountPath: /var/lib/cassandra/data
  volumeClaimTemplates:
    - metadata:
        name: cassandra-data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: standard
        resources:
          requests:
            storage: 1Gi

服务yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: cassandra
  name: cassandra
spec:
  clusterIP: None
  ports:
  - port: 9042
  selector:
    app: cassandra

在作业yaml中,该命令已被修改为在连接和打印所需的集群信息时描述集群。

apiVersion: batch/v1
kind: Job
metadata:
  name: init-db
spec:
  template:
    metadata: 
      name: init-db
      annotations: 
        "helm.sh/hooks": postn-install
    spec:
      restartPolicy: Never
      containers:
      - name: cqlsh
        image: cassandra:3
        command: ["/bin/sh", "-c", "cqlsh cassandra.default.svc.cluster.local 9042 -e 'describe cluster'"]

相关问题