项目作者: nats-io

项目描述 :
NATS Operator
高级语言: Go
项目地址: git://github.com/nats-io/nats-operator.git
创建时间: 2016-12-04T16:07:52Z
项目社区:https://github.com/nats-io/nats-operator

开源协议:Apache License 2.0

下载


NATS Operator

:warning: The recommended way of running NATS on Kubernetes is by using the Helm charts. If looking for JetStream support, this is supported in the Helm charts. The NATS Operator is not recommended to be used for new deployments.

License Apache 2.0
Build Status
Version

NATS Operator manages NATS clusters atop Kubernetes using CRDs. If looking to run NATS on K8S without the operator you can also find Helm charts in the nats-io/k8s repo. You can also find more info about running NATS on Kubernetes in the docs as well as a minimal setup using StatefulSets only without using the operator to get started here.

Requirements

Introduction

NATS Operator provides a NatsCluster Custom Resources Definition (CRD) that models a NATS cluster.
This CRD allows for specifying the desired size and version for a NATS cluster, as well as several other advanced options:

  1. apiVersion: nats.io/v1alpha2
  2. kind: NatsCluster
  3. metadata:
  4. name: example-nats-cluster
  5. spec:
  6. size: 3
  7. version: "2.1.8"

NATS Operator monitors creation/modification/deletion of NatsCluster resources and reacts by attempting to perform the any necessary operations on the associated NATS clusters in order to align their current status with the desired one.

Installing

NATS Operator supports two different operation modes:

  • Namespace-scoped (classic): NATS Operator manages NatsCluster resources on the Kubernetes namespace where it is deployed.
  • Cluster-scoped (experimental): NATS Operator manages NatsCluster resources across all namespaces in the Kubernetes cluster.

The operation mode must be chosen when installing NATS Operator and cannot be changed later.

Namespace-scoped installation

To perform a namespace-scoped installation of NATS Operator in the Kubernetes cluster pointed at by the current context, you may run:

  1. $ kubectl apply -f https://github.com/nats-io/nats-operator/releases/latest/download/00-prereqs.yaml
  2. $ kubectl apply -f https://github.com/nats-io/nats-operator/releases/latest/download/10-deployment.yaml

This will, by default, install NATS Operator in the default namespace and observe NatsCluster resources created in the default namespace, alone.
In order to install in a different namespace, you must first create said namespace and edit the manifests above in order to specify its name wherever necessary.

WARNING: To perform multiple namespace-scoped installations of NATS Operator, you must manually edit the nats-operator-binding cluster role binding in deploy/00-prereqs.yaml file in order to add all the required service accounts.
Failing to do so may cause all NATS Operator instances to malfunction.

WARNING: When performing a namespace-scoped installation of NATS Operator, you must make sure that all other namespace-scoped installations that may exist in the Kubernetes cluster share the same version.
Installing different versions of NATS Operator in the same Kubernetes cluster may cause unexpected behavior as the schema of the CRDs which NATS Operator registers may change between versions.

Alternatively, you may use Helm to perform a namespace-scoped installation of NATS Operator.
To do so you may go to helm/nats-operator and use the Helm charts found in that repo.

Cluster-scoped installation (experimental)

Cluster-scoped installations of NATS Operator must live in the nats-io namespace.
This namespace must be created beforehand:

  1. $ kubectl create ns nats-io

Then, you must manually edit the manifests in deployment/ in order to reference the nats-io namespace and to enable the ClusterScoped feature gate in the NATS Operator deployment.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nats-operator
  5. namespace: nats-io
  6. spec:
  7. (...)
  8. spec:
  9. containers:
  10. - name: nats-operator
  11. (...)
  12. args:
  13. - nats-operator
  14. - --feature-gates=ClusterScoped=true
  15. (...)

Once you have done this, you may install NATS Operator by running:

  1. $ kubectl apply -f https://github.com/nats-io/nats-operator/releases/latest/download/00-prereqs.yaml
  2. $ kubectl apply -f https://github.com/nats-io/nats-operator/releases/latest/download/10-deployment.yaml

WARNING: When performing a cluster-scoped installation of NATS Operator, you must make sure that there are no other deployments of NATS Operator in the Kubernetes cluster.
If you have a previous installation of NATS Operator, you must uninstall it before performing a cluster-scoped installation of NATS Operator.

Creating a NATS cluster

Once NATS Operator has been installed, you will be able to confirm that two new CRDs have been registered in the cluster:

  1. $ kubectl get crd
  2. NAME CREATED AT
  3. natsclusters.nats.io 2019-01-11T17:16:36Z
  4. natsserviceroles.nats.io 2019-01-11T17:16:40Z

To create a NATS cluster, you must create a NatsCluster resource representing the desired status of the cluster.
For example, to create a 3-node NATS cluster you may run:

  1. $ cat <<EOF | kubectl create -f -
  2. apiVersion: nats.io/v1alpha2
  3. kind: NatsCluster
  4. metadata:
  5. name: example-nats-cluster
  6. spec:
  7. size: 3
  8. version: "1.3.0"
  9. EOF

NATS Operator will react to the creation of such a resource by creating three NATS pods.
These pods will keep being monitored (and replaced in case of failure) by NATS Operator for as long as this NatsCluster resource exists.

Listing NATS clusters

To list all the NATS clusters:

  1. $ kubectl get nats --all-namespaces
  2. NAMESPACE NAME AGE
  3. default example-nats-cluster 2m

TLS support

By using a pair of opaque secrets (one for the clients and then another for the routes),
it is possible to set TLS for the communication between the clients and also for the
transport between the routes:

  1. apiVersion: "nats.io/v1alpha2"
  2. kind: "NatsCluster"
  3. metadata:
  4. name: "nats"
  5. spec:
  6. # Number of nodes in the cluster
  7. size: 3
  8. version: "1.3.0"
  9. tls:
  10. # Certificates to secure the NATS client connections:
  11. serverSecret: "nats-clients-tls"
  12. # Certificates to secure the routes.
  13. routesSecret: "nats-routes-tls"

In order for TLS to be properly established between the nodes, it is
necessary to create a wildcard certificate that matches the subdomain
created for the service from the clients and the one for the routes.

By default, the routesSecret has to provide the files: ca.pem, route-key.pem, route.pem,
for the CA, server private and public key respectively.

  1. $ kubectl create secret generic nats-routes-tls --from-file=ca.pem --from-file=route-key.pem --from-file=route.pem

Similarly, by default the serverSecret has to provide the files: ca.pem, server-key.pem, and server.pem
for the CA, server private key and public key used to secure the connection
with the clients.

  1. $ kubectl create secret generic nats-clients-tls --from-file=ca.pem --from-file=server-key.pem --from-file=server.pem

Consider though that you may wish to independently manage the certificate
authorities for routes between clusters, to support the ability to roll
between CAs or their intermediates.

Any filename in the below can also be an absolute path, allowing you to mount
a CA bundle in a place of your choosing.

NATS also supports kubernetes.io/tls secrets (like the ones managed by cert-manager) and any secrets containing a CA, private and public keys with arbitrary names.
It is possible to overwrite the default names as follows:

  1. apiVersion: "nats.io/v1alpha2"
  2. kind: "NatsCluster"
  3. metadata:
  4. name: "nats"
  5. spec:
  6. # Number of nodes in the cluster
  7. size: 3
  8. version: "1.3.0"
  9. tls:
  10. # Certificates to secure the NATS client connections:
  11. serverSecret: "nats-clients-tls"
  12. # Name of the CA in serverSecret
  13. serverSecretCAFileName: "ca.crt"
  14. # Name of the key in serverSecret
  15. serverSecretKeyFileName: "tls.key"
  16. # Name of the certificate in serverSecret
  17. serverSecretCertFileName: "tls.crt"
  18. # Certificates to secure the routes.
  19. routesSecret: "nats-routes-tls"
  20. # Name of the CA, but not from this secret
  21. routesSecretCAFileName: "/etc/ca-bundle/routes-bundle.pem"
  22. # Name of the key in routesSecret
  23. routesSecretKeyFileName: "tls.key"
  24. # Name of the certificate in routesSecret
  25. routesSecretCertFileName: "tls.crt"
  26. template:
  27. spec:
  28. containers:
  29. - name: "nats"
  30. volumeMounts:
  31. - name: "ca-bundle"
  32. mountPath: "/etc/ca-bundle"
  33. readOnly: true
  34. volumes:
  35. - name: "ca-bundle"
  36. configMap:
  37. name: "our-ca-bundle"

Cert-Manager

If cert-manager is available in your cluster, you can easily generate TLS certificates for NATS as follows:

Create a self-signed cluster issuer (or namespace-bound issuer) to create NATS’ CA certificate:

  1. apiVersion: cert-manager.io/v1alpha2
  2. kind: ClusterIssuer
  3. metadata:
  4. name: selfsigning
  5. spec:
  6. selfSigned: {}

Create your NATS cluster’s CA certificate using the new selfsigning issuer:

  1. apiVersion: cert-manager.io/v1alpha2
  2. kind: Certificate
  3. metadata:
  4. name: nats-ca
  5. spec:
  6. secretName: nats-ca
  7. duration: 8736h # 1 year
  8. renewBefore: 240h # 10 days
  9. issuerRef:
  10. name: selfsigning
  11. kind: ClusterIssuer
  12. commonName: nats-ca
  13. usages:
  14. - cert sign # workaround for odd cert-manager behavior
  15. organization:
  16. - Your organization
  17. isCA: true

Create your NATS cluster issuer based on the new nats-ca CA:

  1. apiVersion: cert-manager.io/v1alpha2
  2. kind: Issuer
  3. metadata:
  4. name: nats-ca
  5. spec:
  6. ca:
  7. secretName: nats-ca

Create your NATS cluster’s server certificate (assuming NATS is running in the nats-io namespace, otherwise, set the commonName and dnsNames fields appropriately):

  1. apiVersion: cert-manager.io/v1alpha2
  2. kind: Certificate
  3. metadata:
  4. name: nats-server-tls
  5. spec:
  6. secretName: nats-server-tls
  7. duration: 2160h # 90 days
  8. renewBefore: 240h # 10 days
  9. usages:
  10. - signing
  11. - key encipherment
  12. - server auth
  13. issuerRef:
  14. name: nats-ca
  15. kind: Issuer
  16. organization:
  17. - Your organization
  18. commonName: nats.nats-io.svc.cluster.local
  19. dnsNames:
  20. - nats.nats-io.svc

Create your NATS cluster’s routes certificate (assuming NATS is running in the nats-io namespace, otherwise, set the commonName and dnsNames fields appropriately):

  1. apiVersion: cert-manager.io/v1alpha2
  2. kind: Certificate
  3. metadata:
  4. name: nats-routes-tls
  5. spec:
  6. secretName: nats-routes-tls
  7. duration: 2160h # 90 days
  8. renewBefore: 240h # 10 days
  9. usages:
  10. - signing
  11. - key encipherment
  12. - server auth
  13. - client auth # included because routes mutually verify each other
  14. issuerRef:
  15. name: nats-ca
  16. kind: Issuer
  17. organization:
  18. - Your organization
  19. commonName: "*.nats-mgmt.nats-io.svc.cluster.local"
  20. dnsNames:
  21. - "*.nats-mgmt.nats-io.svc"

Authorization

Using ServiceAccounts

:warning: The ServiceAccounts uses a very rudimentary approach of config reloading and watching CRDs and advanced K8S APIs that may not be available in your cluster. Instead, the decentralized JWT approach should be preferred, to learn more: https://docs.nats.io/developing-with-nats/tutorials/jwt

The NATS Operator can define permissions based on Roles by using any present ServiceAccount in a namespace.
This feature requires a Kubernetes v1.12+ cluster having the TokenRequest API enabled.
To try this feature using minikube v0.30.0+, you can configure it to start as follows:

  1. $ minikube start \
  2. --extra-config=apiserver.service-account-signing-key-file=/var/lib/minikube/certs/sa.key \
  3. --extra-config=apiserver.service-account-key-file=/var/lib/minikube/certs/sa.pub \
  4. --extra-config=apiserver.service-account-issuer=api \
  5. --extra-config=apiserver.service-account-api-audiences=api,spire-server \
  6. --extra-config=apiserver.authorization-mode=Node,RBAC \
  7. --extra-config=kubelet.authentication-token-webhook=true

Please note that availability of this feature across Kubernetes offerings may vary widely.

ServiceAccounts integration can then be enabled by setting the
enableServiceAccounts flag to true in the NatsCluster configuration.

  1. apiVersion: nats.io/v1alpha2
  2. kind: NatsCluster
  3. metadata:
  4. name: example-nats
  5. spec:
  6. size: 3
  7. version: "1.3.0"
  8. pod:
  9. # NOTE: Only supported in Kubernetes v1.12+.
  10. enableConfigReload: true
  11. auth:
  12. # NOTE: Only supported in Kubernetes v1.12+ clusters having the "TokenRequest" API enabled.
  13. enableServiceAccounts: true

Permissions for a ServiceAccount can be set by creating a
NatsServiceRole for that account. In the example below, there are
two accounts, one is an admin user that has more permissions.

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: nats-admin-user
  5. ---
  6. apiVersion: v1
  7. kind: ServiceAccount
  8. metadata:
  9. name: nats-user
  10. ---
  11. apiVersion: nats.io/v1alpha2
  12. kind: NatsServiceRole
  13. metadata:
  14. name: nats-user
  15. namespace: nats-io
  16. # Specifies which NATS cluster will be mapping this account.
  17. labels:
  18. nats_cluster: example-nats
  19. spec:
  20. permissions:
  21. publish: ["foo.*", "foo.bar.quux"]
  22. subscribe: ["foo.bar"]
  23. ---
  24. apiVersion: nats.io/v1alpha2
  25. kind: NatsServiceRole
  26. metadata:
  27. name: nats-admin-user
  28. namespace: nats-io
  29. labels:
  30. nats_cluster: example-nats
  31. spec:
  32. permissions:
  33. publish: [">"]
  34. subscribe: [">"]

The above will create two different Secrets which can then be mounted as volumes
for a Pod.

  1. $ kubectl -n nats-io get secrets
  2. NAME TYPE DATA AGE
  3. ...
  4. nats-admin-user-example-nats-bound-token Opaque 1 43m
  5. nats-user-example-nats-bound-token Opaque 1 43m

Please note that NatsServiceRole must be created in the same namespace as
NatsCluster is running, but bound-token will be created for ServiceAccount
resources that can be placed in various namespaces.

An example of mounting the secret in a Pod can be found below:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: nats-user-pod
  5. labels:
  6. nats_cluster: example-nats
  7. spec:
  8. volumes:
  9. - name: "token"
  10. projected:
  11. sources:
  12. - secret:
  13. name: "nats-user-example-nats-bound-token"
  14. items:
  15. - key: token
  16. path: "token"
  17. restartPolicy: Never
  18. containers:
  19. - name: nats-ops
  20. command: ["/bin/sh"]
  21. image: "wallyqs/nats-ops:latest"
  22. tty: true
  23. stdin: true
  24. stdinOnce: true
  25. volumeMounts:
  26. - name: "token"
  27. mountPath: "/var/run/secrets/nats.io"

Then within the Pod the token can be used to authenticate against
the server using the created token.

  1. $ kubectl -n nats-io attach -it nats-user-pod
  2. /go # nats-sub -s nats://nats-user:`cat /var/run/secrets/nats.io/token`@example-nats:4222 hello.world
  3. Listening on [hello.world]
  4. ^C
  5. /go # nats-sub -s nats://nats-admin-user:`cat /var/run/secrets/nats.io/token`@example-nats:4222 hello.world
  6. Can't connect: nats: authorization violation

Using a single secret with the explicit configuration.

Authorization can also be set for the server by using a secret
where the permissions are defined in JSON:

  1. {
  2. "users": [
  3. { "username": "user1", "password": "secret1" },
  4. { "username": "user2", "password": "secret2",
  5. "permissions": {
  6. "publish": ["hello.*"],
  7. "subscribe": ["hello.world"]
  8. }
  9. }
  10. ],
  11. "default_permissions": {
  12. "publish": ["SANDBOX.*"],
  13. "subscribe": ["PUBLIC.>"]
  14. }
  15. }

Example of creating a secret to set the permissions:

  1. kubectl create secret generic nats-clients-auth --from-file=clients-auth.json

Now when creating a NATS cluster it is possible to set the permissions as
in the following example:

  1. apiVersion: "nats.io/v1alpha2"
  2. kind: "NatsCluster"
  3. pmetadata:
  4. name: "example-nats-auth"
  5. spec:
  6. size: 3
  7. version: "1.1.0"
  8. auth:
  9. # Definition in JSON of the users permissions
  10. clientsAuthSecret: "nats-clients-auth"
  11. # How long to wait for authentication
  12. clientsAuthTimeout: 5

Configuration Reload

On Kubernetes v1.12+ clusters it is possible to enable on-the-fly reloading of configuration for the servers that are part of the cluster.
This can also be combined with the authorization support, so in case the user permissions change, then the servers will reload and apply the new permissions.

  1. apiVersion: "nats.io/v1alpha2"
  2. kind: "NatsCluster"
  3. metadata:
  4. name: "example-nats-auth"
  5. spec:
  6. size: 3
  7. version: "1.1.0"
  8. pod:
  9. # Enable on-the-fly NATS Server config reload
  10. # NOTE: Only supported in Kubernetes v1.12+.
  11. enableConfigReload: true
  12. # Possible to customize version of reloader image
  13. reloaderImage: connecteverything/nats-server-config-reloader
  14. reloaderImageTag: "0.2.2-v1alpha2"
  15. reloaderImagePullPolicy: "IfNotPresent"
  16. auth:
  17. # Definition in JSON of the users permissions
  18. clientsAuthSecret: "nats-clients-auth"
  19. # How long to wait for authentication
  20. clientsAuthTimeout: 5

Connecting operated NATS clusters to external NATS clusters

By using the extraRoutes field on the spec you can make the operated
NATS cluster create routes against clusters outside of Kubernetes:

  1. apiVersion: "nats.io/v1alpha2"
  2. kind: "NatsCluster"
  3. metadata:
  4. name: "nats"
  5. spec:
  6. size: 3
  7. version: "1.4.1"
  8. extraRoutes:
  9. - route: "nats://nats-a.example.com:6222"
  10. - route: "nats://nats-b.example.com:6222"
  11. - route: "nats://nats-c.example.com:6222"

It is also possible to connect to another operated NATS cluster as follows:

  1. apiVersion: "nats.io/v1alpha2"
  2. kind: "NatsCluster"
  3. metadata:
  4. name: "nats-v2-2"
  5. spec:
  6. size: 3
  7. version: "1.4.1"
  8. extraRoutes:
  9. - cluster: "nats-v2-1"

Resolvers

The operator only supports the URL() resolver, see example/example-super-cluster.yaml

Development

Building the Docker Image

To build the nats-operator Docker image:

  1. $ docker build -f docker/operator/Dockerfile . -t <image:tag>

To build the nats-server-config-reloader:

  1. $ docker build -f docker/reloader/Dockerfile . -t <image:tag>

You’ll need Docker 17.06.0-ce or higher.