k8s部署

本文介绍使用 k8s 来部署 Apache ShenYu 网关。

目录

一. 使用 h2 作为数据库

  1. 1. 创建 nameSpace configMap
  2. 2. 部署 shenyu-admin
  3. 3. 部署 shenyu-bootstrap

二. 使用 mysql 作为数据库

  1. h2 过程类似,需要注意的两个地方
  2. 1. 需要加载 mysql-connector.jar,所以需要一个文件存储的地方
  3. 2. 需要指定外部 mysql 数据库配置,通过 endpoint 来代理外部 mysql 数据库
  4. 具体流程如下:
  5. 1. 创建 nameSpace configMap
  6. 2. 创建 endpoint 代理外部 mysql
  7. 3. 创建 pv 存储 mysql-connector.jar
  8. 4. 部署 shenyu-admin
  9. 5. 部署 shenyu-bootstrap

一. 使用 h2 作为数据库

1. 创建 nameSpace 和 configMap

  • 创建文件 shenyu-ns.yaml
  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: shenyu
  5. labels:
  6. name: shenyu
  7. ---
  8. apiVersion: v1
  9. kind: ConfigMap
  10. metadata:
  11. name: shenyu-cm
  12. namespace: shenyu
  13. data:
  14. application-local.yml: |
  15. server:
  16. port: 9195
  17. address: 0.0.0.0
  18. spring:
  19. main:
  20. allow-bean-definition-overriding: true
  21. application:
  22. name: shenyu-bootstrap
  23. management:
  24. health:
  25. defaults:
  26. enabled: false
  27. shenyu:
  28. local:
  29. enabled: true
  30. file:
  31. enabled: true
  32. cross:
  33. enabled: true
  34. dubbo:
  35. parameter: multi
  36. sync:
  37. websocket:
  38. urls: ws://shenyu-admin-svc.shenyu.svc.cluster.local:9095/websocket
  39. exclude:
  40. enabled: false
  41. paths:
  42. - /favicon.ico
  43. extPlugin:
  44. enabled: true
  45. threads: 1
  46. scheduleTime: 300
  47. scheduleDelay: 30
  48. scheduler:
  49. enabled: false
  50. type: fixed
  51. threads: 16
  52. logging:
  53. level:
  54. root: info
  55. org.springframework.boot: info
  56. org.apache.ibatis: info
  57. org.apache.shenyu.bonuspoint: info
  58. org.apache.shenyu.lottery: info
  59. org.apache.shenyu: info
  • 执行 kubectl apply -f shenyu-ns.yaml

2. 部署 shenyu-admin

  • 创建文件 shenyu-admin.yaml
  1. # 示例使用 nodeport 方式暴露端口
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. namespace: shenyu
  6. name: shenyu-admin-svc
  7. spec:
  8. selector:
  9. app: shenyu-admin
  10. type: NodePort
  11. ports:
  12. - protocol: TCP
  13. port: 9095
  14. targetPort: 9095
  15. nodePort: 31095
  16. ---
  17. # shenyu-admin
  18. apiVersion: apps/v1
  19. kind: Deployment
  20. metadata:
  21. namespace: shenyu
  22. name: shenyu-admin
  23. spec:
  24. selector:
  25. matchLabels:
  26. app: shenyu-admin
  27. replicas: 1
  28. template:
  29. metadata:
  30. labels:
  31. app: shenyu-admin
  32. spec:
  33. containers:
  34. - name: shenyu-admin
  35. image: apache/shenyu-admin:latest
  36. imagePullPolicy: Always
  37. ports:
  38. - containerPort: 9095
  39. env:
  40. - name: 'TZ'
  41. value: 'Asia/Beijing'
  • 执行kubectl apply -f shenyu-ns.yaml

3. 部署 shenyu-bootstrap

  • 创建文件 shenyu-bootstrap.yaml
  1. # 示例使用 nodeport 方式暴露端口
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. namespace: shenyu
  6. name: shenyu-bootstrap-svc
  7. spec:
  8. selector:
  9. app: shenyu-bootstrap
  10. type: NodePort
  11. ports:
  12. - protocol: TCP
  13. port: 9195
  14. targetPort: 9195
  15. nodePort: 31195
  16. ---
  17. # shenyu-bootstrap
  18. apiVersion: apps/v1
  19. kind: Deployment
  20. metadata:
  21. namespace: shenyu
  22. name: shenyu-bootstrap
  23. spec:
  24. selector:
  25. matchLabels:
  26. app: shenyu-bootstrap
  27. replicas: 1
  28. template:
  29. metadata:
  30. labels:
  31. app: shenyu-bootstrap
  32. spec:
  33. volumes:
  34. - name: shenyu-bootstrap-config
  35. configMap:
  36. name: shenyu-cm
  37. items:
  38. - key: application-local.yml
  39. path: application-local.yml
  40. containers:
  41. - name: shenyu-bootstrap
  42. image: apache/shenyu-bootstrap:latest
  43. ports:
  44. - containerPort: 9195
  45. env:
  46. - name: TZ
  47. value: Asia/Beijing
  48. volumeMounts:
  49. - name: shenyu-bootstrap-config
  50. mountPath: /opt/shenyu-bootstrap/conf/application-local.yml
  51. subPath: application-local.yml
  • 执行 kubectl apply -f shenyu-bootstrap.yaml

二. 使用 mysql 作为数据库

1. 创建 nameSpace和 configMap

  • 创建文件 shenyu-ns.yaml
  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: shenyu
  5. labels:
  6. name: shenyu
  7. ---
  8. apiVersion: v1
  9. kind: ConfigMap
  10. metadata:
  11. name: shenyu-cm
  12. namespace: shenyu
  13. data:
  14. application-local.yml: |
  15. server:
  16. port: 9195
  17. address: 0.0.0.0
  18. spring:
  19. main:
  20. allow-bean-definition-overriding: true
  21. application:
  22. name: shenyu-bootstrap
  23. management:
  24. health:
  25. defaults:
  26. enabled: false
  27. shenyu:
  28. local:
  29. enabled: true
  30. file:
  31. enabled: true
  32. cross:
  33. enabled: true
  34. dubbo:
  35. parameter: multi
  36. sync:
  37. websocket:
  38. urls: ws://shenyu-admin-svc.shenyu.svc.cluster.local:9095/websocket
  39. exclude:
  40. enabled: false
  41. paths:
  42. - /favicon.ico
  43. extPlugin:
  44. enabled: true
  45. threads: 1
  46. scheduleTime: 300
  47. scheduleDelay: 30
  48. scheduler:
  49. enabled: false
  50. type: fixed
  51. threads: 16
  52. logging:
  53. level:
  54. root: info
  55. org.springframework.boot: info
  56. org.apache.ibatis: info
  57. org.apache.shenyu.bonuspoint: info
  58. org.apache.shenyu.lottery: info
  59. org.apache.shenyu: info
  60. application-mysql.yml: |
  61. spring.datasource.url: jdbc:mysql://mysql.shenyu.svc.cluster.local:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false
  62. spring.datasource.user: {your_mysql_user}
  63. spring.datasource.password: {your_mysql_password}
  • 执行 kubectl apply -f shenyu-ns.yaml

2. 创建 endpoint 代理外部 mysql

  • 创建文件 shenyu-ep.yaml
  1. kind: Service
  2. apiVersion: v1
  3. metadata:
  4. name: mysql
  5. namespace: shenyu
  6. spec:
  7. ports:
  8. - port: 3306
  9. name: mysql
  10. targetPort: {your_mysql_port}
  11. ---
  12. kind: Endpoints
  13. apiVersion: v1
  14. metadata:
  15. name: mysql
  16. namespace: shenyu
  17. subsets:
  18. - addresses:
  19. - ip: {your_mysql_ip}
  20. ports:
  21. - port: {your_mysql_port}
  22. name: mysql
  • 执行 kubectl apply -f shenyu-ep.yaml

3. 创建 pv 存储 mysql-connector.jar

  • 创建文件 shenyu-store.yaml
  1. # 示例使用 pvc、pv、storageClass 来存储文件
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5. name: shenyu-pv
  6. spec:
  7. capacity:
  8. storage: 1Gi
  9. volumeMode: Filesystem
  10. accessModes:
  11. - ReadWriteOnce
  12. persistentVolumeReclaimPolicy: Delete
  13. storageClassName: local-storage
  14. local:
  15. path: /home/shenyu/shenyu-admin/k8s-pv # 指定节点上的目录,该目录下面需要包含 mysql-connector.jar
  16. nodeAffinity:
  17. required:
  18. nodeSelectorTerms:
  19. - matchExpressions:
  20. - key: kubernetes.io/hostname
  21. operator: In
  22. values:
  23. - {your_node_name} # 指定节点
  24. ---
  25. kind: PersistentVolumeClaim
  26. apiVersion: v1
  27. metadata:
  28. name: shenyu-pvc
  29. namespace: shenyu
  30. spec:
  31. accessModes:
  32. - ReadWriteOnce
  33. resources:
  34. requests:
  35. storage: 1Gi
  36. storageClassName: local-storage
  37. ---
  38. apiVersion: storage.k8s.io/v1
  39. kind: StorageClass
  40. metadata:
  41. name: local-storage
  42. provisioner: kubernetes.io/no-provisioner
  43. volumeBindingMode: WaitForFirstConsumer
  • 执行 kubectl apply -f shenyu-store.yaml
  • pv挂载目录下上传 mysql-connector.jar

4. 部署 shenyu-admin

  • 创建文件 shenyu-admin.yaml
  1. # 示例使用 nodeport 方式暴露端口
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. namespace: shenyu
  6. name: shenyu-admin-svc
  7. spec:
  8. selector:
  9. app: shenyu-admin
  10. type: NodePort
  11. ports:
  12. - protocol: TCP
  13. port: 9095
  14. targetPort: 9095
  15. nodePort: 31095
  16. ---
  17. # shenyu-admin
  18. apiVersion: apps/v1
  19. kind: Deployment
  20. metadata:
  21. namespace: shenyu
  22. name: shenyu-admin
  23. spec:
  24. selector:
  25. matchLabels:
  26. app: shenyu-admin
  27. replicas: 1
  28. template:
  29. metadata:
  30. labels:
  31. app: shenyu-admin
  32. spec:
  33. volumes:
  34. - name: mysql-connector-volume
  35. persistentVolumeClaim:
  36. claimName: shenyu-pvc
  37. - name: shenyu-admin-config
  38. configMap:
  39. name: shenyu-cm
  40. items:
  41. - key: application-mysql.yml
  42. path: application-mysql.yml
  43. containers:
  44. - name: shenyu-admin
  45. image: apache/shenyu-admin:latest
  46. imagePullPolicy: Always
  47. ports:
  48. - containerPort: 9095
  49. env:
  50. - name: 'TZ'
  51. value: 'Asia/Beijing'
  52. - name: SPRING_PROFILES_ACTIVE
  53. value: mysql
  54. volumeMounts:
  55. - name: shenyu-admin-config
  56. mountPath: /opt/shenyu-admin/config/application-mysql.yml
  57. subPath: application-mysql.yml
  58. - mountPath: /opt/shenyu-admin/ext-lib
  59. name: mysql-connector-volume
  • 执行kubectl apply -f shenyu-admin.yaml

3. 部署 shenyu-bootstrap

  • 创建文件 shenyu-bootstrap.yaml
  1. # 示例使用 nodeport 方式暴露端口
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. namespace: shenyu
  6. name: shenyu-bootstrap-svc
  7. spec:
  8. selector:
  9. app: shenyu-bootstrap
  10. type: NodePort
  11. ports:
  12. - protocol: TCP
  13. port: 9195
  14. targetPort: 9195
  15. nodePort: 31195
  16. ---
  17. # shenyu-bootstrap
  18. apiVersion: apps/v1
  19. kind: Deployment
  20. metadata:
  21. namespace: shenyu
  22. name: shenyu-bootstrap
  23. spec:
  24. selector:
  25. matchLabels:
  26. app: shenyu-bootstrap
  27. replicas: 1
  28. template:
  29. metadata:
  30. labels:
  31. app: shenyu-bootstrap
  32. spec:
  33. volumes:
  34. - name: shenyu-bootstrap-config
  35. configMap:
  36. name: shenyu-cm
  37. items:
  38. - key: application-local.yml
  39. path: application-local.yml
  40. containers:
  41. - name: shenyu-bootstrap
  42. image: apache/shenyu-bootstrap:latest
  43. ports:
  44. - containerPort: 9195
  45. env:
  46. - name: TZ
  47. value: Asia/Beijing
  48. volumeMounts:
  49. - name: shenyu-bootstrap-config
  50. mountPath: /opt/shenyu-bootstrap/conf/application-local.yml
  51. subPath: application-local.yml
  • 执行 kubectl apply -f shenyu-bootstrap.yaml