k8s1.28.0集群发布一个django4.2.8应用
k8s集群发布自定义的django应用并实现外网访问
·
摘要
1、使用ubuntu:20.04创建一个基础的django服务
2、将第一步的容器打包发布到node节点
3、创建deployment和service的yaml配置
创建一个django的服务基于docker
(这里是在kmaster1上执行的,有docker本地hub的可以在将镜像放进去)
## 拉取基础镜像
root@kmaster1:~/application/python# docker pull ubuntu:20.04
## 开启容器并进入交互端
root@kmaster1:~/application/python# docker run -it ubuntu:20.04
## 安装pip3和django4.2.8
root@a2bc0121d174:/# apt update
root@a2bc0121d174:/# apt install python3-pip vim -y
root@a2bc0121d174:/# pip3 install django==4.2.8
## 创建项目目录以及初始化django服务
root@a2bc0121d174:/# mkdir /app
root@a2bc0121d174:/# cd /app
root@a2bc0121d174:/app/# django-admin startproject MyFirstProject
root@a2bc0121d174:/app/# cd MyFirstProject/
root@a2bc0121d174:/app/MyFirstProject# ls
MyFirstProject manage.py
root@a2bc0121d174:/app/MyFirstProject# python3 manage.py startapp myapp
root@a2bc0121d174:/app/MyFirstProject# cd MyFirstProject
root@a2bc0121d174:/app/MyFirstProject/MyFirstProject# ls
__init__.py __pycache__ asgi.py settings.py urls.py wsgi.py
## 修改django项目配置文件
root@a2bc0121d174:/app/MyFirstProject/MyFirstProject# vim settings.py
...
ALLOWED_HOSTS = ['*'] #修改*表示允许所有主机访问
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', #增加自己建的应用名称
]
...
LANGUAGE_CODE = 'zh-hans' #设置中文
TIME_ZONE = 'Asia/Shanghai' #设置时区为亚洲上海
## 安装时间文件纠正时区和时间
root@a2bc0121d174:/app/MyFirstProject/MyFirstProject# apt install tzdata -y
root@a2bc0121d174:/app/MyFirstProject/MyFirstProject# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa 7) Europe
2) Americas 8) Indian Ocean
3) Antarctica 9) Pacific Ocean
4) Asia 10) coord - I want to use geographical coordinates.
5) Atlantic Ocean 11) TZ - I want to specify the timezone using the Posix TZ format.
6) Australia
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan 11) Christmas Island 21) Iraq 31) Lebanon 41) Qatar 51) Thailand
2) Antarctica 12) Cocos (Keeling) Islands 22) Israel 32) Macau 42) R?union 52) Turkmenistan
3) Armenia 13) Cyprus 23) Japan 33) Malaysia 43) Russia 53) United Arab Emirates
4) Azerbaijan 14) East Timor 24) Jordan 34) Mongolia 44) Saudi Arabia 54) Uzbekistan
5) Bahrain 15) French S. Terr. 25) Kazakhstan 35) Myanmar (Burma) 45) Seychelles 55) Vietnam
6) Bangladesh 16) Georgia 26) Korea (North) 36) Nepal 46) Singapore 56) Yemen
7) Bhutan 17) Hong Kong 27) Korea (South) 37) Oman 47) Sri Lanka
8) Brunei 18) India 28) Kuwait 38) Pakistan 48) Syria
9) Cambodia 19) Indonesia 29) Kyrgyzstan 39) Palestine 49) Taiwan
10) China 20) Iran 30) Laos 40) Philippines 50) Tajikistan
#? 10
Please select one of the following timezones.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Selected time is now: Mon Feb 12 22:09:21 CST 2024.
Universal Time is now: Mon Feb 12 14:09:21 UTC 2024.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
root@a2bc0121d174:/app/MyFirstProject/MyFirstProject# rm -rf /etc/localtime
root@a2bc0121d174:/app/MyFirstProject/MyFirstProject# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@a2bc0121d174:/app/MyFirstProject/MyFirstProject# date
Mon Feb 12 22:10:27 CST 2024
root@a2bc0121d174:/app/MyFirstProject/MyFirstProject# exit
打包容器为新镜像
# 退出a2bc0121d174容器并保存镜像为django:myfirstapp
root@kmaster1:~/application/python# docker commit a2bc0121d174 django:myfirstapp
root@kmaster1:~/application/python# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
django myfirstapp 22d625b4f294 3 minutes ago 546MB
基于dockerfile创建django的后台应用容器
#创建在kmaster1上的应用命令
mkdir /root/application/python
cd /root/application/python
#编写dockerfile
vim Dockerfile
FROM django:myfirstapp
WORKDIR /app/MyFirstProject
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
EXPOSE 8000
#编译新的应用镜像
root@kmaster1:~/application/python# docker build -t myapp:v1 .
[+] Building 0.6s (6/6) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 693B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/django:myfirstapp 0.0s
=> [1/2] FROM docker.io/library/django:myfirstapp 0.0s
=> [2/2] WORKDIR /app/MyFirstProject/ 0.1s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:9e814098e6188b9af6f4ad2134f0b190b23082bc202a05e42a0ba2155579c554 0.0s
=> => naming to docker.io/library/myapp:v1 0.0s
root@kmaster1:~/application/python# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp v1 9e814098e618 55 seconds ago 546MB
#打包新应用为tar包
cd /root/application/python
docker save -o myapp_v1.tar myapp:v1
将新应用镜像传到node节点(使用本地hub的可以忽略此步骤)
# 在kmaster1上scp将myapp_V1.tar传到node节点,在有本地hub的情况下,可以自定义此步骤
scp myapp_v1.tar knode1:/root/
scp myapp_v1.tar knode2:/root/
scp myapp_v1.tar knode3:/root/
scp myapp_v1.tar knode4:/root/
# 分别到knode1,knode2,knode3,knode4上执行
docker load -i myapp_v1.tar
创建deployment-service-django.yaml
cd /root/application/python
vim deployment-service-django.yaml
apiVersion: v1
kind: Namespace
metadata:
name: dev #创建一个dev的namespace
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-django
namespace: dev #创建pod放到dev中
spec:
selector:
matchLabels:
app: myapp
replicas: 2
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
ports:
- containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
name: service-django
labels:
app: myapp
namespace: dev
spec:
type: NodePort
ports:
- port: 8000
targetPort: 8000
nodePort: 32424
selector:
app: myapp
执行deployment-service-django.yaml
root@kmaster1:~/application/python# kubectl apply -f deployment-service-django.yaml
namespace/dev created
deployment.apps/deployment-django created
service/service-django created
root@kmaster1:~/application/python# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
deployment-django-7dc469fd5b-bsn9m 1/1 Running 0 12s
deployment-django-7dc469fd5b-zn5zs 1/1 Running 0 12s
root@kmaster1:~/application/python# kubectl get svc -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-django NodePort 10.98.126.238 <none> 8000:32424/TCP 20s
访问django应用服务
http://192.168.48.210:32424
更多推荐
已为社区贡献6条内容
所有评论(0)