java 在k8s中的部署流程

1.写Docker文件

复制代码
FROM ubuntu:22.04

ENV LANG='C.UTF-8' LC_ALL='C.UTF-8'

RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata curl ca-certificates fontconfig locales binutils \
    && echo "C.UTF-8 UTF-8" >> /etc/locale.gen \
    && locale-gen C.UTF-8 \
    && rm -rf /var/lib/apt/lists/*

ENV JAVA_HOME=/opt/java/dragonwell-17.0.14.0.15+7-GA \
    PATH="/opt/java/dragonwell-17.0.14.0.15+7-GA/bin:$PATH" \ 
    JAVA_VERSION=jdk-17.0.14.0.15+7-GA

ADD Alibaba_Dragonwell_Standard_17.0.14.0.15.7_x64_linux.tar.gz /opt/java
#基础镜像完成

WORKDIR /app
ADD autopick.tar.gz /opt/
ADD qt5.14.2.tar.gz /opt/qt/
COPY autoPointPick  /opt/autopick/binR 
ENV PATH=/opt/autopick/binR:$PATH \ 
	LD_LIBRARY_PATH=/opt/autopick/lib/libR:/opt/autopick/lib/systemlib:/opt/qt/qt5.14.2/gcc_64/lib:/opt/qt/qt5.14.2/gcc_64/plugins
ENV JAVA_OPTS="-Xms128M -Xmx1G" \
    SPRING_PROFILE="default" \
    NACOS_SERVER_ADDR="nacos-server:8848" \
    NACOS_NAMESPACE="public" \
    NACOS_DISCOVERY_ENABLE="true" \
    NACOS_CONFIG_ENABLE="true" \
    NACOS_CONFIG_PREFIX="oa" \
    NACOS_FILE_EXTENSION="yaml"
COPY oa.jar /app/oa.jar
CMD java ${JAVA_OPTS} -jar /app/oa.jar \
      --spring.profiles.active=${SPRING_PROFILE} \
      --spring.cloud.nacos.discovery.enabled=${NACOS_DISCOVERY_ENABLE} \
      --spring.cloud.nacos.discovery.server-addr=${NACOS_SERVER_ADDR} \
      --spring.cloud.nacos.discovery.namespace=${NACOS_NAMESPACE} \
      --spring.cloud.nacos.config.enabled=${NACOS_CONFIG_ENABLE} \
      --spring.cloud.nacos.config.server-addr=${NACOS_SERVER_ADDR} \
      --spring.cloud.nacos.config.namespace=${NACOS_NAMESPACE} \
      --spring.cloud.nacos.config.prefix=${NACOS_CONFIG_PREFIX} \
      --spring.cloud.nacos.config.file-extension=${NACOS_FILE_EXTENSION}

2.构建镜像,并导出

复制代码
docker build -t oa:20250709 .


docker save -o oa.tar  oa:20250709

3.在k8s主节点执行的命令

复制代码
docker load -i oa.tar
docker images
docker tag e5ce39743840 cr.registry.res.sgmc.sgcc.com.cn/beijing/oa:20250709
docker登录 
docker login --username=oa-1666080388355 cr.registry.res.sgmc.oacc.com.cn
docker push cr.registry.res.sgmc.sgcc.com.cn/beijing/oa:20250709

4.编写k8s yaml文件

复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: oa
  namespace: default
  labels:
    app: oa
spec:
  replicas: 1
  selector:
    matchLabels:
      app: oa
  template:
    metadata:
      labels:
        app: oa   
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: In
                    values:
                      - bjdc-1.220.20.22.38
      containers:
      - name: aa
        image: cr.registry.res.sgmc.oacc.com.cn/beijing/oa:20250709
        resources: 
          requests: 
            cpu: "500m"
            memory: "5Gi"
          limits: 
            cpu: "500m"
            memory: "10Gi"
        env:
        - name: SPRING_PROFILE
          value: prod
        - name: NACOS_SERVER_ADDR
          value: nacos:8848
        ports:
        - containerPort: 80
        volumeMounts:
        - name: local-storage
          mountPath: /oss
        - name: pvc-oss
          mountPath: /oss-oa
      volumes:
      - name: local-storage
        hostPath:
         path: /mnt_oa
      - name: pvc-oss-oa
        persistentVolumeClaim:
            claimName: pvc-oss-oa
---
apiVersion: v1
kind: Service
metadata:
  name: oa
spec:
  selector:
    app: oa
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

5.执行k8s命令

复制代码
 kubectl apply -f aa.yaml
删除
kubectl delete -f aa.yaml
相关推荐
xxp432110 分钟前
Qt 网络编程 TCP通信
开发语言·qt
q***062912 分钟前
解决 Tomcat 跨域问题 - Tomcat 配置静态文件和 Java Web 服务(Spring MVC Springboot)同时允许跨域
java·前端·spring
还是鼠鼠21 分钟前
Redisson实现的分布式锁能解决主从一致性的问题吗?
java·数据库·redis·分布式·缓存·面试·redisson
d***956221 分钟前
windows配置永久路由
java
T***u33326 分钟前
PHP在电商中的会员管理
开发语言·wireshark·php·ue4·jina
张丶大帅34 分钟前
JS案例合集
开发语言·javascript·笔记
王桑.1 小时前
IO流高级流--转换流
java
2301_795167201 小时前
Python 高手编程系列八:缓存
开发语言·python·缓存
8***29312 小时前
Go基础之环境搭建
开发语言·后端·golang
qq_12498707532 小时前
基于SpringBoot技术的企业请假审批管理系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·信息可视化·毕业设计