Skip to content

Commit bc865f3

Browse files
committed
Add mongodb things - complete deployment-mongo.yaml
1 parent a6b0b12 commit bc865f3

File tree

8 files changed

+256
-11
lines changed

8 files changed

+256
-11
lines changed

k8s/deployment-http-echo.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: apps/v1
22
kind: Deployment
33
metadata:
4-
name: deployment-foo
4+
name: foo
55
spec:
66
selector:
77
matchLabels:
@@ -30,7 +30,7 @@ spec:
3030
apiVersion: apps/v1
3131
kind: Deployment
3232
metadata:
33-
name: deployment-bar
33+
name: bar
3434
spec:
3535
selector:
3636
matchLabels:

k8s/deployment-mongo.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/
2+
3+
apiVersion: apps/v1
4+
kind: Deployment
5+
metadata:
6+
name: mongo
7+
spec:
8+
selector:
9+
matchLabels:
10+
app: mongodb
11+
replicas: 3
12+
template:
13+
metadata:
14+
labels:
15+
app: mongodb
16+
spec:
17+
containers:
18+
- name: mongo
19+
image: mongo:5.0.7
20+
args:
21+
# - '--config'
22+
# - '/hostroot/mongo/mongod.conf'
23+
- '--bind_ip'
24+
- '0.0.0.0'
25+
- '--replSet'
26+
- 'anyflow-replset'
27+
- '--auth'
28+
- '--clusterAuthMode'
29+
- 'keyFile'
30+
- '--keyFile'
31+
- '/hostroot/mongo/keyfile'
32+
# - "--setParameter"
33+
# - "authenticationMechanisms=SCRAM-SHA-1"
34+
resources:
35+
limits:
36+
memory: '128Mi'
37+
cpu: '500m'
38+
ports:
39+
- containerPort: 27017
40+
volumeMounts:
41+
- name: hostroot
42+
mountPath: /hostroot
43+
volumes:
44+
- name: hostroot
45+
hostPath:
46+
path: /hostroot

k8s/hostroot_in_node/mongo/keyfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 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deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# mongod.conf
2+
3+
# for documentation of all options, see:
4+
# http://docs.mongodb.org/manual/reference/configuration-options/
5+
6+
# storage:
7+
# dbPath: /var/lib/mongodb
8+
# journal:
9+
# enabled: true
10+
11+
net:
12+
# bindIp: localhost
13+
port: 27017
14+
15+
security:
16+
authorization: enabled
17+
keyFile: /hostroot/mongo/keyfile
18+
19+
systemLog:
20+
# destination: file
21+
# logAppend: true
22+
# path: /var/log/mongodb/mongod.log
23+
destination: syslog
24+
timeStampFormat: iso8601-utc
25+
26+
processManagement:
27+
timeZoneInfo: /usr/share/zoneinfo
28+
# fork: true
29+
30+
replication:
31+
replSetName: anyflow-replset

k8s/kind-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,14 @@ nodes:
3030
protocol: TCP
3131

3232
- role: worker # configuration for worker1 node
33+
extraMounts:
34+
- hostPath: ./hostroot_in_node
35+
containerPath: /hostroot
3336
- role: worker # configuration for worker2 node
37+
extraMounts:
38+
- hostPath: ./hostroot_in_node
39+
containerPath: /hostroot
3440
- role: worker # configuration for worker3 node
41+
extraMounts:
42+
- hostPath: ./hostroot_in_node
43+
containerPath: /hostroot

k8s/mongo.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Description
2+
3+
- mongodb replication cluster를 k8s kind cluster로 설정하
4+
5+
# Prerequisite
6+
7+
- kind cluster 생성 : `./readme.md` 참고
8+
9+
# mongodb replicaset 생성
10+
11+
1. 3개의 mongodb instance 생성
12+
13+
```bash
14+
> kubectl apply -f ./deployment-mongo.yaml
15+
```
16+
17+
2. 특정 mongodb pod에 접근
18+
19+
```bash
20+
> kubectl exec --stdin --tty <mongodb pod name> -- /bin/bash
21+
```
22+
23+
3. mongoshell로 해당 mongodb에 접근
24+
25+
```bash
26+
> mongosh --host localhost:27017
27+
```
28+
29+
4. mongodb localhost exception 설정
30+
31+
```bash
32+
> rs.initiate() #initiate replica set
33+
> use admin # admin db 사용
34+
> db.createUser({ #admin 생성
35+
user: "m103-admin",
36+
pwd: "m103-pass",
37+
roles: [{role: "root", db: "admin"}]
38+
})
39+
> exit #localhost exception 빠져나오기
40+
```
41+
42+
5. replica set cluster 구성
43+
44+
```bash
45+
> mongosh --host localhost:27017 -u admin -p admin --authenticationDatabase admin #admin으로 login
46+
> rs.add('<other mongodb pod ip #1>:27017') #첫 번째 replica를 cluster에 추
47+
> rs.add('<other mongodb pod ip #2>:27017') #두 번째 replica를 cluster에 추
48+
> cfg = rs.conf() # cfg = rs.config()와 동일
49+
> cfg.members[0].host = '<current mongodb pod ip>:27017' # 현재 current node가 pod name으로 host가 설정되어 있는데, 타 노드가 pod name으로는 접근할 수 없기에, ip로 변경
50+
> rs.reconfig(cfg)
51+
...
52+
53+
# 기타 replicaset 관련 명령어
54+
> rs.status()
55+
> rs.isMaster()
56+
> rs.stepDown()
57+
```
58+
59+
6.
60+
## 참고
61+
62+
- 특정 pod bash에 접근 : `kubectl exec --stdin --tty <pod name> -- /bin/bash`
63+
- network util pod 생성 및 shell 로그인 `kubectl run my-shell --rm -i --tty --image praqma/network-multitool -- bash`

k8s/mongo.yaml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#https://maruftuhin.com/blog/mongodb-replica-set-on-kubernetes/
2+
3+
apiVersion: v1
4+
kind: Service
5+
metadata:
6+
name: mongodb-service
7+
labels:
8+
name: mongo
9+
spec:
10+
ports:
11+
- port: 27017
12+
targetPort: 27017
13+
clusterIP: None
14+
selector:
15+
role: mongo
16+
---
17+
apiVersion: apps/v1
18+
kind: StatefulSet
19+
metadata:
20+
name: mongod
21+
spec:
22+
serviceName: mongodb-service
23+
replicas: 3
24+
selector:
25+
matchLabels:
26+
role: mongo
27+
environment: test
28+
replicaset: MainRepSet
29+
template:
30+
metadata:
31+
labels:
32+
role: mongo
33+
environment: test
34+
replicaset: MainRepSet
35+
spec:
36+
containers:
37+
- name: mongod-container
38+
image: mongo:5.0.7
39+
command:
40+
- "numactl"
41+
- "--interleave=all"
42+
- "mongod"
43+
- "--bind_ip"
44+
- "0.0.0.0"
45+
- "--replSet"
46+
- "MainRepSet"
47+
- "--auth"
48+
- "--clusterAuthMode"
49+
- "keyFile"
50+
- "--keyFile"
51+
- "/etc/secrets-volume/internal-auth-mongodb-keyfile"
52+
- "--setParameter"
53+
- "authenticationMechanisms=SCRAM-SHA-1"
54+
resources:
55+
requests:
56+
cpu: 0.2
57+
memory: 200Mi
58+
ports:
59+
- containerPort: 27017
60+
volumeMounts:
61+
- name: secrets-volume
62+
readOnly: true
63+
mountPath: /etc/secrets-volume
64+
- name: mongodb-persistent-storage-claim
65+
mountPath: /data/db
66+
volumes:
67+
- name: secrets-volume
68+
secret:
69+
secretName: shared-bootstrap-data
70+
defaultMode: 256
71+
volumeClaimTemplates:
72+
- metadata:
73+
name: mongodb-persistent-storage-claim
74+
annotations:
75+
volume.beta.kubernetes.io/storage-class: "standard"
76+
spec:
77+
accessModes: [ "ReadWriteOnce" ]
78+
resources:
79+
requests:
80+
storage: 1Gi

k8s/readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
- 특히, 로컬에서의 테스트를 위한 전용 포트포워딩 설정이 중요
5454
- 세부 설정에 대한 설명은 파일 내 comment 참조
5555

56-
- `pods.yaml`
56+
- `pods-http-echo.yaml`, `deployment-http-echo.yaml`
5757
- 2개의 http echo 서버(`foo`, `bar`) 컨테이너 pod 설정 (config type : `pod`)
5858
- http echo 서버는 사전 설정에 따라, 호출 시 단순히 `foo!`, `bar!`를 응답
5959
- 세부 설정에 대한 설명은 파일 내 comment 참조
@@ -249,22 +249,22 @@ foo-app 1/1 Running 0 45s
249249
```bash
250250
> kubectl apply -f ./deployment-http-echo.yaml
251251
...
252-
deployment.apps/deployment-foo created
253-
deployment.apps/deployment-bar created
252+
deployment.apps/foo created
253+
deployment.apps/bar created
254254
...
255255
> kubectl get deployments
256256
...
257257
NAME READY UP-TO-DATE AVAILABLE AGE
258-
deployment-bar 2/2 2 2 13m
259-
deployment-foo 2/2 2 2 13m
258+
bar 2/2 2 2 13m
259+
foo 2/2 2 2 13m
260260
...
261261
> kubectl get pods
262262
...
263263
NAME READY STATUS RESTARTS AGE
264-
deployment-bar-565c58bc76-lv9zn 1/1 Running 0 14m
265-
deployment-bar-565c58bc76-p2zt4 1/1 Running 0 14m
266-
deployment-foo-7d77c84f46-lcwnq 1/1 Running 0 14m
267-
deployment-foo-7d77c84f46-lh6qs 1/1 Running 0 14m
264+
bar-565c58bc76-lv9zn 1/1 Running 0 14m
265+
bar-565c58bc76-p2zt4 1/1 Running 0 14m
266+
foo-7d77c84f46-lcwnq 1/1 Running 0 14m
267+
foo-7d77c84f46-lh6qs 1/1 Running 0 14m
268268
```
269269

270270

0 commit comments

Comments
 (0)