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
特此记录下,谨防忘记
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论