CKA [Networking] – Gateway使用实践
Gateway和Ingress一样在Kubernetes当中默认都没有安装的。
因为默认Kubernetes默认是没有安装Gateway的,所以当我查询gatewayclass的时候就会报以下的错误。

这文章的实践教程是安装 NGINX Gateway Fabric Controller。

安装Nginx Gateway Controller
参考了Nginx Gateway Fabric官方文档:https://docs.nginx.com/nginx-gateway-fabric/installation/installing-ngf/manifests/
【安装Gateway API resources - 标准 CRD】
- 这个必须第一个被安装
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v1.6.2" | kubectl apply -f -
【安装Nginx Gateway Fabric - 专属 CRD】
kubectl apply -f https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/v1.6.2/deploy/crds.yaml
【安装Nginx Gateway Fabric - 应用】
- 包含创建了Deployment,GatewayClass
- 默认使用了LoadBalancer,你也可自行更改使用nodeport来暴露service
kubectl apply -f https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/v1.6.2/deploy/default/deploy.yaml

Gateway API 标准CRD会一直在更新, 你可以安装最新版本的
https://github.com/kubernetes-sigs/gateway-api/releases
验证Deployment
kubectl get pods -n nginx-gateway
kubectl get namespaces

验证GatewayClass
应用创建成功之后也会产生这个GatewayClass
kubectl get gatewayclass
kubectl describe gatewayclass nginx

自行创建Gateway
创建nginx-gateway,并且使用了nginx的GatewayClass, 下图看到的ADDRESS就是使用了GatewayClass绑定的GatewayController的LoadBalancer IP
apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
name: nginx-gateway
namespace: default
spec:
gatewayClassName: nginx
listeners:
- name: http
protocol: HTTP
port: 80

自行创建Deployment和Service
kubectl create deployment apache --image=httpd:latest --port=80
kubectl expose deployment apache --name=apache-service --port=80 --target-port=80 --type=ClusterIP
自行创建HTTPRoute
使用以上创建好的nginx-gateway, 然后把流量导去以上创建的service, 名字是apache-service
apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
name: apache-route
namespace: default
spec:
parentRefs:
- name: nginx-gateway
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: apache-service
port: 80
测试可行性
从外部访问LoadBalancer的public IP 就能把流量导到服务来了 , 由于没有set到hostname所以访问IP就能导流到service。

删除资源
如果只是学习的话,那么设定完之后就必须删除创建的资源,尤其是LoadBalancer,避免被Overcharge
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v1.6.2" | kubectl delete -f -
kubectl delete -f https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/v1.6.2/deploy/crds.yaml
kubectl delete -f https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/v1.6.2/deploy/default/deploy.yaml