2

I am new to kubernetes and trying to setup nats streaming cluster. I am using following manifest file. But I am confused with how can I access nats streaming server in my application. I am using azure kubernetes service.


---
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

I tried using nats://nats-streaming:4222, but it gives following error.

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

I am referring https://docs.nats.io/nats-on-kubernetes/minimal-setup

Thecoder
  • 51
  • 2
  • 6
  • @Theocoder, your `Service` seems to have some things missing in it such as `- name:` and `protocol: TCP`. I am not familiar with `ConfigMap` setups so I am limited in how I can help. – Daniel Dec 06 '20 at 06:28
  • @Thecoder, were you able to solve this issue ? The error message you get `"stan: connect request timeout (possibly wrong cluster ID?)"` looks like response from nats, so I would say the problem is not that you're not able to connect to it due to `Service` misconfiguration etc. You are connecting successfully with nats as you are able to see its response but most likely it is misconfigured/your cluster has not formed properly etc. What about its logs ? Did you check them ? – mario Mar 19 '21 at 21:01
  • @Thecoder, Not sure why your yaml file is of `kind` `ConfigMap`, should it not be `kind` of `Deployment`? – Daniel Jun 10 '22 at 23:51

1 Answers1

0

You did not specified client port 4222 of nats in the StatefulSet, which you are calling inside your Service

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

As you can see from the simple-nats.yml they have setup the following ports:

...
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"
...

As for exposing the service outside, I would recommend reading Using a Service to Expose Your App and Exposing an External IP Address to Access an Application in a Cluster.

There is also nice article, maybe a bit old (2017) Exposing ports to Kubernetes pods on Azure, you can also check Azure docs about Quickstart: Deploy an Azure Kubernetes Service cluster using the Azure CLI

Crou
  • 10,232
  • 2
  • 26
  • 31
  • Even after specifying client port 4222 of nats in the StatefulSet. It gives same error – Thecoder May 08 '20 at 17:04
  • Which IP are you using to connect to nats? From where are you trying to connect? Have you tested if it's working locally within the cluster? – Crou May 11 '20 at 13:33