Kubernetes 设置:
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 中:
来自 mailpit pod 的针对 localhost 的测试 #1
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 应用程序)
旁注:
- 当在本地电脑上针对运行 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 发送邮件 它可以自行工作
- 来自testpod A的 sendmail不起作用
- testpod B 中的 java 应用程序无法运行
- 来自testpod A的 telnet确实有效!
- ping
desideria-crm-mailpit
/ IP 可在任何 pod 上使用。 - nslookup 检索正确的 pod 的 IP
- 还尝试了 mailhog pod 而不是 mailpit。相同的结果。
与 telnet 相比,sendmail/java 应用程序可能会做哪些不同的事情?
使用 Pod/服务 IP 而不是服务名称,desideria-crm-mailpit
我得到相同的结果。
我真的很困惑,因为像 mariadb/mongodb 这样的数据库服务除了可以从 java 应用程序访问端口之外以相同的方式设置并且工作得很好。
事实证明 istio 导致了这个问题。一旦 sidecar 不再被注入。一切都按预期进行。
对于我的情况,使用 istio sidecar 必须进行以下调整:
添加到服务 smtp 端口:
或者
最终服务清单