docker的镜像源,docker info
mongodb的yaml文件如下
Registry Mirrors:
https://dockerhub.woa.com/
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongodb
spec:
replicas: 3
serviceName: mongodb
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongo
image: mongo:4.4
# IfNotPresent 仅本地没有镜像时才远程拉,Always 永远都是从远程拉,Never 永远只用本地镜像,本地没有则报错
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: mongodb
spec:
selector:
app: mongodb
type: ClusterIP
# HeadLess
clusterIP: None
ports:
- port: 27017
targetPort: 27017
kubectl apply -f mongo.yaml
来部署
访问时,如果直接使用 Service 名字连接,会随机转发请求
要连接指定 Pod,可以这样 pod-name.service-name
运行一个临时 Pod 连接数据测试下
kubectl run mongodb-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mongodb:4.4.10-debian-10-r20 --command -- bash
kubectl get endpoints mongodb -o yaml
kubectl run mongodb-client --rm --tty -i --restart='Never' --image [docker.io/bitnami/mongodb:4.4.10-debian-10-r20](http://docker.io/bitnami/mongodb:4.4.10-debian-10-r20) --command — bash
mongo --host mongodb-0.mongodb
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use test
switched to db test
> db.users.save({'_id':'easydoc','name':'易文档'})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "easydoc" })
> db.users.find()
{ "_id" : "easydoc", "name" : "易文档” }
> exit
I have no name!@mongodb-client:/$ mongo --host mongodb-1.mongodb
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
> db.users.find()
> use test
switched to db test
> db.users.save({'_id':'machine','name':'mondb-1机器'})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "machine" })
> db.users.find()
{ "_id" : "machine", "name" : "mondb-1机器" }
> exit
I have no name!@mongodb-client:/$ mongo --host mongodb-2.mongodb
> use test
switched to db test
> db.users.save({'_id':'tools','name':'工具箱'})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "tools" })
> db.users.find()
{ "_id" : "tools", "name" : "工具箱" }
> exit
bye
kubectl port-forward service/mongo 28015:27017
以上所有命令都有效。输出类似于:
Forwarding from 127.0.0.1:28015 -> 27017
Forwarding from [::1]:28015 -> 27017
说明:
kubectl port-forward 不会返回。你需要打开另一个终端来继续这个练习
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。