add MongoDB how to guide (#221)

This commit is contained in:
Yaron Schneider 2019-11-01 14:32:14 -07:00 committed by GitHub
parent 8fa330f149
commit f541473d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 0 deletions

View File

@ -48,4 +48,5 @@ kubectl apply -f statestore.yaml
* [Setup Consul](./setup-consul.md)
* [Setup Memcached](./setup-memcached.md)
* [Setup Azure CosmosDB](./setup-azure-cosmosdb.md)
* [Setup MongoDB](./setup-mongodb.md)
* [Supported State Stores](./supported-state-stores.md)

View File

@ -0,0 +1,103 @@
# Setup MongoDB
## Locally
You can run MongoDB locally using Docker:
```
docker run --name some-mongo -d mongo
```
You can then interact with the server using `localhost:27017`.
## Kubernetes
The easiest way to install MongoDB on Kubernetes is by using the [Helm chart](https://github.com/helm/charts/tree/master/stable/mongodb):
```
helm install --name mongo stable/mongodb
```
This will install MongoDB into the `default` namespace.
To interact with MongoDB, find the service with: `kubectl get svc mongo-mongodb`.
For example, if installing using the example above, the MongoDB host address would be:
`mongo-mongodb.default.svc.cluster.local:27017`
Follow the on-screen instructions to get the root password for MongoDB.
The username will be `admin` by default.
## Create a Dapr component
The next step is to create a Dapr component for MongoDB.
Create the following YAML file named `mongodb.yaml`:
```
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: <name>
spec:
type: state.mongodb
metadata:
- name: host
value: <REPLACE-WITH-HOST> # Required. Example: "mongo-mongodb.default.svc.cluster.local:27017"
- name: username
value: <REPLACE-WITH-USERNAME> # Optional. Example: "admin"
- name: password
value: <REPLACE-WITH-PASSWORD> # Optional.
- name: databaseName
value: <REPLACE-WITH-DATABASE-NAME> # Optional. default: "daprStore"
- name: collectionName
value: <REPLACE-WITH-COLLECTION-NAME> # Optional. default: "daprCollection"
- name: writeconcern
value: <REPLACE-WITH-WRITE-CONCERN> # Optional.
- name: readconcern
value: <REPLACE-WITH-READ-CONCERN> # Optional.
- name: operationTimeout
value: <REPLACE-WITH-OPERATION-TIMEOUT> # Optional. default: "5s"
```
The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described [here](../../concepts/components/secrets.md).
The following example uses the Kubernetes secret store to retrieve the username and password:
```
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: <name>
spec:
type: state.mondodb
metadata:
- name: host
value: <REPLACE-WITH-HOST>
- name: username
secretKeyRef:
name: <KUBERNETES-SECRET-NAME>
key: <KUBERNETES-SECRET-KEY>
- name: password
secretKeyRef:
name: <KUBERNETES-SECRET-NAME>
key: <KUBERNETES-SECRET-KEY>
...
```
## Apply the configuration
### In Kubernetes
To apply the MondoDB state store to Kubernetes, use the `kubectl` CLI:
```
kubectl apply -f mongodb.yaml
```
### Running locally
The Dapr CLI will automatically create a directory named `components` in your current working directory with a Redis component.
To use MongoDB, replace the redis.yaml file with the mongodb.yaml above.

View File

@ -9,3 +9,4 @@
| Hashicorp Consul | :white_check_mark: | :x: |
| etcd | :white_check_mark: | :x: |
| Memcached | :white_check_mark: | :x: |
| MongoDB | :white_check_mark: | :x: |