k8s部署需要设置哪些端口?



我不明白如何为k8s部署正确配置端口。

假设有一个nextJS应用程序监听端口3003(默认为3000)。我构建docker映像:

FROM node:16.14.0
RUN apk add dumb-init
# ...
EXPOSE 3003
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD npx next start -p 3003

所以在这个Dockerfile中有两个地方定义了端口值3003。这有必要吗?

然后我定义这个k8s manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
name: example
spec:
spec:
containers:
- name: example
image: "hub.domain.com/example:1.0.0"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3003
---
apiVersion: v1
kind: Service
metadata:
name: example
spec:
ports:
- protocol: TCP
port: 80
targetPort: 3003
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example
namespace: default
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: "true"
nginx.ingress.kubernetes.io/ssl-passthrough: "true"
spec:
tls:
- hosts:
- domain.com
secretName: tls-key
rules:
- host: domain.com
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: example
port:
number: 80

部署工作不正常。呼叫domain.com显示503 Service Temporarily Unavailable错误。

如果我在pod上做端口转发,我可以在localhost:3003看到工作的应用程序。我无法在服务上创建端口转发。

所以很明显我做了一些错误的端口。有人能解释一下应该设置哪个值以及为什么吗?

您缺少部署中的标签和服务中的选择器。试试这个:

apiVersion: apps/v1
kind: Deployment
metadata:
name: example
labels:
app: example
spec:
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example
image: "hub.domain.com/example:1.0.0"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3003
---
apiVersion: v1
kind: Service
metadata:
name: example
spec:
selector:
app: example
ports:
- protocol: TCP
port: 80
targetPort: 3003
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example
namespace: default
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: "true"
nginx.ingress.kubernetes.io/ssl-passthrough: "true"
spec:
tls:
- hosts:
- domain.com
secretName: tls-key
rules:
- host: domain.com
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: example
port:
number: 80

部署:https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

服务:https://kubernetes.io/docs/concepts/services-networking/service/

标签和选择器:https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

你可以命名你的标签键和值任何你喜欢的,你甚至可以有一个标签作为whatever: something而不是app: example,但这些是一些推荐的标签:https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/https://kubernetes.io/docs/reference/labels-annotations-taints/

相关内容

  • 没有找到相关文章

最新更新