Springboot+Vue+Mysql+Redis
#前端打包上传,必须与文件在同一目录
#创建文件夹
mkdir dist
cd dist
#用来挂载到nginx上
vi default.conf
server {
listen 80; #端口
server_name localhost; #ip
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/error.log error;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /prod-api/ {
proxy_pass http://192.168.184.128:31004/; #指向后端地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
#Dockerfile文件,生成镜像
vi Dockerfile
#用nginx挂载前端项目
FROM nginx
ADD dist/ /usr/share/nginx/html
ADD default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
ENTRYPOINT nginx -g "daemon off;"
#创建镜像
docker build . -t app:v1.0
#tag,供远程
docker tag app:v1.0 dockerdjyc/app:v1.0
#推送远程仓库,供k8s进行pull部署
docker push dockerdjyc/app:v1.0
#创建deploment.yaml文件,生成pod节点,并创建svc,端口暴露
vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: vue #pod节点名称
namespace: default
labels:
app: vue
spec:
replicas: 1
selector:
matchLabels:
app: vue # has to match .spec.template.metadata.labels
template:
metadata:
labels:
app: vue # has to match .spec.selector.matchLabels
spec:
imagePullSecrets:
- name: harbor-secret
containers:
- name: vue
image: dockerdjyc/vue:v1.0 #pull镜像
imagePullPolicy: Always
env:
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 80 #项目端口
name: vue
resources:
requests:
memory: 300Mi
cpu: 200m
limits:
memory: 500Mi
cpu: 400m
apiVersion: v1
kind: Service
metadata:
name: vue
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
#NOde上的静态端口
nodePort: 31005
type: NodePort
selector:
app: vue
#k8s进行部署
kubectl apply -f deployment.yaml
#端口暴露,外网访问
kubectl apply -f service.yaml
#创建文件夹
mkdir /home/demo
#编写Dockerfile,供docker进行build
vi Dockerfile
#基于哪个镜像
FROM openjdk:8
VOLUME /tmp
#复制文件到容器
ADD ./jar包 别名.jar
RUN bash -c 'touch /别名.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/别名.jar"]
docker build . -t 别名:v1.0
docker images
docker tag 镜像名称:版本号(v1.0) 远程仓库地址‘
docker push tag的名称
# 新建 PV 配置文件
vi mysql-pv.yaml
# mysql-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
labels:
pv: mysql-pv
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /root/data
# 创建PV
kubectl create -f mysql-pv.yaml
# 检验
kubectl get pv
# 新建 PVC 配置文件
vi mysql-pvc.yaml
# cat mysql-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
selector:
matchLabels:
pv: mysql-pv
# 创建 PVC
kubectl create -f mysql-pvc.yaml
# 校验
kubectl get pvc
# 新建 Deployment 配置文件
vi mysql-deploy.yaml
# cat mysql-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deploy
spec:
replicas: 1
selector:
matchLabels:
name: mysql-ops
template:
metadata:
labels:
name: mysql-ops
spec:
containers:
- name: mysql
image: mysql #镜像
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: "/var/lib/mysql"
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
# 创建 Deployment
kubectl create -f mysql-deploy.yaml
# 校验
kubectl get deploy
# 新建 Service 配置文件
vi mysql-service.yaml
# cat mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
labels:
name: mysql-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 30306
selector:
name: mysql-ops
# 创建 Service
kubectl create -f mysql-service.yaml
# 校验
kubectl get svc
kubectl exec -it mysql-deploy-6fd5745c87-ltmwb -- mysql -u root -p
kubectl get po -o wide
kubectl get svc
mkdir -p /data/redis/redis-config/
touch /data/redis/redis-config/redis.conf
cat >> /data/redis/redis-config/redis.conf << EOF
#配置密码
#requirepass 123456
bind 0.0.0.0
save 900 1
save 300 10
save 60 10000
EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: default # 默认空间
labels:
name: redis
spec:
replicas: 1
selector:
matchLabels:
name: redis
template:
metadata:
labels:
name: redis
spec:
containers:
- name: redis
image: redis
volumeMounts:
- name: redis-config
mountPath: "/usr/local/etc/redis"
- name: redis-data
mountPath: "/data"
command:
- "redis-server"
args:
- "/usr/local/etc/redis/redis.conf"
volumes:
- name: redis-config
hostPath:
path: /data/redis/redis-config
type: DirectoryOrCreate
- name: redis-data
hostPath:
path: /data/redis/redis-data
type: DirectoryOrCreate
apiVersion: v1
kind: Service
metadata:
# 定义服务名称,其他Pod可以通过服务名称作为域名进行访问
name: redis-service
namespace: default # 默认空间
labels:
name: redis-service
spec:
type: NodePort # 如果需要集群外部访问,这里改为NodePort
ports:
- port: 6379
protocol: TCP
targetPort: 6379
name: redis
# Node上的静态端口
nodePort: 30379
# 管理标签app为mysql的Pod
selector:
name: redis
#部署redis
kubectl apply -f deployment.yaml
#端口暴露
kubeclt apply -f service.yaml
kubectl get pods --all-namespaces -o wide
kubectl exec -it redis-6f59d6d4cd-dzlq5 /bin/bash
redis-cli
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-redis-mysql
labels:
app: demo-redis-mysql
spec:
replicas: 1
selector:
matchLabels:
app: demo-redis-mysql
template:
metadata:
labels:
app: demo-redis-mysql
spec:
containers:
- name: demo-redis-mysql
# 指定Docker Hub中的镜像地址
image: dockerdjyc/demo-redis-mysql:v1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
env:
# 指定数据库连接地址
- name: spring.datasource.url
value: jdbc:mysql://mysql-service:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai #mysql-service:是刚才部署的mysql的service.yaml的名
# 指定数据库连接密码
- name: spring.datasource.password
value: "root"
# 指定redis连接地址
- name: spring.redis.host
value: redis-service #刚才部署的redis的service.yaml的名
# 指定日志文件路径
- name: logging.path
value: /var/logs
- name: TZ
value: Asia/Shanghai
volumeMounts:
- mountPath: /var/logs
name: log-volume
volumes:
- name: log-volume
hostPath:
path: /home/docker/mydata/app/mall-tiny-fabric/logs
type: DirectoryOrCreate
apiVersion: v1
kind: Service
metadata:
name: demo-redis-mysql #暴露名
spec:
ports:
- port: 8080 #项目端口
targetPort: 8080 #pod节点内部服务的端口
protocol: TCP
#NOde上的静态端口
nodePort: 31004
type: NodePort
selector:
app: demo-redis-mysql
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml