nginx-ingress配置4/7层测试

这节我们测试下nginx-ingress的4/7层负载,这里的测试环境主要是barematal场景下使用的nodeport方式。
首先部署一个ingress,这里使用最新的0.21.0版本:

1
2
3
4
5
6
7
8
9
git clone https://github.com/kubernetes/ingress-nginx.git
git checkout nginx-0.20.1
cd ~/ingress-nginx/deploy
kubectl apply -f mandatory.yaml


# baremetal方式部署
# 这里可以修改yaml,指定nodePort,默认是动态生成
kubectl apply -f provider/baremetal/service-nodeport.yaml

注意:0.21.0和之前的版本有了变化,default-backend 不在单独一个pod。所以这里部署成功,默认就一个pod。

部署完成之后,访问测试:

1
2
3
4
5
6
7
8
➜  ~ curl 10.7.12.201:31075
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.6</center>
</body>
</html>

7层

我们创建两个一个nginx,另一个httpd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
vi my-nginx.yaml
粘贴以下内容:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 1
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
app: my-nginx
spec:
ports:
- port: 80
protocol: TCP
name: http
selector:
run: my-nginx
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-nginx
annotations:
kubernetes.io/ingress.class: "nginx"
# 禁止跳转https
nginx.ingress.kubernetes.io/ssl-redirect: "false"

spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: my-nginx
servicePort: 80

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

my-httpd.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-httpd
spec:
replicas: 1
template:
metadata:
labels:
run: my-httpd
spec:
containers:
- name: my-httpd
image: httpd
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-httpd
labels:
app: my-httpd
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
name: http
selector:
run: my-httpd
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-httpd
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/ssl-redirect: "false"

spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: my-httpd
servicePort: 80

创建:

1
kubectl apply -f .

查看状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

[root@k8s-m1 ingress-nginx]# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 5 5h1m
my-httpd-6b4494fddc-nztf9 1/1 Running 0 11m
my-nginx-756f645cd7-v5nvg 1/1 Running 0 8m24s
[root@k8s-m1 ingress-nginx]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21d
my-httpd NodePort 10.105.105.99 <none> 80:31300/TCP 11m
my-nginx NodePort 10.100.242.232 <none> 80:30750/TCP 8m26s

[root@k8s-m1 ingress-nginx]# kubectl get ing
NAME HOSTS ADDRESS PORTS AGE
my-httpd * 80 12m
my-nginx * 80 8m30s

这时配置解析,如果没有dns,直接写入hosts:

1
2
10.7.12.201	my-nginx
10.7.12.201 my-httpd

这时访问:

1
2
3
4
curl my-nginx:31075
curl my-httpd:31075
#
curl 10.7.12.201:30175

这时,会出现问题,当curl 10.7.12.201:30175时并不会返回404的页面,而是返回的第一个创建的 ingress 站点内容。

4层

创建资源,这里简单创建一个mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
apiVersion: v1
kind: Pod
metadata:
name: mysql
namespace: default
labels:
name: mysql
spec:
containers:
- name: mysql
image: docker.io/bitnami/mariadb:10.3.20-debian-9-r19
ports:
- containerPort: 3306
protocol: TCP
env:
- name: MARIADB_ROOT_PASSWORD
value: "123456"
---
apiVersion: v1
kind: Service
metadata:
name: svc-mysql
namespace: default
labels:
run: mysql
spec:
ports:
- port: 3306
targetPort: 3306
protocol: TCP
selector:
name: mysql

tcp-services-configmap 添加:

1
3306: "default/svc-mysql:3306"

Nginx Ingress Service 添加端口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
type: LoadBalancer
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
- name: https
port: 443
targetPort: 443
protocol: TCP
- name: proxie-tcp-mysql
port: 3306
targetPort: 3306
protocol: TCP
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx

这里我们就可以通过lb的3306来访问mysql:

1
root@node1:~/peng/l4# mysql -uroot -p123456 -h10.7.13.202 -P3306