gateway

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2025 License: MIT Imports: 10 Imported by: 0

README

服务网关

采用K8s原生Gateway

安装网关

nginx-gateway-fabric

Install the Gateway API resources
$ kubectl kustomize "https://github.com/nginxinc/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v1.3.0" | kubectl apply -f -
customresourcedefinition.apiextensions.k8s.io/gatewayclasses.gateway.networking.k8s.io created
customresourcedefinition.apiextensions.k8s.io/gateways.gateway.networking.k8s.io created
customresourcedefinition.apiextensions.k8s.io/grpcroutes.gateway.networking.k8s.io created
customresourcedefinition.apiextensions.k8s.io/httproutes.gateway.networking.k8s.io created
customresourcedefinition.apiextensions.k8s.io/referencegrants.gateway.networking.k8s.io created

确认CRD已经安装成功:

$ kubectl get crds | grep gateway
NAME                                        CREATED AT
gatewayclasses.gateway.networking.k8s.io    2024-06-16T03:42:18Z
gateways.gateway.networking.k8s.io          2024-06-16T03:42:18Z
grpcroutes.gateway.networking.k8s.io        2024-06-16T03:42:18Z
httproutes.gateway.networking.k8s.io        2024-06-16T03:42:18Z
referencegrants.gateway.networking.k8s.io   2024-06-16T03:42:18Z
Deploy the NGINX Gateway Fabric CRDs
$ kubectl apply -f https://raw.githubusercontent.com/nginxinc/nginx-gateway-fabric/v1.3.0/deploy/crds.yaml
customresourcedefinition.apiextensions.k8s.io/clientsettingspolicies.gateway.nginx.org created
customresourcedefinition.apiextensions.k8s.io/nginxgateways.gateway.nginx.org created
customresourcedefinition.apiextensions.k8s.io/nginxproxies.gateway.nginx.org created
customresourcedefinition.apiextensions.k8s.io/observabilitypolicies.gateway.nginx.org created

确认CRD已经安装成功:

$ kubectl get crds | grep nginx
clientsettingspolicies.gateway.nginx.org    2024-06-16T03:49:37Z
nginxgateways.gateway.nginx.org             2024-06-16T03:49:37Z
nginxproxies.gateway.nginx.org              2024-06-16T03:49:37Z
observabilitypolicies.gateway.nginx.org     2024-06-16T03:49:37Z
Deploy NGINX Gateway Fabric

提前拉却镜像:

docker pull ghcr.io/nginxinc/nginx-gateway-fabric:1.3.0
docker pull ghcr.io/nginxinc/nginx-gateway-fabric/nginx:1.3.0

创建部署

$ kubectl apply -f https://github.com/nginxinc/nginx-gateway-fabric/releases/download/v1.3.0/nginx-gateway.yaml
namespace/nginx-gateway created
serviceaccount/nginx-gateway created
clusterrole.rbac.authorization.k8s.io/nginx-gateway created
clusterrolebinding.rbac.authorization.k8s.io/nginx-gateway created
deployment.apps/nginx-gateway created
gatewayclass.gateway.networking.k8s.io/nginx created
nginxgateway.gateway.nginx.org/nginx-gateway-config created

确认部署已经运行:

$ kubectl get pods -n nginx-gateway 
NAME                             READY   STATUS    RESTARTS        AGE
nginx-gateway-5d49f68457-hj98x   2/2     Running   1 (7m27s ago)   7m38s
Expose NGINX Gateway Fabric
  1. Create a NodePort service
kubectl apply -f https://raw.githubusercontent.com/nginxinc/nginx-gateway-fabric/v1.3.0/deploy/manifests/service/nodeport.yaml
  1. 确认service已经创建成功
kubectl get svc nginx-gateway -n nginx-gateway

NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
nginx-gateway   NodePort   10.104.170.23   <none>        80:31146/TCP,443:30140/TCP   65s
  1. 确认NodePort端口能访问成功
$ curl localhost:31146
curl: (52) Empty reply from server
$ curl localhost:30140
curl: (52) Empty reply from server

网关使用

alt text

Create the Gateway API resources

Gateway 用来描述流量处理基础设施的一个实例, 也就是我们刚才安装的 NGINX Gateway Fabric(你也可以选择其他实现)

创建一个名为cafe的 Nginx类型的网关(网关描述信息)

kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: cafe
spec:
  gatewayClassName: nginx
  listeners:
  - name: http
    port: 80
    protocol: HTTP
EOF

确认网关已经创建

$ kubectl get gateway
NAME   CLASS   ADDRESS   PROGRAMMED   AGE
cafe   nginx             True         6s
$ kubectl describe gateway cafe
...
Create the coffee application
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coffee
spec:
  replicas: 1
  selector:
    matchLabels:
      app: coffee
  template:
    metadata:
      labels:
        app: coffee
    spec:
      containers:
      - name: coffee
        image: kennethreitz/httpbin
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: coffee
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: coffee
EOF
$ kubectl get po,svc | grep coffee            
pod/coffee-6bfcb6dc5f-gl92m           1/1     Running     0               84s
service/coffee            ClusterIP   10.103.207.208   <none>        80/TCP      84s
Routing Traffic to Your Application

alt text

kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: coffee
spec:
  parentRefs:
  - name: cafe
  hostnames:
  - "cafe.example.com"
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /
    backendRefs:
    - name: coffee
      port: 80
EOF
$ kubectl get httproute
NAME     HOSTNAMES              AGE
coffee   ["cafe.example.com"]   9s
curl --location --request GET "localhost:31146/get?foo1=bar1&foo2=bar2" -H "Host: cafe.example.com"
{
  "args": {
    "foo1": "bar1", 
    "foo2": "bar2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Connection": "close", 
    "Host": "cafe.example.com", 
    "User-Agent": "curl/8.6.0"
  }, 
  "origin": "192.168.65.3", 
  "url": "http://cafe.example.com/get?foo1=bar1&foo2=bar2"
}

参考

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

https://github.com/kubernetes-sigs/gateway-api

func NewGateway

func NewGateway(restconf *rest.Config, resources *meta.ApiResourceList) *Client

func (*Client) CreateGRPCRoute

func (c *Client) CreateGRPCRoute(
	ctx context.Context,
	ins *gatewayv1.GRPCRoute,
	req *meta.CreateRequest) (
	*gatewayv1.GRPCRoute, error)

func (*Client) CreateGateway

func (c *Client) CreateGateway(
	ctx context.Context,
	ins *gatewayv1.Gateway,
	req *meta.CreateRequest,
) (
	*gatewayv1.Gateway, error)

func (*Client) CreateGatewayClass

func (c *Client) CreateGatewayClass(
	ctx context.Context,
	ins *gatewayv1.GatewayClass,
	req *meta.CreateRequest,
) (
	*gatewayv1.GatewayClass, error)

func (*Client) CreateHttpRoute

func (c *Client) CreateHttpRoute(
	ctx context.Context,
	ins *gatewayv1.HTTPRoute,
	req *meta.CreateRequest) (
	*gatewayv1.HTTPRoute, error)

func (*Client) CreateReferenceGrant

func (c *Client) CreateReferenceGrant(
	ctx context.Context,
	req *gatewayv1beta1.ReferenceGrant) (
	*gatewayv1beta1.ReferenceGrant, error)

func (*Client) DeleteGRPCRoute

func (c *Client) DeleteGRPCRoute(
	ctx context.Context,
	req *meta.DeleteRequest) (
	*gatewayv1.GRPCRoute, error)

func (*Client) DeleteGateway

func (c *Client) DeleteGateway(
	ctx context.Context,
	req *meta.DeleteRequest) (
	*gatewayv1.Gateway, error)

func (*Client) DeleteGatewayClass

func (c *Client) DeleteGatewayClass(
	ctx context.Context,
	req *meta.DeleteRequest) (*gatewayv1.GatewayClass, error)

func (*Client) DeleteHttpRoute

func (c *Client) DeleteHttpRoute(
	ctx context.Context,
	req *meta.DeleteRequest) (
	*gatewayv1.HTTPRoute, error)

func (*Client) DeleteReferenceGrant

func (c *Client) DeleteReferenceGrant(
	ctx context.Context,
	req *meta.DeleteRequest) error

func (*Client) GetGRPCRoute

func (c *Client) GetGRPCRoute(
	ctx context.Context,
	req *meta.GetRequest) (
	*gatewayv1.GRPCRoute, error)

func (*Client) GetGateway

func (c *Client) GetGateway(
	ctx context.Context,
	req *meta.GetRequest) (
	*gatewayv1.Gateway, error)

func (*Client) GetGatewayClass

func (c *Client) GetGatewayClass(
	ctx context.Context,
	req *meta.GetRequest) (
	*gatewayv1.GatewayClass, error)

func (*Client) GetHttpRoute

func (c *Client) GetHttpRoute(
	ctx context.Context,
	req *meta.GetRequest) (
	*gatewayv1.HTTPRoute, error)

func (*Client) GetReferenceGrant

func (c *Client) GetReferenceGrant(
	ctx context.Context,
	req *meta.GetRequest) (
	*gatewayv1beta1.ReferenceGrant, error)

func (*Client) ListGRPCRouteList

func (c *Client) ListGRPCRouteList(
	ctx context.Context,
	req *meta.ListRequest) (
	*gatewayv1.GRPCRouteList, error)

func (*Client) ListGateway

func (c *Client) ListGateway(
	ctx context.Context,
	req *meta.ListRequest) (
	*gatewayv1.GatewayList, error)

func (*Client) ListGatewayClass

func (c *Client) ListGatewayClass(
	ctx context.Context,
	req *meta.ListRequest) (
	*gatewayv1.GatewayClass, error)

func (*Client) ListHttpRoute

func (c *Client) ListHttpRoute(
	ctx context.Context,
	req *meta.ListRequest) (
	*gatewayv1.HTTPRouteList, error)

func (*Client) ListReferenceGrant

func (c *Client) ListReferenceGrant(
	ctx context.Context,
	req *meta.ListRequest) (
	*gatewayv1beta1.ReferenceGrantList, error)

func (*Client) UpdateGRPCRoute

func (c *Client) UpdateGRPCRoute(
	ctx context.Context,
	req *gatewayv1.GRPCRoute) (
	*gatewayv1.GRPCRoute, error)

func (*Client) UpdateGateway

func (c *Client) UpdateGateway(
	ctx context.Context,
	req *gatewayv1.Gateway) (
	*gatewayv1.Gateway, error)

func (*Client) UpdateGatewayClass

func (c *Client) UpdateGatewayClass(
	ctx context.Context,
	req *gatewayv1.GatewayClass) (
	*gatewayv1.GatewayClass, error)

func (*Client) UpdateHttpRoute

func (c *Client) UpdateHttpRoute(
	ctx context.Context,
	req *gatewayv1.HTTPRoute) (
	*gatewayv1.HTTPRoute, error)

func (*Client) UpdateReferenceGrant

func (c *Client) UpdateReferenceGrant(
	ctx context.Context,
	req *gatewayv1beta1.ReferenceGrant) (
	*gatewayv1beta1.ReferenceGrant, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL