【流量网关】apisix统一的流量入口方案(外网版),实现https访问
整体方案是通过apisix网关,在apisix-dashboard里面在后面配置k8s里面svc的地址,apisix不认识这个地址,会去做域名解析,而如果将dns的配置文件,加上k8s里面的coredns,就能查询到svc对应到的clusterip,进而发送请求至clusterip,得到数据。
1、配置dns配置文件
vim /etc/resolv.conf
[root@k8s-node0204 conf]# cat /etc/resolv.conf
; generated by /usr/sbin/dhclient-script
nameserver 10.96.0.10
nameserver 183.60.83.19
nameserver 183.60.82.98
# 其中183.60.83.19及183.60.82.98是自已就有的 10.96.0.10是k8s自带的coredns的clusterip
# 这个值,可能通过以下命令查询(在master节点上执行)
[root@k8s-master ~]# kubectl get svc -A -owide | grep kube-dns
*** ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP ***
2、安装etcd
APISIX 使用 etcd 作为配置中心进行保存和同步配置。在安装 APISIX 之前,需要在你的主机上安装 etcd。
ETCD_VERSION='3.5.4'
wget https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-amd64.tar.gz
tar -xvf etcd-v${ETCD_VERSION}-linux-amd64.tar.gz && \
cd etcd-v${ETCD_VERSION}-linux-amd64 && \
sudo cp -a etcd etcdctl /usr/bin/
nohup etcd >/tmp/etcd.log 2>&1 &
3、rpm形式安装apisix
3.1 安装apisix
建议在非master上面执行,但一定要在k8s的节点上,因为节点上能解析到coredns的地址(coredns的clusterip),进而解析到svc的地址。
sudo yum install -y https://repos.apiseven.com/packages/centos/apache-apisix-repo-1.0-1.noarch.rpm
sudo yum-config-manager --add-repo https://repos.apiseven.com/packages/centos/apache-apisix.repo
sudo yum install apisix
3.2 启动apisix
apisix init
apisix start
此时已经可能通过节点ip+9080(默认是9080端口而不是80端口,后面会改成80端口)访问到数据了。
3.3 配置apisix
默认的apisix的配置文件在以下文件/usr/local/apisix/conf
/config.yaml,按以下添加即可
...
apisix:
node_listen: 80 # APISIX listening port
deployment:
role: traditional
...
添加后需要重启
apisix stop
apisix start
此时应该可以通过节点ip+80端口访问到数据。
4、rpm形式安装apisix-dashboard
Apisix-Dashboard 是 Apisix 的管理面板,旨在通过一个前端界面让用户尽可能更方便地操作 Apisix。
sudo yum install -y https://github.com/apache/apisix-dashboard/releases/download/v3.0.1/apisix-dashboard-3.0.1-0.el7.x86_64.rpm
systemctl start apisix-dashboard
默认情况下,ip+9000直接在浏览器中不能打开,但直接在该台机器通过curl 的方式可以打开,原因是在其配置文件中有相关设置
# 配置文件是这个文件:/usr/local/apisix/dashboard/conf/conf.yaml
原来的
...
allow_list: # If we don't set any IP list, then any IP access is allowed by default.
- 127.0.0.1 # The rules are checked in sequence until the first match is found.
- ::1 # In this example, access is allowed only for IPv4 network 127.0.0.1, and for IPv6 network ::1.
# It also support CIDR like 192.168.1.0/24 and 2001:0db8::/32
...
更改之后的(注释掉下面的)
allow_list: # If we don't set any IP list, then any IP access is allowed by default.
#- 127.0.0.1 # The rules are checked in sequence until the first match is found.
#- ::1 # In this example, access is allowed only for IPv4 network 127.0.0.1, and for IPv6 network ::1.
# It also support CIDR like 192.168.1.0/24 and 2001:0db8::/32
更改之后需要重新启动
systemctl stop apisix-dashboard
systemctl start apisix-dashboard
启动后可以用ip+9000(默认)访问到前端界面(默认的用户名及密码均为admin)。
5、一个简单的例子
当前有一个nacos地址,其nodeport外网访问地址为:
http://nacos.howlaisi.com:30293/nacos
其svc对应到的dns为:
http://nacos-node.infrastructure.svc.cluster.local:8848
svc对应到的这个地址,在k8s内部能够识别,是因为coredns的原因,能够被域名解析,正常情况下,直接在虚拟机上面是不能够被识别的,现在由于虚拟机上面的/etc/ressolv.conf配置了k8s内部的coredns的地址,按理说能够被解析到,如图所示。
]
整体流程没有问题,现在去apisix-dashboard配置路由,将流量统一由apisix(80端口)导入k8s集群内部。
接下来,一路next即可。
使用安装apisix的机器的ip+80(80可以省略)+/nacos(这个是上面配置的匹配规则,匹配到了/nacos,就将流量往nacos的这个服务上面转发)
再配置一个域名,指向此台机器。
此时,在浏览器中输入http://nacos.howlaisi.com/nacos即可访问到数据了。
https访问
张师傅的域名是在腾讯云上面买的,在这个界面可以下载一年的证书操作。根据最新的政策,个人可免费下载50份证书,证书一年过期。
https://console.cloud.tencent.com/ssl
根据张师傅的经验,验证及签发,原则上是1天,但实际上几分钟就能完成。
下载证书
在apisix中配置证书,配置后提交即可
默认情况下:apisix开放的是9443端口,乍为https的端口,而如果不想写端口的情况下,需要将apisix的默认的https端口,修改成为443。
具体修改如下,找到apisix的配置文件(/usr/local/apisix/conf/config.yaml),修改端口,然后重启即可(apisix stop && apisix start),至于为什么这样子修改,原来apisix有一个默认的配置文件,叫做/usr/local/apisix/conf/config-default.yaml,可以在里面查找相关默认值,然后依照格式,修改在config.yaml即可。
apisix:
node_listen: 80 # APISIX listening port
deployment:
role: traditional
role_traditional:
config_provider: etcd
# 将以上的文件,加上https的端口即可
apisix:
node_listen: 80 # APISIX listening port
ssl:
enable: true
listen: # APISIX listening port in https.
- port: 443
deployment:
role: traditional
role_traditional:
config_provider: etcd
张师傅的kubesphere环境是:
http://ks.howlaisi.com
评论区