2 nginx kubernetes kubernetes-ingress
我正在版本 上运行 3 节点裸机集群1.9.5。
3个节点的IP分别是:
[root@node1 new]# kubectl get nodes
NAME STATUS ROLES AGE VERSION IP
node1 Ready master,node 1d v1.9.5 172.16.16.1
node2 Ready node 1d v1.9.5 172.16.16.2
node3 Ready node 1d v1.9.5 172.16.16.3
Run Code Online (Sandbox Code Playgroud)
我在下面解释的所有内容都是在 1 个命名空间中完成的,即ingress-nginx
我部署了 2 个应用程序。
[root@node1 new]# kubectl get po -n ingress-nginx
NAME READY STATUS RESTARTS AGE
app1-5d4d466cc7-595lc 1/1 Running 0 25m
app2-55cf97d86d-9v8gl 1/1 Running 0 25m
Run Code Online (Sandbox Code Playgroud)
他们的服务是:
[root@node1 new]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
appsvc1 NodePort 10.233.60.145 <none> 80:32601/TCP 25m
appsvc2 NodePort 10.233.46.230 <none> 80:30616/TCP 25m
Run Code Online (Sandbox Code Playgroud)
因此,当我通过访问它们时NodePort,我得到了我想要的结果。
curl http://172.16.16.2:32601
<h1>Hello app1!</h1>
curl http://172.16.16.2:30616
<h1>Hello app2!</h1>
Run Code Online (Sandbox Code Playgroud)
现在我的目标是使用 nginx 入口控制器配置基于路径的路由,以便最后我可以使用路由到 app1
curl http://172.16.16.2/app1
<h1>Hello app1!</h1>
&
curl http://172.16.16.2/app2
<h1>Hello app2!</h1>
Run Code Online (Sandbox Code Playgroud)
所以现在我已经使用ingress-nginx设置了入口控制器。
nginx 控制器也部署在应用程序的同一命名空间中,即ingress-nginx
我的入口控制器已成功部署,如日志所示:
[root@node1 new]# kubectl logs nginx-ingress-controller-9c7b694-bjn6h -n ingress-nginx
-------------------------------------------------------------------------------
NGINX Ingress controller
Release: 0.12.0
Build: git-1df421a
Repository: https://github.com/kubernetes/ingress-nginx
-------------------------------------------------------------------------------
I0415 16:08:18.736790 5 main.go:225] Running in Kubernetes Cluster version v1.9 (v1.9.5) - git (clean) commit f01a2bf98249a4db383560443a59bed0c13575df - platform linux/amd64
I0415 16:08:18.743855 5 main.go:84] validated ingress-nginx/default-http-backend as the default backend
I0415 16:08:19.182913 5 stat_collector.go:77] starting new nginx stats collector for Ingress controller running in namespace (class nginx)
I0415 16:08:19.182944 5 stat_collector.go:78] collector extracting information from port 18080
I0415 16:08:19.211749 5 nginx.go:281] starting Ingress controller
I0415 16:08:20.325839 5 event.go:218] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"app-ingress", UID:"918405ac-410e-11e8-a473-080027917402", APIVersion:"extensions", ResourceVersion:"75539", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress default/app-ingress
I0415 16:08:20.326503 5 event.go:218] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"ingress-nginx", Name:"nginx-ingress", UID:"8dc22500-410e-11e8-a473-080027917402", APIVersion:"extensions", ResourceVersion:"75538", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress ingress-nginx/nginx-ingress
I0415 16:08:20.413514 5 store.go:614] running initial sync of secrets
I0415 16:08:20.413591 5 nginx.go:302] starting NGINX process...
I0415 16:08:20.418356 5 leaderelection.go:174] attempting to acquire leader lease...
W0415 16:08:20.422596 5 controller.go:777] service ingress-nginx/nginx-ingress does not have any active endpoints
I0415 16:08:20.422686 5 controller.go:183] backend reload required
I0415 16:08:20.422694 5 stat_collector.go:34] changing prometheus collector from to default
I0415 16:08:20.439620 5 status.go:196] new leader elected: nginx-ingress-controller-9c7b694-h2n4b
I0415 16:08:20.534277 5 controller.go:192] ingress backend successfully reloaded...
W0415 16:08:28.768140 5 controller.go:777] service ingress-nginx/nginx-ingress does not have any active endpoints
I0415 16:09:00.478068 5 leaderelection.go:184] successfully acquired lease ingress-nginx/ingress-controller-leader-nginx
I0415 16:09:00.478207 5 status.go:196] new leader elected: nginx-ingress-controller-9c7b694-bjn6h
Run Code Online (Sandbox Code Playgroud)
然后我使用以下命令配置了我的入口:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
name: app-ingress
spec:
rules:
- host: my-test.com
http:
paths:
- backend:
serviceName: appsvc1
servicePort: 80
path: /app1
- backend:
serviceName: appsvc2
servicePort: 80
path: /app2
Run Code Online (Sandbox Code Playgroud)
我使用以下方法创建了这个:
kubectl create -f app-ingress.yaml -n ingress-nginx
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法公开它:
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress
spec:
type: NodePort
externalIps:
- 172.16.16.1
- 172.16.16.2
- 172.16.16.3
ports:
- port: 80
nodePort: 30000
name: http
selector:
app: nginx-ingress
Run Code Online (Sandbox Code Playgroud)
app: nginx-ingress指向 ingress-controller pod 中我的标签中的标签。
我使用以下方式部署它:
kubectl create -f nginx-ingress-controller-service.yaml -n=ingress
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用 URL 访问应用程序时,我得到:
curl http://172.16.16.2/app1
default backend - 404
&
curl http://172.16.16.2/app2
default backend - 404
Run Code Online (Sandbox Code Playgroud)
甚至做
curl http://my-test.com/app1
default backend - 404
&
curl http://my-test.com/app2
default backend - 404
Run Code Online (Sandbox Code Playgroud)
不起作用。
我的 /etc/hosts 文件是:
172.16.16.1 my-test.com
Run Code Online (Sandbox Code Playgroud)
我是否错过了什么或做错了什么?
[root@node1 new]# kubectl get all -n ingress-nginx
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/app1 2 2 2 2 48m
deploy/app2 2 2 2 2 48m
deploy/default-http-backend 1 1 1 1 3h
deploy/nginx-ingress-controller 1 1 1 1 3h
NAME DESIRED CURRENT READY AGE
rs/app1-5d4d466cc7 2 2 2 48m
rs/app2-55cf97d86d 2 2 2 48m
rs/default-http-backend-55c6c69b88 1 1 1 3h
rs/nginx-ingress-controller-9c7b694 1 1 1 3h
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/app1 2 2 2 2 48m
deploy/app2 2 2 2 2 48m
deploy/default-http-backend 1 1 1 1 3h
deploy/nginx-ingress-controller 1 1 1 1 3h
NAME DESIRED CURRENT READY AGE
rs/app1-5d4d466cc7 2 2 2 48m
rs/app2-55cf97d86d 2 2 2 48m
rs/default-http-backend-55c6c69b88 1 1 1 3h
rs/nginx-ingress-controller-9c7b694 1 1 1 3h
NAME READY STATUS RESTARTS AGE
po/app1-5d4d466cc7-595lc 1/1 Running 0 48m
po/app1-5d4d466cc7-5dn72 1/1 Running 0 48m
po/app2-55cf97d86d-9v8gl 1/1 Running 0 48m
po/app2-55cf97d86d-lckpn 1/1 Running 0 48m
po/default-http-backend-55c6c69b88-8shkt 1/1 Running 0 3h
po/nginx-ingress-controller-9c7b694-bjn6h 1/1 Running 0 52m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/appsvc1 NodePort 10.233.60.145 <none> 80:32601/TCP 48m
svc/appsvc2 NodePort 10.233.46.230 <none> 80:30616/TCP 48m
svc/default-http-backend ClusterIP 10.233.5.30 <none> 80/TCP 3h
svc/ingress-nginx NodePort 10.233.6.186 <none> 80:31301/TCP,443:32103/TCP 3h
svc/nginx-ingress NodePort 10.233.9.163 172.16.16.1,172.16.16.2,172.16.16.3 80:30000/TCP 2h
Run Code Online (Sandbox Code Playgroud)
我什至更改了入口配置以删除主机并对所有主机开放:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
name: app-ingress
spec:
rules:
- http:
paths:
- path: /app1
backend:
serviceName: appsvc1
servicePort: 80
- path: /app2
backend:
serviceName: appsvc2
servicePort: 80
Run Code Online (Sandbox Code Playgroud)
现在我被重定向了。
[root@node1 ~]# curl http://172.16.16.1/app1
<html>
<head><title>308 Permanent Redirect</title></head>
<body bgcolor="white">
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>nginx/1.13.9</center>
</body>
</html>
[root@node1 ~]# curl http://172.16.16.1/app2
<html>
<head><title>308 Permanent Redirect</title></head>
<body bgcolor="white">
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>nginx/1.13.9</center>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
入口控制器日志显示:
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:35:52 +0000] "GET /app1 HTTP/1.1" 308 187 "-" "curl/7.29.0" 80 0.000 [ingress-nginx-appsvc1-80] - - - -
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:36:09 +0000] "GET /app1 HTTP/1.1" 308 187 "-" "curl/7.29.0" 79 0.000 [ingress-nginx-appsvc1-80] - - - -
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:36:11 +0000] "GET /app2 HTTP/1.1" 308 187 "-" "curl/7.29.0" 79 0.000 [ingress-nginx-appsvc2-80] - - - -
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:36:25 +0000] "GET /app2 HTTP/1.1" 308 187 "-" "curl/7.29.0" 85 0.000 [ingress-nginx-appsvc2-80] - - - -
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:36:51 +0000] "GET /app2 HTTP/1.1" 308 187 "-" "curl/7.29.0" 80 0.000 [ingress-nginx-appsvc2-80] - - - -
Run Code Online (Sandbox Code Playgroud)
除了与注释相关的一件事外,您的所有配置都很好:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
Run Code Online (Sandbox Code Playgroud)
您不需要此注释,因为它的处理早于路由,并且您始终有对/. 您没有该路径的路由,因此只需删除此注释,它就应该可以工作。
| 归档时间: |
|
| 查看次数: |
7586 次 |
| 最近记录: |