
本篇文章继续介绍deployment
在开始介绍deployment之前,先来聊下ReplicaSets
ReplicaSet 是确保每个 Pod 的所需数量的副本都已启动并正在运行。每当 Pod 宕机时,ReplicaSet 都会部署一个新的 Pod 以保持高可用性。
如下这个yaml文件将会创建一个rs
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: tutum-rs
labels:
app: tutum
spec:
replicas: 3
selector:
matchLabels:
app: tutum
template:
metadata:
labels:
app: tutum
spec:
containers:
- name: tutum
image: tutum/hello-world
ports:
- containerPort: 80其中比较重要的是两个配置,一个是replicas,一个是selector。
replicas确保有多少个在活跃的pod
selector用来匹配一组pod
部署一个ReplicaSet与部署一个pod一样
kubectl create -f ./resources/tutum-rs.yaml如上可以创建三个tutum pod
我们可以通过如下命令来详细查看ReplicaSet的具体情况
kubectl describe rs tutum-rs如果要修改启动的副本数目,可以通过如下命令
kubectl edit rs tutm-rs上述修改完成后,将会自动扩缩
也可以采用命令行的方式完成上述操作
kubectl scale rs tutum-rs --replicas=2如果你想要实现自动扩缩容,可以通过 autoscale 方式来完成
kubectl autoscale rs tutum-rs --max=10 --min=3 --cpu-percent=50 --dry-run=true -o=yaml通过上述dry-run方式可以生成一段yaml文件,但是不会对现有环境产生影响,而上述的autoscale是当所有Pod的平均CPU使用率达到50%时,触发扩缩操作
管理我们的 Deployment 的最有效和可重复的方法是使用 Manifest 文件。
如下是一个最简单的清单文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: tutum-deployment
spec:
template:
spec:
containers:
- name: tutum
image: tutum/hello-world
ports:
- containerPort: 80如果我们看一下这个 Deployment,它看起来与我们的 PodSpec 和 RS Manifest 非常相似。我们可以将 Pod 部分中已经介绍过的任何配置添加到此清单中。我们还应该配置 ReplicaSet 以满足我们的复制需求。
所有 Kubernetes deployment YAML 文件都必须包含以下规范:
apiVersion 指定要使用的 API 版本。API 对象按组定义。deployment对象属于 apps API 组。Group 对象可以声明为 alpha、beta 或 stable
kind 值声明要在 Yaml 文件中描述的 Kubernetes 对象的类型。Kubernetes 支持以下 'kind' 对象:
元数据声明其他数据以唯一标识 Kubernetes 对象。可以添加到对象的关键元数据:
让我们看看创建 Deployment 时发生了什么:
kubectl describe deployment tutum-deployment
我们可以看到,在 Deployment 的事件中,创建了一个 ReplicaSet。让我们看看带有 kubectl 的 ReplicaSet 描述了什么 rs。您的 ReplicaSet 具有唯一的名称,因此您需要 tab-complete。
当我们查看 ReplicaSet 事件时,我们可以看到它正在创建 Pod。
当我们使用 kubectl describe pod 查看 Pod 时,我们会看到主机拉取了镜像,并启动了容器。
可以在命令行上使用 set 更新部署。下面是一个示例:
kubectl set image deployment/tutum-deployment tutum=nginx:alpine --record我们将上述镜像调整后,可以通过查看历史记录查看发生了什么:
kubectl rollout history deployment tutum-deployment现在,让我们用 Deployment 的基本内容填充这个文件:
apiVersion:
kind:
metadata:
spec: 我们应该为这些中的每一个设置什么值?
apiVersion: apps/v1
kind: Deployment
metadata:
name: tutum-deployment
labels:
app: tutum
spec:继续完成spec,需要配置一个RS,并且能够匹配标签
spec:
replicas: 3
selector:
matchLabels:
app: tutum然后需要配置容器、端口、以及镜像
spec:
replicas: 3
selector:
matchLabels:
app: tutum
template:
metadata:
labels:
app: tutum
spec:
containers:
- name: tutum
image: tutum/hello-world
ports:
- containerPort: 80apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 5
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
env:
- name: user
value: "admin"
- name: password
value: "root"
- name: host
value: "test"
resources:
limits:
cpu: "1"
memory: "256Mi"
requests:
cpu: "1"
memory: "256Mi"strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate获取deployment rollout历史
kubectl rollout history deployment/nginx-deployment跳会指定版本
kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=X原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。