构建和部署 Kubernetes 控制器

本篇介绍如何使用 ShenYu Kubernetes Controller。

构建

建议参考自定义部署构建自定义网关,在网关的 Maven 依赖中加入 shenyu-kubernetes-controller 的依赖,网关即可集成 kubernetes 控制器。

  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-spring-boot-starter-k8s</artifactId>
  4. <version>${project.version}</version>
  5. </dependency>

也可以直接使用官方构建的 docker 镜像(TODO,未完成)

部署

K8s 部署文件可参考:

  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: shenyu-ingress
  5. ---
  6. apiVersion: v1
  7. automountServiceAccountToken: true
  8. kind: ServiceAccount
  9. metadata:
  10. name: shenyu-ingress-controller
  11. namespace: shenyu-ingress
  12. ---
  13. apiVersion: apps/v1
  14. kind: Deployment
  15. metadata:
  16. name: shenyu-ingress-controller
  17. namespace: shenyu-ingress
  18. labels:
  19. app: shenyu-ingress-controller
  20. all: shenyu-ingress-controller
  21. spec:
  22. replicas: 1
  23. selector:
  24. matchLabels:
  25. app: shenyu-ingress-controller
  26. template:
  27. metadata:
  28. labels:
  29. app: shenyu-ingress-controller
  30. spec:
  31. containers:
  32. - name: shenyu-ingress-controller
  33. image: apache/shenyu-integrated-test-k8s-ingress:latest
  34. ports:
  35. - containerPort: 9195
  36. imagePullPolicy: IfNotPresent
  37. serviceAccountName: shenyu-ingress-controller
  38. ---
  39. apiVersion: v1
  40. kind: Service
  41. metadata:
  42. name: shenyu-ingress-controller
  43. namespace: shenyu-ingress
  44. spec:
  45. selector:
  46. app: shenyu-ingress-controller
  47. type: NodePort
  48. ports:
  49. - port: 9195
  50. targetPort: 9195
  51. nodePort: 30095
  52. ---
  53. apiVersion: rbac.authorization.k8s.io/v1
  54. kind: ClusterRole
  55. metadata:
  56. name: shenyu-ingress-controller
  57. rules:
  58. - apiGroups:
  59. - ""
  60. resources:
  61. - namespaces
  62. - services
  63. - endpoints
  64. - secrets
  65. - pods
  66. verbs:
  67. - get
  68. - list
  69. - watch
  70. - apiGroups:
  71. - networking.k8s.io
  72. resources:
  73. - ingresses
  74. verbs:
  75. - get
  76. - list
  77. - watch
  78. ---
  79. apiVersion: rbac.authorization.k8s.io/v1
  80. kind: ClusterRoleBinding
  81. metadata:
  82. name: shenyu-ingress-controller
  83. namespace: shenyu-ingress
  84. roleRef:
  85. apiGroup: rbac.authorization.k8s.io
  86. kind: ClusterRole
  87. name: shenyu-ingress-controller
  88. subjects:
  89. - kind: ServiceAccount
  90. name: shenyu-ingress-controller
  91. namespace: shenyu-ingress

其中,Service 可以根据实际情况改成 LoadBalancer 类型。