linux12Devops -->07jenkins部署python
Posted FikL-09-19
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux12Devops -->07jenkins部署python相关的知识,希望对你有一定的参考价值。
文章目录
Jenkins部署Python
编译环境(python)
部署环境
一、部署环境
1、设计
user ---> ingress ---> endprints ---> pod
# 因为endpoints上面有ingress,所以在写配置清单时要用无头service
# ingress
URL: www.django.cluster.local.com
# endPrints
service ---> 8080
# Pod
django:2+
## 构建镜像 :python:3.6.13
2.准备代码
1)克隆仓库k8s-master1 部署一个基础环境
- k8s-master1 部署一个基础环境
- 克隆并生成,上传至自己的代码仓库
- 过程中若需要输入密码,则需将ssh公钥添加到gitlab内,否则密码正确也无效!
[root@k8s-m-01 ~]# yum install python -y
[root@k8s-m-01 ~]# git clone git@gitee.com:chenyang0910/django_test.git
[root@k8s-m-01 django]# cd django/
[root@k8s-m-01 django]# rm -rf .git/
[root@k8s-m-01 django]#
git init
git add .git remote add origin git@192.168.12.82:root/django.git
git remote add origin git@192.168.12.82:root/django.git
git add .
git commit -m 'init'
git push -u origin master
# 拷贝代码链接
git@192.168.12.82:root/django.git # ssh
http://192.168.12.82/root/django.git # http
2)编写Dockerfile
- 编写jenkins-python的Dockerfile与启动脚本
1.编写Dockerfile
[root@k8s-m-01 django]# cat > Dockerfile << EOF
# 指定基础镜像
FROM python:3.6.13
# 安装django框架
RUN pip3 install django==2.0.7
# 设置需要挂载的目录
VOLUME ['/opt']
# 暴露端口,默认也是8080
EXPOSE 8080
# 设置工作目录
WORKDIR /opt
# 启动命令,监听所有ip的8080端口
CMD ["./start"]
EOF
2.编写启动脚本
[root@k8s-m-01 django]# cat > start <<EOF
#!/bin/bash
# 安装插件
pip3 install -r requirements.txt
python manage.py runserver 0.0.0.0:8080
EOF
[root@k8s-m-01 django]# chmod +x start
3)构建镜像
# 构建
[root@k8s-m-01 django]# docker build -t registry.cn-shanghai.aliyuncs.com/op_devops/python:jenkins-python:py3.6.13-dj2.0.7 .
# 推送
[root@k8s-m-01 django]# docker push registry.cn-shanghai.aliyuncs.com/op_devops/python:jenkins-python:py3.6.13-dj2.0.7
4)编写配置清单
- 部署python配置清单
cat > django-python.yaml <<EOF
kind: Namespace
apiVersion: v1
metadata:
name: python-mysql
---
kind: Service
apiVersion: v1
metadata:
name: python-mysql-svc
namespace: python-mysql
spec:
ports:
- port: 3306
targetPort: 3306
selector:
app: python-mysql
deploy: mysql
---
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: python-mysql
name: python-mysql-deployment
spec:
selector:
matchLabels:
app: python-mysql
deploy: mysql
template:
metadata:
labels:
app: python-mysql
deploy: mysql
spec:
nodeName: k8s-master1
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123"
- name: MYSQL_DATABASE
value: "migege"
---
kind: Namespace
apiVersion: v1
metadata:
name: python-django
---
kind: Service
apiVersion: v1
metadata:
name: python-django-svc
namespace: python-django
spec:
ports:
- port: 8080
targetPort: 8080
clusterIP: None
selector:
app: python-django
deploy: django
---
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: python-django
name: python-django-svc
spec:
selector:
matchLabels:
app: python-django
deploy: django
template:
metadata:
labels:
app: python-django
deploy: django
spec:
#nodeName: k8s-n-01
containers:
- name: django
image: registry.cn-shanghai.aliyuncs.com/op_devops/python:jenkins-python:py3.6.13-dj2.0.7
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
volumeMounts:
- mountPath: /opt
name: python-django-nfs
volumes:
- name: python-django-nfs
nfs:
path: /nfs/v5
server: 172.16.15.111
---
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: python-django-ingress
namespace: python-django
spec:
rules:
- host: www.django.cluster.local.com
http:
paths:
- backend:
serviceName: python-django-svc
servicePort: 8080
path: /
EOF
二、部署环境
1.部署python
[root@k8s-m-01 ~]# kubectl django-python.yaml
[root@k8s-m-01 ~]# kubectl get ingress -n python-django
NAME CLASS HOSTS ADDRESS PORTS AGE
python-django-ingress <none> www.django.cluster.local.com 80 2m33s
[root@k8s-m-01 ~]# kubectl get pod -n python-mysql
NAME READY STATUS RESTARTS AGE
python-mysql-deployment-7f8c9ff55-srgs6 1/1 Running 0 10m
# 查看访问域名
[root@k8s-m-01 ~]# kubectl get ingress -n python-django
NAME CLASS HOSTS ADDRESS PORTS AGE
python-django-ingress <none> www.django.cluster.local.com 80 11m
# 查看访问端口:32145
[root@k8s-m-01 ~]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.111.205.92 <none> 80:32145/TCP,443:32075/TCP 4d22h
2.访问测试
-
访问到如下界面即为成功
www.django.cluster.local.com:32145
以上是关于linux12Devops -->07jenkins部署python的主要内容,如果未能解决你的问题,请参考以下文章
linux12Devops -->07jenkins部署python
linux12Devops --> 01软件开发生命周期持续集成代码管理工具Git