今天是K8S实战-构建Django项目,最后一讲。前面5将我们一步步通过k8s构建了一个django项目,通过考虑实际生产环境,我们添加了初始化功能,自动化构建功能,共享存储,数据加密,监控方案,日志方案。从无到有,我们一同走了一遍。最后一讲,我将带着大家利用现有的资源实现最简单的持续构建。
持续构建
设计
gitlab+jenkins+node+dockerfile+k8s
gitlab作为仓库,jenkins设置触发器,node作为执行节点,dockerfile生产镜像,k8s构建资源。
实现自动化构建环境
你需要参照我之前的教程搭建gitlab+jenkins创建一个自动化构建环境。请一定要确保你的自动化构建环境是正常的!
创建gitlab工程项目
创建一个工程,这里的工程名是接下来jenkins的任务名,通过设定好的gitlab地址,可以获取到gitlab上代码的变更,从而触发jenkins任务。
编写Jenkins触发任务
新建一个job,然后选择一个可以进行构建的节点。
我之前已经构建好了相关镜像,这次就不用重新构建镜像。
验证node节点
这里只需要确保node节点有java环境,docker环境,k8s环境。
验证持续集成
通过更新gitlab相关工程代码,jenkins检测到gitlab代码变更,开始执行shell 任务,在选定的执行机上,执行了django项目的构建。
从控制台输出中可以看出资源已经创建,然后再去访问项目地址,发现项目可以正常访问。
如果你要重现这些,我建议你必须阅读之前的《》《》《》《》。还有我上传到gitlab上的代码是3.0版本的django项目的django_deploy.yaml和django_nfs.yaml。如果你需要测试4.0版本django项目,你还需自行上传相关资源。最后附上目录gitlab上django_polls的工程结构和相关代码。
django_deploy.yaml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
apiVersion: extensions/v1beta1kind: Deploymentmetadata: labels: app: mysql name: mysqlspec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: initContainers: - name: mysql-init image: busybox imagePullPolicy: IfNotPresent command: - sh - "-c" - | set -ex cat > /start_script/mysql_init.sh <<EOF #!/bin/bash sed -i "/log-error/iskip-grant-tables" /etc/my.cnf systemctl restart mysqld sleep 50 mysql -uroot -p123qwe -e "CREATE DATABASE polls DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;" mysql -uroot -p123qwe -e "use mysql;" mysql -uroot -p123qwe -e "grant all privileges on *.* to [email protected]'%' identified by "123qwe";" mysql -uroot -p123qwe -e "flush privileges;" systemctl restart mysqld EOF chmod +x /start_script/mysql_init.sh volumeMounts: - name: mysql-initdb mountPath: /start_script volumes: - name: mysql-initdb persistentVolumeClaim: claimName: mypvc1 - name: mysql-data persistentVolumeClaim: claimName: mysqlpvc containers: - image: centos7:mysql3 name: mysql imagePullPolicy: IfNotPresent env: - name: MYSQL_ROOT_PASSWORD value: 123qwe readinessProbe: exec: command: - /bin/sh - "-c" - MYSQL_PWD="${MYSQL_ROOT_PASSWORD}" - mysql -h 127.0.0.1 -u root -p $MYSQL_PWD -e "SELECT 1" initialDelaySeconds: 10 timeoutSeconds: 1 successThreshold: 1 failureThreshold: 3 periodSeconds: 5 volumeMounts: - name: mysql-initdb mountPath: /start_script - name: mysql-data mountPath: /raiddisk apiVersion: v1kind: Servicemetadata: name: mysql-svcspec: selector: app: mysql clusterIP: 10.101.1.1 ports: - protocol: TCP port: 3306 targetPort: 3306 apiVersion: extensions/v1beta1kind: Deploymentmetadata: labels: app: django name: djangospec: replicas: 1 selector: matchLabels: app: django template: metadata: labels: app: django spec: initContainers: - name: django-init image: busybox imagePullPolicy: IfNotPresent command: - sh - "-c" - | set -ex cat > /start_script/django_init.sh <<EOF #!/bin/bash mkdir /root/django cd /root/django git clone https://github.com/cuigelasi/learn_django.git cd /root/django/learn_django git checkout -t origin/polls sed -i "s/172.10.1.2/10.101.1.1/" learn_django/settings.py sleep 60 python manage.py makemigrations python manage.py migrate echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', '[email protected]', '123qwe')" | python manage.py shell python manage.py runserver 0.0.0.0:8000 EOF chmod +x /start_script/django_init.sh volumeMounts: - name: django-initdb mountPath: /start_script containers: - image: centos7:django3 name: django volumeMounts: - name: django-initdb mountPath: /start_script readinessProbe: exec: command: - cat - /root/django/learn_django/learn_django/settings.py initialDelaySeconds: 10 periodSeconds: 5 volumes: - name: django-initdb persistentVolumeClaim: claimName: mypvc1 - name: django-data persistentVolumeClaim: claimName: djangopvc apiVersion: v1kind: Servicemetadata: name: django-svcspec: type: NodePort selector: app: django clusterIP: 10.101.1.2 ports: - protocol: TCP nodePort: 30008 port: 8000 targetPort: 8000 |
django_nfs.yaml
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
apiVersion: v1kind: PersistentVolumemetadata: name: mypv1spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle storageClassName: mynfs nfs: path: /nfs-share server: 172.16.2.237 apiVersion: v1kind: PersistentVolumeClaimmetadata: name: mypvc1spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: mynfs apiVersion: v1kind: PersistentVolumemetadata: name: mysqlpvspec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle storageClassName: mysqlnfs nfs: path: /mysql-share server: 172.16.2.237 --- apiVersion: v1kind: PersistentVolumeClaimmetadata: name: mysqlpvcspec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: mysqlnfs --- apiVersion: v1kind: PersistentVolumemetadata: name: djangopvspec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle storageClassName: djangonfs nfs: path: /django-share server: 172.16.2.237 --- apiVersion: v1kind: PersistentVolumeClaimmetadata: name: djangopvcspec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: djangonfs |
结束语
K8S实战-构建Django项目,已经圆满结束。感谢大家的一路支持,作为一个IT人,每天都有无尽的知识等待学习,我相信天道酬勤,努力学习总有展示才华的机会。
原文:大专栏 K8S实战-构建Django项目-06-持续构建
原文地址:https://www.cnblogs.com/chinatrump/p/11606890.html