Working on putting a Django API into Kubernetes.
Any traffic being sent to /
the ingress-nginx
controller sends to the React FE. Any traffic being sent to /api
is sent to the Django BE.
This is the relevant part of ingress-serivce.yaml
:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- path: /?(.*)
backend:
serviceName: client-cluster-ip-service
servicePort: 3000
- path: /api/?(.*)
backend:
serviceName: server-cluster-ip-service
servicePort: 5000
This is the url.py
:
from django.contrib import admin from django.urls import include, path
urlpatterns = [
path('auth/', include('authentication.urls'), name='auth'),
path('admin/', admin.site.urls, name='admin'),
]
The client
portion works just fine. The minikube
IP is 192.168.99.105
. Navigating to that IP loads the react front-end.
Navigating to 192.168.99.105/api/auth/test/
brings me to a `"Hello World!" response I quickly put together.
However, when I try to go to 192.168.99.105/api/admin
. It automatically redirects me to /admin/login/?next=/admin/
which doesn't exist given /api
is being removed. Is there anyway to prevent this behavior?
I've also just tried this:
ingress-service.yaml
- http:
paths:
- path: /?(.*)
backend:
serviceName: client-cluster-ip-service
servicePort: 3000
- path: /api/?(.*)
backend:
serviceName: server-cluster-ip-service
servicePort: 5000
- path: /admin/?(.*)
backend:
serviceName: server-cluster-ip-service
servicePort: 5000
urls.py
urlpatterns = [
path('auth/', include('authentication.urls'), name='auth'),
path('/', admin.site.urls),
]
Which just produces "Not Found".
I tried to prefix also using this pattern that shows up in the documentation:
urlpatterns = [
path('api/', include([
path('auth/', include('authentication.urls'), name='auth'),
path('admin/', admin.site.urls),
])),
]
But that just made it /api/api
.
Here are the routes that are defined for admin/
in the site-packages/django/contrib/admin/sites
:
# Admin-site-wide views.
urlpatterns = [
path('', wrap(self.index), name='index'),
path('login/', self.login, name='login'),
path('logout/', wrap(self.logout), name='logout'),
path('password_change/', wrap(self.password_change, cacheable=True), name='password_change'),
path(
'password_change/done/',
wrap(self.password_change_done, cacheable=True),
name='password_change_done',
),
path('jsi18n/', wrap(self.i18n_javascript, cacheable=True), name='jsi18n'),
path(
'r/<int:content_type_id>/<path:object_id>/',
wrap(contenttype_views.shortcut),
name='view_on_site',
),
]
I guess the ''
is what is causing Django to strip the /api
off the URL and make it just 192.168.99.105/admin
instead of 192.168.99.105/api/admin
.