之前曲折的铺路
https://blog.csdn.net/cds992/article/details/106170316
https://blog.csdn.net/cds992/article/details/106246616

环境说明

腾讯云环境, 新搭的环境。

[root@test service]# kubectl get svc -n ingress-nginx
NAME                                 TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             LoadBalancer   10.109.50.146    <pending>     80:30045/TCP,443:31111/TCP   5h40m
ingress-nginx-controller-admission   ClusterIP      10.108.173.252   <none>        443/TCP                      5h40m
mappsvc2                             ClusterIP      10.101.137.171   <none>        80/TCP                       59m
myappsvc                             ClusterIP      10.103.198.99    <none>        80/TCP                       92m

[root@test service]# kubectl describe ingress/ingressmyapp  -n ingress-nginx
Name:             ingressmyapp
Namespace:        ingress-nginx
Address:
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host            Path  Backends
  ----            ----  --------
  pphqq.vicp.net
                  /v1      myappsvc:80 (10.244.1.3:80)
                  /v2      mappsvc2:80 (10.244.1.4:80)
Annotations:      Events:  <none>

所有配置

[root@test service]# kubectl get all -n ingress-nginx
NAME                                            READY   STATUS      RESTARTS   AGE
pod/ingress-nginx-admission-create-g42ds        0/1     Completed   0          5h51m
pod/ingress-nginx-admission-patch-jgqkv         0/1     Completed   0          5h51m
pod/ingress-nginx-controller-866488c6d4-vqzk4   1/1     Running     0          5h51m
pod/myappv1                                     1/1     Running     0          7h3m
pod/myappv2                                     1/1     Running     0          7h3m

NAME                                         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
service/ingress-nginx-controller             LoadBalancer   10.109.50.146    <pending>     80:30045/TCP,443:31111/TCP   5h51m
service/ingress-nginx-controller-admission   ClusterIP      10.108.173.252   <none>        443/TCP                      5h51m
service/mappsvc2                             ClusterIP      10.101.137.171   <none>        80/TCP                       70m
service/myappsvc                             ClusterIP      10.103.198.99    <none>        80/TCP                       103m

NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/ingress-nginx-controller   1/1     1            1           5h51m

NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/ingress-nginx-controller-866488c6d4   1         1         1       5h51m

NAME                                       COMPLETIONS   DURATION   AGE
job.batch/ingress-nginx-admission-create   1/1           16m        5h51m
job.batch/ingress-nginx-admission-patch    1/1           16m        5h51m

继续配置

需要再重提一下, 配置的命令使用的是云环境的deploy.yaml (见https://blog.csdn.net/cds992/article/details/106170316 )
其中svc/ingress-nginx-controller 被设定成了LoadBalancer.

NAME                                         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
service/ingress-nginx-controller             LoadBalancer   10.109.50.146    <pending>     80:30045/TCP,443:31111/TCP   5h51m

笔者尝试用腾讯云的负载均衡引导至这个svc, 但是没有成功。如果有成功的小伙伴麻烦评论告诉我,谢谢

将service/ingress-nginx-controller改成NodePort模式

  1. 删除旧的svc ,改一下cloud/deploy.yaml 中service/ingress-nginx-controller的定义配置,另存后apply
[root@test ~]# kubectl get svc/ingress-nginx-controller -n ingress-nginx
NAME                       TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller   NodePort   10.110.65.164   <none>        80:30045/TCP,443:31111/TCP   12h
  1. 腾讯云安全组放行30045端口,将域名指向有公网的那台ip。 在公网开启链接, 发现未指定路径的于指定路径的链接所返回的nginx版本不一致。

这里有两个问题:

a. 无目录的链接返回 not found ,因为没有设定默认的backend, 先忽略,后续再整理
b. v1,v2 应该返回正常的结果
在这里插入图片描述

[root@test ~]#  kubectl  exec  -n ingress-nginx -it ingress-nginx-controller-866488c6d4-vqzk4  -- /bin/sh
/etc/nginx $ nginx -v
nginx version: nginx/1.17.10
[root@test ~]# kubectl get ingress -n ingress-nginx
NAME           CLASS    HOSTS            ADDRESS         PORTS   AGE
ingressmyapp   <none>   pphqq.vicp.net   10.110.65.164   80      5d
[root@test ~]# curl 10.110.65.164
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.10</center>
</body>
</html>
[root@test ~]# kubectl describe ingress/ingressmyapp -n ingress-nginx
Name:             ingressmyapp
Namespace:        ingress-nginx
Address:          10.110.65.164
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host            Path  Backends
  ----            ----  --------
  pphqq.vicp.net
                  /v1      myappsvc:80 (10.244.1.3:80)
                  /v2      mappsvc2:80 (10.244.1.4:80)
Annotations:      Events:  <none>
[root@test ~]# curl 10.244.1.3
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
############下面的链接可以看到请求其实已经发到了对应的pod 
[root@test ~]# curl 10.244.1.3/v1
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
链接nginx版本谁返回的
x.net:300451.17.0ingress-nginx-controller-866488c6d4-vqzk4
x.net:30045/v11.12.2myappsv1

** 所以问题b应该是ingress没有处理好入口的链接 **
https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md
官网描述“ Ingress 经常使用注解(annotations)来配置一些选项,具体取决于 Ingress 控制器,例如 rewrite-target annotation。”
所以需要再设定annotations
在ingress定义文件中新增 nginx.ingress.kubernetes.io/rewrite-target: / , 就是入口后跟上的子路径(/v1)会被直接改写为根路径

[root@test ingress]# kubectl describe ingress/ingressmyapp -n ingress-nginx
Name:             ingressmyapp
Namespace:        ingress-nginx
Address:          10.110.65.164
####没设定,所以打错误路径直接返回404 
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host            Path  Backends
  ----            ----  --------
  pphqq.vicp.net
                  /v1   myappsvc:80 (10.244.1.3:80)
                  /v2   mappsvc2:80 (10.244.1.4:80)
#####下面这行很重要,当然也要根据实际情况选择和设定#####
Annotations:      nginx.ingress.kubernetes.io/rewrite-target: /
Events:
  Type    Reason  Age    From                      Message
  ----    ------  ----   ----                      -------
  Normal  CREATE  7m6s   nginx-ingress-controller  Ingress ingress-nginx/ingressmyapp
  Normal  UPDATE  6m12s  nginx-ingress-controller  Ingress ingress-nginx/ingressmyapp

最后的结果:
在这里插入图片描述

至此,初体验结束。
ps: 发现自己好蠢,图片还划什么域名,也没划干净。而且配置里面也有,欸一股。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐