$ kubectl apply -f mongo.yaml statefulset.apps/mongodb created service/mongodb created
# 查看 statefulset $ kubectl get statefulset NAME READY AGE mongodb 3/3 4m37s
# 查看 pod $ kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mongodb-0 1/1 Running 0 2m28s 172.17.0.4 minikube <none> <none> mongodb-1 1/1 Running 0 75s 172.17.0.3 minikube <none> <none> mongodb-2 1/1 Running 0 72s 172.17.0.5 minikube <none> <none>
StatefulSet 特性
Service 的 CLUSTER-IP 是空的,Pod 名字也是固定的。
1 2 3 4 5
# 查看 service $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d8h mongodb ClusterIP None <none> 27017/TCP 4m1s
# mongodb-client: pod 名称 # --rm:表示 临时的,退出即删除 # --image: 使用的镜像地址 # --command -- bash:进入容器内命令行 $ kubectl run mongodb-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mongodb:4.4.10-debian-10-r20 --command -- bash If you don't see a command prompt, try pressing enter. # 使用 'mongodb-0.mongodb'连接其中一个 I have no name!@mongodb-client:/$ $ mongo --host mongodb-0.mongodb MongoDB shell version v4.4.10 connecting to: mongodb://mongodb-0.mongodb:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("4e965766-ac5b-4e81-aaba-c78f404fed68") } MongoDB server version: 4.4.11 --- The server generated these startup warnings when booting: 2022-01-11T10:10:12.341+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2022-01-11T10:10:12.342+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' 2022-01-11T10:10:12.342+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() --- > show dbs admin 0.000GB config 0.000GB local 0.000GB > use test switched to db test > db.users.save({'_id':'buubiu', 'name':'buubiu_k8s'}) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "buubiu" }) > db.user.find() > db.users.find() { "_id" : "buubiu", "name" : "buubiu_k8s" } >