k8s 搭建mongodb

2022-11-02  本文已影响0人  繁华丶凋零

1.共享盘创建

mkdir -p /data/mongodb/pv
cat /etc/exports

#/etc/exports内容如下
/data/mongodb/pv *(rw,sync,no_root_squash)
/data/mysql/pv *(rw,sync,no_root_squash)
/data/redis/pv *(rw,sync,no_root_squash)
#重启服务
systemctl restart rpcbind
systemctl restart nfs

2.yaml文件

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongodb-pv
  namespace: default
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-mongodb
  nfs:
    path: /data/mongodb/pv
    server: 1.1.1.1

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mongodb-pvc
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs-mongodb

---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-svc
  namespace: default
spec:
  type: NodePort
  ports:
    - name: mongo
      port: 27017
      targetPort: 27017
      nodePort: 27017
      protocol: TCP
  selector:
    app: mongodb

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-deploy
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
        - name: mongodb
          image: 1.1.1.1:5000/mongo:5.0.9
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-pvc
              mountPath: /data/db
      volumes:
        - name: mongo-pvc
          persistentVolumeClaim:
            claimName: mongodb-pvc

3.创建

kubectl apply -f mongodb.yaml

4.mongo初始化

#登录 pod
#登录mongo

#禁止免认证登录
db.createUser(
  {
    user: "root",
    pwd: "xxxxxxx",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)


#创建库 用户
use devops
db.createUser(
    {
      user: "zhangsan",
      pwd: "yyyyyyy",
      roles: ["readWrite","dbAdmin"]
    }
 )

上一篇下一篇

猜你喜欢

热点阅读