Docker & K8s

Docker

注意

Yearning安装包内已含有Dockerfile文件,可直接进行build打包成镜像

Yearning 从v2.0.4版本开始支持环境变量传参

容器启动时可通过环境变量的方式传入数据库地址。

如下所示

  1. docker run -d -it -p8000:8000 -e MYSQL_USER=root -e MYSQL_ADDR=10.0.0.3:3306 -e MYSQL_PASSWORD=123123 -e MYSQL_DB=Yearning chaiyd/yearning

K8s

k8s部署参考K8S | 部署 Yearning SQL 审核平台Docker & K8s - 图1open in new window

Secret

  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: db-conf
  5. namespace: yearning
  6. type: Opaque # 使用的是generic类型
  7. data: # 这里配置的是数据库的相关信息,使用base64加密输入:
  8. addr: cm9vdAweqw==
  9. user: cm9vdAweqw==
  10. pass: cm9vdAweqw==
  11. data: WWVhcm5pbmc= # echo -n 'Yearning' | base64

Service

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: yearning
  6. name: yearning
  7. namespace: yearning
  8. spec:
  9. ports:
  10. - port: 80 # svc内部端口,通过clusterIP访问
  11. protocol: TCP
  12. targetPort: 8000 # 镜像内服务的端口
  13. selector: # 标签选择器,与deployment中的标签保持一致
  14. app: yearning
  15. type: NodePort # Service类型

Ingress

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: yearing
  5. namespace: zheng
  6. spec:
  7. rules:
  8. # 需要在本地机器的hosts文件中添加配置:192.168.111.111 www.yearning.com
  9. - host: www.yearning.com
  10. http:
  11. paths:
  12. - path: /
  13. pathType: Prefix
  14. backend:
  15. service:
  16. name: yearning
  17. port:
  18. number: 80

Deployment

  1. apiVersion: apps/v1 # API版泵
  2. kind: Deployment # 资源类型
  3. metadata: # 元数据
  4. labels: # 标签
  5. app: SQL-Review
  6. name: yearning # deployment的名字
  7. namespace: yearning # 所属命名空间
  8. spec:
  9. replicas: 3 # 副本数
  10. selector: # 选择器,选择针对谁做
  11. matchLabels:
  12. app: yearning
  13. template: # 镜像的模板
  14. metadata: # 元数据
  15. labels: # 标签
  16. app: yearning
  17. spec:
  18. containers: # 容器信息
  19. - image: 192.168.189.111/project/yearning # 容器镜像
  20. name: yearning # 容器的名字
  21. imagePullPolicy: IfNotPresent # 镜像的下载策略
  22. env: # 容器中的变量
  23. - name: MYSQL_ADDR
  24. valueFrom:
  25. secretKeyRef: # 存储的变量信息
  26. name: db-conf
  27. key: addr
  28. - name: MYSQL_USER
  29. valueFrom:
  30. secretKeyRef:
  31. name: db-conf
  32. key: user
  33. - name: MYSQL_PASSWORD
  34. valueFrom:
  35. secretKeyRef:
  36. name: db-conf
  37. key: pass
  38. - name: MYSQL_DB
  39. valueFrom:
  40. secretKeyRef:
  41. name: db-conf
  42. key: data
  43. ports: # 定义容器中的端口信息
  44. - containerPort: 8000
  45. name: web
  46. protocol: TCP
  47. readinessProbe: # 就绪检查
  48. httpGet:
  49. path: /
  50. port: web
  51. scheme: HTTP
  52. initialDelaySeconds: 25
  53. periodSeconds: 2
  54. livenessProbe: # 存活检查
  55. httpGet:
  56. path: /
  57. port: web
  58. scheme: HTTP
  59. initialDelaySeconds: 30
  60. periodSeconds: 2
  61. resources: # 资源限制
  62. requests:
  63. cpu: 200m
  64. memory: 1Gi
  65. limits:
  66. cpu: 250m
  67. memory: 2Gi