Skip to content

Commit 600b2d4

Browse files
committed
[ingress] feat: add ingress document and images.
1 parent ad891d1 commit 600b2d4

2 files changed

Lines changed: 1426 additions & 45 deletions

File tree

README.md

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ kubectl exec -it nginx-pod /bin/bash
852852

853853
可以看到,我们多次 `curl 10.104.96.153:3000` 访问 `hellok8s` Service IP 地址,返回的 `hellok8s:v3` `hostname` 不一样,说明 Service 可以接收请求并将它们传递给它后面的所有 pod,还可以自动负载均衡。你也可以试试增加或者减少 `hellok8s:v3` pod 副本数量,观察 Service 的请求是否会动态变更。调用过程如下图所示:
854854

855-
![service-clusterip](/Users/guangzheng.li/Downloads/service-clusterip.png)
855+
![service-clusterip-fix](https://cdn.jsdelivr.net/gh/guangzhengli/PicURL@master/uPic/service-clusterip-fix.png)
856856

857857
除了上述的 `ClusterIp` 的方式外,Kubernetes `ServiceTypes` 允许指定你所需要的 Service 类型,默认是 `ClusterIP`。`Type` 的值包括如下:
858858

@@ -924,32 +924,47 @@ curl http://192.168.59.100:30000
924924

925925
## ingress
926926

927+
[Ingress](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#ingress-v1beta1-networking-k8s-io) 公开从集群外部到集群内[服务](https://kubernetes.io/docs/concepts/services-networking/service/)的 HTTP 和 HTTPS 路由。 流量路由由 Ingress 资源上定义的规则控制。Ingress 可为 Service 提供外部可访问的 URL、负载均衡流量、 SSL/TLS,以及基于名称的虚拟托管。你必须拥有一个 [Ingress 控制器](https://kubernetes.io/zh-cn/docs/concepts/services-networking/ingress-controllers) 才能满足 Ingress 的要求。 仅创建 Ingress 资源本身没有任何效果。 [Ingress 控制器](https://kubernetes.io/docs/concepts/services-networking/ingress-controllers) 通常负责通过负载均衡器来实现 Ingress,例如 `minikube` 默认使用的是 [nginx-ingress](https://minikube.sigs.k8s.io/docs/tutorials/nginx_tcp_udp_ingress/),目前 `minikube` 也支持 [Kong-Ingress](https://minikube.sigs.k8s.io/docs/handbook/addons/kong-ingress/)。
928+
929+
Ingress 可以“简单理解”为服务的网关 Gateway,它是所有流量的入口,经过配置的路由规则,将流量重定向到后端的服务。
930+
931+
在 `minikube` 中,可以通过下面命令开启 Ingress-Controller 的功能。默认使用的是 [nginx-ingress](https://minikube.sigs.k8s.io/docs/tutorials/nginx_tcp_udp_ingress/)。
932+
927933
```shell
928934
minikube addons enable ingress
935+
```
936+
937+
接着删除之前创建的所有 `pod`, `deployment`, `service` 资源。
929938

939+
``` shell
930940
kubectl delete deployment,service --all
931941
```
932942

943+
接着根据之前的教程,创建 `hellok8s:v3` 和 `nginx` 的`deployment`与 `service` 资源。Service 的 type 为 ClusterIP 即可。
944+
945+
`hellok8s:v3` 的端口映射为 `3000:3000`,`nginx` 的端口映射为 `4000:80`,这里后续写 Ingress Route 规则时会用到。
946+
933947
```yaml
934948
apiVersion: v1
935949
kind: Service
936950
metadata:
937-
name: hellok8s-svc
951+
name: service-hellok8s-clusterip
938952
spec:
953+
type: ClusterIP
939954
selector:
940955
app: hellok8s
941956
ports:
942-
- port: 4567
943-
targetPort: 4567
957+
- port: 3000
958+
targetPort: 3000
944959
945960
---
946961
947962
apiVersion: apps/v1
948963
kind: Deployment
949964
metadata:
950-
name: hellok8s
965+
name: hellok8s-deployment
951966
spec:
952-
replicas: 2
967+
replicas: 3
953968
selector:
954969
matchLabels:
955970
app: hellok8s
@@ -959,29 +974,29 @@ spec:
959974
app: hellok8s
960975
spec:
961976
containers:
962-
- image: guangzhengli/hellok8s:v3
963-
name: hellok8s-container
977+
- image: guangzhengli/hellok8s:v3
978+
name: hellok8s-container
964979
```
965980

966981
```yaml
967-
# nginx.yaml
968982
apiVersion: v1
969983
kind: Service
970984
metadata:
971-
name: nginx-svc
985+
name: service-nginx-clusterip
972986
spec:
987+
type: ClusterIP
973988
selector:
974989
app: nginx
975990
ports:
976-
- port: 1234
991+
- port: 4000
977992
targetPort: 80
978993
979994
---
980995
981996
apiVersion: apps/v1
982997
kind: Deployment
983998
metadata:
984-
name: nginx
999+
name: nginx-deployment
9851000
spec:
9861001
replicas: 2
9871002
selector:
@@ -998,27 +1013,32 @@ spec:
9981013
```
9991014

10001015
```shell
1001-
kubectl apply -f hellok8s.yaml
1002-
# service/hellok8s-svc created
1003-
# deployment.apps/hellok8s created
1016+
kubectl apply -f hellok8s.yaml
1017+
# service/service-hellok8s-clusterip created
1018+
# deployment.apps/hellok8s-deployment created
10041019
1005-
kubectl apply -f nginx.yaml
1006-
# service/nginx-svc created
1007-
# deployment.apps/nginx created
1020+
kubectl apply -f nginx.yaml
1021+
# service/service-nginx-clusterip created
1022+
# deployment.apps/nginx-deployment created
10081023
1009-
kubectl get pods
1010-
# NAME READY STATUS RESTARTS
1011-
# hellok8s-7f4c57d446-6c8b8 1/1 Running 0
1012-
# hellok8s-7f4c57d446-jkqbl 1/1 Running 0
1013-
# nginx-77c5c66899-dgkk2 1/1 Running 0
1014-
# nginx-77c5c66899-w9srw 1/1 Running 0
1024+
kubectl get pods
1025+
# NAME READY STATUS RESTARTS AGE
1026+
# hellok8s-deployment-5d5545b69c-4wvmf 1/1 Running 0 55s
1027+
# hellok8s-deployment-5d5545b69c-qcszp 1/1 Running 0 55s
1028+
# hellok8s-deployment-5d5545b69c-sn7mn 1/1 Running 0 55s
1029+
# nginx-deployment-d47fd7f66-d9r7x 1/1 Running 0 34s
1030+
# nginx-deployment-d47fd7f66-hp5nf 1/1 Running 0 34s
10151031
10161032
kubectl get service
1017-
# NAME TYPE CLUSTER-IP PORT(S)
1018-
# hellok8s-svc ClusterIP 10.102.242.233 4567/TCP
1019-
# nginx-svc ClusterIP 10.96.19.78 1234/TCP
1033+
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
1034+
# service-hellok8s-clusterip ClusterIP 10.97.88.18 <none> 3000/TCP 77s
1035+
# service-nginx-clusterip ClusterIP 10.103.161.247 <none> 4000/TCP 56s
10201036
```
10211037

1038+
这样在 k8s 集群中,就有 3 个 `hellok8s:v3` 的 pod,2 个 `nginx` 的 pod。并且`hellok8s:v3` 的端口映射为 `3000:3000`,`nginx` 的端口映射为 `4000:80`。在这个基础上,接下来编写 Ingress 资源的定义,`nginx.ingress.kubernetes.io/ssl-redirect: "false"` 的意思是这里关闭 `https` 连接,只使用 `http` 连接。
1039+
1040+
匹配前缀为 `/hello` 的路由规则,重定向到 `hellok8s:v3` 服务,匹配前缀为 `/` 的跟路径重定向到 `nginx`。
1041+
10221042
```yaml
10231043
apiVersion: networking.k8s.io/v1
10241044
kind: Ingress
@@ -1032,42 +1052,43 @@ spec:
10321052
rules:
10331053
- http:
10341054
paths:
1035-
- path: /
1055+
- path: /hello
10361056
pathType: Prefix
10371057
backend:
10381058
service:
1039-
name: nginx-svc
1059+
name: service-hellok8s-clusterip
10401060
port:
1041-
number: 1234
1042-
- path: /hello
1061+
number: 3000
1062+
- path: /
10431063
pathType: Prefix
10441064
backend:
10451065
service:
1046-
name: hellok8s-svc
1066+
name: service-nginx-clusterip
10471067
port:
1048-
number: 4567
1068+
number: 4000
1069+
10491070
```
10501071

10511072
```shell
10521073
kubectl apply -f ingress.yaml
10531074
# ingress.extensions/hello-ingress created
10541075

1055-
kubectl get ingress
1056-
# NAME HOSTS ADDRESS PORTS AGE
1057-
# hello-ingress * localhost 80 1m
1058-
```
1059-
1060-
```shell
1061-
kubectl apply -f ingress.yaml
1062-
# ingress.extensions/hello-ingress configured
1076+
kubectl get ingress
1077+
# NAME CLASS HOSTS ADDRESS PORTS AGE
1078+
# hello-ingress nginx * 80 16s
10631079

1064-
curl http://localhost/hello
1065-
# [v3] Hello, Kubernetes, from hellok8s-7f4c57d446-qth54!
1080+
# replace 192.168.59.100 by your minikube ip
1081+
curl http://192.168.59.100/hello
1082+
# [v3] Hello, Kubernetes!, From host: hellok8s-deployment-5d5545b69c-sn7mn
10661083

1067-
curl http://localhost
1068-
# (nginx welcome page)
1084+
curl http://192.168.59.100/
1085+
# (....Thank you for using nginx.....)
10691086
```
10701087

1088+
上面的教程中将所有流量都发送到 Ingress 的架构如下图所示:
1089+
1090+
![ingress](https://cdn.jsdelivr.net/gh/guangzhengli/PicURL@master/uPic/ingress.png)
1091+
10711092
## Configmap
10721093

10731094
### env var

0 commit comments

Comments
 (0)