vim mongodb.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
nodeName: lan # 固定在 k8s-node1 节点
containers:
- name: mongodb
image: mongo:4.2.9
env:
- name: MONGO_INITDB_ROOT_USERNAME # 设置用户名
value: root
- name: MONGO_INITDB_ROOT_PASSWORD # 设置密码
value: ‘123123‘
volumeMounts:
- mountPath: /data/db
name: mongodb-volume
volumes:
- name: mongodb-volume
hostPath:
path: /data/mongo # 映射的宿主机目录
type: DirectoryOrCreate
---
apiVersion: v1
kind: Service
metadata:
name: mongodb
spec:
type: ClusterIP
selector:
app: mongodb
ports:
- port: 27017
targetPort: 27017