在Kubernetes中的deployment.yaml中定义2个端口

时间:2018-09-09 16:03:15

标签: kubernetes kubernetes-deployment

我有一个正在做的docker映像

docker run --name test -h test -p 9043:9043 -p 9443:9443 -d ibmcom/websphere-traditional:install

我正在尝试放入kubernetes部署文件,并且我有这个文件:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: websphere
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: websphere
    spec:
      containers:
      - name: websphere
        image: ibmcom/websphere-traditional:install
        ports:
        - containerPort: 9443
        resources:
          requests: 
            memory: 500Mi
            cpu: 0.5
          limits:
            memory: 500Mi
            cpu: 0.5
        imagePullPolicy: Always

我的service.yaml

apiVersion: v1
kind: Service
metadata:
  name: websphere
  labels:
    app: websphere
spec:
  type: NodePort #Exposes the service as a node ports
  ports:
  - port: 9443
    protocol: TCP
    targetPort: 9443
  selector:
    app: websphere

我可以指导如何在部署文件中映射2个端口吗?

2 个答案:

答案 0 :(得分:1)

您可以根据需要添加任意数量的端口。

这里是您的deployment.yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: websphere
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: websphere
    spec:
      containers:
      - name: websphere
        image: ibmcom/websphere-traditional:install
        ports:
        - containerPort: 9043
        - containerPort: 9443
        resources:
          requests: 
            memory: 500Mi
            cpu: 0.5
          limits:
            memory: 500Mi
            cpu: 0.5
        imagePullPolicy: IfNotPresent

这里是您的service.yml

apiVersion: v1
kind: Service
metadata:
  name: websphere
  labels:
    app: websphere
spec:
  type: NodePort #Exposes the service as a node ports
  ports:
  - port: 9043
    name: hello
    protocol: TCP
    targetPort: 9043
    nodePort: 30043
  - port: 9443
    name: privet
    protocol: TCP
    targetPort: 9443
    nodePort: 30443
  selector:
    app: websphere

检查您的kubernetes api-server配置,nodePort的范围是多少(通常为30000-32767,但它是可配置的)。

编辑

如果我从Deployment.yml中删除resources部分,它将正确启动(大约5分钟后)。 这是日志的片段:

  

[9/10/18 8:08:06:004 UTC] 00000051 Web容器I   com.ibm.ws.webcontainer.VirtualHostImpl addWebApplication SRVE0250I:   Web模块默认Web应用程序已绑定到   default_host [:9080,:80,:9443,:506 0,:5061,:443]。

由于证书(我想),问题连接到它(我在traefik中使用ingress):

  

[9/10/18 10:15:08:413 UTC] 000000a4 SSLHandshakeE E SSLC0008E:   无法初始化SSL连接。未经授权的访问被拒绝   或安全设置已过期。例外是   javax.net.ssl.SSLException:无法识别的SSL消息,纯文本   连接吗?

要解决这个问题(我没有进一步介绍),这可能会有所帮助:SSLHandshakeE E SSLC0008E: Unable to initialize SSL connection. Unauthorized access was denied or security settings have expired

尝试与port-forward连接:

enter image description here

并使用dthe浏览器进行连接,我进入了此页面:

enter image description here

答案 1 :(得分:0)

在Kubernetes中,您可以使用#port标签定义端口。此标签位于部署中的端口配置下。根据配置,您可以简单地定义任意数量的端口。以下示例显示了如何定义两个端口。

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 9376
    - name: https
      protocol: TCP
      port: 443
      targetPort: 9377