Skip to main content
Version: 2.4.1

K8s Deployment

This article introduces the use of K8s to deploy the Apache ShenYu gateway.

Catalog

I. Using h2 as a database

  1. create Namespace and ConfigMap
  2. deploying shenyu-admin
  3. deploy shenyu-bootstrap II. Use MySQL as the database

Similar to the h2 process, there are two points to note

  1. you need to load mysql-connector.jar, so you need a place to store the file
  2. you need to specify an external MySQL database configuration to proxy the external MySQL database via Endpoints

The process is as follows.

  1. create Namespace and ConfigMap
  2. create Endpoints to proxy external MySQL
  3. create PV store mysql-connector.jar
  4. deploy shenyu-admin
  5. deploy shenyu-bootstrap

I. Using h2 as a database#

1. Create Namespace and ConfigMap#

  • create shenyu-ns.yaml
apiVersion: v1kind: Namespacemetadata:  name: shenyu  labels:    name: shenyu---apiVersion: v1kind: ConfigMapmetadata:  name: shenyu-cm  namespace: shenyudata:  application-local.yml: |    server:        port: 9195        address: 0.0.0.0    spring:        main:            allow-bean-definition-overriding: true        application:            name: shenyu-bootstrap    management:        health:            defaults:            enabled: false    shenyu:          cross:            enabled: true            allowedHeaders:            allowedMethods: "*"            allowedOrigin: "*"            allowedExpose: "*"            maxAge: "18000"            allowCredentials: true          switchConfig:            local: true          file:            enabled: true            maxSize : 10          exclude:            enabled: false            paths:              - /favicon.ico          extPlugin:            path:            enabled: true            threads: 1            scheduleTime: 300            scheduleDelay: 30           sync:            websocket:              urls: ws://shenyu-admin-svc.shenyu.svc.cluster.local:9095/websocket           scheduler:            enabled: false            type: fixed            threads: 16    logging:        level:            root: info            org.springframework.boot: info            org.apache.ibatis: info            org.apache.shenyu.bonuspoint: info            org.apache.shenyu.lottery: info            org.apache.shenyu: info
  • execute kubectl apply -f shenyu-ns.yaml

2. Create shenyu-admin#

  • create shenyu-admin.yaml
# Example of using the nodeport type to expose portsapiVersion: v1kind: Servicemetadata:  namespace: shenyu  name: shenyu-admin-svcspec:  selector:    app: shenyu-admin  type: NodePort  ports:  - protocol: TCP    port: 9095    targetPort: 9095    nodePort: 31095---# shenyu-adminapiVersion: apps/v1kind: Deploymentmetadata:  namespace: shenyu  name: shenyu-adminspec:  selector:    matchLabels:      app: shenyu-admin  replicas: 1  template:    metadata:      labels:        app: shenyu-admin    spec:      containers:      - name: shenyu-admin        image: apache/shenyu-admin:2.4.1        imagePullPolicy: Always        ports:        - containerPort: 9095        env:        - name: 'TZ'          value: 'Asia/Beijing'
  • executekubectl apply -f shenyu-admin.yaml

3. Create shenyu-bootstrap#

  • create shenyu-bootstrap.yaml
# Example of using the nodeport type to expose portsapiVersion: v1kind: Servicemetadata:  namespace: shenyu  name: shenyu-bootstrap-svcspec:  selector:    app: shenyu-bootstrap  type: NodePort  ports:  - protocol: TCP    port: 9195    targetPort: 9195    nodePort: 31195---# shenyu-bootstrapapiVersion: apps/v1kind: Deploymentmetadata:  namespace: shenyu  name: shenyu-bootstrapspec:  selector:    matchLabels:      app: shenyu-bootstrap  replicas: 1  template:    metadata:      labels:        app: shenyu-bootstrap    spec:      volumes:      - name: shenyu-bootstrap-config        configMap:          name: shenyu-cm          items:          - key: application-local.yml            path: application-local.yml      containers:      - name: shenyu-bootstrap        image: apache/shenyu-bootstrap:2.4.1        ports:        - containerPort: 9195        env:        - name: TZ          value: Asia/Beijing        volumeMounts:        - name: shenyu-bootstrap-config          mountPath: /opt/shenyu-bootstrap/conf/application-local.yml          subPath: application-local.yml
  • execute kubectl apply -f shenyu-bootstrap.yaml

II. Use MySQL as the database#

1. Create Namespace and ConfigMap#

  • create shenyu-ns.yaml
apiVersion: v1kind: Namespacemetadata:  name: shenyu  labels:    name: shenyu---apiVersion: v1kind: ConfigMapmetadata:  name: shenyu-cm  namespace: shenyudata:  application-local.yml: |    server:        port: 9195        address: 0.0.0.0    spring:        main:            allow-bean-definition-overriding: true        application:            name: shenyu-bootstrap    management:        health:            defaults:            enabled: false    shenyu:          cross:            enabled: true            allowedHeaders:            allowedMethods: "*"            allowedOrigin: "*"            allowedExpose: "*"            maxAge: "18000"            allowCredentials: true          switchConfig:            local: true          file:            enabled: true            maxSize : 10          exclude:            enabled: false            paths:              - /favicon.ico          extPlugin:            path:            enabled: true            threads: 1            scheduleTime: 300            scheduleDelay: 30           sync:            websocket:              urls: ws://shenyu-admin-svc.shenyu.svc.cluster.local:9095/websocket           scheduler:            enabled: false            type: fixed            threads: 16    logging:        level:            root: info            org.springframework.boot: info            org.apache.ibatis: info            org.apache.shenyu.bonuspoint: info            org.apache.shenyu.lottery: info            org.apache.shenyu: info  application-mysql.yml: |    spring.datasource.url: jdbc:mysql://mysql.shenyu.svc.cluster.local:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false    spring.datasource.username: {your_mysql_user}    spring.datasource.password: {your_mysql_password}
  • execute kubectl apply -f shenyu-ns.yaml

2. Create Endpoints to represent MySQL#

  • create shenyu-ep.yaml
kind: ServiceapiVersion: v1metadata:  name: mysql  namespace: shenyuspec:  ports:  - port: 3306    name: mysql    targetPort: {your_mysql_port}---kind: EndpointsapiVersion: v1metadata:  name: mysql  namespace: shenyusubsets:- addresses:  - ip: {your_mysql_ip}  ports:  - port: {your_mysql_port}    name: mysql
  • execute kubectl apply -f shenyu-ep.yaml

3. Create PV to store mysql-connector.jar#

  • create shenyu-store.yaml
# Example of using PVC、PV、StorageClass to store jar fileapiVersion: v1kind: PersistentVolumemetadata:  name: shenyu-pvspec:  capacity:    storage: 1Gi  volumeMode: Filesystem  accessModes:  - ReadWriteOnce  persistentVolumeReclaimPolicy: Delete  storageClassName: local-storage  local:    path: /home/shenyu/shenyu-admin/k8s-pv  # Specify the directory on the node, which should contain `mysql-connector.jar`  nodeAffinity:    required:      nodeSelectorTerms:      - matchExpressions:        - key: kubernetes.io/hostname          operator: In          values:          - {your_node_name} # Specify node---kind: PersistentVolumeClaimapiVersion: v1metadata:  name: shenyu-pvc  namespace: shenyuspec:  accessModes:  - ReadWriteOnce  resources:    requests:      storage: 1Gi  storageClassName: local-storage---apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:  name: local-storageprovisioner: kubernetes.io/no-provisionervolumeBindingMode: WaitForFirstConsumer
  • execute kubectl apply -f shenyu-pv.yaml
  • PV mounted directory upload mysql-connector.jar

4. Create shenyu-admin#

  • create shenyu-admin.yaml
# Example of using the nodeport type to expose portsapiVersion: v1kind: Servicemetadata:  namespace: shenyu  name: shenyu-admin-svcspec:  selector:    app: shenyu-admin  type: NodePort  ports:  - protocol: TCP    port: 9095    targetPort: 9095    nodePort: 31095---# shenyu-adminapiVersion: apps/v1kind: Deploymentmetadata:  namespace: shenyu  name: shenyu-adminspec:  selector:    matchLabels:      app: shenyu-admin  replicas: 1  template:    metadata:      labels:        app: shenyu-admin    spec:      volumes:      - name: mysql-connector-volume        persistentVolumeClaim:          claimName: shenyu-pvc      - name: shenyu-admin-config        configMap:          name: shenyu-cm          items:          - key: application-mysql.yml            path: application-mysql.yml      containers:      - name: shenyu-admin        image: apache/shenyu-admin:2.4.1        imagePullPolicy: Always        ports:        - containerPort: 9095        env:        - name: 'TZ'          value: 'Asia/Beijing'        - name: SPRING_PROFILES_ACTIVE          value: mysql        volumeMounts:        - name: shenyu-admin-config          mountPath: /opt/shenyu-admin/config/application-mysql.yml          subPath: application-mysql.yml        - mountPath: /opt/shenyu-admin/ext-lib          name: mysql-connector-volume
  • executekubectl apply -f shenyu-admin.yaml

3. Create shenyu-bootstrap#

  • create shenyu-bootstrap.yaml
# Example of using the nodeport type to expose portsapiVersion: v1kind: Servicemetadata:  namespace: shenyu  name: shenyu-bootstrap-svcspec:  selector:    app: shenyu-bootstrap  type: NodePort  ports:  - protocol: TCP    port: 9195    targetPort: 9195    nodePort: 31195---# shenyu-bootstrapapiVersion: apps/v1kind: Deploymentmetadata:  namespace: shenyu  name: shenyu-bootstrapspec:  selector:    matchLabels:      app: shenyu-bootstrap  replicas: 1  template:    metadata:      labels:        app: shenyu-bootstrap    spec:      volumes:      - name: shenyu-bootstrap-config        configMap:          name: shenyu-cm          items:          - key: application-local.yml            path: application-local.yml      containers:      - name: shenyu-bootstrap        image: apache/shenyu-bootstrap:2.4.1        ports:        - containerPort: 9195        env:        - name: TZ          value: Asia/Beijing        volumeMounts:        - name: shenyu-bootstrap-config          mountPath: /opt/shenyu-bootstrap/conf/application-local.yml          subPath: application-local.yml
  • execute kubectl apply -f shenyu-bootstrap.yaml