Lesson 12.4: Ingress
[root@master ingress]# ls
Flask
[root@master ingress]# cd Flask/
[root@master Flask]# docker build -t hello-world .
[+] Building 53.5s (11/11) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 546B 0.0s
=> [internal] load metadata for docker.io/library/python:3.7-slim 5.2s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [1/5] FROM docker.io/library/python:3.7-slim@sha256:b53f496ca43e5af6994f8e316cf03af31050bf7944e0e4a308ad 7.8s
=> => resolve docker.io/library/python:3.7-slim@sha256:b53f496ca43e5af6994f8e316cf03af31050bf7944e0e4a308ad 0.0s
=> => sha256:e886f0f47ef56fcadb6ecaf2116056bbdb273e0afe07ed034498b198d386c04e 29.16MB / 29.16MB 4.8s
=> => sha256:bc3b0687866d8779be6da27119f80a03882efc62ba3540c29925586d24e26a2e 3.32MB / 3.32MB 1.8s
=> => sha256:f4e74734688847a18b13767a3a003547ed73c00a939759b369db4e08ef328ed9 11.32MB / 11.32MB 3.6s
=> => sha256:b53f496ca43e5af6994f8e316cf03af31050bf7944e0e4a308ad86c001cf028b 1.86kB / 1.86kB 0.0s
=> => sha256:5035afd8de57d811fdd95d6a7b02c70a1a2876c553e6db72c70f0334895d8d30 1.37kB / 1.37kB 0.0s
=> => sha256:5f0f64c76e3a4930f573a293cebeb872b26220e4b20cbec2705a03609fd9e074 7.55kB / 7.55kB 0.0s
=> => sha256:755c9c80f96367902e207fb8ea419a97d56a02867e5c5798da6258c1b2370b98 244B / 244B 2.5s
=> => sha256:e468929dc1a9cb521e4f19e37d59ec75f21538a41b2650d74e2163e877578f84 3.13MB / 3.13MB 3.9s
=> => extracting sha256:e886f0f47ef56fcadb6ecaf2116056bbdb273e0afe07ed034498b198d386c04e 1.6s
=> => extracting sha256:bc3b0687866d8779be6da27119f80a03882efc62ba3540c29925586d24e26a2e 0.2s
=> => extracting sha256:f4e74734688847a18b13767a3a003547ed73c00a939759b369db4e08ef328ed9 0.4s
=> => extracting sha256:755c9c80f96367902e207fb8ea419a97d56a02867e5c5798da6258c1b2370b98 0.0s
=> => extracting sha256:e468929dc1a9cb521e4f19e37d59ec75f21538a41b2650d74e2163e877578f84 0.2s
=> [internal] load build context 0.1s
=> => transferring context: 2.37kB 0.0s
=> [2/5] WORKDIR /app 0.0s
=> [3/5] COPY requirements.txt . 0.1s
=> [4/5] RUN pip install -r requirements.txt 6.6s
=> [5/5] COPY . . 0.1s
=> exporting to image 32.8s
=> => exporting layers 32.8s
=> => writing image sha256:7a1eb148612c303e969abf8155f5a09d8a67333c0502c53e3d61e8656a31a1af 0.0s
=> => naming to docker.io/library/hello-world 0.0s
[root@master Flask]# docker tag hello-world sanjeebkc/hello-world
[root@master Flask]# docker push sanjeebkc/hello-world
Using default tag: latest
The push refers to repository [docker.io/sanjeebkc/hello-world]
20b58a7de70d: Pushed
f66863bfdaa7: Pushed
6b4826e68cc5: Pushed
bb78ad60e819: Pushed
3e6dd70b6f1e: Mounted from library/python
22754cfc2ebe: Mounted from library/python
ad2caf80614d: Mounted from library/python
c4d9cc0a5063: Mounted from library/python
311627f8702d: Mounted from library/python
latest: digest: sha256:85ddd6079b10ddc8518d1812807103a06235fde730fc1a90286daff45b42f04a size: 2202
[root@master ingress]# vim deployment.yml
[root@master ingress]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
labels:
app: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: sanjeebkc/hello-world:latest
ports:
- containerPort: 80
[root@master ingress]# vim service.yml
[root@master ingress]# cat service.yml
apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
selector:
app: hello-world
ports:
- protocol: TCP
port: 80
targetPort: 80
[root@master ingress]# kubectl apply -f deployment.yml
deployment.apps/hello-world created
[root@master ingress]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
hello-world 1/1 1 1 101s
[root@master ingress]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-7d8fdfbddc-d9zn9 1/1 Running 0 113s
[root@master ingress]# kubectl apply -f service.yml
service/hello-world created
[root@master ingress]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world ClusterIP 10.96.49.80 <none> 80/TCP 5s
[root@master ingress]# kubectl exec -it hello-world-7d8fdfbddc-d9zn9 -- sh
# curl 10.96.49.80
Hello, World!# exit
[root@master ingress]# kubectl apply -f https://kind.sigs.k8s.io/examples/ingress/deploy-ingress-nginx.yaml
namespace/ingress-nginx created
serviceaccount/ingress-nginx created
serviceaccount/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
configmap/ingress-nginx-controller created
service/ingress-nginx-controller created
service/ingress-nginx-controller-admission created
deployment.apps/ingress-nginx-controller created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
[root@master ingress]# cat ingress.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hello-world
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: "example.com"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: hello-world
port:
number: 80
We are using ingressClassName as nginx because
[root@master ingress]# kubectl get pods -A | grep nginx
ingress-nginx ingress-nginx-admission-create-8fsrg 0/1 Completed 0 84s
ingress-nginx ingress-nginx-admission-patch-x2s7d 0/1 Completed 0 84s
ingress-nginx ingress-nginx-controller-696d4c4c5-srbwg 1/1 Running 0 84s
# - --ingress-class=nginx is present in the pod ingress-controller yaml
[root@master ingress]# kubectl edit pod ingress-nginx-controller-696d4c4c5-srbwg -n ingress-nginx
Type Loadbalancer
[root@master ingress]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller LoadBalancer 10.96.145.13 <pending> 80:30641/TCP,443:31995/TCP 9m29s
ingress-nginx-controller-admission ClusterIP 10.96.151.202 <none> 443/TCP 9m29s
[root@master ingress]#
[root@master ingress]# curl 10.96.145.13
^C
[root@master ingress]# cat ingress.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hello-world
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: "example.com"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: hello-world
port:
number: 80
Type NodePort
# Change the type to nodeport
[root@master ingress]# kubectl edit svc ingress-nginx-controller -n ingress-nginx
service/ingress-nginx-controller edited
[root@master ingress]# kubectl delete ing hello-world
ingress.networking.k8s.io "hello-world" deleted
[root@master ingress]# kubectl apply -f ingress.yml
ingress.networking.k8s.io/hello-world created
[root@master ingress]# kubectl get ing
NAME CLASS HOSTS ADDRESS PORTS AGE
hello-world nginx example.com localhost 80 61s
[root@master ingress]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.18.0.2 example.com
[root@master ingress]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.96.145.13 <none> 80:30641/TCP,443:31995/TCP 27m
ingress-nginx-controller-admission ClusterIP 10.96.151.202 <none> 443/TCP 27m
[root@master ingress]#
[root@master ingress]# curl example.com:30641
Hello, World![root@master ingress]#