1

我是 kubernetes 的新手,正在尝试设置 nats 流集群。我正在使用以下清单文件。但是我对如何在我的应用程序中访问 nats 流服务器感到困惑。我正在使用 azure kubernetes 服务。


---
apiVersion: v1
kind: ConfigMap
metadata:
  name: stan-config
data:
  stan.conf: |
    # listen: nats-streaming:4222
    port: 4222
    http: 8222

    streaming {
      id: stan
      store: file
      dir: /data/stan/store
      cluster {
        node_id: $POD_NAME
        log_path: /data/stan/log
        # Explicit names of resulting peers
        peers: ["nats-streaming-0", "nats-streaming-1", "nats-streaming-2"]
      }
    }
---

apiVersion: v1
kind: Service
metadata:
  name: nats-streaming
  labels:
    app: nats-streaming
spec:
  type: ClusterIP
  selector:
    app: nats-streaming
  ports:
    - port: 4222
      targetPort: 4222

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nats-streaming
  labels:
    app: nats-streaming
spec:
  selector:
    matchLabels:
      app: nats-streaming
  serviceName: nats-streaming
  replicas: 3
  volumeClaimTemplates:
  - metadata:
      name: stan-sts-vol
    spec:
      accessModes:
      - ReadWriteOnce
      volumeMode: "Filesystem"
      resources:
        requests:
          storage: 1Gi
  template:
    metadata:
      labels:
        app: nats-streaming
    spec:
      # Prevent NATS Streaming pods running in same host.
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - topologyKey: "kubernetes.io/hostname"
            labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nats-streaming
      # STAN Server
      containers:
      - name: nats-streaming
        image: nats-streaming
        ports:
        - containerPort: 8222
          name: monitor
        - containerPort: 7777
          name: metrics
        args:
          - "-sc"
          - "/etc/stan-config/stan.conf"

        # Required to be able to define an environment variable
        # that refers to other environment variables.  This env var
        # is later used as part of the configuration file.
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        volumeMounts:
          - name: config-volume
            mountPath: /etc/stan-config
          - name: stan-sts-vol
            mountPath: /data/stan

        # Disable CPU limits.
        resources:
          requests:
            cpu: 0

        livenessProbe:
          httpGet:
            path: /
            port: 8222
          initialDelaySeconds: 10
          timeoutSeconds: 5
      volumes:
      - name: config-volume
        configMap:
          name: stan-config

我尝试使用nats://nats-streaming:4222,但它给出了以下错误。

stan: connect request timeout (possibly wrong cluster ID?)

我指的是https://docs.nats.io/nats-on-kubernetes/minimal-setup

4

1 回答 1

0

您没有在您的内部调用4222nats 的客户端端口StatefulSetService

...
  ports:
    - port: 4222
      targetPort: 4222
...

simple-nats.yml中可以看出,他们设置了以下端口:

...
containers:
      - name: nats
        image: nats:2.1.0-alpine3.10
        ports:
        - containerPort: 4222
          name: client
          hostPort: 4222
        - containerPort: 7422
          name: leafnodes
          hostPort: 7422
        - containerPort: 6222
          name: cluster
        - containerPort: 8222
          name: monitor
        - containerPort: 7777
          name: metrics
        command:
         - "nats-server"
         - "--config"
         - "/etc/nats-config/nats.conf"
...

至于将服务暴露在外部,我建议阅读Using a Service to Exposure Your App and Exposed an External IP Address to Access an Application in a Cluster

还有一篇不错的文章,可能有点旧(2017 年)在 Azure 上向 Kubernetes pod 公开端口,您还可以查看有关快速入门的 Azure 文档:使用 Azure CLI 部署 Azure Kubernetes 服务集群

于 2020-05-08T16:48:40.180 回答