Technology stack: Vue + Golang + MySQL + Redis
Background: The MySQL database needs to be initialized before being used
Steps:
1. Write Dockerfile for front-end Vue project, back-end Go project, MySQL database, package image, push to Dockerhub 2. 3. Kubectl create execute Service, DeploymentCopy the code
Attached project address: github.com/TeanLee/mal…
Frontend: mall-admin-frontend (contains database initialization content, in MySQL folder)
The backend: mall – admin server – go
The yamL files are as follows:
The front-end project
First write Dockerfile to package the front-end project, Dockerfile is as follows:
#! Note: The node version must be the same as the local successful version!!
FROM node:16.12
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY.
ENV VUE_APP_API_URL='http://127.0.0.1:8081'
EXPOSE 8080
CMD [ "npm"."run"."serve" ]
Copy the code
.dockerignore (not copying node_modules, reducing file content)
node_modules
Copy the code
Run the following command in the current Dockerfile directory:
docker build -t mall-admin-server-frontend .
docker tag mall-admin-server-frontend {your dockerhub name}/mall-admin-server-frontend:1.0.0
docker login
docker push {your dockerhub name}/mall-admin-server-frontend:1.0.0
Copy the code
K8s build Service/Deployment file as follows. Replace image (image) with image (image).
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: mall-frontend
name: mall-frontend
spec:
ports:
- name: "8080"
port: 8080
targetPort: 8080
selector:
io.kompose.service: mall-frontend
status:
loadBalancer: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: mall-frontend
name: mall-frontend
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: mall-frontend
strategy: {}
template:
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: mall-frontend
spec:
containers:
- env:
- name: VUE_APP_API_URL
value: http://127.0.0.1:8081
image: Tean123 / mall - admin server - frontend: 1.0.0
name: mall-admin-server-front-end
ports:
- containerPort: 8080
resources: {}
tty: true
restartPolicy: Always
status: {}
Copy the code
MySQL Database section
The dump20220317. SQL file is the file I need to initialize, following the way I initialized mysql data when Docker started.
Dockerfile is as follows:
FROM mysql:latest
COPY Dump20220317.sql /docker-entrypoint-initdb.d/Dump20220317.sql
Copy the code
Execute in the directory where the Dockerfile resides:
docker build -t mall-admin-server-mysql .
docker tag mall-admin-server-frontend {your dockerhub name}/mall-admin-server-mysql:1.0.0
docker login
docker push {your dockerhub name}/mall-admin-server-mysql:1.0.0
Copy the code
MySQL Service/Deployment Deployment file
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: mydb
name: mydb
spec:
ports:
- name: "3306"
port: 3306
targetPort: 3306
selector:
io.kompose.service: mydb
status:
loadBalancer: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: mydb
name: mydb
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: mydb
strategy: {}
template:
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: mydb
spec:
containers:
- env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
image: Tean123 / mall - admin server - mysql: 1.0.0
name: mall-admin-server-db
ports:
- containerPort: 3306
resources: {}
tty: true
restartPolicy: Always
status: {}
Copy the code
Back-end Go part
Dockerfile is as follows:
FROM golang:latest
RUN mkdir /app
WORKDIR /app
ADD go.mod .
ADD go.sum .
RUN go mod download
ADD.
RUN go build -o main .
EXPOSE 8081
ENTRYPOINT ["./main"]
Copy the code
Execute under this Dockerfile:
docker build -t mall-admin-server-backend .
docker tag mall-admin-server-frontend {your dockerhub name}/mall-admin-server-backend:1.0.0
docker login
docker push {your dockerhub name}/mall-admin-server-backend:1.0.0
Copy the code
The Service and Deployment Deployment files of the back-end Go project are as follows:
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: app
name: app
spec:
ports:
- name: "8081"
port: 8081
targetPort: 8081
selector:
io.kompose.service: app
status:
loadBalancer: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: app
name: app
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: app
strategy:
type: Recreate
template:
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: app
spec:
containers:
- env:
- name: env
value: production
image: Tean123 / mall - admin server - backend: 1.0.0
name: mall-admin-server-app
ports:
- containerPort: 8081
resources: {}
tty: true
volumeMounts:
- mountPath: /workspace
name: app-claim0
restartPolicy: Always
volumes:
- name: app-claim0
persistentVolumeClaim:
claimName: app-claim0
status: {}
Copy the code
Redis part
Since there is no data initialization and so on, it is relatively easy to deploy the cluster’s Service and Deployment without a Dockerfile
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: cache
name: cache
spec:
ports:
- name: "6379"
port: 6379
targetPort: 6379
selector:
io.kompose.service: cache
status:
loadBalancer: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: cache
name: cache
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: cache
strategy: {}
template:
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.24. 0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: cache
spec:
containers:
- image: Redis: 6.2 - alpine
name: mall-admin-server-redis
ports:
- containerPort: 6379
resources: {}
restartPolicy: Always
status: {}
Copy the code
storage
Note that persistent Volumn content also needs to be written since database and other content is Volumn mounted:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
creationTimestamp: null
labels:
io.kompose.service: app-claim0
name: app-claim0
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}
Copy the code
Deploy the service
The next step is to deploy the service. You can choose to place all the YAML for the deployed service in a single folder and execute it
kubectl create -f {deployfile}
Copy the code
The execution result is shown as follows:
The docker-compose method is also recommended: Kompose
Kompose convert -f docker-composing. Yaml will convert docker compose to Kubernetes or OpenShift files.