Istio网关Gateway 启用TLS

Istio网关Gateway概述


Istio网关Gateway是一个负责处理南北向流量的组件,它通常会暴露服务网格内部的服务,以便外部的请求能够访问到服务网格中的服务。Istio网关Gateway支持多种协议,包括HTTP、HTTPS和GRPC等。

在Istio网关Gateway中,每个服务器都包含一个或多个端口,每个端口都定义了一种协议和相应的配置Istio网关Gateway还可以定义多个TLS证书,以便对传输的数据进行加密和解密。

在配置Istio网关Gateway时,我们需要指定其所使用的负载均衡算法和服务发现机制。Istio网关Gateway支持多种服务发现机制,包括Kubernetes服务发现、Consul服务发现和Eureka服务发现等。

先来部署有TLS的网关。

1.生成密钥对

首选来生成密钥对,-keyout是生成私钥。-out是公钥,生成的密钥对。生成好了要放在指定的目录下,在/etc/istio/ingressgateway-certs/。

[root@k8s-master ~]# mkdir -p /etc/istio/ingressgateway-certs
[root@k8s-master ~]# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/istio/ingressgateway-certs/mykey.key -out /etc/istio/ingressgateway-certs/mycrt.crt -subj "/CN=mytest/O=my-test"
Generating a 2048 bit RSA private key
..............................................................................+++
........................+++
writing new private key to '/etc/istio/ingressgateway-certs/mykey.key'
-----
[root@k8s-master ~]# ls /etc/istio/ingressgateway-certs
mycrt.crt  mykey.key

这个证书并不是合法的CA颁发的,而是我们自己生成的。

2.创建tls类型的secret

[root@k8s-master ~]# kubectl create secret generic istio-ingressgateway-certs --from-file /etc/istio/ingressgateway-certs/mykey.key --from-file /etc/istio/ingressgateway-certs/mycrt.crt -n istio

这里的证书正常情况下是CA帮我们颁发的,只不过我们这里并没有使用到CA。

serverCertificate: /etc/istio/ingressgateway-certs/mycrt.crt
privateKey: /etc/istio/ingressgateway-certs/mykey.key

扫描二维码关注公众号,回复: 15899817 查看本文章

多套证书,基于多个域名


其实也就是两套证书,两套证书分配给不同的域名。 

上面是直接服务器端生成了公钥和私钥。如果是生产环境那么就需要去购买证书,购买证书可以到阿里云等厂商。

前提

  • EKS集群可用
  • istio-gateway可用

创建TLS证书secret

kubectl create secret generic shanhaitls-credential -n istio-system \
--from-file=cert=$Path/certs/server.cer \
--from-file=key=$Path/certs/server.key

Gateway添加TLS配置

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: swbom-g-gateway
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "swbom-dev-g.shanhai.huawei.com"
    - "swbom-g-test-kuboard-elb.shanhai.huawei.com"
  - port:
      number: 443
      name: https
      protocol: HTTPS
    tls:
      mode: SIMPLE
      credentialName: shanhaitls-credential # must be the same as secret
    hosts:
    - "swbom-dev-g.shanhai.huawei.com"
    - "swbom-g-test-kuboard-elb.shanhai.huawei.com"
---

 Gateway配置示例

以下是一个使用Istio Gateway进行南北流量管理的示例:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: my-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - my-service.com
    tls:
      mode: SIMPLE
      serverCertificate: /etc/certs/server.pem
      privateKey: /etc/certs/private_key.pem
  - port:
      number: 443
      name: https
      protocol: HTTPS
    hosts:
    - my-service.com
    tls:
      mode: SIMPLE
      serverCertificate: /etc/certs/server.pem
      privateKey: /etc/certs/private_key.pem
  - port:
      number: 8443
      name: https-admin
      protocol: HTTPS
    hosts:
    - my-admin.com
    tls:
      mode: SIMPLE
      serverCertificate: /etc/certs/server.pem
      privateKey: /etc/certs/private_key.pem
  - port:
      number: 8080
      name: grpc
      protocol: GRPC
    hosts:
    - my-service.com
    tls:
      mode: SIMPLE
      serverCertificate: /etc/certs/server.pem
      privateKey: /etc/certs/private_key.pem

在上述示例中,我们首先定义了一个名为my-gateway的Gateway对象。该对象有一个标签选择器istio: ingressgateway,用于将其指定为Istio Ingress Gateway。

在该Gateway对象中,我们定义了四个服务器,分别处理不同的端口和协议。

其中,第一个服务器用于处理HTTP流量,第二个服务器用于处理HTTPS流量,第三个服务器用于处理HTTPS管理员流量,第四个服务器用于处理GRPC流量。每个服务器都定义了一个名为port的子对象,用于指定其所使用的端口、协议和端口名称。每个服务器还定义了一个名为hosts的子对象,用于指定其所支持的主机名。此外,每个服务器还定义了一个名为tls的子对象,用于指定其所使用的TLS证书的相关配置。

猜你喜欢

转载自blog.csdn.net/qq_34556414/article/details/131924780