k8s安装mongodb(NFS持久化)

2,938 阅读3分钟

yaml文件

cat <<EOF> /root/k3s_yaml/mongodb_nfs/mongodb-nfs.yaml

# 创建PV
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongodb-nfs-pv
  namespace: default
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-mongodb
  nfs:
    path: /root/data/nfs/mongodb
    server: 192.168.72.100

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

---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-nfs-svc
  namespace: default
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: default
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

EOF

安装

kubectl apply -f /root/k3s_yaml/mongodb_nfs/mongodb-nfs.yaml

查看相关pod

[root@cs100 k3s_yaml]# kubectl get pods -A -o wide

NAMESPACE       NAME                                      READY   STATUS             RESTARTS   AGE     IP           NODE         NOMINATED NODE   READINESS GATES
kube-system     helm-install-traefik-rb69p                0/1     Completed          0          7d20h   10.42.0.9    k3s-master   <none>           <none>
kube-system     metrics-server-86cbb8457f-nkck4           1/1     Running            7          7d20h   10.42.0.83   k3s-master   <none>           <none>
kube-system     local-path-provisioner-5ff76fc89d-s698r   1/1     Running            10         7d20h   10.42.0.87   k3s-master   <none>           <none>
fleet-system    fleet-agent-55bfc495bd-g7mt8              1/1     Running            7          7d19h   10.42.0.85   k3s-master   <none>           <none>
default         redis-84bfc7c68c-zmqwf                    1/1     Running            2          40h     10.42.0.90   k3s-master   <none>           <none>
kube-system     coredns-854c77959c-mvlqz                  1/1     Running            7          7d20h   10.42.0.92   k3s-master   <none>           <none>
kube-system     traefik-6f9cbd9bd4-ppcmc                  1/1     Running            7          7d20h   10.42.0.86   k3s-master   <none>           <none>
db-mysql        mysql-nfs-deploy-5f4fc57696-7cjkn         1/1     Running            6          5d17h   10.42.0.91   k3s-master   <none>           <none>
default         seata-server-fb6557d89-xgz9l              1/1     Running            4          2d22h   10.42.0.89   k3s-master   <none>           <none>
kube-system     svclb-traefik-8rk4w                       2/2     Running            19         7d20h   10.42.0.88   k3s-master   <none>           <none>
cattle-system   cattle-cluster-agent-68b9fbcc5d-ghp2x     0/1     CrashLoopBackOff   642        7d19h   10.42.0.84   k3s-master   <none>           <none>
default         mongo-nfs-deploy-75447854c9-l247c         1/1     Running            0          56s     10.42.0.93   k3s-master   <none>           <none>

进入mongo

[root@cs100 k3s_yaml]# kubectl exec -it mongo-nfs-deploy-75447854c9-l247c -- bash

root@mongo-nfs-deploy-75447854c9-l247c:/# mongo

MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("34c2aa26-783d-4da9-8969-858bae11d186") }
MongoDB server version: 4.4.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2021-04-08T02:30:51.295+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-04-08T02:30:51.295+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-04-08T02:30:51.295+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        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"
		}
	]
}
>