赞
踩
直接上yaml,参考文档:https://github.com/fjc0k/docker-YApi
--- kind: Deployment apiVersion: apps/v1 metadata: labels: app: mongo name: mongo spec: selector: matchLabels: app: mongo template: metadata: labels: app: mongo spec: restartPolicy: Always containers: - image: mongo:latest imagePullPolicy: IfNotPresent ports: - containerPort: 27017 name: mongo env: - name: MONGO_INITDB_ROOT_USERNAME value: "yapi" - name: MONGO_INITDB_ROOT_PASSWORD value: "admin.yapi" - name: MONGO_INITDB_DATABASE value: "yapi" livenessProbe: tcpSocket: port: 27017 initialDelaySeconds: 10 periodSeconds: 10 timeoutSeconds: 5 readinessProbe: tcpSocket: port: 27017 initialDelaySeconds: 5 periodSeconds: 10 timeoutSeconds: 1 # 优雅退出 lifecycle: preStop: exec: command: ["/bin/bash", "-c", "sleep 20"] volumeMounts: - name: mongo-db mountPath: /data/db volumes: - name: mongo-db hostPath: path: /data/yapi/mongo type: DirectoryOrCreate --- apiVersion: v1 kind: Service metadata: name: mongo spec: type: ClusterIP selector: app: mongo ports: - protocol: TCP port: 27017 targetPort: 27017
--- kind: Deployment apiVersion: apps/v1 metadata: labels: app: yapi name: yapi spec: selector: matchLabels: app: yapi template: metadata: labels: app: yapi spec: restartPolicy: Always containers: - image: jayfong/yapi:latest imagePullPolicy: IfNotPresent ports: - containerPort: 3000 name: yapi env: #管理员账号,禁止注册,默认密码: ymfe.org 登录请修改 - name: YAPI_ADMIN_ACCOUNT value: "admin@qq.com" - name: YAPI_ADMIN_PASSWORD value: "admin123" - name: YAPI_CLOSE_REGISTER value: "true" #mongodb 配置 - name: YAPI_DB_SERVERNAME value: "mongo.default.svc.cluster.local" - name: YAPI_DB_PORT value: "27017" - name: YAPI_DB_DATABASE value: "yapi" - name: YAPI_DB_USER value: "yapi" - name: YAPI_DB_PASS value: "admin.yapi" - name: YAPI_DB_AUTH_SOURCE value: "admin" #mail 邮件功能 - name: YAPI_MAIL_ENABLE value: "true" - name: YAPI_MAIL_HOST value: "smtp.exmail.qq.com" - name: YAPI_MAIL_PORT value: "465" - name: YAPI_MAIL_FROM value: "admin@qq.com" - name: YAPI_MAIL_AUTH_USER value: "xxx" - name: YAPI_MAIL_AUTH_PASS value: "xxx" #ldap 功能 - name: YAPI_LDAP_LOGIN_ENABLE value: "false" initContainers: - name: init-mongo image: busybox command: ['sh', '-c', 'until nslookup mongo; do echo waiting for mongo; sleep 2; done;'] --- apiVersion: v1 kind: Service metadata: name: yapi spec: type: NodePort selector: app: yapi ports: - protocol: TCP port: 3000 nodePort: 30000
如果给管理员设置账号的同时,你还开启mail 服务的话,管理员登录邮箱得填写 mail 服务发件者账号,不然启动会提示初始化管理员账号密码错误,登录时不可以使用自定义设置的密码,只能使用默认密码(ymfe.org)
我在yapi Deployment的Pod定义中添加了initContainers,它会通过检查 momgo
域名是否可以解析来判断所依赖的mongo服务是否就绪。
同时,在mongo Deployment中也引入了readinessProbe 和
livenessProbe探针,它们会判定是否mongo进程已经业务就绪。在K8S中,只有健康的Pod才可以通过ClusterIP访问或者DNS解析。
注:
Liveness探针:主要用于判断Container是否处于运行状态,比如当服务死锁或者响应缓慢等情况。
Readiness探针:主要用于判断服务是否已经正常工作。
在init container中不允许使用readiness探针。
如果Pod重启了,其所有init Container都需重新运行。
方法参考k8s官方文档
https://kubernetes.io/zh/docs/concepts/workloads/pods/init-containers/
https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
kubectl apply -f mongodb.yaml
kubectl apply -f yapi.yaml
mongodb启动状态
yapi启动状态
浏览器访问:http:///localhost:30000
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。