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

本文涉及的产品
容器镜像服务 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应用,也适合调试第三方的应用,希望能帮助到大家,谢谢大家的阅读,本文完!

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
3月前
|
Java
轻松上手Java字节码编辑:IDEA插件VisualClassBytes全方位解析
本插件VisualClassBytes可修改class字节码,包括class信息、字段信息、内部类,常量池和方法等。
232 6
|
14天前
|
存储 安全 算法
Java容器及其常用方法汇总
Java Collections框架提供了丰富的接口和实现类,用于管理和操作集合数据。
Java容器及其常用方法汇总
|
1月前
|
Kubernetes Linux 虚拟化
入门级容器技术解析:Docker和K8s的区别与关系
本文介绍了容器技术的发展历程及其重要组成部分Docker和Kubernetes。从传统物理机到虚拟机,再到容器化,每一步都旨在更高效地利用服务器资源并简化应用部署。容器技术通过隔离环境、减少依赖冲突和提高可移植性,解决了传统部署方式中的诸多问题。Docker作为容器化平台,专注于创建和管理容器;而Kubernetes则是一个强大的容器编排系统,用于自动化部署、扩展和管理容器化应用。两者相辅相成,共同推动了现代云原生应用的快速发展。
249 11
|
2月前
|
Prometheus Kubernetes 监控
OpenAI故障复盘 - 阿里云容器服务与可观测产品如何保障大规模K8s集群稳定性
聚焦近日OpenAI的大规模K8s集群故障,介绍阿里云容器服务与可观测团队在大规模K8s场景下我们的建设与沉淀。以及分享对类似故障问题的应对方案:包括在K8s和Prometheus的高可用架构设计方面、事前事后的稳定性保障体系方面。
|
3月前
|
存储 安全 Java
Java多线程编程中的并发容器:深入解析与实战应用####
在本文中,我们将探讨Java多线程编程中的一个核心话题——并发容器。不同于传统单一线程环境下的数据结构,并发容器专为多线程场景设计,确保数据访问的线程安全性和高效性。我们将从基础概念出发,逐步深入到`java.util.concurrent`包下的核心并发容器实现,如`ConcurrentHashMap`、`CopyOnWriteArrayList`以及`BlockingQueue`等,通过实例代码演示其使用方法,并分析它们背后的设计原理与适用场景。无论你是Java并发编程的初学者还是希望深化理解的开发者,本文都将为你提供有价值的见解与实践指导。 --- ####
|
3月前
|
运维 Kubernetes Shell
【赵渝强老师】K8s中Pod的临时容器
Pod 是 Kubernetes 中的基本调度单位,由一个或多个容器组成,包括业务容器、基础容器、初始化容器和临时容器。临时容器用于故障排查和性能诊断,不适用于构建应用程序。当 Pod 中的容器异常退出或容器镜像不包含调试工具时,临时容器非常有用。文中通过示例展示了如何使用 `kubectl debug` 命令创建临时容器进行调试。
|
3月前
|
Kubernetes 调度 容器
【赵渝强老师】K8s中Pod中的业务容器
Pod 是 Kubernetes 中的基本调度单元,由一个或多个容器组成。除了业务容器,Pod 还包括基础容器、初始化容器和临时容器。本文通过示例介绍如何创建包含业务容器的 Pod,并提供了一个视频讲解。示例中创建了一个名为 "busybox-container" 的业务容器,并使用 `kubectl create -f firstpod.yaml` 命令部署 Pod。
|
3月前
|
Kubernetes 容器 Perl
【赵渝强老师】K8s中Pod中的初始化容器
Kubernetes的Pod包含业务容器、基础容器、初始化容器和临时容器。初始化容器在业务容器前运行,用于执行必要的初始化任务。本文介绍了初始化容器的作用、配置方法及优势,并提供了一个示例。
|
4月前
|
存储 缓存 Java
java基础:IO流 理论与代码示例(详解、idea设置统一utf-8编码问题)
这篇文章详细介绍了Java中的IO流,包括字符与字节的概念、编码格式、File类的使用、IO流的分类和原理,以及通过代码示例展示了各种流的应用,如节点流、处理流、缓存流、转换流、对象流和随机访问文件流。同时,还探讨了IDEA中设置项目编码格式的方法,以及如何处理序列化和反序列化问题。
142 1
java基础:IO流 理论与代码示例(详解、idea设置统一utf-8编码问题)
|
3月前
|
存储 运维 Kubernetes
云端迁移:备份中心助力企业跨云迁移K8s容器服务平台
本文将简要介绍阿里云容器服务ACK的备份中心,并以某科技公司在其实际的迁移过程中遇到具体挑战为例,阐述如何有效地利用备份中心来助力企业的容器服务平台迁移项目。

热门文章

最新文章

相关产品

  • 容器服务Kubernetes版