IDEA远程调试k8s容器里面的Java应用

简介: IDEA远程调试k8s容器里面的Java应用

01 调试步骤

要远程调试运行在 Kubernetes 容器中的 Java 应用,可以使用以下步骤:

step1: 在部署容器的 Kubernetes YAML 文件中,为容器添加远程调试的 JVM 参数,如:

env:
- name: JAVA_TOOL_OPTIONS
  value: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

step2: 在容器内运行的 Java 应用启动时,会自动开启一个监听 5005 端口的调试服务,可以使用远程调试工具(如 IntelliJ IDEA、Eclipse 等)连接到该端口进行调试。

step3: 如果应用运行在 Kubernetes 集群内部,则可以使用 Kubernetes 的端口转发功能,将容器内部的调试端口转发到本地端口上,如:

kubectl port-forward pod-name 5005:5005

其中,pod-name 是要进行调试的容器名称或 ID。

step4: 在本地的远程调试工具中,设置连接到转发后的本地端口进行调试。

注意:在进行远程调试时,需要注意容器内部和外部的 IP 地址和端口的映射关系,以及安全性等问题

02 案例配置

2.1 deployment配置

某应用deployment完整配置文件(有3个容器,其中appmanager容器的debugger端口为8888gateway容器的的debugger端口为8899):

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "8"
    meta.helm.sh/release-name: vvp
    meta.helm.sh/release-namespace: vvp
  generation: 8
  labels:
    app: vvp-ververica-platform
  name: vvp-ververica-platform
  namespace: vvp
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: vvp-ververica-platform
      component: ververica-platform
      release: vvp
      system: ververica-platform
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: vvp-ververica-platform
        component: ververica-platform
        release: vvp
        system: ververica-platform
    spec:
      containers:
      - env:
        - name: spring.profiles.active
          value: prod,prod-user
        - name: spring.config.additional-location
          value: file:/vvp/etc/
        - name: JAVA_TOOL_OPTIONS
          value: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8888
        image: registry.ververica.com/v2.9/vvp-appmanager:2.9.1
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /actuator/health
            port: management
            scheme: HTTP
          initialDelaySeconds: 90
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 10
        name: appmanager
        ports:
        - containerPort: 9080
          name: http
          protocol: TCP
        - containerPort: 9081
          name: management
          protocol: TCP
        - containerPort: 8888
          name: debug
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /actuator/health
            port: management
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 10
        resources:
          limits:
            cpu: "1"
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 1Gi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /vvp/etc
          name: config
          readOnly: true
        - mountPath: /vvp/secrets/blob-storage-creds
          name: blob-storage-creds
          readOnly: true
        - mountPath: /vvp/data
          name: data
      - env:
        - name: spring.profiles.active
          value: prod,prod-defaults,prod-user
        - name: spring.config.additional-location
          value: file:/vvp/etc/
        - name: JAVA_TOOL_OPTIONS
          value: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8899
        image: registry.ververica.com/v2.9/vvp-gateway:2.9.1
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /actuator/health
            port: management
            scheme: HTTP
          initialDelaySeconds: 90
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        name: gateway
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
        - containerPort: 8081
          name: management
          protocol: TCP
        - containerPort: 8899
          name: debug2
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /actuator/health
            port: management
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          limits:
            cpu: "1"
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 1Gi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /vvp/etc
          name: config
          readOnly: true
        - mountPath: /vvp/secrets/blob-storage-creds
          name: blob-storage-creds
          readOnly: true
        - mountPath: /vvp/secrets/saml-creds
          name: saml-creds
          readOnly: true
        - mountPath: /vvp/data
          name: data
      - image: registry.ververica.com/v2.9/vvp-ui:2.9.1
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: http
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        name: ui
        ports:
        - containerPort: 4200
          name: http
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: http
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          limits:
            cpu: 100m
            memory: 32Mi
          requests:
            cpu: 100m
            memory: 32Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        fsGroup: 999
      serviceAccount: vvp-ververica-platform
      serviceAccountName: vvp-ververica-platform
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          defaultMode: 420
          name: vvp-ververica-platform-config
        name: config
      - name: blob-storage-creds
        secret:
          defaultMode: 420
          secretName: vvp-ververica-platform-blob-storage-credentials
      - emptyDir: {}
        name: saml-creds
      - name: data
        persistentVolumeClaim:
          claimName: vvp-ververica-platform

2.2 svc配置

配置svc文件,使得集群外都可以访问:

apiVersion: v1
kind: Service
metadata:
  annotations:
    meta.helm.sh/release-name: vvp
    meta.helm.sh/release-namespace: vvp
  labels:
    app: vvp-ververica-platform
    app.kubernetes.io/managed-by: Helm
    chart: ververica-platform-5.5.1
    component: ververica-platform
    release: vvp
    system: ververica-platform
  name: vvp-ververica-platform
  namespace: vvp
spec:
  clusterIP: 10.109.161.91
  clusterIPs:
  - 10.109.161.91
  externalTrafficPolicy: Cluster
  internalTrafficPolicy: Cluster
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - name: http
    nodePort: 32621
    port: 80
    protocol: TCP
    targetPort: 8080
  - name: debug
    nodePort: 31449
    port: 8888
    protocol: TCP
    targetPort: 5005
  - name: debug2
    nodePort: 31822
    port: 8899
    protocol: TCP
    targetPort: 8899
  selector:
    app: vvp-ververica-platform
    component: ververica-platform
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer:
    ingress:
    - hostname: localhost

2.3 idea配置

在这里插入图片描述
在这里插入图片描述
开始调试:
在这里插入图片描述

03 文末

本文主要讲解如何在本地远程调试k8s容器里面的Java应用,也适合调试第三方的应用,希望能帮助到大家,谢谢大家的阅读,本文完!

相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
8月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
1140 108
|
8月前
|
运维 监控 数据可视化
小白也能部署应用,3个免费的容器化部署工具测评
本文对比了三款容器化部署工具:Docker Compose、Portainer 和 Websoft9。Docker Compose 适合开发者编排多容器应用,Portainer 提供图形化管理界面,而 Websoft9 则面向中小企业和非技术人员,提供一键部署与全流程运维支持,真正实现“开箱即用”。三款工具各有定位,Websoft9 更贴近大众用户需求。
小白也能部署应用,3个免费的容器化部署工具测评
|
6月前
|
监控 Kubernetes 安全
还没搞懂Docker? Docker容器技术实战指南 ! 从入门到企业级应用 !
蒋星熠Jaxonic,技术探索者,以代码为笔,在二进制星河中书写极客诗篇。专注Docker与容器化实践,分享从入门到企业级应用的深度经验,助力开发者乘风破浪,驶向云原生新世界。
706 51
还没搞懂Docker? Docker容器技术实战指南 ! 从入门到企业级应用 !
|
9月前
|
存储 监控 Java
如何对迁移到Docker容器中的应用进行性能优化?
如何对迁移到Docker容器中的应用进行性能优化?
564 59
|
10月前
|
数据可视化 API UED
126. [HarmonyOS NEXT 实战案例二:SideBarContainer] 侧边栏容器实战:电商应用商品筛选侧边栏 进阶篇
在基础篇中,我们已经实现了电商应用商品筛选侧边栏的基本布局和功能。在本篇教程中,我们将深入探讨如何通过状态管理和数据绑定,实现更加复杂的交互功能,提升用户体验。
155 2
126. [HarmonyOS NEXT 实战案例二:SideBarContainer] 侧边栏容器实战:电商应用商品筛选侧边栏 进阶篇
|
10月前
|
UED 容器
125.[HarmonyOS NEXT 实战案例二:SideBarContainer] 侧边栏容器实战:电商应用商品筛选侧边栏 基础篇
在现代电商应用中,商品筛选功能是提升用户购物体验的关键元素。HarmonyOS NEXT提供的`SideBarContainer`组件非常适合实现这类功能,它可以创建一个可显示和隐藏的侧边栏,用于放置各种筛选条件,帮助用户快速找到心仪的商品。
202 1
125.[HarmonyOS NEXT 实战案例二:SideBarContainer] 侧边栏容器实战:电商应用商品筛选侧边栏 基础篇
|
9月前
|
缓存 Java Docker
如何对应用代码进行优化以提高在Docker容器中的性能?
如何对应用代码进行优化以提高在Docker容器中的性能?
407 1
|
9月前
|
存储 监控 测试技术
如何将现有的应用程序迁移到Docker容器中?
如何将现有的应用程序迁移到Docker容器中?
671 57

相关产品

  • 容器服务Kubernetes版
  • 推荐镜像

    更多