当前位置:首页 > Web开发 > 正文

如果要实现访问http自动跳转到https

2024-03-31 Web开发

标签:

写在前面

上一篇文章中介绍了基于Nginx实现Ingress Controller的实现,介绍了Nginx Ingress Controller安置、相关成果,TLS,高级特性等介绍,,本章开始介绍基于腾讯云TKE实现ingress处事袒露。

1. TKE ingress 1.1 TKE ingress架构

TKE是Tencent Kubernetes Engine即腾讯云基于kubernetes供给的公有云上容器云处事,TKE供给了两种袒露处事的方法:service和ingress。

内网CLB,四层负载均衡,供给VPC内访谒,通过node节点的NodePort转发至service;

外网CLB,四层负载均衡,供给公网访谒,需要node节点具有访谒公网的能力;

ingress, 七层负载均衡,供给http和https接入,供给ingress控制器的成果,借助NodePort转发

要使用TKE的ingress成果,需要了解一下相关的组件内容:

l7-lb-controller ingress客户端,安置在kube-system,用于解析ingress配置并更新CLB的法则

CLB 七层负载均衡,供给ingress controller的成果,按照ingress法则创建http/https监听器,配置转发法则,以NodePort端口绑定后端RS

Service 用于ingress处事发明,通过NodePort方法接入CLB

证书 用于供给https接入,配置在CLB负载均衡上,供给CA签名证书,通过Secrets封装给CLB使用

由于nginx ingress controller是直接以Pod的形势部署在kubernetes集群中,借助于service的处事发明可直接实现和pod通讯,而TKE中ingress controller未直接部署在k8s集群中,网络的接入需借助于service的NodePort实现接入,其数据流如下图:

1.2 ingress虚拟主机

环境说明: 创建两个Deployment并以NodePort方法袒露处事,www1.happylau.cn对应tke-app-1处事,同理www2.happylau.cn对应tke-app-2处事,如下演示操纵过程:

1、创建两个Deployments

[[email protected]_10_2_centos ingress]# kubectl create deployment tke-app-1 --image=nginx:1.7.9 [[email protected]_10_2_centos ingress]# kubectl create deployment tke-app-2 --image=nginx:1.7.9

2、 将两个Deployment以NodePort的方法袒露处事

[[email protected]_10_2_centos ~]# kubectl expose deployment tke-app-1 --port=80 --type=NodePort [[email protected]_10_2_centos ~]# kubectl expose deployment tke-app-2 --port=80 --type=NodePort 检察处事列表 [[email protected]_10_2_centos ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 172.16.255.1 <none> 443/TCP 83d tke-app-1 NodePort 172.16.255.91 <none> 80:30597/TCP 2s tke-app-2 NodePort 172.16.255.236 <none> 80:31674/TCP 73s

3、界说ingress法则,界说两个host将差别主机转发至backend差此外service

apiVersion: extensions/v1beta1 kind: Ingress metadata: name: tke-ingress-demo annotations: kubernetes.io/ingress.class: qcloud spec: rules: - host: www1.happylau.cn http: paths: - path: / backend: serviceName: tke-app-1 servicePort: 80 - host: www2.happylau.cn http: paths: - path: / backend: serviceName: tke-app-2 servicePort: 80

4、 应用ingress法则,并检察ingress详情,可以看到ingress创建了一个公网CLB实例

#应用ingress法则 [[email protected]_10_2_centos ingress]# kubectl apply -f tke-ingress-demo.yaml ingress.extensions/tke-ingress-demo created #检察ingress列表 [[email protected]_10_2_centos ingress]# kubectl get ingresses NAME HOSTS ADDRESS PORTS AGE tke-ingress-demo www1.happylau.cn,www2.happylau.cn 140.143.84.xxx 80 67s #检察 ingress详情 [[email protected]_10_2_centos ingress]# kubectl describe ingresses tke-ingress-demo Name: tke-ingress-demo Namespace: default Address: 140.143.84.xxx Default backend: default-http-backend:80 (<none>) Rules: Host Path Backends ---- ---- -------- www1.happylau.cn / tke-app-1:80 (172.16.1.15:80) www2.happylau.cn / tke-app-2:80 (172.16.2.17:80) Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"qcloud"},"name":"tke-ingress-demo","namespace":"default"},"spec":{"rules":[{"host":"www1.happylau.cn","http":{"paths":[{"backend":{"serviceName":"tke-app-1","servicePort":80},"path":"http://www.mamicode.com/"}]}},{"host":"www2.happylau.cn","http":{"paths":[{"backend":{"serviceName":"tke-app-2","servicePort":80},"path":"http://www.mamicode.com/"}]}}]}} kubernetes.io/ingress.class: qcloud kubernetes.io/ingress.qcloud-loadbalance-id: lb-a0xwhcx3 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal EnsuringIngress 69s (x3 over 89s) loadbalancer-controller Ensuring ingress Normal CREATE 69s (x2 over 70s) loadbalancer-controller create loadbalancer succ Normal EnsuredIngress 68s (x3 over 70s) loadbalancer-controller Ensured ingress

5、测试验证,将IP和域名写入到hosts文件中,访谒域名测试验证,如下通过curl解析的方法测试验证

6、ingress会创建一个CLB,并在CLB中创建监听器、设置转发法则、绑定后端RS,下图是CLB上自动生成的法则

通过上面演示可知:

自动创建CLB实例

CLB实例上配置监听器

配置转发法则

绑定Node节点

绑定端口为service创建的NodePort

1.3 ingress证书加密

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32212.html