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应用,也适合调试第三方的应用,希望能帮助到大家,谢谢大家的阅读,本文完!

Logo

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

更多推荐