Ingress Nginx k8s
Ingress Nginx k8s
With this step by step guide we are going through the creation of small python
Pods up to deploy our minimalistic cluster environment using NGINX as
kubernetes ingress controller. The steps below will illustrate steps by steps how
to deploy your local or remote cluster, create pods from the docker images and
deploy!
What is NGINX
What is an ingress controller
Deploy a local kubernetes cluster with some pods
Create your Images
Build your local image
[Optional] Build your images with Minikube
[Optional] Import the images
Create your Namespace
Deploy the Pods
Deploy the services
Install NGINX
Ingress YAML definition
Final YAML – NGINX ingress controller with 2 pods
What is NGINX
From the NGINX website (https://www.nginx.com/resources/glossary/nginx/)
NGINX is open source software for web serving, reverse proxying, caching, load
balancing, media streaming, and more. It started out as a web server designed for
maximum performance and stability. In addition to its HTTP server capabilities,
NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and
a reverse proxy and load balancer for HTTP, TCP, and UDP servers.
Ingress: Exposes HTTP and HTTPS routes from outside the cluster to services
within the cluster. Traffic routing is controlled by rules defined on the Ingress
resource.
Ingress Controllers: In order for an Ingress to work in your cluster, there must be
an ingress controller running. You need to select at least one ingress controller
and make sure it is set up in your cluster. This page lists common ingress
controllers that you can deploy.
Install k3s
Disable traefik (https://keepforyourself.com/kubernetes/disable-traefik-
from-k3s/)
or alternatively you can run through Docker desktop or minikube
(https://keepforyourself.com/docker/run-a-kubernetes-cluster-on-your-pc/)
Deploy some pods https://keepforyourself.com/docker/kubernetes-pod-
using-a-local-image/
Or you can deploy a python pod with FastAPI by creating a local image and
deploy to kubernetes as explained below step by step
Dockerfile
1 FROM python:3.9
2 WORKDIR /code
3 COPY ./requirements.txt /code/requirements.txt
4 RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
5 COPY ./app /code/app
6 CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
7 EXPOSE 8080
Create a requirements.txt
requirements.txt
1 fastapi
2 pydantic
3 uvicorn
pod-example/app/main.py
pod-example/app/main.py
app = FastAPI(
title="Example-pod",
description="keep-4-yourself-example-pod",
)
@app.get("/pod2")
def say_hello():
return {"hello": "world 2"}
so that we can have 2 different images (for testing purpose) and build using the
command
kind: Namespace
apiVersion: v1
metadata:
name: k4y
labels:
name: k4y
kind: Namespace
apiVersion: v1
metadata:
name: k4y
labels:
name: k4y
---
apiVersion: v1
kind: Pod
metadata:
name: pod1
namespace: k4y
labels:
app: pod1
spec:
containers:
- name: pod1
image: local-fastapi-example-pod-1
imagePullPolicy: Never
restartPolicy: Never
---
apiVersion: v1
kind: Pod
metadata:
name: pod2
namespace: k4y
labels:
app: pod2
spec:
containers:
- name: pod2
image: local-fastapi-example-pod-2
imagePullPolicy: Never
restartPolicy: Never
kubectl apply
service definition
kind: Service
apiVersion: v1
metadata:
name: <the name we want to give to our service>
namespace: <the namespace in which this service must be>
spec:
ports:
- port: <port we want to expose>
protocol: TCP
targetPort: <port of the pod we want to hit
selector:
app: <the pod name>
---
In our case then we will have 2 services, one for each pod, our yaml will look like
the one below
kind: Namespace
apiVersion: v1
metadata:
name: k4y
labels:
name: k4y
---
apiVersion: v1
kind: Pod
metadata:
name: pod1
namespace: k4y
spec:
containers:
- name: pod1
image: local-fastapi-example-pod-1
imagePullPolicy: Never
restartPolicy: Never
---
apiVersion: v1
kind: Pod
metadata:
name: pod2
namespace: k4y
spec:
containers:
- name: pod1
image: local-fastapi-example-pod-2
imagePullPolicy: Never
restartPolicy: Never
---
kind: Service
apiVersion: v1
metadata:
name: pod1-service
namespace: k4y
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: pod1
---
kind: Service
apiVersion: v1
metadata:
name: pod2-service
namespace: k4y
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: pod2
---
What is still missing is our ingress, so let’s go and install our NGINX ingress by
following the next step
Install NGINX
For installing NGINX I would strongly recommend to give it a read to this article
https://kubernetes.github.io/ingress-nginx/deploy/ it explains case by case what
is needed. However the most used command should be
If you are having issues around ip that is pending you can patch your service
(for learning purpose only) by using this command
each path has a match, a type and to which backend service should point to
the host is any host string we want to use, as long is being added as entry
in the /etc/hosts file (in my hosts file I have 192.168.1.56 local-dev-env)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: local-ingress
namespace: k4y
spec:
ingressClassName: nginx
rules:
- host: local-dev-env
http:
paths:
- path: /pod1
pathType: Prefix
backend:
service:
name: pod1-service
port:
number: 80
- path: /pod2
pathType: Prefix
backend:
service:
name: pod2-service
port:
number: 80
Now we have all the building blocks needed for our minimal deployment:
Namespace definition
Ingress definition
and our final yaml file will be like the one below
kind: Namespace
apiVersion: v1
metadata:
name: k4y
labels:
name: k4y
---
apiVersion: v1
kind: Pod
metadata:
name: pod1
namespace: k4y
labels:
app: pod1
spec:
containers:
- name: pod1
image: local-fastapi-example-pod-1
imagePullPolicy: Never
restartPolicy: Never
---
apiVersion: v1
kind: Pod
metadata:
name: pod2
namespace: k4y
labels:
app: pod2
spec:
containers:
- name: pod2
image: local-fastapi-example-pod-2
imagePullPolicy: Never
restartPolicy: Never
---
kind: Service
apiVersion: v1
metadata:
name: pod1-service
namespace: k4y
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: pod1
---
kind: Service
apiVersion: v1
metadata:
name: pod2-service
namespace: k4y
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: pod2
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: local-ingress
namespace: k4y
spec:
ingressClassName: nginx
rules:
- host: local-dev-env
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: pod1-service
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: pod2-service
port:
number: 80
this will create everything for us, and for verifying that is working we can just
curl
$ curl http://local-dev-env/pod1
{"hello":"world 1"}
$ curl http://local-dev-env/pod2
{"hello":"world 2"}
Please note that in this article we used Python as base technology, in this article
we are explaining how to use go/golang (https://keepforyourself.com
/kubernetes/create-a-golang-microservice)
I hope this is opening your mind on the infinite possibilities you have at this
stage! I also hope this helped you in some way and if so … share and let us
grow!
If you want to know more about NGINX you can have a look at this book
#deploy nginx ingress controller #deploy pods #deploy your local imagea as pod
#import local image into k3s #ingress controller #instal nginx #kubernetes
#local cluster #NGINX #NGINX ingress controller #use NGINX #use nginx as ingress
PREVIOUS NEXT
Similar Posts
Latest Articles
How to use PHP to interact with MySQL and Query the Database Tables
A Beginner’s Guide on how to use Python to interact with MySQL and Query the
Database Tables
NGINX NGINX ingress controller Phalcon Phalcon framework php php basics
php tutorial pod productivity Pydantic pytest Python python tutorial REST