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

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 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应用,也适合调试第三方的应用,希望能帮助到大家,谢谢大家的阅读,本文完!

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
3天前
|
Kubernetes Java Docker
使用Kubernetes和Docker部署Java微服务
使用Kubernetes和Docker部署Java微服务
|
3天前
|
jenkins 持续交付 开发者
利用Docker容器化部署应用的实战指南
【6月更文挑战第27天】本文详述Docker应用部署,涵盖Docker基本概念、安装、镜像制作及运行。通过编写Dockerfile构建镜像,使用`docker build`、`run`、`push`及`stop`命令管理。集成CI/CD工具如Jenkins,实现自动化构建、测试和部署,提升开发效率与部署质量。Docker助力轻量级、可移植的微服务架构。
|
6天前
|
存储 运维 Kubernetes
容器化技术在现代运维中的应用与挑战
【6月更文挑战第23天】随着云计算技术的不断进步,容器化技术已经成为现代运维的重要组成部分。它以其独特的轻量级、可移植性和易于管理的特性,为运维工作带来了革命性的变化。本文将深入探讨容器化技术的核心概念、优势以及在实际应用中遇到的挑战,同时提供应对这些挑战的策略和建议,帮助运维团队更有效地利用容器化技术提升运维效率。
|
4天前
|
XML Java 数据格式
经验大分享:Spring基础篇——Spring容器和应用上下文理解
经验大分享:Spring基础篇——Spring容器和应用上下文理解
|
9天前
|
存储 运维 监控
容器化技术在现代运维中的应用与挑战
【6月更文挑战第20天】随着技术的迭代和云计算的普及,容器化技术已经成为现代运维不可或缺的一部分。本文将深入探讨容器化技术的核心优势、面临的主要挑战以及在企业运维中的实际应用案例,旨在为运维人员提供容器化部署和维护的实用指南。
|
11天前
|
Kubernetes 安全 Cloud Native
云上攻防-云原生篇&Kubernetes&K8s安全&API&Kubelet未授权访问&容器执行
云上攻防-云原生篇&Kubernetes&K8s安全&API&Kubelet未授权访问&容器执行
|
2天前
|
Kubernetes 监控 Java
阿里云云效产品使用问题之进行Kubernetes分批发布时应用名被更改,是什么导致的
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
2天前
|
Kubernetes 安全 测试技术
超大规模商用 K8s 场景下,阿里巴巴如何动态解决容器资源的按需分配问题?
超大规模商用 K8s 场景下,阿里巴巴如何动态解决容器资源的按需分配问题?
|
2天前
|
运维 Serverless 文件存储
函数计算产品使用问题之在利用Docker镜像部署应用时,容器内的应用如何能访问函数计算配置的NAS挂载
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
3天前
|
Java 程序员 容器
老程序员分享:java容器体系(三)
老程序员分享:java容器体系(三)

相关产品

  • 容器服务Kubernetes版