用Helm 部署NATS

The NATS Helm charts can be used to deploy a StatefulSet of NATS servers using Helm templates which are easy to extend. Using Helm3 you can add the NATS Helm repo as follows:

helm repo add nats https://nats-io.github.io/k8s/helm/charts/
helm install my-nats nats/nats

The ArtifactHub NATS Helm packagearrow-up-right contains a complete list of configuration options. Some common scenarios are outlined below.

Configuration

Server Image

nats:
  image: nats:2.7.4-alpine
  pullPolicy: IfNotPresent

Limits

nats:
  # The number of connect attempts against discovered routes.
  connectRetries: 30

  # How many seconds should pass before sending a PING
  # to a client that has no activity.
  pingInterval:

  # Server settings.
  limits:
    maxConnections:
    maxSubscriptions:
    maxControlLine:
    maxPayload:

    writeDeadline:
    maxPending:
    maxPings:
    lameDuckDuration:

  # Number of seconds to wait for client connections to end after the pod termination is requested
  terminationGracePeriodSeconds: 60

Logging

Note: It is not recommended to enable trace or debug in production since enabling it will significantly degrade performance.

TLS setup for client connections

You can find more on how to set up and troubleshoot TLS connections at: running-a-nats-service/configuration/securing_nats/tls

Example of creating the nats-client-tls k8s secret with three named values matching the above setup:

Clustering

If clustering is enabled, then a 3-node cluster will be set up. More info at: running-a-nats-server/configuration/clustering#nats-server-clustering

Example:

Leafnodes

Leafnode connections to extend a cluster. More info at: running-a-nats-server/configuration/leafnodes

Websocket Configuration

Setting up External Access

Using HostPorts

In case of both external access and advertisements being enabled, an initializer container will be used to gather the public IPs. This container will be required to have enough RBAC policy to be able to make a look up of the public IP of the node where it is running.

For example, to set up external access for a cluster and advertise the public IP to clients:

Where the service account named nats-server has the following RBAC policy for example:

The container image of the initializer can be customized via:

Using LoadBalancers

When using a load balancer for external access, it is recommended to disable advertisement so that internal IPs from the NATS Servers are not advertised to the clients connecting through the load balancer.

You could then use an L4 enabled load balancer to connect to NATS, for example:

Gateways

A supercluster can be formed by pointing to remote gateways. You can find more about gateways in the NATS documentation: running-a-nats-server/configuration/gateways.

Auth setup

Auth with a Memory Resolver

Auth using an Account Server Resolver

JetStream

Setting up Memory and File Storage

File Storage is always recommended, since JetStream's RAFT Meta Group will be persisted to file storage. The Storage Class used should be block storage. NFS is not recommended.

Using with an existing PersistentVolumeClaim

For example, given the following PersistentVolumeClaim:

You can start JetStream so that one pod is bound to it:

Clustering example

Misc

NATS Box

A lightweight container with NATS and NATS Streaming utilities deployed along the cluster to confirm the setup. You can find the image at: https://github.com/nats-io/nats-boxarrow-up-right

Configuration Reload sidecar

The NATS config reloader image to use:

Prometheus Exporter sidecar

You can toggle whether to start the sidecar to be used to feed metrics to Prometheus:

Prometheus operator ServiceMonitor support

You can enable Prometheus operator ServiceMonitor:

Pod Customizations

Security Context

Affinity

https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinityarrow-up-right

matchExpressions must be configured according to your setup

Service topology

Service topologyarrow-up-right is disabled by default but can be enabled by setting topologyKeys. For example:

CPU/Memory Resource Requests/Limits

Sets the pods CPU/memory requests/limits

No resources are set by default.

Annotations

https://kubernetes.io/docs/concepts/overview/working-with-objects/annotationsarrow-up-right

Name Overrides

Can change the name of the resources as needed with:

Image Pull Secrets

Adds this to the StatefulSet:

最后更新于