build基础镜像到K8s部署微服务实战篇

使用Centos7系统安装jdk启动 jar包程序

  • 首先做centos和jdk的基础镜像
# cat  dockerfile #后续可使用在线下载的方式安装jdk
FROM centos:7
MAINTAINER Mr.Zhao  Centos7 To Jdk1.8.0
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& yum install kde-l10n-Chinese -y \
&& localedef -c -f UTF-8 -i zh_CN zh_CN.UFT-8 \
&& echo 'LANG="zh_CN.uft8"' > /etc/locale.conf \
&& source /etc/locale.conf

ADD jdk-8u161-linux-x64.tar.gz  /usr/local/
RUN rm -rf jdk-8u161-linux-x64.tar.gz

ENV JAVA_HOME=/usr/local/jdk1.8.0_161
ENV JRE_HOME=/usr/local/jdk1.8.0_161/jre
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
ENV PATH $PATH:$JAVA_HOME/bin:$JRE_HOME/bin

  • 使用使用基础jdk镜像启动jar包
cat  Dockerfile #配置文件可后期优化
FROM centostest/jdk:0.1.0
LABEL maintainer="Test Jdk app Image "

COPY ccod-bills-admin-1.0.0.jar /root/admin/ccod-bills-admin-1.0.0.jar
COPY application.properties  /root/admin/application.properties
COPY startup.sh /root/admin/startup.sh
RUN chmod +x /root/admin/startup.sh

EXPOSE 9001
WORKDIR /root/admin
ENTRYPOINT ["java","-jar","/root/admin/ccod-bills-admin-1.0.0.jar"]

  • dockerbuild镜像然后run

docker  build  -t="billsa/admin:0.1.0" .
 docker run  -it  -d -p 9001:9001  --name billsa-test2   billsa/admin:0.1.0  /bin/bash


  • 验证访问宿主机的端口


    image.png

K8s运行基础镜像和jar包

  • 需要把启动的jar包打包到镜像里面,配置文件未打包在里面,是通过后期k8s configmap的方式挂载进去
[root@kubernetes-node2 time-manager]# more Dockerfile
FROM centostest/jdk:0.1.0
MAINTAINER Build Time Manger Jar package

ADD timeManager.jar /root/jar/

  • build镜像然后推送到docker hub里面
 docker build  -t="xxxx.io:1180/jar/timemanager:0.1.0"  .

docker push  xxx.io:1180/jar/timemanager:0.1.0

  • 然后定义资源清单 timemanager.yaml
kind: ConfigMap
apiVersion: v1
metadata:
  name: time-manager-conf
  namespace: jar
data:
  init.sh: |
    cd /root
    cat /cfg/application.yml > /root/application.yml
    java -jar /root/timeManager.jar
  application.yml: |
    server:
      port: 8089
      servlet:
        context-path: /timeManage
    spring:
      main:
        allow-bean-definition-overriding: true
      datasource:
        url: jdbc:mysql://mysql/time-manager?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
        username: ucds
        password: ccodccod
        type: com.alibaba.druid.pool.DruidDataSource #druid连接池
        maxActive: 20 #最大活跃数
        initialSize: 5 #初始化数量
        maxWait: 60000 #最大连接等待超时时间
        poolPreparedStatements: true #打开PSCache,并且指定每个连接PSCache的大小
        maxPoolPreparedStatementPerConnectionSize: 20
        #通过connectionProperties属性来打开mergeSql功能;慢SQL记录
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
        minIdle: 5
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 1 from dual
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
        filters: stat, wall, logback
      resources:
          staticLocations: classpath:static
    mybatis:
      mapperLocations: classpath:/mapper/*.xml
      configuration:
        map-underscore-to-camel-case: true  #开启驼峰命名
    management:
      endpoints:
        web:
          base-path: /  #Spring Boot 2.0的端点基础路径由“/”调整到”/actuator”下,如:/info调整为/actuator/info,可以通过以下配置改为和旧版本一致
      info:
        git:
          mode: simple  #项目git版本信息展示 full 展示信息多 simple 展示信息少
    info:
      app:
        version: TIME-MANAGER v0.1  #指定的系统版本,根据需要制定
    #logback日志配置
    logging:
      config: classpath:logback-mess.xml
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: time-manager
  name: time-manager
  namespace: jar
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      run: time-manager
  template:
    metadata:
      labels:
        run: time-manager
    spec:
      nodeSelector:
        nodes: jar
      containers:
      - command:
        - /bin/sh
        - /cfg/init.sh
        image: xxxx.io:1180/jar/dtimemanager:0.1.0
        imagePullPolicy: IfNotPresent
        name: time-manager
        ports:
        - containerPort: 8089
          protocol: TCP
        volumeMounts:
        - mountPath: /cfg
          name: cfg 
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      terminationGracePeriodSeconds: 30
      volumes:
      - name: cfg
        configMap:
          name: time-manager-conf
---
apiVersion: v1
kind: Service
metadata:
  name: time-manager
  namespace: jar
spec:
  ports:
  - port: 9987
    protocol: TCP
    targetPort: 8089
  selector:
    run: time-manager

  • 定义需要连接外部Mysql资源清单
kind: Endpoints
apiVersion: v1
metadata:
  name: mysql
  namespace: jar
subsets:
  - addresses:
      - ip: xxx.xxx.xxx.xxx
    ports:
      - port: 3306
---
kind: Service
apiVersion: v1
metadata:
  name: mysql
  namespace: jar
spec:
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306 
      
  • 定义需要对外提供访问入口ingress清单,使用PathPrefix路径前缀的方式进行匹配
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: time-mager
  namespace: jar
  annotations:
    traefik.ingress.kubernetes.io/rule-type: PathPrefix
spec:
  rules:
  - host: xxxx.xxxx.com
    http:
      paths:
      - path: /timeManage/
        backend:
         serviceName: time-manager
         servicePort: 9987

  • 在应用这些清单时,则需要单独使用名称空间和node标签

    • 上面清单里面使用的是jar的名称空间
    • node标签使用的是nodes=jar
  • 然后在apply全部清单

kubectl apply -f ./

你可能感兴趣的:(build基础镜像到K8s部署微服务实战篇)