kubernetes之部署war项目(三)

k8s系列 源自我工作上的实际场景,记录于此。
k8s部署springboot项目
假设我的jar项目是:myjar.jar

用到的环境

centos 7.9 + docker + docker harbor + k8s

1、编写Dockerfile

harbor 是写在hosts中的域名

FROM harbor/openjdk:8-jdk-alpine
ADD *.jar app.jar
EXPOSE 8097
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Duser.timezone=GMT+08","-jar","/app.jar"]

将jar与Dockerfile放在同一文件夹
执行构建镜像:

docker build -t harbor/myjar:2021102101 .

推送到私服

docker push harbor/myjar:2021102101

导出镜像

docker save harbor/myjar:2021102101 | gzip > myjar-2021102101.tar.gz

2、编写yaml

编写k8s的yaml,myjar.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: myjar
  name: myjar-service
spec:
  ports:
  - port: 8097
    protocol: TCP
    # 指向dockerfile暴露的端口
    targetPort: 8097
  selector:
    app: myjar
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: myjar
  managedFields:
  name: myjar-deployment
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: myjar
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: myjar
    spec:
      containers:
      # 需要注意修改对应的镜像
      - image: harbor/myjar:2021102101
        imagePullPolicy: IfNotPresent
        name: myjar
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 10m
            memory: 256Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /application.yml
          name: application-yml
          subPath: application.yml
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccount: default
      serviceAccountName: default
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          defaultMode: 420
          name: myjar-configmap
        name: application-yml
---
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: myjar-configmap
  name: myjar-configmap
# 配置application.yml,注意根据实际情况修改数据DNS库域名、端口和数据库的名称
data:
  application.yml: |
    DB_DOMAIN: 10.8.6.125
    DB_PORT: 3306
    DB_NAME: temp
    TOKEN_STORE_STYLE: memory # memory | redis | db

    AUTH-CENTER: http://10.8.6.125:8070
    BASEDIR: /data/${spring.application.name}
    
    server:
      port: 8097
      tomcat:
        basedir: ${BASEDIR}/tmp

    spring:
      application:
        name: preometheus-console
      main:
        allow-bean-definition-overriding: true
      datasource:
        name: druidDatasource
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://${DB_DOMAIN}:${DB_PORT}/${DB_NAME}?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
          username: root
          password: root
          min-idle: 1
          max-active: 100
          max-open-prepared-statements: 50
          max-pool-prepared-statement-per-connection-size: 20
          max-wait: 60000
          min-evictable-idle-time-millis: 300000
          pool-prepared-statements: true
          test-while-idle: true
          time-between-eviction-runs-millis: 60000
          validation-query: select 'x'
      jpa:
        hibernate:
          ddl-auto: none
        properties:
          hibernate:
            # dialect: org.hibernate.dialect.Oracle12cDialect (数据库方言,oracle时使用)
            # org.hibernate.dialect.PostgreSQL9Dialect (opengauss数据库使用方言)
            dialect: org.hibernate.dialect.MySQL8Dialect
        show-sql: false
        open-in-view: false
      messages:
        cache-duration: 3600
        encoding: UTF-8
      cloud:
        inetutils:
          timeoutSeconds: 30
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8

    #  redis:
    #      host: localhost
    #      port: 6379


    #policy:
    #  password:
    #    update:
    #      userStatus: false

    security:
      oauth2:
        resource:
          user-info-uri: ${AUTH-CENTER}/current
          
    prometheus:
      prometheus-config-name: alerts.yml
      alertmanager-config-name: alertmanager.yml
      variables:
        config:
          - name: "cluster_id"
            zhName: "集群"
            promQL: "label_values(jvm_memory_used_bytes,cluster_id)"
            isMulti: false
            sort: 1
          - name: "instance"
            zhName: "节点"
            promQL: "label_values(jvm_memory_used_bytes{cluster_id=~'$cluster_id'},instance)"
            isMulti: false
            sort: 2
          - name: "service_name"
            zhName: "服务"
            promQL: "label_values(agree_afa_engine_runtime_milliseconds{cluster_id=~'$cluster_id'},service_name)"
            isMulti: false
            sort: 3
          - name: "scene_name"
            zhName: "场景"
            promQL: "label_values(agree_afa_engine_svc_scene_executed_milliseconds{service_name=~'$service_name'},scene_name)"
            isMulti: false
            sort: 4
    mail:
      # smtp服务器地址
      host: smtp.163.com
      # smtp服务器端口
      port: 25
      # smtp是否使用ssl
      smtp-ssl-enable: false
      # 邮箱名
      mail-username:
      # 邮箱密码
      mail-password:
      mail-debug: false
      is-open: false  #是否开启邮件

    logging.file: ${BASEDIR}/logs/mnt.log
    logging.file.max-size: 10MB
    logging.file.max-history: 15 # 默认保存15天的日志
    logging.pattern.file: '%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'

注意修改上面的数据库连接账号密码
部署k8s:

kubectl create -f myjar.yaml

查看项目部署日志

kubectl get pod

# 查看对应的pod
kubectl logs myjar-deployment-679bfdcbfc-sk4pc

到此部署完成,查看对外开放的端口

kubectl get svc

在这里插入图片描述

访问:http://10.8.4.181:31186/actuator/info
效果:
在这里插入图片描述
至此,部署完毕!
删除对应的pod、service等:

kubectl delete configmaps myjar-configmap -n
kubectl delete deployments.apps myjar-deployment
kubectl delete svc myjar-service

觉得对你有帮助,点个赞再走吧 ~

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐