Skip to content

Commit 1b5925c

Browse files
priyolahiriNikolas De Giorgis
and
Nikolas De Giorgis
authored
CLOUDP-90359: Tested split-horizon and added docs (#629)
* CLOUDP-90359: Tested split-horizon and added docs Tested split horizon, added docs and example manifests for deploying MongoDB with external access. * CLOUDP-90359: Added kube-linter annotation to ignore Added kube-linter annotation to ignore external_services.yaml * CLOUDP-90359: Fixed annotation Fixed kube-linter annotation * Update docs/external_access.md Co-authored-by: Nikolas De Giorgis <[email protected]> * CLOUDP-90359: Implemented PR feedback Removed namespaces from yml files, corrected cert retrieval step, misc other changes Co-authored-by: Nikolas De Giorgis <[email protected]>
1 parent 829a384 commit 1b5925c

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
apiVersion: cert-manager.io/v1alpha2
3+
kind: Certificate
4+
metadata:
5+
name: cert-manager-certificate
6+
spec:
7+
secretName: mongodb-tls
8+
issuerRef:
9+
name: ca-issuer
10+
kind: Issuer
11+
commonName: "*.<mongodb-name>-svc.<your-namespace>.svc.cluster.local"
12+
dnsNames:
13+
- "*.<mongodb-name>-svc.<your-namespace>.svc.cluster.local"
14+
- <domain-rs-1>
15+
- <domain-rs-2>
16+
- <domain-rs-3>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
apiVersion: cert-manager.io/v1alpha2
3+
kind: Issuer
4+
metadata:
5+
name: ca-issuer
6+
spec:
7+
ca:
8+
secretName: ca-key-pair
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
kind: Service
3+
apiVersion: v1
4+
metadata:
5+
name: external-mongo-service-0
6+
annotations:
7+
kube-linter.io/ignore-all: "used for sample"
8+
spec:
9+
type: NodePort
10+
selector:
11+
app: <mongodb-name>-svc
12+
statefulset.kubernetes.io/pod-name: <mongodb-name>-0
13+
ports:
14+
- protocol: TCP
15+
nodePort: 31181
16+
port: 31181
17+
targetPort: 27017
18+
19+
20+
---
21+
kind: Service
22+
apiVersion: v1
23+
metadata:
24+
name: external-mongo-service-1
25+
annotations:
26+
kube-linter.io/ignore-all: "used for sample"
27+
spec:
28+
type: NodePort
29+
selector:
30+
app: <mongodb-name>-svc
31+
statefulset.kubernetes.io/pod-name: <mongodb-name>-1
32+
ports:
33+
- nodePort: 31182
34+
port: 31182
35+
targetPort: 27017
36+
37+
38+
---
39+
kind: Service
40+
apiVersion: v1
41+
metadata:
42+
name: external-mongo-service-2
43+
annotations:
44+
kube-linter.io/ignore-all: "used for sample"
45+
spec:
46+
type: NodePort
47+
selector:
48+
app: <mongodb-name>-svc
49+
statefulset.kubernetes.io/pod-name: <mongodb-name>-2
50+
ports:
51+
- nodePort: 31183
52+
port: 31183
53+
targetPort: 27017
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
apiVersion: mongodbcommunity.mongodb.com/v1
3+
kind: MongoDBCommunity
4+
metadata:
5+
name: <mongodb-name>
6+
spec:
7+
members: 3
8+
type: ReplicaSet
9+
version: "4.2.6"
10+
replicaSetHorizons:
11+
- horizon: <domain-rs-1>:31181
12+
- horizon: <domain-rs-2>:31182
13+
- horizon: <domain-rs-3>:31183
14+
security:
15+
tls:
16+
enabled: true
17+
certificateKeySecretRef:
18+
name: mongodb-tls
19+
caConfigMapRef:
20+
name: ca-config-map
21+
authentication:
22+
modes: ["SCRAM"]
23+
users:
24+
- name: my-user
25+
db: admin
26+
passwordSecretRef: # a reference to the secret that will be used to generate the user's password
27+
name: my-user-password
28+
roles:
29+
- name: clusterAdmin
30+
db: admin
31+
- name: userAdminAnyDatabase
32+
db: admin
33+
scramCredentialsSecretName: my-scram
34+
35+
36+
# the user credentials will be generated from this secret
37+
# once the credentials are generated, this secret is no longer required
38+
---
39+
apiVersion: v1
40+
kind: Secret
41+
metadata:
42+
name: my-user-password
43+
type: Opaque
44+
stringData:
45+
password: <your-admin-password>
46+

docs/external_access.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
## Enabling External Access to MongoDB deployment
2+
3+
This guide assumes that the operator is installed and a MongoDB deployment is yet to be done but you have a chosen namespace that you are installing into. We will install cert-manager and then generate certificates and configure split-horizon to support internal and external DNS names for configuring external access to the replicaset.
4+
5+
### Install cert-manager
6+
7+
```sh
8+
kubectl create namespace cert-manager
9+
helm repo add jetstack https://charts.jetstack.io
10+
helm repo update
11+
helm install \
12+
cert-manager jetstack/cert-manager \
13+
--namespace cert-manager \
14+
--version v1.3.1 \
15+
--set installCRDs=true
16+
```
17+
18+
### Install mkcert and generate CA
19+
20+
```sh
21+
brew install mkcert # for Mac
22+
#for Linux / Windows systems look at https://github.com/FiloSottile/mkcert
23+
mkcert -install
24+
```
25+
26+
Execute ```mkcert --CAROOT``` to note the location of the generated root CA key and cert.
27+
28+
### Retrieve the CA and create configmaps and secrets
29+
30+
Use the files that you found in the previous step. Replace ```<your-namespace>``` with your chosen namespace
31+
32+
```sh
33+
kubectl create configmap ca-config-map --from-file=ca.crt --namespace <your-namespace>
34+
35+
kubectl create secret tls ca-key-pair --cert=ca.crt --key=ca.key --namespace <your-namespace>
36+
```
37+
38+
### Create the Cert Manager issuer and secret
39+
40+
Edit the file ```cert-manager-certificate.yaml``` to replace ```<mongodb-name>``` with your MongoDB deployment name. Also replace ```<domain-rs-1>```, ```<domain-rs-2>```, and ```<domain-rs-3>``` with the external FQDNs of the MongoDB replicaset members. Please remember that you will have to add an equal number of entries for each member of the replicaset.
41+
42+
Apply the manifests. Replace ```<your-namespace>``` with the namespace you are using for the deployment.
43+
44+
```sh
45+
kubectl apply -f config/samples/external_access/cert-manager-issuer.yaml --namespace <your-namespace>
46+
kubectl apply -f config/samples/external_access/cert-manager-certificate.yaml --namespace <your-namespace>
47+
```
48+
49+
### Create the MongoDB deployment
50+
51+
Edit ```config/samples/external_access/mongodb.com_v1_mongodbcommunity_cr.yaml```. Replace <mongodb-name> with the desired MongoDB deployment name -- this should be the same as in the previous step. Replace ```<domain-rs-1>```, ```<domain-rs-2>```, and ```<domain-rs-3>``` with the external FQDNs of the MongoDB replicaset members. Please remember that you should have the same number of entries in this section as the number of your replicaset members. You can also edit the ports for external access to your preferred numbers in this section -- you will have to remember to change them in the next step too. Change ```<your-admin-password>``` to your desired admin password for MongoDB.
52+
53+
Apply the manifest.
54+
55+
```sh
56+
kubectl apply -f config/samples/external_access/mongodb.com_v1_mongodbcommunity_cr.yaml
57+
```
58+
59+
Wait for the replicaset to be available.
60+
61+
### Create the external NodePort services for accessing the MongoDB deployment from outside the Kubernetes cluster
62+
63+
Edit ```config/samples/external_access/external_services.yaml``` and replace ```<mongodb-name>``` with the MongoDB deployment name that you have used in the preceeding steps. You can change the ```nodePort``` and ```port``` to reflect the changes (if any) you have made in the previous steps.
64+
65+
Apply the manifest.
66+
67+
```sh
68+
kubectl apply -f config/samples/external_access/external_services.yaml
69+
```
70+
71+
### Retrieve the certificates from a MongoDB replicaset member
72+
73+
```sh
74+
kubectl exec --namespace mcommunity -it <mongodb-name>-0 -c mongod -- bash
75+
```
76+
77+
Once inside the container ```cat``` and copy the contents of the ```.pem``` file in ```/var/lib/tls/server``` into a file on your local system.
78+
79+
### Connect to the MongoDB deployment from outside the Kubernetes cluster
80+
81+
This is an example to connect to the MongoDB cluster with Mongo shell. Use the CA from ```mkcert``` and the certificate from the previous step. Replace the values in the command from the preceeding steps.
82+
83+
```sh
84+
mongosh --tls --tlsCAfile ca.crt --tlsCertificateKeyFile key.pem --username my-user --password <your-admin-password> mongodb://<domain-rs-1>:31181,<domain-rs-2>:31182,<domain-rs-3>:31183
85+
```
86+
87+
### Conclusion
88+
At this point, you should be able to connect to the MongoDB deployment from outside the cluster. Make sure that you can resolve to the FQDNs for the replicaset members where you have the Mongo client installed.

0 commit comments

Comments
 (0)