在Istio上创建自定义的ingress-gateway

简介: 我们都知道,在istio中可以通过ingress gateway将服务暴露给外部使用,但是我们使用的ingress规则都是落在istio部署时默认创建的istio-ingressgateway上,如果我们希望创建自定义的ingressgateway该怎么操作呢,本文就带大家一步步操作,创建一个自定义的ingressgateway 环境准备 创建Kubernetes集群 阿里云容器服务Kubernetes 1.11.2目前已经上线,可以通过容器服务管理控制台非常方便地快速创建 Kubernetes 集群。

我们都知道,在istio中可以通过ingress gateway将服务暴露给外部使用,但是我们使用的ingress规则都是落在istio部署时默认创建的istio-ingressgateway上,如果我们希望创建自定义的ingressgateway该怎么操作呢,本文就带大家一步步操作,创建一个自定义的ingressgateway

环境准备

创建Kubernetes集群

阿里云容器服务Kubernetes 1.11.2目前已经上线,可以通过容器服务管理控制台非常方便地快速创建 Kubernetes 集群。

部署istio

部署bookinfo

首先为default 命名空间打上标签 istio-injection=enabled

kubectl label namespace default istio-injection=enabled

使用Kubectl命令部署Bookinfo示例应用

kubectl apply -f https://raw.githubusercontent.com/istio/istio/1.0.2/samples/bookinfo/platform/kube/bookinfo.yaml

部署完成后效果如下图:

$ kubectl get pod,svc
NAME                                  READY   STATUS    RESTARTS   AGE
pod/details-v1-5d88f495b7-cvxk5       2/2     Running   0          19h
pod/productpage-v1-774fd75c99-5l898   2/2     Running   0          19h
pod/ratings-v1-64664b6bcf-j7lzh       2/2     Running   0          19h
pod/reviews-v1-fd7c6fdf5-4px4g        2/2     Running   0          19h
pod/reviews-v2-56b67454cc-fmtl8       2/2     Running   0          19h
pod/reviews-v3-86878d875-6xztb        2/2     Running   0          19h

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/details       ClusterIP   172.21.13.128   <none>        9080/TCP   19h
service/kubernetes    ClusterIP   172.21.0.1      <none>        443/TCP    21h
service/productpage   ClusterIP   172.21.7.104    <none>        9080/TCP   19h
service/ratings       ClusterIP   172.21.7.176    <none>        9080/TCP   19h
service/reviews       ClusterIP   172.21.1.207    <none>        9080/TCP   19h

创建自定义ingressgateway

---
# Source: istio/charts/gateways/templates/serviceaccount.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: customgateway-service-account
  namespace: default
  labels:
    app: customgateway
---

---
# Source: istio/charts/gateways/templates/clusterrole.yaml

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  labels:
    app: gateways
  name: customgateway-default # default should replaced by actual namespace
rules:
- apiGroups: ["extensions"]
  resources: ["thirdpartyresources", "virtualservices", "destinationrules", "gateways"]
  verbs: ["get", "watch", "list", "update"]
---

---
# Source: istio/charts/gateways/templates/clusterrolebindings.yaml

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: customgateway-default # default should replaced by actual namespace
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: customgateway-default # default should replaced by actual namespace
subjects:
  - kind: ServiceAccount
    name: customgateway-service-account
    namespace: default
---

---
# Source: istio/charts/gateways/templates/service.yaml

apiVersion: v1
kind: Service
metadata:
  name: customgateway
  namespace: default
  annotations:
  labels:
    istio: customgateway
spec:
  type: LoadBalancer
  selector:
    istio: customgateway
  ports:
    -
      name: http
      port: 80
      targetPort: 80
    -
      name: https
      port: 443
      targetPort: 443
---

---
# Source: istio/charts/gateways/templates/deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: customgateway
  namespace: default
  labels:
    istio: customgateway
spec:
  replicas: 1
  template:
    metadata:
      labels:
        istio: customgateway
      annotations:
        sidecar.istio.io/inject: "false"
        scheduler.alpha.kubernetes.io/critical-pod: ""
    spec:
      serviceAccountName: customgateway-service-account
      containers:
        - name: istio-proxy
          image: "registry.cn-beijing.aliyuncs.com/aliacs-app-catalog/proxyv2:1.0.3"
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
            - containerPort: 443

          args:
          - proxy
          - router
          - -v
          - "2"
          - --discoveryRefreshDelay
          - '1s' #discoveryRefreshDelay
          - --drainDuration
          - '45s' #drainDuration
          - --parentShutdownDuration
          - '1m0s' #parentShutdownDuration
          - --connectTimeout
          - '10s' #connectTimeout
          - --serviceCluster
          - customgateway
          - --zipkinAddress
          - zipkin.istio-system:9411
          - --proxyAdminPort
          - "15000"
          - --controlPlaneAuthPolicy
          - NONE
          - --discoveryAddress
          - istio-pilot.istio-system:8080
          resources:
            requests:
              cpu: 10m

          env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace
          - name: INSTANCE_IP
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: status.podIP
          - name: ISTIO_META_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          volumeMounts:
          - name: istio-certs
            mountPath: /etc/certs
            readOnly: true
          - name: customgateway-certs
            mountPath: "/etc/istio/customgateway-certs"
            readOnly: true
          - name: customgateway-ca-certs
            mountPath: "/etc/istio/customgateway-ca-certs"
            readOnly: true
      volumes:
      - name: istio-certs
        secret:
          secretName: istio.customgateway-service-account
          optional: true
      - name: customgateway-certs
        secret:
          secretName: "istio-customgateway-certs"
          optional: true
      - name: customgateway-ca-certs
        secret:
          secretName: "istio-customgateway-ca-certs"
          optional: true
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: beta.kubernetes.io/arch
                operator: In
                values:
                - amd64
                - ppc64le
                - s390x
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 2
            preference:
              matchExpressions:
              - key: beta.kubernetes.io/arch
                operator: In
                values:
                - amd64
          - weight: 2
            preference:
              matchExpressions:
              - key: beta.kubernetes.io/arch
                operator: In
                values:
                - ppc64le
          - weight: 2
            preference:
              matchExpressions:
              - key: beta.kubernetes.io/arch
                operator: In
                values:
                - s390x
---

---
# Source: istio/charts/gateways/templates/autoscale.yaml

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
    name: customgateway
    namespace: default
spec:
    maxReplicas: 5
    minReplicas: 1
    scaleTargetRef:
      apiVersion: apps/v1beta1
      kind: Deployment
      name: customgateway
    metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: 80
---

上面这段yaml在default namespace定义了一个名叫customgateway的ingressgateway,并为他创建了serviceaccount,HPA等一系列相关的配置,如果我们需要定义多个,需要替换yaml里的default和customgateway为自己想要的名字

定义入口路由

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: customgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
          number: 9080

这里最重要的是为Gateway指定规则落在哪个deploy上,这里指定的是istio: customgateway

访问应用

获取自定义ingressgateway的入口然后访问应用

export INGRESS_HOST=$(kubectl -n default get service customgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
export INGRESS_PORT=$(kubectl -n default get service customgateway -o jsonpath='{.spec.ports[?(@.name=="http")].port}')
curl -o /dev/null -s -w "%{http_code}\n" http://${INGRESS_HOST}:${INGRESS_PORT}/productpage

如果返回200则说明配置正确

总结

上面通过示例演示了Istio如何创建一个自定义的ingress gateway

欢迎大家使用阿里云上的容器服务,快速搭建微服务的开放治理平台Istio,简单地集成到自己项目的微服务开发中。

相关实践学习
使用ACS算力快速搭建生成式会话应用
阿里云容器计算服务 ACS(Container Compute Service)以Kubernetes为使用界面,采用Serverless形态提供弹性的算力资源,使您轻松高效运行容器应用。本文将指导您如何通过ACS控制台及ACS集群证书在ACS集群中快速部署并公开一个容器化生成式AI会话应用,并监控应用的运行情况。
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
负载均衡 Kubernetes API
Istio:Gateway设计与实现
Istio:Gateway设计与实现
Istio:Gateway设计与实现
|
Java Spring
Spring Cloud Alibaba - 26 Gateway-自定义谓词工厂RoutePredicateFactory
Spring Cloud Alibaba - 26 Gateway-自定义谓词工厂RoutePredicateFactory
311 0
|
12月前
|
JSON API Go
Golang工程组件:自定义HTTP规则的grpc-gateway选项
总的来说,grpc-gateway提供了一种简单有效的方式来为你的gRPC服务提供RESTful风格的API。通过自定义HTTP规则,你可以灵活地定义你的API的行为,以满足你的应用的需求。
318 27
|
开发框架 Java .NET
线上debug&gateway自定义路由规则
【10月更文挑战第20天】本文介绍了线上调试和网关自定义路由规则的配置方法。线上调试部分涵盖日志记录、远程调试等内容,包括如何设置详细的日志级别、添加自定义日志信息以及使用ELK堆栈进行日志分析。网关自定义路由规则部分则讲解了Spring Cloud Gateway和Kong中基于路径、请求头、请求参数等条件的路由配置方法。
322 1
|
Kubernetes JavaScript API
如何理解 Istio Ingress, 它与 API Gateway 有什么区别?东西流量?南北流量?
这三者都和流量治理密切相关,那么流量治理在过去和现在有什么区别呢?都是如何做的呢? 在学习istio的时候对流量管理加深了理解。什么是东西流量?什么是南北流量?
740 0
|
Java 微服务 Spring
SpringCloud gateway自定义请求的 httpClient
SpringCloud gateway自定义请求的 httpClient
764 3
|
JSON Java 数据格式
Spring Cloud Gateway-自定义异常处理
我们平时在用SpringMVC的时候,只要是经过DispatcherServlet处理的请求,可以通过@ControllerAdvice和@ExceptionHandler自定义不同类型异常的处理逻辑,具体可以参考ResponseEntityExceptionHandler和DefaultHandlerExceptionResolver,底层原理很简单,就是发生异常的时候搜索容器中已经存在的异常处理器并且匹配对应的异常类型,匹配成功之后使用该指定的异常处理器返回结果进行Response的渲染,如果找不到默认的异常处理器则用默认的进行兜底。
1078 0
Spring Cloud Gateway-自定义异常处理
|
JSON 安全 关系型数据库
SpringCloud Gateway 实现自定义全局过滤器 + JWT权限验证
SpringCloud Gateway 实现自定义全局过滤器 + JWT权限验证
|
Java Spring
springcloud gateway sential 限流 自定义参数限流执行顺序问题
springcloud gateway sential 限流 自定义参数限流执行顺序问题
356 1
SpringCloud学习(十七):Gateway网关的自定义全局GlobalFilter
虽然官方为Gateway提供了很多filter,但其实并不使用,我们更多的还是使用自己的配置。 在9527网关模块中新建一个filter包,在里面写一个类来实现自定义filter
382 0
SpringCloud学习(十七):Gateway网关的自定义全局GlobalFilter