定义资源的状态,对于Pod来说,属性就是containers

2024-08-22 10:53:34 浏览数 (1)

apiVersion: v1

kind: Pod # 定义Kubernetes资源的类型为Pod

metadata:

name: demo-web # 定义资源的名称

labels: # 为Pod贴上标签,后面会介绍其用处

app: demo-web

spec: # 定义资源的状态,对于Pod来说,最重要属性就是containers

containers: # containers一个数组类型,如果你希望部署多个容器,可以添加多项

- name: web # 定义本Pod中该容器的名称

image: rainingnight/aspnetcore-web # 定义Pod启动的容器镜像地址

ports:

- containerPort: 80 # 定义容器监听的端口(与Dockerfile中的EXPOSE类似,只是为了提供文档信息)

apiVersion: v1

kind: Service # 定义Kubernetes资源的类型为Service

metadata:

name: demo-web-service # 定义资源的名称

spec:

selector: # 指定对应的Pod

app: demo-web # 指定Pod的标签为demo-web

ports:

- protocol: TCP # 协议类型

port: 80 # 指定Service访问的端口

targetPort: 80 # 指定Service转发请求的端口

nodePort: 30000

type: NodePort # 指定Service的类型,在这里使用NodePort来对外访问

apiVersion: v1

kind: Pod

metadata:

name: demo-web-copy

labels:

app: demo-web

spec:

containers:

- name: web

image: rainingnight/aspnetcore-web

ports:

- containerPort: 80

apiVersion: apps/v1

kind: Deploym www.laipuhuo.com ent # 定义Kubernetes资源的类型为Deployment

metadata:

name: demo-web-deployment # 定义资源的名称

labels:

app: demo-web-deployment

spec: # 定义资源的状态。

replicas: 2 # 定义我们想运行多少个Pod,在这里我们希望运行2个

selector:

matchLabels: # 定义该部署匹配哪些Pod

app: demo-web

minReadyS www.laipuhuo.com econds: 5 # 可选,指定Pod可以变成可用状态的最小秒数,默认是0

strategy: # 指定更新版本时,部署使用的策略

type: RollingUpdate # 策略类型,使用RollingUpdate可以保证部署期间服务不间断

rollingUpdate:

maxUnavailable: 1 # 部署时最大允许停止的Pod数量(与replicas相比)

maxSurge: 1 # 部署时最大允许创建的Pod数量(与replicas相比)

template: # 用来指定Pod的模板,与Pod的定义类似

metadata:

labels: # 根据模板创建的Pod会被贴上该标签,与上面的matchLabels对应

app: demo-web

spec:

containers:

- name: web

image: rainin www.laipuhuo.com gnight/aspnetcore-web

imagePullPolicy: Always # 默认是IfNotPresent,如果设置成Always,则每一次部署都会重新拉取容器映像(否则,如果本地存在指定的镜像版本,就不会再去拉取)

ports:

- containerPort: 80

kubectl rollout status deployment demo-web-deployment

# 输出

# Waiting for deployment "demo-web-deployment" rollout to finish: 1 old replicas are pending termination...

# Waiting for deployment "demo-web-deployment" rollout to finish: 1 old replicas are pending termination...

# Waiting for deployment "demo-web-deployment" rollout to finish: 1 old replicas are pending termination...

# Waiting for deployment "demo-web-deployment" rollout to finish: 1 old replicas are pending termination...

# Waiting for deployment "demo-web-deployment" rollout to finish: 1 of 2 updated replicas are available...

# Waiting for deployment "www.laipuhuo.com demo-web-deployment" rollout to finish: 1 of 2 updated replicas are available...

# deployment "demo-web-deployment" successfully rolled out

0 人点赞