• k8s定义Deployment,和service


    定义一个Deployment和service做个简单的笔记

    有时候我们需要开放Pod的多个端口,比如nginx的80和443端口,那如何定义Deployment文件呢,定义单个端口如下

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name:  nginx
            image: nginx
            resources:
              requests:
                memory: "1000Mi"
                cpu: "1000m"
              limits:
                memory: "2048Mi"
                cpu: "2000m"
            ports:
            - containerPort: 80

    如果需要再开放一个443端口呢?

    我们该怎么定义呢

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name:  nginx
            image: nginx
            resources:
              requests:
                memory: "1000Mi"
                cpu: "1000m"
              limits:
                memory: "2048Mi"
                cpu: "2000m"
            ports:
            - containerPort: 80
            ports:
            - containerPort: 443

    以上的定义是错误的,如果再最后面定义了一个ports标签,并且指定端口是443的话,那么Pod创建后,容器只是打开了443端口并未打开80端口,下面的配置覆盖上面的,这样定义是不对的

    以下才是正确的

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name:  nginx
            image: nginx
            resources:
              requests:
                memory: "1000Mi"
                cpu: "1000m"
              limits:
                memory: "2048Mi"
                cpu: "2000m"
            ports:
            - containerPort: 80
            - containerPort: 443

    这样定义后创建POD后才是同时打开了80和443

    那么service呢,也是同样的

    以下的是错误的定义方式

    kind: Service
    apiVersion: v1
    metadata:
      name: nginx
    spec:
      selector:
        app: nginx
      type: NodePort
      ports:
        - name: http
          protocol: TCP
          port: 8001
          targetPort: 80
          nodePort: 12000
      ports:
        - name: https
          protocol: TCP
          port: 8002
          targetPort: 443
          nodePort: 13000

    同样是红色部分覆盖了上面的定义

    下面是正确的定义

    kind: Service
    apiVersion: v1
    metadata:
      name: nginx
    spec:
      selector:
        app: nginx
      type: NodePort
      ports:
        - name: http
          protocol: TCP
          port: 8001
          targetPort: 80
          nodePort: 12000
        - name: https
          protocol: TCP
          port: 8002
          targetPort: 443
          nodePort: 13000

    特此记录下,谨防忘记

  • 相关阅读:
    linux基础
    1-1python自动化测试环境搭建及开发工具安装
    Linux常用命令
    049.NET5_中间件
    045.NET5_基本鉴权授权
    044.NET5_基于Session_Cookies认证
    042-043.NET5_ResultFilter以及双语言应用
    041.NET5_ExceptionFilter
    040.NET5_ExceptionFilter
    039.NET5_自定义Filter匿名
  • 原文地址:https://www.cnblogs.com/hh2737/p/9530003.html
Copyright © 2020-2023  润新知