mailpit/kubernetes:SMTP sendmail 无法连接-telnet 工作正常

mailpit/kubernetes:SMTP sendmail 无法连接-telnet 工作正常

kuberntes设置:

mailpit 服务 + 部署(入口工作,我可以连接到 UI)

---
apiVersion: v1
kind: Service
metadata:
  name: desideria-crm-mailpit
  namespace: desideria-crm
  labels:
    app: desideria-crm-mailpit
spec:
  ports:
    - port: 8025
      name: http-sv
      targetPort: http
      protocol: TCP
    - port: 1025
      name: smtp-sv-tcp
      targetPort: smtp-tcp
      protocol: TCP
  selector:
    app: desideria-crm-mailpit
  clusterIP: None

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: desideria-crm-mailpit
  namespace: desideria-crm
  labels:
    app: desideria-crm-mailpit
spec:
  replicas: 1
  selector:
    matchLabels:
      app: desideria-crm-mailpit
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: desideria-crm-mailpit
    spec:
      containers:
        - image: axllent/mailpit:v1.9.8
          name: desideria-crm-mailpit
          env:
            - name: MP_VERBOSE
              value: "true"         
          ports:
            - containerPort: 8025
              name: http
              protocol: TCP
            - containerPort: 1025
              name: smtp-tcp
              protocol: TCP

在 pod 上创建了一个文件 email.txt:

From: [email protected]
To: [email protected]
Subject: Email Subject

This is the body of the email.
It can contain multiple lines of text.

然后从 mailpit pod 上的 shell 中:

测试 #1 针对来自 mailpit pod 的 localhost

sendmail -v -w 5 -t -oLogLevel=1 -S localhost:1025 < email.txt

结果没问题,邮件显示在 mailpit UI 中

sendmail: recv:'220 desideria-crm-mailpit-7cfbdb55d7-krdcw Mailpit ESMTP Service ready'
sendmail: send:'EHLO desideria-crm-mailpit-7cfbdb55d7-krdcw'
sendmail: recv:'250-desideria-crm-mailpit-7cfbdb55d7-krdcw greets desideria-crm-mailpit-7cfbdb55d7-krdcw'
sendmail: recv:'250-SIZE 0'
sendmail: recv:'250 ENHANCEDSTATUSCODES'
sendmail: send:'MAIL FROM:<root@desideria-crm-mailpit-7cfbdb55d7-krdcw>'
sendmail: recv:'250 2.1.0 Ok'
sendmail: send:'RCPT TO:<[email protected]>'
sendmail: recv:'250 2.1.5 Ok'
sendmail: send:'DATA'
sendmail: recv:'354 Start mail input; end with <CR><LF>.<CR><LF>'
sendmail: send:'From: [email protected]'
sendmail: send:'To: [email protected]'
sendmail: send:'Subject: Email Subject'
sendmail: send:''
sendmail: send:'This is the body of the email.'
sendmail: send:'It can contain multiple lines of text.'
sendmail: send:'.'
sendmail: recv:'250 2.0.0 Ok: queued'
sendmail: send:'QUIT'
sendmail: recv:'221 2.0.0 desideria-crm-mailpit-7cfbdb55d7-krdcw Mailpit ESMTP Service closing transmission channel'

测试 #2 desideria-crm-mailpit(这次来自同一 kubernetes 命名空间内的另一个 pod)

sendmail -v -w 5 -t -oLogLevel=1 -S desideria-crm-mailpit:1025 < email.txt

5秒后超时,更高的超时也不起作用。

Alarm clock

测试 #3 desideria-crm-mailpit(使用来自同一命名空间内另一个 pod 的 Java 应用程序)

附注:

  • 这在本地 PC 上针对运行 mailpit docker 容器/无 kubernetes 的本地主机进行测试时有效)
  • 该应用程序还可与真正的 smtp 服务器身份验证等配合使用...
DEBUG: Jakarta Mail version 2.1.1
DEBUG: URL jar:file:/var/lib/jar/desideriacare-crm-api.jar!/BOOT-INF/lib/jakarta.mail-1.1.0.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/var/lib/jar/desideriacare-crm-api.jar!/BOOT-INF/lib/jakarta.mail-1.1.0.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=jakarta.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.pop3.POP3Store=jakarta.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.sun.mail.imap.IMAPSSLStore=jakarta.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.smtp.SMTPSSLTransport=jakarta.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.pop3.POP3SSLStore=jakarta.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=jakarta.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]}
DEBUG: Providers Listed By Protocol: {imap=jakarta.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtp=jakarta.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], pop3=jakarta.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], imaps=jakarta.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], smtps=jakarta.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3s=jakarta.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: URL jar:file:/var/lib/jar/desideriacare-crm-api.jar!/BOOT-INF/lib/jakarta.mail-1.1.0.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/var/lib/jar/desideriacare-crm-api.jar!/BOOT-INF/lib/jakarta.mail-1.1.0.jar!/META-INF/javamail.address.map
DEBUG: getProvider() returning jakarta.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "desideria-crm-mailpit", port 1025, isSSL false
DEBUG SMTP: exception reading response, THROW: 
java.net.SocketTimeoutException: Read timed out
    at java.base/sun.nio.ch.NioSocketImpl.timedRead(NioSocketImpl.java:280)
    at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:306)
..... shortened

测试 #4 desideria-crm-mailpit(使用与测试 #2 相同的 pod 的 telnet)

telnet desideria-crm-mailpit 1025

结果有效-但是为什么呢?

Connected to desideria-crm-mailpit
EHLO testingstuff         
220 desideria-crm-mailpit-7cfbdb55d7-krdcw Mailpit ESMTP Service ready
250-desideria-crm-mailpit-7cfbdb55d7-krdcw greets testingstuff
250-SIZE 0
250 ENHANCEDSTATUSCODES
MAIL FROM:<[email protected]>
250 2.1.0 Ok
RCPT TO:<[email protected]>
250 2.1.5 Ok
DATA
354 Start mail input; end with <CR><LF>.<CR><LF>
From: [email protected]
To: [email protected]
Subject: Email Subject

r1
r2
last row
.
250 2.0.0 Ok: queued

概括

  • 从 mailpit pod 发送邮件可以自行运行
  • 发送邮件来自测试舱A不起作用
  • testpod B 中的 java 应用程序无法运行
  • telnet 来自测试舱A确实有效!!
  • ping desideria-crm-mailpit/IP 可从任意 pod 运行。
  • nslookup 检索正确的 pod 的 IP
  • 还尝试了 mailhog pod 而不是 mailpit。结果相同。

与 telnet 相比,sendmail/java 应用程序可能有哪些不同?

使用 pods/service IP 而不是服务名称desideria-crm-mailpit我得到了相同的结果。

我真的很困惑,因为像 mariadb/mongodb 这样的数据库服务除了端口之外以相同的方式设置,可以从 java 应用程序访问并且运行良好。

答案1

事实证明,问题出在 istio 上。一旦不再注入 sidecar,一切就会按预期运行。

对于我来说,使用 istio sidecar 需要做以下调整:

添加到服务 smtp 端口:

appProtocol: tcp

或者

appProtocol: tls

最终服务清单

---
apiVersion: v1
kind: Service
metadata:
  name: desideria-crm-mailpit
  namespace: desideria-crm
  labels:
    app: desideria-crm-mailpit
spec:
  ports:
    - port: 8025
      name: http-sv
      targetPort: http
      protocol: TCP
    - port: 1025
      name: smtp-sv-tcp
      targetPort: smtp-tcp
      protocol: TCP
      appProtocol: tcp # IMPORTANT for mailpit SMTP
  selector:
    app: desideria-crm-mailpit
  clusterIP: None

相关内容