一、NFS部署

# apt-get install nfs-kernel-server nfs-common
root@k8s-master:~/mysql# cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/home/pvdata/mysql_s *(rw,sync,insecure,no_subtree_check,no_root_squash)
/data/nfs/mysql *(rw,sync,insecure,no_subtree_check,no_root_squash)
# systemctl restart nfs-server

二、yaml文件

命名空间统一设置成database和数据库保持一致

root@k8s-master:~/mongo# cat /root/mongo/mongodb-nfs.yaml
# 创建PV
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongodb-nfs-pv
  namespace: database
spec:
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-mongodb
  nfs:
    path: /data/nfs/mongodb
    server: 10.10.100.31
# 创建pvc
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mongodb-nfs-pvc
  namespace: database
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs-mongodb
---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-nfs-svc
  namespace: database
spec:
  type: NodePort
  ports:
  - name: mongo
    port: 27017
    targetPort: 27017
    nodePort: 30017
    protocol: TCP
  selector:
    app: mongodb
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-nfs-deploy
  namespace: database
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo:4.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-pvc
          mountPath: /data/db
      volumes:
       - name: mongo-pvc
         persistentVolumeClaim:
           claimName: mongodb-nfs-pvc

安装

kubectl apply -f  mongodb-nfs.yaml

进入mongo

root@k8s-node12:~# docker exec -it k8s_mongodb_mongo-nfs-deploy-7654dc6877-jvww5_database_1b86786d-1526-4f9f-999f-1021162ab5a5_0  /bin/bash
root@mongo-nfs-deploy-7654dc6877-jvww5:/# mongo
MongoDB shell version v4.0.23
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("4bd4c71a-e937-4c9c-af05-198c1b0a5acc") }
MongoDB server version: 4.0.23
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
2021-04-19T11:58:48.861+0000 I CONTROL  [initandlisten]
2021-04-19T11:58:48.861+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-04-19T11:58:48.861+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2021-04-19T11:58:48.861+0000 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

创建admin管理用户

> use admin
switched to db admin
> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}]})
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}

创建具有读写权限的用户 mongo> db.auth('admin','123456');

1
> db.createUser({ user:'mongo',pwd:'mongo',roles:[ { role:'readWrite', db: 'charge_data'}]});
Successfully added user: {
        "user" : "mongo",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "charge_data"
                }
        ]
}
>

码云地址
https://gitee.com/ethnicity_admin/k8s-deepwise.git

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐