NACK (NATS Controllers for Kubernetes)
repository·main·Indexed 42 days ago
https://github.com/nats-io/nackKubernetes controllers for managing NATS JetStream resources—including Streams, Consumers, Key/Value stores, and Object Stores—and NATS Server configurations using Custom Resource Definitions (CRDs). Features a JetStream Controller with Legacy and Control-loop modes, a NATS Server Config Reloader sidecar, and NATS Boot Config utility.
What's inside NACK
- NACK (NATS Controllers for Kubernetes) provides controllers to manage NATS resources directly via Kubernetes Custom Resource Definitions (CRDs). It is primarily used to manage NATS JetStream resources and NATS Server configurations within a Kubernetes environment.
Use the Account Resource for Connection and TLS
mainThe
Accountresource functions as a connection and authentication configuration for managed resources. It does not create or manage NATS accounts on the server side; instead, it provides the credentials and server information that NACK uses to connect to NATS.You can define
servers,creds(via Kubernetes secrets), andtlssettings (via Kubernetes secrets) within anAccountresource. Once defined, you can link aStreamto it using thespec.accountfield.apiVersion: jetstream.nats.io/v1beta2 kind: Account metadata: name: a spec: name: a servers: - nats://nats:4222 tls: secret: name: nack-a-tls ca: "ca.crt" cert: "tls.crt" key: "tls.key" --- apiVersion: jetstream.nats.io/v1beta2 kind: Stream metadata: name: foo spec: name: foo subjects: ["foo", "foo.">"] storage: file replicas: 1 account: a # Links this stream to Account 'a'Configure KeyValue mirroring
mainYou can create a mirror of a KV Store using the
mirrorfield inKeyValue.spec. This allows you to replicate a KV Store to another location.Mirror Configuration (
KeyValue.spec.mirror):name: Name of the mirror.externalApiPrefix: Prefix for external API.externalDeliverPrefix: Prefix for external delivery.filterSubject: Subject to filter.optStartSeq: Optional starting sequence.optStartTime: Optional start time (must be RFC3339 format).subjectTransforms: A list ofsource/destpairs to transform subjects during mirroring.
# Example mirror configuration snippet spec: mirror: name: "my-mirror" optStartTime: "2023-01-01T00:00:00Z" subjectTransforms: - source: "old.subject" dest: "new.subject"Understand Account.status
mainThe
Account.statusfield provides information about the current state of the Account resource.observedGeneration: The last generation of the spec that the controller observed.conditions: An array of status conditions. Each condition includes:type: The type of condition.status: The status of the condition.reason: A machine-readable reason for the condition.message: A human-readable message describing the condition.lastTransitionTime: When the condition last changed.
JetStream Controller and its modes
mainThe JetStream Controller manages NATS JetStream resources via Kubernetes CRDs. It operates in two distinct modes, which determine the level of resource support available:
- Legacy (default): Supports Streams and Consumers.
- Control-loop (enabled via
--control-loopflag): Provides full support for Streams, Consumers, Key/Value stores, Object Stores, and Accounts.
Critical Requirement: Key/Value stores and Object stores require the controller to be running in
control-loopmode. If you attempt to create KeyValue or ObjectStore resources while the controller is in Legacy mode, they will not be reconciled.Manage Multiple NATS Systems and Accounts
mainNACK provides three primary ways to manage multiple NATS systems or accounts within a single Kubernetes cluster:
- Run Multiple Namespaced Controllers: By setting
namespaced: true(or--set config.namespaced=true), the controller will only reconcile resources within its own namespace. This is useful for isolation. - Use the Accounts Resource: Define
Accountresources that act as connection configurations. EachAccountcan point to different NATS servers and use different credentials. You can then link aStreamor other resource to a specificAccountusing theaccountfield. - Define Connection Config in the CRD Manifest: You can specify connection details (like
servers) directly within the resource manifest (e.g., aStream).- If using the
--control-loopmode, resource-level config always overrides global config. - If NOT using
--control-loop, you must set the--crd-connectflag in the controller configuration.
- If using the
- Run Multiple Namespaced Controllers: By setting
Develop or Build NATS Boot Config locally
mainYou can build the NATS Boot Config project and run it locally, or build a Docker image using the Makefile.
# Build the project make nats-boot-config # Run the project ./nats-boot-config # Build Docker image make nats-boot-config-docker ver=1.2.3Use Kubernetes Secrets for Account credentials
mainInstead of providing raw strings or file paths, you can reference Kubernetes Secrets for sensitive information in an
Accountresource.Credential Secrets (
spec.creds.secret)Use the
namefield to specify the name of the Secret containing the NATS credentials file.TLS Secrets (
spec.tls.secret)Use the
namefield to specify the name of the Secret containing the TLS certificates.Token Secrets (
spec.token.secret)Use the
namefield to specify the Secret, and thetokenfield to specify the specific key within that Secret that holds the token.User/Password Secrets (
spec.user.secret)Use the
namefield to specify the Secret. Use theuserfield to specify the key for the username and thepasswordfield to specify the key for the password.spec: creds: secret: name: nats-creds-secret token: secret: name: nats-token-secret token: "AUTH_TOKEN_KEY" user: secret: name: nats-user-secret user: "USERNAME_KEY" password: "PASSWORD_KEY"Set up NACK for Local Development
mainTo develop locally, you can build the
jetstream-controllerand run it against a local NATS server with JetStream enabled.- Build the controller using
make. - Start a local NATS server with JetStream (
-js). - Run the controller pointing to your local NATS server.
Tip: Use the
-vflag to adjust log verbosity.-v=10will print raw HTTP requests and responses.# Build the controller make jetstream-controller # Start a local JetStream-enabled NATS server nats-server -DV -js # Run the controller ./jetstream-controller -kubeconfig ~/.kube/config -s nats://localhost:4222 # For high verbosity debugging ./jetstream-controller -v=10- Build the controller using
Enable ObjectStore support in the JetStream controller
mainTo use
ObjectStoreresources, you must run the JetStream controller in control-loop mode by using the--control-loopflag.ObjectStoreresources are not supported in the default legacy mode.# Example command to enable control-loop mode # (Actual command depends on your deployment method, e.g., kubectl or a binary execution) jetstream-controller --control-loopEnable Experimental `controller-runtime` Controllers
mainYou can enable the updated
controller-runtimecontrollers by passing the--control-loopargument. These controllers more reliably enforce resource state. When enabled, thejetstream-controllerlogs will contain a diff of any changes the controller has made.Note: If you are migrating from an older version of NACK, no modifications are expected as long as all NATS resources are in-sync with NACK resources.
helm upgrade nack nats/nack \ --set jetstream.nats.url=nats://nats.default.svc.cluster.local:4222 \ --set jetstream.additionalArgs={--control-loop} --waitInstall the NATS Server Config Reloader with Helm
mainThe NATS Server Config Reloader is a sidecar used to automatically reload your NATS Server configuration file. You can install it using the official NATS Helm chart.
helm repo add nats https://nats-io.github.io/k8s/helm/charts/ helm upgrade --install nats nats/nats