forked from kloudvsol/K8s_class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s-ingress-example
124 lines (92 loc) · 2.48 KB
/
k8s-ingress-example
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
How to Use Nginx Ingress Controller
Creating a Kubernetes Ingress
First, let’s create two services to demonstrate how the Ingress routes our request. We’ll run two web applications that output a slightly different response.
vi apple.yaml
kind: Pod
apiVersion: v1
metadata:
name: apple-app
labels:
app: apple
spec:
containers:
- name: apple-app
image: hashicorp/http-echo
args:
- "-text=apple"
---
kind: Service
apiVersion: v1
metadata:
name: apple-service
spec:
selector:
app: apple
ports:
- port: 5678 # Default port for image
vi banana.yaml
kind: Pod
apiVersion: v1
metadata:
name: banana-app
labels:
app: banana
spec:
containers:
- name: banana-app
image: hashicorp/http-echo
args:
- "-text=banana"
---
kind: Service
apiVersion: v1
metadata:
name: banana-service
spec:
selector:
app: banana
ports:
- port: 5678 # Default port for image
### Create the resources
kubectl apply -f apple.yaml
kubectl apply -f banana.yaml
## Now, declare an Ingress to route requests to /apple to the first service, and requests to /banana to second service.
## Check out the Ingress’ rules field that declares how requests are passed along.
vi ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /apple
pathType: Prefix
backend:
service:
name: apple-service
port:
number: 5678
- path: /banana
pathType: Prefix
backend:
service:
name: banana-service
port:
number: 5678
## Create the Ingress in the cluster
kubectl create -f ingress.yaml
[root@master-node kuberntes-nginx-controller]# k get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx LoadBalancer 10.102.93.210 <pending> 80:32435/TCP,443:32454/TCP 64m
[root@master-node kuberntes-nginx-controller]# vi /etc/hosts
[root@master-node kuberntes-nginx-controller]# curl -kL http://example.com/apple
apple
[root@master-node kuberntes-nginx-controller]# curl -kL http://example.com/banana
banana
[root@master-node kuberntes-nginx-controller]# curl -k example.com:32435/apple
apple
[root@master-node kuberntes-nginx-controller]# curl -k example.com:32435/banana
banana