10

k8s入门之Ingress(七) - 景少

 2 years ago
source link: https://www.cnblogs.com/chuandao/p/16180723.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

k8s入门之Ingress(七)

Ingress 的功能其实很容易理解:所谓 Ingress,就是 Service 的“Service”,代理不同后端 Service 而设置的负载均衡服务。

一、安装ingress

1.查找ingress安装文件

从github上查看ingress对应k8s版本的deploy文件,链接地址是:

https://github.com/kubernetes/ingress-nginx/tree/main/deploy/static/provider/baremetal

2.点击Raw按钮可得到该文件的url链接

https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/baremetal/1.21/deploy.yaml

3.在master节点上下载该文件

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/baremetal/1.21/deploy.yaml

4.更改文件名

mv deploy.yaml ingress-nginx.yaml

5.替换镜像

由于国内访问不了谷歌,需要更改ingress-nginx.yaml文件中的镜像,从docker hub上搜索相关镜像进行替换

k8s.gcr.io/ingress-nginx/controller:v1.1.2@sha256:28b11ce69e57843de44e3db6413e98d09de0f6688e33d4bd384002a44f7
8405c 替换成 liangjw/ingress-nginx-controller:v1.1.2

k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a
91b20cc67868660 替换成 liangjw/kube-webhook-certgen:v1.1.1

6.创建nginx ingress

kubectl apply -f ingress-nginx.yaml

7.查看资源

kubectl get pod,svc -n ingress-nginx

8.在集群外通过集群内任意一台节点的IP+Port就可以访问

由于还没有配置被代理的服务,所以返回404

二、配置

1.参考前面的章节部署nginx的deploy与service

服务名为:svc-nginx

2.配置ingress

创建ingress-svc.yaml文件

vi ingress-svc.yaml

编写文件内容

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-demo
  # 名称空间不要忘了
  namespace: dev
spec:
  ingressClassName: nginx
  rules:
  - host: "demo.k8s.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            # 代理的服务名称
            name: svc-nginx
            port:
              # 服务端口
              number: 8000

3.创建ingress对象

kubectl apply -f ingress-svc.yaml

4.查看ingress对象的信息

5.配置域名访问

在windows宿主机上找到hosts文件:

C:\Windows\System32\drivers\etc\hosts,

添加一条映射记录(从三台节点中任选一台的IP进行映射):

192.168.59.112 demo.k8s.com

然后通过该域名加上ingress controller服务自己暴露的端口就可以访问了,千万要注意不是你代理的服务的端口。如下图所示获取ingress服务的端口

在浏览器就可以访问被ingress代理的k8s服务了

三、路径重写

1.更新yaml文件

vi ingress-svc.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    # 加上此重写配置
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: ingress-demo
  namespace: dev
spec:
  ingressClassName: nginx
  rules:
  - host: "demo.k8s.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: svc-nginx
            port:
              number: 8000
  - host: "demo2.k8s.com"
    http:
      paths:
      - pathType: Prefix
        path: "/nginx(/|$)(.*)"
        backend:
          service:
            name: svc-nginx
            port:
              number: 8000

2.更新ingress对象

kubectl apply -f ingress-svc.yaml

3.查看ingress对象

4.配置域名

在windows宿主机上找到hosts文件:

C:\Windows\System32\drivers\etc\hosts,添加一条映射记录:

192.168.59.112 demo2.k8s.com

在浏览器中访问

四、限流配置

1.更新yaml文件

vi ingress-svc.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    # 加上此重写配置
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    # 限流
    nginx.ingress.kubernetes.io/limit-rps: "1"
  name: ingress-demo
  namespace: dev
spec:
  ingressClassName: nginx
  rules:
  - host: "demo.k8s.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: svc-nginx
            port:
              number: 8000
  - host: "demo2.k8s.com"
    http:
      paths:
      - pathType: Prefix
        path: "/nginx(/|$)(.*)"
        backend:
          service:
            name: svc-nginx
            port:
              number: 8000

2.更新ingress对象

kubectl apply -f ingress-svc.yaml

3.测试

快速刷新访问会报异常,你可以自定义错误页面

五、总结

目前,Ingress 只能工作在七层,而 Service 只能工作在四层。所以当你想要在 Kubernetes 里为应用进行 TLS 配置等 HTTP 相关的操作时,都必须通过 Ingress 来进行。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK