【问题标题】:defining 2 ports in deployment.yaml in Kubernetes在 Kubernetes 的 deployment.yaml 中定义 2 个端口
【发布时间】:2023-04-03 11:10:01
【问题描述】:

我有一个我正在做的 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

我的服务.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 个端口的指导?

【问题讨论】:

    标签: kubernetes kubernetes-deployment


    【解决方案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 配置,nodePorts 的范围是多少(通常是 30000-32767,但它是可配置的)。

    编辑

    如果我从 deployment.yml 中删除 resources 部分,它会正确启动(大约 5 分钟后)。 这里是日志的 sn-p:

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

    由于证书(我想),连接到它的问题(我使用带有 traefik 的入口):

    [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:

    并使用浏览器连接,我登陆了这个页面:

    【讨论】:

    • 嗨 Nicola,我尝试了 $ kubectl apply -f WB_Sphere_service.yml 服务“websphere”无效:* spec.ports[0].name: 必需值 * spec.ports[1] .name:必填值
    • 谢谢尼古拉。我试图向我的本地主机进行转发的港口:Kubectl端口前进WebSphere-7874884868-NTZ54 9043:9443但它告诉我043 - > 9043:错误转发端口9043到POD 7E184665BF11F95EE02D07D40224A5240F0C7FAIV1A335D5B87A86E,UID:退出状态1 : 2018/09/09 16:49:06 socat[1668] E connect(5, AF=2 127.0.0.1:9043, 16): 连接被拒绝
    • 您的镜像 ibmcom/websphere-traditional 是否打开了这些端口号?
    • 是的,基于文档。 docker run --name test -h test -p 9043:9043 -p 9443:9443 -d \ ibmcom/websphere-traditional:install
    • 我添加了一些信息。
    【解决方案2】:

    在 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
    

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 2017-10-11
      • 2020-11-28
      • 2020-03-01
      • 2019-12-03
      • 2022-01-16
      • 2017-12-20
      • 2021-07-05
      • 1970-01-01
      相关资源
      最近更新 更多