K8S combat - -06- continuous build project to build Django

Today is K8S combat - build Django project, the final say. 5 in front of us step by step to build a django project by k8s, by taking into account the actual production environment, we added the initialization function, building automation functions, shared storage, data encryption, monitoring program, log program. From scratch, we go together again. The last to speak, I will bring everyone to use existing resources to achieve the simplest continuous build.

Continuous build

design

gitlab+jenkins+node+dockerfile+k8s

gitlab as a warehouse, provided the trigger Jenkins, node as execution node, dockerfile mirror production, k8s build resources.

Automated build environment

You need to refer to my previous tutorial to build gitlab + jenkins create an automated build environment. Make sure your automated build environment is normal!

Creating gitlab project

1542616288522

Create a project, where the project name is the name of the next task jenkins, by setting a good gitlab address, you can get the code to change gitlab, triggering jenkins task.

Write Jenkins triggered task

1542614177951

A new job, then select a node can be constructed.

1542614411445

I've built before the relevant image, this time without having to rebuild the mirror.

Verification node node

Here there is only a need to ensure that node node java environment, docker environment, k8s environment.

Verify Continuous Integration

By updating gitlab related project code, jenkins detected gitlab code changes, started shell task on selected machine execution, execution of the construction project django.

1542614992154

1542615016486

As can be seen from the console output resources have been created, and then go visit the project address and found items can be a normal visit.

If you want to reproduce these, I recommend "" "" "" "Before you must read." There I learned to code on gitlab is django_deploy.yaml and django_nfs.yaml 3.0 version of django projects. If you need to test django project version 4.0, you need to upload their own resources. Finally, attach the relevant code django_polls engineering structures and the directory gitlab.

1542615397419

django_deploy.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
app: mysql
name: mysql
spec:
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 root@'%' 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: v1
kind: Service
metadata:
name: mysql-svc
spec:
selector:
app: mysql
clusterIP: 10.101.1.1
ports:
- protocol: TCP
port: 3306
targetPort: 3306




apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
app: django
name: django
spec:
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: v1
kind: Service
metadata:
name: django-svc
spec:
type: NodePort
selector:
app: django
clusterIP: 10.101.1.2
ports:
- protocol: TCP
nodePort: 30008
port: 8000
targetPort: 8000

django_nfs.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: mynfs
nfs:
path: /nfs-share
server: 172.16.2.237



apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: mynfs




apiVersion: v1
kind: PersistentVolume
metadata:
name: mysqlpv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: mysqlnfs
nfs:
path: /mysql-share
server: 172.16.2.237

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysqlpvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: mysqlnfs

---

apiVersion: v1
kind: PersistentVolume
metadata:
name: djangopv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: djangonfs
nfs:
path: /django-share
server: 172.16.2.237

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: djangopvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: djangonfs

Conclusion

K8S combat - build Django project has been successfully completed. Thank you for your support all the way, as an IT person, every day, waiting to learn endless knowledge, I believe Him, and strive to learn there is always a chance to showcase their talents.

Original: Big Box  K8S combat - -06- continuous build project to build Django


Guess you like

Origin www.cnblogs.com/petewell/p/11606829.html