不使用pvc的方式在K8S中部署apisix-gateway

简介

因为公司项目准备重构,现在做技术储备,之前公司项目使用的ocelot做网关,ocelot是.net平台下的一个网关,也是很不错,但是在选型的时候需要考虑到性能问题,所以在这次重构中抛弃了ocelot,看了apisix和kong,kong也是一个很不错的网关,不过因为对kong不太了解,刚好有朋友在用apisix所以就选了apisix来做新的网关,避免了重复掉到坑里面。不单单是部署,后面还要使用apisix进行身份认证等一系列的插件都会使用,所以慢慢更新吧。

  • 我的apisix使用etcd作为数据存储服务器,官方的使用pvc方式或者docker-compose的方式,对于新手不太友好,本篇是从etcd的安装到apisix的打通都会涉及。
  • apisix是服务端,用来进行网络请求转发。
  • apisix-dashboard是他的控制面板,用来进行可视化配置。
    • apisix介绍

      apisix是基于 OpenResty + etcd 实现的云原生、高性能、可扩展的微服务 API 网关。它是国人开源,目前已经进入 Apache 进行孵化。APISIX 通过插件机制,提供了动态负载平衡、身份验证、限流限速等等功能,当然我们也可以自己开发插件进行拓展。

      • 动态负载均衡:跨多个上游服务的动态负载均衡,目前已支持 round-robin 轮询和一致性哈希算法。
      • 身份验证:支持 key-auth、JWT、basic-auth、wolf-rbac 等多种认证方式。
      • 限流限速:可以基于速率、请求数、并发等维度限制。

1、部署etcd

etcd 是一个分布式键值对存储,设计用来可靠而快速的保存关键数据并提供访问。通过分布式锁,leader选举和写屏障(write barriers)来实现可靠的分布式协作。etcd集群是为高可用,持久性数据存储和检索而准备。

  • ubuntu部署etcd

    • ubuntu中部署etcd的两种方式:

      一种是去GitHub下载二进制的安装包,还有一种是apt-get install etcd,第二种方式我也尝试过,可能是我软件源的问题,版本有点老,所以我就换成了使用第一种方式,而且也比较推荐使用第一种方式。

    • 我用的etcd下载的版本是3.5.2,废话不多说直接看步骤:

    • 1.1、将etcd etcdctl etcdutl 二进制文件拷贝到/usr/local/bin目录下

      1. /usr/local/bin
    • 1.2、创建一个etcd的etcd.conf.yml,将下面代码拷贝进去,我这里etcd就简单的配置了一下,没有做集群,所以yml很简单。

      1. name: etcd-1
      2. data-dir: /home/etcd/data
      3. listen-client-urls: http://0.0.0.0:2379
      4. advertise-client-urls: http://0.0.0.0:2379
    • 1.3、通过etcd --config-file etcd.conf.yml的路径运行,如下图就是成功了,也可以使用etcd manager客户端来测试。



    • 1.4、如果使用etcd直接启动的话没有办法后台运行,所以我们需要在/etc/systemd/system目录下创建一个etcd.service来进行后台运行。

      1. [Unit]
      2. Description=ETCD Server
      3. Documentation=https://github.com/coreos/etcd
      4. After=network-online.target
      5. Wants=network-online.target
      6. [Service]
      7. User=root
      8. Group=root
      9. ExecStart= etcd --config-file /home/etcd/etcd.conf.yml
      10. [Install]
      11. WantedBy=multi-user.target
    • 1.5、创建好之后可以通过以下命令来确定运行状态如下图:

      1. # 启动
      2. sudo systemctl start etcd.service
      3. # 查看状态
      4. sudo systemctl status etcd.service
      5. # 开机自启
      6. sudo systemctl enable etcd.service

    • 1.6、设置用户名和密码

      1. # 设置版本为V3
      2. export ETCDCTL_API=3
      3. # 添加用户
      4. etcdctl user add root
      5. # 开启认证
      6. etcdctl auth enable

2、K8S部署apisix

apisix-gateway在部署的时候分为两块,分别是apisix和apisix-dashboard面板,所以看起来比较绕,不过apisix在部署的时候使用的是yaml文件覆盖的方式,所以我这里是将yaml存储到configmap中了,方便进行统一管理。我使用的k8s是Ubuntu出品的microk8s,用它的主要原因是因为配置简单。

  • 2.1部署apisix

    2.1.1、创建apisix.conf.yaml,并存储到configmap中,

    1. apisix:
    2. node_listen: 9080 # APISIX listening port
    3. enable_ipv6: false
    4. allow_admin: # http://nginx.org/en/docs/http/ngx_http_access_module.html#allow
    5. - 0.0.0.0/0 # We need to restrict ip access rules for security. 0.0.0.0/0 is for test.
    6. admin_key:
    7. - name: "admin"
    8. key: edd1c9f034335f136f87ad84b625c8f1
    9. role: admin # admin: manage all configuration data
    10. # viewer: only can view configuration data
    11. - name: "viewer"
    12. key: 4054f7cf07e344346cd3f287985e76a2
    13. role: viewer
    14. enable_control: true
    15. control:
    16. ip: "0.0.0.0"
    17. port: 9092
    18. etcd:
    19. host: # supports defining multiple etcd host addresses for an etcd cluster
    20. - "http://192.168.31.170:2379"
    21. user: "root" # ignore etcd username if not enable etcd auth
    22. password: "root" # ignore etcd password if not enable etcd auth
    23. discovery:
    24. nacos:
    25. host:
    26. - "http://47.100.213.49:8848"
    27. prefix: "/nacos/v1/"
    28. fetch_interval: 30 # default 30 sec
    29. weight: 100 # default 100
    30. timeout:
    31. connect: 2000 # default 2000 ms
    32. send: 2000 # default 2000 ms
    33. read: 5000 # default 5000 ms
    34. plugin_attr:
    35. prometheus:
    36. export_addr:
    37. ip: "0.0.0.0"
    38. port: 9091
    39. plugins:
    40. - client-control
    41. - ext-plugin-pre-req
    42. - zipkin
    43. - request-id
    44. - fault-injection
    45. - serverless-pre-function
    46. - batch-requests
    47. - cors
    48. - ip-restriction
    49. - ua-restriction
    50. - referer-restriction
    51. - uri-blocker
    52. - request-validation
    53. - openid-connect
    54. - wolf-rbac
    55. - hmac-auth
    56. - basic-auth
    57. - jwt-auth
    58. - key-auth
    59. - consumer-restriction
    60. - authz-keycloak
    61. - proxy-mirror
    62. - proxy-cache
    63. - proxy-rewrite
    64. - api-breaker
    65. - limit-conn
    66. - limit-count
    67. - limit-req
    68. - gzip
    69. - server-info
    70. - traffic-split
    71. - redirect
    72. - response-rewrite
    73. - grpc-transcode
    74. - prometheus
    75. - echo
    76. - http-logger
    77. - sls-logger
    78. - tcp-logger
    79. - kafka-logger
    80. - syslog
    81. - udp-logger
    82. - serverless-post-function
    83. - ext-plugin-post-req
    84. stream_plugins:
    85. - ip-restriction
    86. - limit-conn
    87. - mqtt-proxy

    2.1.2、使用kubectl命令创建configmap

    1. # 将config.yaml 存储到k8s的configmap中
    2. kubectl create configmap sukt-apisix-gateway-config --from-file=config.yaml=/home/sukt-platform/apisix/apisix-gateway-config.yaml -n sukt-platform

    2.1.3、新建apisix-deployment.yaml

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: sukt-apisix-gateway
    5. namespace: sukt-platform
    6. spec:
    7. selector:
    8. matchLabels:
    9. app: sukt-apisix-gateway
    10. template:
    11. metadata:
    12. labels:
    13. app: sukt-apisix-gateway
    14. spec:
    15. containers:
    16. - name: sukt-apisix-gateway
    17. image: apache/apisix:2.10.3-alpine
    18. imagePullPolicy: IfNotPresent
    19. resources:
    20. limits:
    21. cpu: 500m
    22. memory: 1Gi
    23. requests:
    24. cpu: 250m
    25. memory: 256Mi
    26. securityContext:
    27. privileged: false
    28. terminationMessagePath: /dev/termination-log
    29. terminationMessagePolicy: File
    30. volumeMounts:
    31. - mountPath: /usr/local/apisix/conf/config.yaml
    32. name: config
    33. subPath: config.yaml
    34. ports:
    35. - containerPort: 9080
    36. - containerPort: 9443
    37. dnsPolicy: ClusterFirst
    38. restartPolicy: Always
    39. schedulerName: default-scheduler
    40. securityContext: {}
    41. terminationGracePeriodSeconds: 30
    42. volumes:
    43. - configMap:
    44. defaultMode: 420
    45. name: sukt-apisix-gateway-config
    46. name: config

    2.1.4、新建apisix-service.yaml

    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4. name: sukt-apisix-gateway-nodetype
    5. labels:
    6. app: sukt-apisix-gateway-nodetype
    7. namespace: sukt-platform
    8. spec:
    9. type: NodePort
    10. selector:
    11. app: sukt-apisix-gateway
    12. ports:
    13. - port: 9080
    14. name: transfer1
    15. targetPort: 9080
    16. nodePort: 30107
    17. - port: 9443
    18. name: transfer2
    19. targetPort: 9443
    20. nodePort: 30108
  • 2、部署apisix-dashboard

    2.2.1、创建apisix-dashboard-config.yaml,并存储到configmap中,

    1. #
    2. # Licensed to the Apache Software Foundation (ASF) under one or more
    3. # contributor license agreements. See the NOTICE file distributed with
    4. # this work for additional information regarding copyright ownership.
    5. # The ASF licenses this file to You under the Apache License, Version 2.0
    6. # (the "License"); you may not use this file except in compliance with
    7. # the License. You may obtain a copy of the License at
    8. #
    9. # http://www.apache.org/licenses/LICENSE-2.0
    10. #
    11. # Unless required by applicable law or agreed to in writing, software
    12. # distributed under the License is distributed on an "AS IS" BASIS,
    13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14. # See the License for the specific language governing permissions and
    15. # limitations under the License.
    16. #
    17. conf:
    18. listen:
    19. host: 0.0.0.0 # `manager api` listening ip or host name
    20. port: 9000 # `manager api` listening port
    21. allow_list: # If we don't set any IP list, then any IP access is allowed by default.
    22. - 0.0.0.0/0
    23. etcd:
    24. endpoints: # supports defining multiple etcd host addresses for an etcd cluster
    25. - "http://192.168.31.170:2379"
    26. # yamllint disable rule:comments-indentation
    27. # etcd basic auth info
    28. username: "root" # ignore etcd username if not enable etcd auth
    29. password: "root" # ignore etcd password if not enable etcd auth
    30. mtls:
    31. key_file: "" # Path of your self-signed client side key
    32. cert_file: "" # Path of your self-signed client side cert
    33. ca_file: "" # Path of your self-signed ca cert, the CA is used to sign callers' certificates
    34. # prefix: /apisix # apisix config's prefix in etcd, /apisix by default
    35. log:
    36. error_log:
    37. level: warn # supports levels, lower to higher: debug, info, warn, error, panic, fatal
    38. file_path:
    39. logs/error.log # supports relative path, absolute path, standard output
    40. # such as: logs/error.log, /tmp/logs/error.log, /dev/stdout, /dev/stderr
    41. access_log:
    42. file_path:
    43. logs/access.log # supports relative path, absolute path, standard output
    44. # such as: logs/access.log, /tmp/logs/access.log, /dev/stdout, /dev/stderr
    45. # log example: 2020-12-09T16:38:09.039+0800 INFO filter/logging.go:46 /apisix/admin/routes/r1 {"status": 401, "host": "127.0.0.1:9000", "query": "asdfsafd=adf&a=a", "requestId": "3d50ecb8-758c-46d1-af5b-cd9d1c820156", "latency": 0, "remoteIP": "127.0.0.1", "method": "PUT", "errs": []}
    46. authentication:
    47. secret:
    48. secret # secret for jwt token generation.
    49. # NOTE: Highly recommended to modify this value to protect `manager api`.
    50. # if it's default value, when `manager api` start, it will generate a random string to replace it.
    51. expire_time: 3600 # jwt token expire time, in second
    52. users: # yamllint enable rule:comments-indentation
    53. - username: admin # username and password for login `manager api`
    54. password: P@ssW0rd
    55. - username: user
    56. password: P@ssW0rd
    57. plugins: # plugin list (sorted in alphabetical order)
    58. - api-breaker
    59. - authz-keycloak
    60. - basic-auth
    61. - batch-requests
    62. - consumer-restriction
    63. - cors
    64. # - dubbo-proxy
    65. - echo
    66. # - error-log-logger
    67. # - example-plugin
    68. - fault-injection
    69. - grpc-transcode
    70. - hmac-auth
    71. - http-logger
    72. - ip-restriction
    73. - jwt-auth
    74. - kafka-logger
    75. - key-auth
    76. - limit-conn
    77. - limit-count
    78. - limit-req
    79. # - log-rotate
    80. # - node-status
    81. - openid-connect
    82. - prometheus
    83. - proxy-cache
    84. - proxy-mirror
    85. - proxy-rewrite
    86. - redirect
    87. - referer-restriction
    88. - request-id
    89. - request-validation
    90. - response-rewrite
    91. - serverless-post-function
    92. - serverless-pre-function
    93. # - skywalking
    94. - sls-logger
    95. - syslog
    96. - tcp-logger
    97. - udp-logger
    98. - uri-blocker
    99. - wolf-rbac
    100. - zipkin
    101. - server-info
    102. - traffic-split

    2.2.2、使用kubectl命令创建configmap

    1. # 将config.yaml 存储到k8s的configmap中
    2. kubectl create configmap sukt-apisix-dashboard-config --from-file=config.yaml=/home/sukt-platform/apisix/apisix-dashboard-config.yaml -n sukt-platform

    2.2.3、新建apisix-dashboard-deployment.yaml

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: sukt-apisix-dashboard
    5. namespace: sukt-platform
    6. spec:
    7. selector:
    8. matchLabels:
    9. app: sukt-apisix-dashboard
    10. template:
    11. metadata:
    12. labels:
    13. app: sukt-apisix-dashboard
    14. spec:
    15. nodeName: microk8sslave1 # 部署到指定的node节点
    16. containers:
    17. - name: sukt-apisix-dashboard
    18. image: apache/apisix-dashboard:2.10.1-alpine
    19. imagePullPolicy: IfNotPresent
    20. resources:
    21. limits:
    22. cpu: 500m
    23. memory: 1Gi
    24. requests:
    25. cpu: 250m
    26. memory: 256Mi
    27. securityContext:
    28. privileged: false
    29. terminationMessagePath: /dev/termination-log
    30. terminationMessagePolicy: File
    31. volumeMounts:
    32. - mountPath: /usr/local/apisix-dashboard/conf/conf.yaml
    33. name: config
    34. subPath: config.yaml #这个位置对应的是comfigmap中的名字,不是 /usr/local/apisix-dashboard/conf/conf.yaml
    35. ports:
    36. - containerPort: 9000
    37. dnsPolicy: ClusterFirst
    38. restartPolicy: Always
    39. schedulerName: default-scheduler
    40. securityContext: {}
    41. terminationGracePeriodSeconds: 30
    42. volumes:
    43. - configMap:
    44. defaultMode: 420
    45. name: sukt-apisix-dashboard-config
    46. name: config

    2.2.4、新建apisix-dashboard-service.yaml

    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4. name: sukt-apisix-dashboard-nodetype
    5. labels:
    6. app: sukt-apisix-dashboard-nodetype
    7. namespace: sukt-platform
    8. spec:
    9. type: NodePort
    10. selector:
    11. app: sukt-apisix-dashboard
    12. ports:
    13. - port: 9000
    14. name: transfer1
    15. targetPort: 9000
    16. nodePort: 30109
  • 运行效果图

    可以通过dashboard面板的系统信息查看apisix-gateway的运行信息



结语

apisix-gateway文章分为一个专题,本篇只是讲解了如何在k8s中安装以及启动,后面会讲解如何进行转发以及其他功能等。

K8S中部署apisix(非ingress)的更多相关文章

  1. k8s中部署springcloud

    安装和配置数据存储仓库MySQL 1.MySQL简介 2.MySQL特点 3.安装和配置MySQL 4.在MySQL数据库导入数据 5.对MySQL数据库进行授权 1.MySQL简介 MySQL 是一 ...

  2. 不使用pvc的方式在K8S中部署apisix-gateway

    不使用pvc的方式在K8S中部署apisix-gateway 简介 我的apisix使用etcd作为数据存储服务器,官方的使用pvc方式或者docker-compose的方式,对于新手不太友好,本篇是 ...

  3. 在k8s中部署前后端分离项目进行访问的两种配置方式

    第一种方式 (1) nginx配置中只写前端项目的/根路径配置 前端项目使用的Dockerfile文件内容 把前端项目编译后生成的dist文件夹放在nginx的html默认目录下,浏览器访问前端项目时 ...

  4. Prometheus K8S中部署Alertmanager

    Prometheus K8S中部署Alertmanager 设置告警和通知的主要步骤如下:一.部署Alertmanager二.配置Prometheus与Alertmanager通信三.配置告警 1. ...

  5. Kubernetes之在k8s中部署Java应用

    部署好了k8s以后 部署参考https://www.cnblogs.com/minseo/p/12055731.html 怎么在k8s部署应用 项目迁移到k8s平台是怎样的流程 1,制作镜像 2,控制 ...

  6. Docker & k8s 系列三:在k8s中部署单个服务实例

    本章将会讲解: pod的概念,以及如何向k8s中部署一个单体应用实例. 在上面的篇幅中,我们了解了docker,并制作.运行了docker镜像,然后将镜像发布至中央仓库了.然后又搭建了本机的k8s环境 ...

  7. 【转】K8S中部署Helm

    K8S中的包管理工具 1. 客户端Helm(即Helm)  通过脚本安装:curl https://raw.githubusercontent.com/helm/helm/master/scripts ...

  8. 使用 Nocalhost 开发 Kubernetes 中的 APISIX Ingress Controller

    本文作者:黄鑫鑫 - Nocalhost 项目核心开发者 腾讯云 CODING DevOps 研发工程师.硕士毕业于中山大学数据科学与计算机学院,曾负责过平安云主机及国家超算中心容器云平台等相关业务, ...

  9. k8s部署高可用Ingress

    部署高可用Ingress 官网地址https://kubernetes.github.io/ingress-nginx/deploy/ 获取ingress的编排文件 wget https://raw. ...

随机推荐

  1. torch.nn.MSELoss()函数解读

    转载自:https://www.cnblogs.com/tingtin/p/13902325.html

  2. 【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结

    华为运动健康服务(HUAWEI Health Kit)为三方生态应用提供了REST API接口,通过其接口可访问数据库,为用户提供运动健康类数据服务.在实际的集成过程中,开发者们可能会遇到各种问题,这 ...

  3. Spring Security整合企业微信的扫码登录,企微的API震惊到我了

    本文代码: https://gitee.com/felord/spring-security-oauth2-tutorial/tree/wwopen/ 现在很多企业都接入了企业微信,作为私域社群工具, ...

  4. 29.MySQL高级SQL语句

    MySQL高级SQL语句 目录 MySQL高级SQL语句 创建两个表 SELECT DISTINCT WHERE AND OR IN BETWEEN 通配符 LIKE ORDER BY 函数 数学函数 ...

  5. 11.Firewalld防火墙

    Firewalld防火墙 目录 Firewalld防火墙 Firewalld概述 Firewalld Firewalld和iptables的关系 netfilter Firewalld/iptable ...

  6. BUUCTF-签到题

    签到题 很简单写在介绍里面了.

  7. cmd命令与bat编程

    命令解压缩文件 winrar 命令行解压文件 winrar x 要解压的文件 要解压到的路径   (保存压缩文件内的目录结果) 直接覆盖   -o+           覆盖已存在文件    在不提示 ...

  8. IP寻址与规划

    一.IP寻址和子网划分 IP地址的主机部分可被分为三种地址:网络地址.主机地址和定向广播地址. 网络地址是网络号中的第一个地址.它用来将网络内的其他所有网段唯一标识为一个网段或广播域.定向广播地址是网 ...

  9. JavaScript扩展原型链浅析

    前言 上文对原型和原型链做了一些简单的概念介绍和解析,本文将浅析一些原型链的扩展. javaScript原型和原型链 http://lewyon.xyz/prototype.html 扩展原型链 使用 ...

  10. Visio Professional之活动图

    1 什么叫活动图? 活动图在本质上是一种流程图. 活动图(Activity diagram)是UML用于对系统的动态行为建模的一种常用工具,它描述活动的顺序,表示一个活动到另一个活动的控制流. 2.活 ...