当前位置 博文首页 > 张善友:简单测试 APISIX2.6 网关

    张善友:简单测试 APISIX2.6 网关

    作者:张善友 时间:2021-06-13 18:22

    Apache APISIX是一个动态的、实时的、高性能的 API 网关。它提供丰富的流量管理功能,例如负载均衡、动态上游服务、金丝雀发布、断路、身份验证、可观察性等。您可以使用 Apache APISIX 来处理传统的南北流量,以及服务之间的东西流量。2019 年 10 月份,深圳支流科技把网关 APISIX 贡献给 Apache 基金会,他们提供商业版本,以下内容基于社区版本。

    APISIX地址:https://github.com/apache/apisix

    DashBoard:https://github.com/apache/apisix-dashboard

    中文文档地址:https://apisix.apache.org/zh/docs/apisix/getting-started/

    1、安装APISIX 和 APISIX Dashboard

    官方文档介绍了源码包、RPM 包、Docker 以及Helm Chart安装方式,这里我们在K8s 环境下使用 apisix, 所以选择使用Helm Chart方式安装,安装文档参见 https://apisix.apache.org/zh/docs/helm-chart/apisix/ 。

    日前支流科技提供了一个在线 Helm Charts 仓库 https://charts.apiseven.com, 用户可通过该仓库轻松安装 Apache APISIX、Apache apisix-dashboard 和 Apache apisix-ingress-controller (而不需要提前 clone 对应的项目)。

    一共有3个Helm Chart:

    • Apache APISIX Helm Chart :https://apisix.apache.org/zh/docs/helm-chart/apisix/
    • Apache APISIX Dashboard Helm Chart: https://apisix.apache.org/zh/docs/helm-chart/apisix-dashboard 
    • Apache APISIX Ingress Controller Helm Chart: https://apisix.apache.org/zh/docs/helm-chart/apisix-ingress-controller 

    添加仓库并获取更新

    helm repo add apisix https://charts.apiseven.com
    helm repo update

    查看仓库中可用的 Charts 包
    helm search repo apisix

    > helm search repo apisix
    NAME                                    CHART VERSION   APP VERSION     DESCRIPTION                                   
    apisix/apisix                           0.3.4           2.6.0           A Helm chart for Apache APISIX                
    apisix/apisix-dashboard                 0.1.4           2.6.0           A Helm chart for Apache APISIX Dashboard      
    apisix/apisix-ingress-controller        0.4.2           0.6.0           Apache APISIX Ingress Controller for Kubernetes

    安装 Apache APISIX 到目标 Kubernetes 集群中

    helm install apisix apisix/apisix  --set gateway.type = NodePort --set admin.allow.ipList="{0.0.0.0/0}"   --namespace ingress-apisix

    创建了四个Service资源,apisix-etcd 和 apisix-etcd-headless是etcd服务, 一个是处理真实流量的apisix-gateway,;另一个是充当控制平面来处理所有配置更改的服务apisix-admin

    网关服务类型设置为NodePort,以便客户端可以通过节点 IP 和分配的端口访问 Apache APISIX。

    还有一点需要注意的是,该allow.ipList字段要根据Pod CIDR设置进行自定义,请注意我这里偷懒把所有的ip 都打开了,这个在生产环境下是不推荐这么干的。这样apisix-ingress-controller实例才能访问APISIX实例

    安装apisix-dashboard也建议通过 Helm Charts 安装,将其安装在与 Apache APISIX 相同的命名空间中

    helm install apisix-dashboard apisix/apisix-dashboard --namespace ingress-apisix

    安装 apisix-ingress-controller

    通过 Helm Charts 安装 apisix-ingress-controller,建议将其安装在与 Apache APISIX 相同的命名空间中。

    helm install apisix-ingress-controller apisix/apisix-ingress-controller   --set config.apisix.baseURL=http://apisix-admin:9180/apisix/admin  --set config.apisix.adminKey=edd1c9f034335f136f87ad84b625c8f1  --namespace apisix-gateway

    上述命令中使用的管理密钥是默认的,如果您在部署 APISIX 时更改了管理密钥配置,请记住在此处更改。将image.tag 更改为您想要的 apisix-ingress-controller 版本。

    kubectl get service --namespace apisix-gateway 检测一下是否成功安装了:

    image

    访问apisix-dashboard 的默认用户名/密码是admin/admin:

    image

    登录成功进入控制台

    image

    我们下面部署一个asp.net core应用程序来测试一下apisix:

    我选用asp.net core 的官方示例:https://github.com/dotnet/dotnet-docker/tree/main/samples/aspnetapp ,创建一个 Deployment:aspnetappdemo 和 Service:aspnetdemo

    apiVersion: apps/v1
    kind: Deployment
    metadata:
       annotations:
         deployment.kubernetes.io/revision: "1"
       name: aspnetappdemo
       namespace: default
    spec:
       progressDeadlineSeconds: 600
       replicas: 1
       revisionHistoryLimit: 10
       selector:
         matchLabels:
           workload.user.cattle.io/workloadselector: apps.deployment-default-aspnetappdemo
       strategy:
         rollingUpdate:
           maxSurge: 25%
           maxUnavailable: 25%
         type: RollingUpdate
       template:
         metadata:
           labels:
             workload.user.cattle.io/workloadselector: apps.deployment-default-aspnetappdemo
         spec:
           containers:
           - image: mcr.microsoft.com/dotnet/samples:aspnetapp
             imagePullPolicy: Always
             name: container-0
             resources: {}
             terminationMessagePath: /dev/termination-log
             terminationMessagePolicy: File
           dnsPolicy: ClusterFirst
           restartPolicy: Always
           schedulerName: default-scheduler
           securityContext: {}
           terminationGracePeriodSeconds: 30

    ----------------------------------------------------------------------------------------------------

    apiVersion: v1
    kind: Service

    metadata:
       name: aspnetdemo
       namespace: default 
    spec:
       clusterIP: 10.43.240.212
       clusterIPs:
       - 10.43.240.212
       externalTrafficPolicy: Cluster
       ports:
       - name: asphttp
         nodePort: 30002
         port: 80
         protocol: TCP
         targetPort: 80
       selector:
         workload.user.cattle.io/workloadselector: apps.deployment-default-aspnetappdemo
       sessionAffinity: None
       type: LoadBalancer

    使用apisix-dashboard 创建一个上游服务代表我们的aspnetdemo:aspnetdemo.default.svc.cluster.local,在 APISIX 控制台的「上游」菜单中,创建一个 APISIX Upstream。如下图所示:

    image

    接下来就是创建一个APISIX Route,字面意思就是路由,通过定义一些规则来匹配客户端的请求,然后根据匹配结果加载并执行相应的 插件,并把请求转发给到指定 Upstream。

    image

    点击下一步,选择上游服务aspnetdemo:

    image

    点击下一步,关于插件部门后面的问题后续会陆续更新。

    image

    现在,我们来请求 APISIX 网关,转发请求到后端服务。 http://13.72.208.130:31856/ 

    image

    好了,暂时先了解到这里。

    bk