老蒋的知识库

  • 首页
  • 文章归档
  • 关于页面

  • 搜索

Helm安装部署Apisix,apisix-ingress配置域名转发

发表于 2022-11-07 | 分类于 K8S部署 | 0 | 阅读次数 90

简介

当K8S部署应用过多时或者需要对外暴露服务做域名转发时,使用NodePort暴露不够方便也不合理,需要部署一个Api网关统一暴露K8S内服务。此文章教学部署Apisix

坑

  1. Apisix helm文档比较落后,有能力的最好直接下载chart包解压之后参考的进行部署
  2. helm install如果遇到很慢的情况,建议先helm pull之后用本地文件进行安装。或者使用代理连接github,chart包来源是github。
  3. 官网helm使用的是bitnami部署etcd集群,其中有个Bug,etcd的Pod奔溃后自启动无法加入集群。要么安装时配置集群节点数为1 etcd.replicaCount=1,或者配置外部etcd后自行进行维护etcd.host=xxxxxxx
  4. etcd需要定时清理磁盘空间,否则会磁盘爆炸而崩溃,添加etcd.autoCompactionRetention=5m只保留5小时内的键空间历史记录。

环境

K8S: 1.24+
Helm: 3.0

helm安装时使用包
apisix: apisix-0.11.1.tgz
apisix-dashboard: apisix-dashboard-0.6.1.tgz
apisix-ingress-controller: apisix-ingress-controller-0.10.1.tgz

参考文献

apisix官方文档: https://apisix.apache.org/zh/docs/apisix/getting-started/
apisix helm官方文档: https://github.com/apache/apisix-helm-chart
apisix config文件: https://github.com/apache/apisix/blob/master/conf/config-default.yaml
apisix-dashboard config文件: https://github.com/apache/apisix-dashboard/blob/master/api/conf/conf.yaml
apisixRoute 配置: https://apisix.apache.org/zh/docs/ingress-controller/concepts/apisix_route/
apisixRoute 字段说明: https://apisix.apache.org/zh/docs/ingress-controller/next/references/apisix_route_v2/
ingess 配置: https://apisix.apache.org/zh/docs/ingress-controller/tutorials/proxy-the-httpbin-service-with-ingress/
bitnami\etcd 集群pod奔溃后自启动无法加入集群: https://github.com/apache/apisix-helm-chart/issues/290

部署apisix

helm部署apisix

# `gateway.tls.enabled=true` 是为了开启https,helm部署默认是关闭的
# `global.storageClass=nfs-local-k8s-2` 修改默认的sc
# `admin.allow.ipList={0.0.0.0/0}` 9180端口可以访问的网段,如果不配置后续的apisix-ingress-controller将无法访问apisix
# `gateway.tls.containerPort=443` 配置容器对外暴露http端口为80
# `gateway.http.containerPort=80` 配置容器内对外暴露https端口为443,这个最好配置否则后续使用插件做http重定向https,会重定向到9443端口
# `etcd.replicaCount=1` 配置etcd集群数量为1,这里有个大坑因为官网helm使用的是`bitnami\etcd`部署的集群,有个Bug如果etcd pod挂了就无法重新再次加入etcd集群。所以要么设置集群数量1,要么配置外部etcd。
# `etcd.autoCompactionRetention=5m`只保留5小时内的键空间历史记录。
helm install --set gateway.tls.enabled=true --set gateway.http.containerPort=80 --set gateway.tls.containerPort=443 --set global.storageClass=nfs-local-k8s-0 --set admin.allow.ipList={0.0.0.0/0} --set etcd.autoCompactionRetention=5m --create-namespace --namespace apisix apisix apisix/apisix

修改配置,我这边是私人服务器,使用hostPort对外暴露

kubectl -n apisix edit deployments.apps apisix

编辑deployments

...

          ports:
            - name: http
              hostPort: 80  # 添加hostPort,将服务器端口80映射到容器端口9080。如果如此配置不能使用多副本、滚动更新
              containerPort: 80
              protocol: TCP
            - name: tls
              hostPort: 443 # 添加hostPort,将服务器端口80映射到容器端口9443。如果如此配置不能使用多副本、滚动更新
              containerPort: 443
              protocol: TCP
            - name: admin
              containerPort: 9180
              protocol: TCP
...
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      affinity: {}
      schedulerName: default-scheduler
      nodeName: k8s-1 # 指定node服务器启动,确保apisix服务不会到处乱跑
...
  # 注销删除原有滚动更新配置
  strategy:
  #   rollingUpdate:
  #     maxSurge: 25%
  #     maxUnavailable: 25%
  #   type: RollingUpdate
    type: Recreate # 重启pod会杀死原有pod后再启动。因为占用了服务器80\443端口,滚动更新时两个pod会同时占用80\443端口导致冲突

helm部署apisix-ingress-controller

# `config.apisix.serviceNamespace=apisix` 是因为apisix所在ns为`apisix`需要修改
helm install apisix-ingress-controller apisix/apisix-ingress-controller --set config.apisix.serviceNamespace=apisix --namespace ingress-apisix --create-namespace

helm部署apisix-dashboard

默认登录用户密码: admin/admin

helm install apisix-dashboard apisix/apisix-dashboard --create-namespace --namespace apisix

测试Ingress、ApisixRoute创建反向代理

Ingress、ApisixRoute区别

Ingress为K8S官方统一域名转发格式资源,不同的Ingress Controller都会读取并生成针对域名的反向代理,Apisix Ingress Controller只是其中一个。
ApisixRoute是属于Apisix Ingress Controller独有的路由转发资源,可以更加定制化配置。

创建Ingress,登录apisix-dashboard可以看到自动创建路由

# apisix-ingress-controller会自动扫描获取Ingress资源,并添加路由信息。
# 这里的svc一定是要真实存在的否则apisix-ingress-controller不会创建路由。
cat > nginx_ingress.yaml << EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  ingressClassName: apisix
  rules:
  - host: "www.test1.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: nginx-service
            port:
              number: 80
EOF

# 创建ingress
kubectl apply -f nginx_ingress.yaml

通过ApisixRoute创建路由,登录apisix-dashboard可以看到自动创建路由

# apisix-ingress-controller会自动扫描获取ApisixRoute资源,并添加路由信息。
# 这里的svc一定是要真实存在的否则apisix-ingress-controller不会创建路由。
# 参考配置: https://apisix.apache.org/zh/docs/ingress-controller/next/references/apisix_route_v2/
cat > route_test.yaml <<EOF
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  name: nginx
spec:
  http:
  - name: test
    match:
      hosts:
      - www.test2.com
      paths:
      - /*
    backends:
       - serviceName: nginx-service
         servicePort: 80
 #   plugins: # 添加插件,http请求跳转到https
 #     - name: redirect
 #       enable: true
 #       config:
 #         http_to_https: true
EOF

# 部署
kubectl apply -f route_test.yaml
  • 本文作者: jagger
  • 本文链接: /archives/helm-an-zhuang-bu-shu-apisixapisix-ingress-pei-zhi-yu-ming-zhuan-fa
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
K8S通过Ingress配置外网访问特定服务
正向代理与反向代理
jagger

jagger

66 日志
31 分类
0 标签
Creative Commons
0%
© 2026 jagger
由 Halo 强力驱动