用k8s部署简单应用
本次部署两个后端java服务 一个前端dist包
1、制作镜像
uap-server镜像
点击查看代码
FROM openjdk:8-jdk-slim
label maintainer=mhfang3
#LANGUAGE ENV
ENV LANG C.UTF-8
ENV LANGUAGE en_US:en
#设置容器时间
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
# 安装系统依赖
RUN apt-get update && \
apt-get install -y \
fontconfig \
fonts-wqy-microhei \
procps && \
rm -rf /var/lib/apt/lists/*
ADD ./config/ /usr/share/config/
COPY ./uap-server.jar /usr/share/
EXPOSE 8980
#指定容器启动程序及参数
ENTRYPOINT ["java","-Djasypt.encryptor.password=uap@ifly123456","-Dspring.config.location=file:/usr/share/config/application.properties", "-Dfile.encoding=utf-8", "-jar","/usr/share/uap-server.jar"]
uap-manager镜像
点击查看代码
FROM openjdk:8-jdk-slim
label maintainer=mhfang3
#LANGUAGE ENV
ENV LANG C.UTF-8
ENV LANGUAGE en_US:en
#设置容器时间
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
# 安装系统依赖
RUN apt-get update && \
apt-get install -y \
fontconfig \
fonts-wqy-microhei \
procps && \
rm -rf /var/lib/apt/lists/*
ADD ./config/ /usr/share/config/
ADD ./excelTemplate/ /usr/share/excelTemplate/
COPY ./uap-manager.jar /usr/share/
EXPOSE 8880
#指定容器启动程序及参数
ENTRYPOINT ["java","-Djasypt.encryptor.password=uap@ifly123456","-Dspring.config.location=file:/usr/share/config/application.properties", "-Dfile.encoding=utf-8", "-jar","/usr/share/uap-manager.jar"]
uap-web镜像
点击查看代码
FROM nginx:alpine
label maintainer=mhfang3
# 删除默认的 Nginx 配置
RUN rm -rf /usr/share/nginx/html/*
# 复制构建好的文件到 Nginx 的公共目录
COPY ./dist /usr/share/nginx/html
# 复制自定义的 Nginx 配置文件(如果有)
COPY nginx.conf /etc/nginx/nginx.conf
# 暴露端口
EXPOSE 80
# 启动 Nginx
CMD ["nginx", "-g", "daemon off;"]
2、K8s部署
创建命名空间
点击查看代码
apiVersion: v1
kind: Namespace
metadata:
name: uap
部署configmap
点击查看代码
apiVersion: v1
kind: ConfigMap
metadata:
name: uap-server-cm
namespace: iflydata
data:
application.properties: |
##########------------------ usp-server,必须配置项 ---------------------##########
部署deployment
点击查看代码
apiVersion: apps/v1
kind: Deployment
metadata:
name: uap-server-dep
namespace: iflydata
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: uap-server
template:
metadata:
labels:
app: uap-server
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values: ["uap-server"]
topologyKey: "kubernetes.io/hostname"
imagePullSecrets:
- name: secret
containers:
- name: uap-server
image: uap-server:V3.3.1_RSTC2.0
imagePullPolicy: Always
ports:
- containerPort: 8980
volumeMounts:
- name: config-volume
mountPath: /usr/share/config/application.properties
subPath: application.properties
- name: config-volume
mountPath: /usr/share/config/application-base.properties
subPath: application-base.properties
resources:
requests:
memory: "1024Mi"
cpu: "1000m"
limits:
memory: "2048Mi"
cpu: "2000m"
volumes:
- name: config-volume
configMap:
name: uap-server-cm
部署service
点击查看代码
apiVersion: v1
kind: Service
metadata:
name: uap-server-svc
namespace: iflydata
spec:
selector:
app: uap-server
type: NodePort # 可以根据需求使用ClusterIP, NodePort, LoadBalancer等类型
ports:
- name: rest
protocol: TCP
port: 8980
targetPort: 8980
nodePort: 30004
uap-manager同理
部署dist
点击查看代码
apiVersion: v1
kind: ConfigMap
metadata:
name: uap-front-cm
namespace: iflydata
data:
nginx.conf: |
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /uap-manager {
proxy_pass http://uap-platform-manager;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /uap-server {
proxy_pass http://uap-platform-server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /uap-auth {
proxy_pass http://uap-platform-auth;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
upstream uap-platform-manager {
#配置为uap manager IP+端口
server xxxx:30005;
}
upstream uap-platform-server {
#配置为uap manager IP+端口
server xxxx:30004;
}
}
部署deployment
点击查看代码
apiVersion: apps/v1
kind: Deployment
metadata:
name: uap-front-deployment
namespace: iflydata
labels:
app: uap-front
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: uap-front
template:
metadata:
labels:
app: uap-front
spec:
imagePullSecrets:
- name: secret
containers:
- name: uap-front
image: uap-web:V3.3.1_RSTC2.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
resources:
requests:
memory: "500Mi"
cpu: "500m"
limits:
memory: "1024Mi"
cpu: "2000m"
volumes:
- name: config-volume
configMap:
name: uap-front-cm
部署service
点击查看代码
apiVersion: v1
kind: Service
metadata:
name: uap-front-svc
namespace: iflydata
spec:
selector:
app: uap-front
type: NodePort # 可根据需要更改为 NodePort 或其他类型
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30018