K8S 使用 Deployment 运行一个无状态应用:Nginx
一、目标
- 创建一个 nginx Deployment
- 使用 kubectl 列举关于 Deployment 的信息
- 更新 Deployment
二、准备
你必须拥有一个 Kubernetes 的集群,同时你的 Kubernetes 集群必须带有 kubectl 命令行工具。
如果你还没有集群,参考 用 kubeadm 在 Debian 或 Ubuntu 中创建 k8s 集群。
三、教程
1 创建并了解一个 nginx Deployment
你可以通过创建一个 Kubernetes Deployment 对象来运行一个应用,且你使用 yaml 格式的文件创建一个 Deployment 的配置文件。例如, 下面是一个运行 nginx:1.14.2 Docker 镜像的 Deployment 的配置文件:
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
使用 yaml 文件创建一个 Deployment:
kubectl apply -f deployment.yaml
显示 Deployment 相关信息:
kubectl describe deployment nginx-deployment
输出:
Name: nginx-deployment
Namespace: default
CreationTimestamp: Thu, 27 May 2021 20:44:33 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.14.2
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-66b6c48dd5 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 80s deployment-controller Scaled up replica set nginx-deployment-66b6c48dd5 to 2
列出 Deployment 创建的 Pods:
kubectl get pods -l app=nginx
输出:
NAME READY STATUS RESTARTS AGE
nginx-deployment-66b6c48dd5-8mbkg 1/1 Running 0 2m32s
nginx-deployment-66b6c48dd5-w46gw 1/1 Running 0 2m32s
显示某一个 Pod 信息:
kubectl describe pod <pod-name>
这里的 <pod-name>
是某一 Pod 的名称。
2 更新 Deployment
你可以通过更新一个新的 YAML 文件来更新 Deployment。下面的 YAML 文件指定该 Deployment 镜像更新为 nginx 1.16.1。
deployment-update.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1 # Update the version of nginx from 1.14.2 to 1.16.1
ports:
- containerPort: 80
应用新的 YAML:
kubectl apply -f deployment-update.yaml
查看该 Deployment 以新的名称创建 Pods 同时删除旧的 Pods:
kubectl get pods -l app=nginx
3 通过增加副本数来扩缩应用
你可以通过应用新的 YAML 文件来增加 Deployment 中 Pods 的数量。 下面的 YAML 文件将 replicas
设置为 4,指定该 Deployment 应有 4 个 Pods:
deployment-scale.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 4 # Update the replicas from 2 to 4
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
应用新的 YAML 文件:
kubectl apply -f deployment-scale.yaml
验证 Deployment 有 4 个 Pods:
kubectl get pods -l app=nginx
输出:
NAME READY STATUS RESTARTS AGE
nginx-deployment-66b6c48dd5-h4x8h 1/1 Running 0 39s
nginx-deployment-66b6c48dd5-p7bgv 1/1 Running 0 38s
nginx-deployment-66b6c48dd5-xt5gh 1/1 Running 0 39s
nginx-deployment-66b6c48dd5-z8kj4 1/1 Running 0 38s
4 删除 Deployment
通过名称删除 Deployment:
kubectl delete deployment nginx-deployment