定义一个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

特此记录下,谨防忘记

转载于:https://www.cnblogs.com/hh2737/p/9530003.html

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐