1、建立項目project:mysite和應用APP:polls(Writing your first Django app, part 1 | Django documentation | Django (djangoproject.com))
- django-admin自動生成一些代碼建立一個Django項目——一個Django執行個體的設定集合,包括資料庫配置、Django特定的選項和應用程式特定的設定。
- 在polls檔案夾下建立URLconf:
polls/urls.py,mysite/
- 下一步是指向根URLconf:
mysite/mysite/
,urls.py
PS E:\pyStudy> django-admin startproject mysite
\> py manage.py startapp polls
修改 polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
修改 mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
//include()函數引用其他urlconf,當Django遇到include()時,它會删除URL中與之比對的部分,并将剩下的字元串發送給所包含的URLconf進行進一步處理。 admin.site.urls is the only exception to this.
//The path() function is passed four arguments, two required: route and view, and two optional: kwargs, and name
2、監聽所有可用的公共ip
PS E:\pyStudy\mysite> py manage.py runserver 0.0.0.0:8000
Django version 4.1.3, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8000/
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '192.168.31.30:8000'. You may need to add '192.168.31.30' to ALLOWED_HOSTS.
修改settings.py後OK
ALLOWED_HOSTS = ['*']
3、APP遷移到容器。需是公共ip(0.0.0.0,127.0.0.1容器外不能通路)
基礎鏡像:silintl/django
# python3
Python 3.9.5 (default, Nov 18 2021, 16:00:48)
[GCC 10.3.0] on linux
# python3 -m django --version
4.0.3
# python3 manage.py migrate //apply the migrations for app(s): admin, auth, contenttypes, sessions.
# python3 manage.py createsuperuser //建立一個超級使用者帳戶(具有所有權限的使用者),當安裝了Django的身份驗證系統(django.contrib.auth)時,此指令才可用。
# python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
November 09, 2022 - 07:49:38
Django version 4.0.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
使用dockerfile建立鏡像:(以下基礎鏡像不需要安裝第三方庫。PS \> pip freeze > requirements.txt)
FROM silintl/django:4.0.3
MAINTAINER mizy<[email protected]>
LABEL maintainer="mizy<[email protected]>"
LABEL description="first Django app:https://docs.djangoproject.com/en/4.1/intro/tutorial01/ python3.9 manage.py runserver 0.0.0.0:8000"
# 安裝所需要的python包,将資源拉取連結修改為國内的鏡像源,添加 pip 清華鏡像源
# RUN pip3 install -r requirement.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
WORKDIR /home/mysite
ENV RUN_PORT=8000
EXPOSE 8000
ADD ./mysite /home/mysite
#makemigrations記錄對models.py的所有改動,将這個改動遷移到migrations這個檔案下生成一個檔案。例如:0001檔案,如果還要改動可能生成就是另外一個檔案不一定都是0001檔案,但是這個指令并沒有作用到資料庫
#而migrate指令作用是把這些改動作用到資料庫也就是執行migrations裡面新改動的遷移檔案更新資料庫,比如建立資料表,或者增加字段屬性
#RUN python3.9 manage.py makemigrations && python3.9 manage.py migrate
RUN python3.9 manage.py migrate
CMD python3.9 manage.py runserver 0.0.0.0:8000
PS E:\pyStudy> docker build -t midjango:v1.1 .
4、Windows10上push到私有倉庫失敗,修改Docker的配置檔案C:\Users\admin\.docker\daemon.json
PS E:\pyStudy\mysite> docker push core.harbor.domain/library/midjango:v1.2
The push refers to repository [core.harbor.domain/library/midjango]
Get "https://core.harbor.domain/v2/": x509: certificate signed by unknown authority
在daemon.json增加:
"insecure-registries": [
"core.harbor.domain"
]
5、k8s部署,健康檢查不能使用httpGet(往容器的IP:Port發送HTTP GET請求Get "http://172.17.54.145:80/healthCheck",如果Probe收到2xx或3xx,說明已經就緒。)
readinessProbe: # Pod 準備服務健康檢查設定
# httpGet:
# path: /healthCheck
# port: 80
# scheme: HTTP
tcpSocket: # 通過tcpSocket檢查健康
port: 8000
不支援httpGet
Warning Unhealthy 7s (x2 over 17s) kubelet Readiness probe failed: Get "http://172.17.54.145:80/healthCheck": dial tcp 172.17.54.145:80: connect: connection refused
6、域名dj.atc.com解析到ingress。
C:\Windows\System32\drivers\etc\hosts添加:192.168.31.218 ingress.test.com dj.atc.com
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
name: ingress-test
namespace: study
spec:
ingressClassName: nginx
rules:
- host: dj.atc.com
http:
paths:
- backend:
service:
name: django
port:
number: 8000
path: /
pathType: ImplementationSpecific
status:
loadBalancer:
ingress:
- ip: 10.16.205.49