关于java:Kubernetes用Helm安装Ingress并踩一下使用的坑

49次阅读

共计 3982 个字符,预计需要花费 10 分钟才能阅读完成。

1 前言

欢送拜访南瓜慢说 www.pkslow.com 获取更多精彩文章!

IngressKubernetes 一个十分重要的 Controller,它相似一个路由转发的组件,能够让外界拜访Kubernetes 外部的 Service。除了Ingress,还有NodePortLoadBalance 等形式,Ingress裸露给外界的形式还是很罕用的。

2 装置 Ingress

咱们通过 helm 来装置,会不便一些,先更新 helm 的仓库。

$ helm repo update

Helm相干文章:用 Helm 部署 Kubernetes 利用,反对多环境部署与版本回滚

容器技术相干文章

更新完之后,查找仓库对于 Ingress 的包有哪些:

$ $ helm search repo ingress
NAME                              CHART VERSION    APP VERSION    DESCRIPTION                                       
azure/gce-ingress                 1.2.0            1.4.0          A GCE Ingress Controller                          
azure/ingressmonitorcontroller    1.0.48           1.0.47         IngressMonitorController chart that runs on kub...
azure/nginx-ingress               1.41.2           v0.34.1        An nginx Ingress controller that uses ConfigMap...
stable/nginx-ingress              0.9.5            0.10.2         An nginx Ingress controller that uses ConfigMap...
azure/contour                     0.2.0            v0.15.0        Contour Ingress controller for Kubernetes         
azure/external-dns                1.8.0            0.5.14         Configure external DNS servers (AWS Route53, Go...
azure/kong                        0.36.7           1.4            DEPRECATED The Cloud-Native Ingress and API-man...
azure/lamp                        1.1.3            7              Modular and transparent LAMP stack chart suppor...
azure/nginx-lego                  0.3.1                           Chart for nginx-ingress-controller and kube-lego  
azure/traefik                     1.87.2           1.7.24         A Traefik based Kubernetes ingress controller w...
azure/voyager                     3.2.4            6.0.0          DEPRECATED Voyager by AppsCode - Secure Ingress...
stable/external-dns               0.4.9            0.4.8          Configure external DNS servers (AWS Route53, Go...
stable/lamp                       0.1.4                           Modular and transparent LAMP stack chart suppor...
stable/nginx-lego                 0.3.1                           Chart for nginx-ingress-controller and kube-lego  
stable/traefik                    1.24.1           1.5.3          A Traefik based Kubernetes ingress controller w...
stable/voyager                    3.1.0            6.0.0-rc.0     Voyager by AppsCode - Secure Ingress Controller...

抉择 azure/nginx-ingress 来装置,留神是有版本的。装置如下:

$ helm install pkslow-ingress azure/nginx-ingress

装置胜利后,控制台会有输入相干的应用阐明。但要留神的是,要去 Kubernetes Dashboard 查看一下是否真的装置胜利。我装置遇到过失败,起因都是因为镜像下载失败。解决方案是关上全局 代理,先手动下载好相干镜像。如:

us.gcr.io/k8s-artifacts-prod/ingress-nginx/controller:v0.34.1
k8s.gcr.io/defaultbackend-amd64:1.5
jettech/kube-webhook-certgen:v1.0.0
quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.32.0

之后就装置胜利了,相干的 Pods 都跑起来了。相干的 Deployment 有:

3 应用 Ingress

3.1 拜访一个服务

一个最简略的例子如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.0
          ports:
            - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx-service
spec:
  ports:
    - port: 80
      name: nginx-service
      protocol: TCP
      targetPort: 80
  selector:
    app: nginx
  type: ClusterIP

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: nginx-service
              servicePort: 80
      host: localhost

这样配置后,当咱们拜访 http://localhost/ 时,就会把咱们的申请转发到 nginx-service80端口下来。如下所示:

3.2 拜访多个服务

当要拜访多个服务时,事件就变得复杂起来了。拜访多个服务,有两种配置形式,一种是通过 URL 门路匹配再转发,另一种是通过 子域名 转发。

3.2.1 子域名形式

通过子域名转发如下配置:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: nginx-service
              servicePort: 80
      host: nginx.localhost
    - http:
        paths:
          - path: /
            backend:
              serviceName: springboot-service
              servicePort: 8080
      host: springboot.localhost

为了节俭篇幅,这里就只展现 Ingress 的配置了。

拜访 http://nginx.localhost/ 如下:

拜访 http://springboot.localhost/swagger-ui.html 如下,留神这个 URL 带了子门路swagger-ui.html

3.2.2 URL 门路匹配形式

那通过 URL 门路匹配形式是不是这样配置呢?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - http:
        paths:
          - path: /nginx
            backend:
              serviceName: nginx-service
              servicePort: 80
      host: localhost
    - http:
        paths:
          - path: /springboot
            backend:
              serviceName: springboot-service
              servicePort: 8080
      host: localhost

这样配置后,会间接报 404,但不是Ingress404,而是 NginxSpringboot404。阐明申请曾经胜利转发到对应的service 了,但门路有问题。起因是,当这样配置时,Ingress会把 path 也转发到 service 上。所以实际效果如下:

localhost/nginx      --> nginx-service/nginx
localhost/springboot --> springboot-service/springboot

所以服务的 Web Context 门路要与配置的 path 匹配。比方 nginx-service 的根底门路就要改为 /nginx,而不能是/ 了。

如果就想放弃服务的 Web Context 门路是 /,那就须要配置rewrite 规定,如nginx.ingress.kubernetes.io/rewrite-target: /$1

4 总结

过了一遍,坑真不少。应用 子域名 感觉是比拟好的形式。另外,Ingress还有一个坑,它是实现 HTTP/HTTPS 转发的,但 TCP 就不行了,比方我在 Kubernetes 装置了一个 MySQL 数据库,须要把地址和 3306TCP形式裸露给里面,就比拟麻烦了,咱们后续再探讨吧。


欢送关注微信公众号 <南瓜慢说>,将继续为你更新 …

多读书,多分享;多写作,多整顿。

正文完
 0