Kubernetes

k8s 单机版mongodb

本文主要是介绍k8s 单机版mongodb,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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
这篇关于k8s 单机版mongodb的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!