当前位置: 首页 > news >正文

Kubernetes学习笔记-项目简单部署

继续之前的移除Nacos迁移至K8s,现在把项目服务简单的部署到Kubernetes上面。

  • 项目服务配置

    • Kubernetes的ConfigMap有以下几种创建方式:
      • kubectl create configmap special-config --from-literal=special.k1=v1 --from-literal=special.k2=v2
      • kubectl create configmap special-config --from-file=special-config.properties
      • kubectl apply -f special-config.yaml
    • 这里使用第三种方式创建ConfigMap。
      • 根据项目服务的需要新建common-config.yaml、service-config.yaml两个ConfigMap文件,文件内容见下方。
      • kubectl apply -f common-config.yaml
      • kubectl apply -f service-config.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: common-config
      namespace: default
    data:
      common-config.yml: |
        spring:
          main:
            allow-bean-definition-overriding: true
          mvc:
            throw-exception-if-no-handler-found: true
          messages:
            basename: i18n/messages
          datasource:
            driver-class-name: com.mysql.cj.jdbc.Driver
            type: com.zaxxer.hikari.HikariDataSource
            hikari:
              connection-timeout: 10000
              validation-timeout: 10000
              idle-timeout: 30000
              max-lifetime: 60000
              maximum-pool-size: 20
              minimum-idle: 10
              read-only: false
              connection-test-query: SELECT 1 FROM DUAL
    
        server:
          undertow:
            threads:
              io: 4
              worker: 32
            buffer-size: 1024
            direct-buffers: true
            max-http-post-size: 10485760
            max-http-header-size: 16384
          compression:
            enabled: true
            mime-types:
              - application/json
              - application/xml
              - application/javascript
              - text/html
              - text/xml
              - text/plain
              - text/javascript
              - text/css
            min-response-size: 1024
    
        management:
          endpoints:
            enabled-by-default: true
            web:
              exposure:
                include: health
            shutdown:
              enabled: true
    
        mybatis-plus:
          mapper-locations: classpath*:mapper/**/*.xml
          configuration:
            log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: service-config
      namespace: default
    data:
      service-config.yml: |
        spring:
          profiles:
            active: dev
          application:
            name: demo-service
          datasource:
            url: jdbc:mysql://192.168.0.1:3306/demo-service?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
            username: root
            password: 123456
    
        server:
          port: 18080
          servlet:
            context-path: /demo-service
    
  • 项目服务打包镜像

    • 项目打包:mvn clean -X -U package -DskipTests
    • 创建Dockerfile文件,简单内容见下方
    • 构建镜像:docker build -t swr.cn-east-3.myhuaweicloud.com/develop/demo-service:latest .
    • 上传镜像:docker push swr.cn-east-3.myhuaweicloud.com/develop/demo-service:latest
    FROM swr.cn-east-3.huaweicloud.com/develop/java:jdk1.8_jmx
    
    EXPOSE 18080
    EXPOSE 18081
    
    ENV DEF_JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom -Duser.timezone=Asia/Shanghai -Dfile.encoding=utf-8 -Dhttps.protocols=TLSv1.2"
    ENV APP_JAR_PATH=app.jar
    
    ADD target/*.jar /app.jar
    
    ENTRYPOINT ["sh", "-c", "java ${DEF_JAVA_OPTS} -jar /app.jar"]
    
  • 项目服务部署

    • 创建管理应用Pod。
      • 新建Deployment.yaml文件,简单内容见下方。
      • 执行命令:kubectl apply -f Deployment.yaml
      • 查看Pod状态:kubectl get pods
      • 查看Pod日志:kubectl logs
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: demo-service
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: demo-service
        template:
          metadata:
            labels:
              app: demo-service
          spec:
            imagePullSecrets:
              - name: imagesecret
            containers:
              - name: demo-service
                image: swr.cn-east-3.myhuaweicloud.com/develop/demo-service:latest
                imagePullPolicy: Always
                ports:
                  - containerPort: 18080
      
    • 可以为动态变化的Pod提供稳定的访问入口,实现服务发现和负载均衡。
      • 新建Service.yaml文件,简单内容见下方。
      • 执行命令:kubectl apply -f Service.yaml
      • 查看Service信息:kubectl get svc
      • 访问项目服务:http://192.168.0.1:32749/demo-service/doc.html
      apiVersion: v1
      kind: Service
      metadata:
        name: demo-service
      spec:
        selector:
          app: demo-service
        ports:
          - protocol: TCP
            port: 18080
            targetPort: 18080
        type: NodePort
      

相关文章:

  • C语言复习笔记--数组
  • 网络编程之解除udp判断客户端是否断开
  • 调研报告:Hadoop 3.x Ozone 全景解析
  • 网络安全设备配置与管理-实验4-防火墙AAA服务配置
  • 仿新浪微博typecho主题源码
  • VulnHub-Web-Machine-N7通关攻略
  • 【DeepSeek学C++】移动构造函数
  • html5-qrcode前端打开摄像头扫描二维码功能
  • 【嵌入式学习】时钟 - 边缘触发锁存器
  • C# 零基础入门篇(19.DateTime 使用指南)
  • 【动态规划篇】91. 解码方法
  • Arduino示例代码讲解:Pitch follower 跟随
  • 舞狮表演(dp)
  • 基于32单片机的无人机直流电机闭环调速系统设计
  • xpath轴
  • git 子模块的使用
  • EMQX安装与配置
  • java项目之基于ssm的疫苗预约系统(源码+文档)
  • 基于分类算法的学习失败预警(上)
  • 力扣热题100(方便自己复习,自用)
  • 商务部新闻发言人就出口管制管控名单答记者问
  • 微软将裁员3%,减少管理层
  • 阿尔巴尼亚执政党连续第四次赢得议会选举,反对党此前雇用特朗普竞选经理
  • 河南信阳拟发文严控预售许可条件:新出让土地开发的商品房一律现房销售
  • 来伊份发布关于消费者反映蜜枣粽问题处理的情况说明:与消费者达成和解
  • 广东省人大教科文卫委原主任委员梁万里被开除党籍:退休后受贿仍不知止