cat <<END>demon.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: test-mongo
name: test-mongo
#namespace: devops
spec:
replicas: 1
selector:
matchLabels:
k8s-app: test-mongo
template:
metadata:
labels:
k8s-app: test-mongo
name: test-mongo
spec:
containers:
- name: test-mongo
image: mongo:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 27017
name: web
protocol: TCP
resources:
limits:
cpu: 8
memory: 8Gi
requests:
cpu: 1
memory: 1000Mi
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: root
- name: MONGO_INITDB_ROOT_PASSWORD
value: 'klvu9chen123'
#securityContext:
#privileged: true
#runAsUser: 0
volumeMounts:
- mountPath: /data/db
name: data
- mountPath: /etc/localtime
name: times
volumes:
- name: data
persistentVolumeClaim:
claimName: mongo
- name: times
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: test-mongo
name: test-mongo
#namespace: devops
spec:
type: NodePort
ports:
- name: web
port: 27017
targetPort: 27017
nodePort: 31177
selector:
k8s-app: test-mongo
END
mongodb 简单操作
# 可以通过 kubectl -n klvhcen exec -it POD_NAME /bin/bash 进入容器
# 进入 mongodb 客户端
mongo admin
# 认证
db.auth('root','klvu9chen123')
# mongodb 创建 test 数据库,创建用户和密码
use test
db.createUser(
{
user: "test",
pwd: "test123",
roles: [ { role: "readWrite", db: "test" } ]
}
)
# 备份,会在当前目录下默认创建名为 dump 的备份文件夹
mongodump --host 127.0.0.1 --port 27017 -u test --authenticationDatabase test
# 恢复
mongorestore --host 127.0.0.1 --port 27017 -u test --authenticationDatabase recommend dump