目录

  • 相关链接
  • 前提
  • 获取higress-gateway的端口
  • 部署Demo服务
  • yaml文件
  • 部署应用
  • 创建路由
  • 方式一: 使用 Ingress CRD 创建路由
  • 创建路由
  • 访问测试
  • 方式二: 使用Higress Console 创建路由
  • 创建路由
  • 访问测试




前提

Higress已经部署成功,参考 Higress部署

获取higress-gateway的端口

kubectl get svc -n higress-system

Higress实战-02.路由配置示例_云原生


由于我们本地部署的K8S集群, 不支持LoadBalancer, 因此后续我们通过对应的 NodePort (1105)来访问gateway.

# 后续我们将通过该地址来访问gateway
# 注意, 在没有配置任何服务路由的时候, 直接请求该地址将不会返回任何值
curl 192.168.21.223:1105

部署Demo服务

示例服务监听8080端口, 并且返回 Hello World

yaml文件

demo_nginx.yaml

---
# 创建ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: demo-nginx-configmap
  labels:
    app: demo-nginx-configmap
data:
  nginx.conf: |
        user  nginx;
        worker_processes  auto;

        error_log  /var/log/nginx/error.log notice;
        pid        /var/run/nginx.pid;

        events {
              worker_connections  1024;
        }

        http {
            include       /etc/nginx/mime.types;
            default_type  application/octet-stream;
            underscores_in_headers on;

            log_format logsjson '{"@timestamp":"$time_iso8601",'
                                  '"client_ip":"$remote_addr",'
                                  '"http_user_agent":"$http_user_agent",'
                                  '"server_protocol":"$server_protocol",'
                                  '"request_method":"$request_method",'
                                  '"http_host":"$host",'
                                  '"request_uri":"$request_uri",'
                                  '"upstream_addr":"$upstream_addr",'
                                  '"upstream_status":"$upstream_status",'
                                  '"upstream_response_time":"$upstream_response_time",'
                                  '"http_x_forwarded_for":"$http_x_forwarded_for",'
                                  '"http_referer":"$http_referer",'
                                  '"request_time":"$request_time",'
                                  '"request_length":"$request_length",'
                                  '"body_bytes_sent":"$body_bytes_sent",'
                                  '"status":"$status"}';

            access_log  /var/log/nginx/access.log  logsjson;

            sendfile        on;
            #tcp_nopush     on;
            keepalive_timeout  65;
            #gzip  on;

            server {
                listen       8080;

                location / {
                    default_type application/json;
                    return 200 '{"status":"success","result":"nginx json"}';
                }

            }
        }


---
# 创建Service
apiVersion: v1
kind: Service
metadata:
  name: demo-nginx-svc
  labels:
    app: demo-nginx-svc
spec:
  type: ClusterIP
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
  selector:
    app: demo-nginx


---
# 创建Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-nginx
  labels:
    app: demo-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-nginx
  template:
    metadata:
      labels:
        app: demo-nginx
    spec:
      containers:
      - name: demo-nginx
        image: docker.io/nginx:1.22.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: nginx-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
      - name: nginx-config
        configMap:
          name: demo-nginx-configmap
          items:
          - key: nginx.conf
            path: nginx.conf

部署应用

kubectl apply -f demo_nginx.yaml

创建路由

方式一: 使用 Ingress CRD 创建路由

创建路由

# demo_nginx_ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: demo-nginx-ingress
spec:
  # 指定higress
  ingressClassName: higress
  rules:
    # 匹配任意host  
  - host: ""
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: demo-nginx-svc
            port:
              number: 8080

访问测试

使用浏览器访问 higress-gateway的NodePort. 返回了我们期望的 Hello World

Higress实战-02.路由配置示例_nginx_02

方式二: 使用Higress Console 创建路由

获取 higress console访问地址

kubectl -n higress-system get svc higress-console

Higress实战-02.路由配置示例_云原生_03

使用浏览器访问 NodeIP:39474来访问 Higress Console

创建路由

Higress实战-02.路由配置示例_json_04


Higress实战-02.路由配置示例_json_05

访问测试

使用浏览器访问 higress-gateway的NodePort. 返回了我们期望的 Hello World

Higress实战-02.路由配置示例_json_06