1
1
---
2
2
title : 运行一个单实例有状态应用
3
3
content_type : tutorial
4
+ weight : 20
4
5
---
5
6
6
7
<!-- overview -->
7
8
8
- 本文介绍在 Kubernetes 中使用 PersistentVolume 和 Deployment 如何运行一个单实例有状态应用. 该应用是 MySQL.
9
-
10
-
11
-
9
+ <!--
10
+ This page shows you how to run a single-instance stateful application
11
+ in Kubernetes using a PersistentVolume and a Deployment. The
12
+ application is MySQL.
13
+ -->
14
+ 本文介绍在 Kubernetes 中如何使用 PersistentVolume 和 Deployment 运行一个单实例有状态应用。该应用是 MySQL.
12
15
13
16
## {{% heading "objectives" %}}
14
17
15
-
16
- * 在环境中通过磁盘创建一个PersistentVolume.
17
- * 创建一个MySQL Deployment.
18
- * 在集群内以一个已知的 DNS 名将 MySQL 暴露给其他 pods.
19
-
20
-
21
-
18
+ <!--
19
+ * Create a PersistentVolume referencing a disk in your environment.
20
+ * Create a MySQL Deployment.
21
+ * Expose MySQL to other pods in the cluster at a known DNS name.
22
+ -->
23
+ * 在你的环境中创建一个引用磁盘的 PersistentVolume
24
+ * 创建一个 MySQL Deployment.
25
+ * 在集群内以一个已知的 DNS 名称将 MySQL 暴露给其他 Pod
22
26
23
27
## {{% heading "prerequisites" %}}
24
28
25
-
26
29
* {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
27
-
28
30
* {{< include "default-storage-class-prereqs.md" >}}
29
31
30
-
31
-
32
32
<!-- lessoncontent -->
33
33
34
- ## 部署MySQL
35
-
36
- 注意: 在配置的 yaml 文件中定义密码的做法是不安全的. 具体安全解决方案请参考
37
- [ Kubernetes Secrets] ( /docs/concepts/configuration/secret/ ) .
34
+ ## 部署 MySQL {#deploy-mysql}
35
+
36
+ <!--
37
+ You can run a stateful application by creating a Kubernetes Deployment
38
+ and connecting it to an existing PersistentVolume using a
39
+ PersistentVolumeClaim. For example, this YAML file describes a
40
+ Deployment that runs MySQL and references the PersistentVolumeClaim. The file
41
+ defines a volume mount for /var/lib/mysql, and then creates a
42
+ PersistentVolumeClaim that looks for a 20G volume. This claim is
43
+ satisfied by any existing volume that meets the requirements,
44
+ or by a dynamic provisioner.
45
+ -->
46
+ 你可以通过创建一个 Kubernetes Deployment 并使用 PersistentVolumeClaim 将其连接到
47
+ 某已有的 PV 卷来运行一个有状态的应用。
48
+ 例如,这里的 YAML 描述的是一个运行 MySQL 的 Deployment,其中引用了 PVC 申领。
49
+ 文件为 /var/lib/mysql 定义了加载卷,并创建了一个 PVC 申领,寻找一个 20G 大小的卷。
50
+ 该申领可以通过现有的满足需求的卷来满足,也可以通过动态供应卷的机制来满足。
51
+
52
+ <!--
53
+ Note: The password is defined in the config yaml, and this is insecure. See
54
+ [Kubernetes Secrets](/docs/concepts/configuration/secret/)
55
+ for a secure solution.
56
+ -->
57
+ 注意:在配置的 YAML 文件中定义密码的做法是不安全的。具体安全解决方案请参考
58
+ [ Kubernetes Secrets] ( /zh/docs/concepts/configuration/secret/ ) .
38
59
39
60
{{< codenew file="application/mysql/mysql-deployment.yaml" >}}
40
61
{{< codenew file="application/mysql/mysql-pv.yaml" >}}
41
62
42
- 1 . 部署 YAML 文件中定义的 PV 和 PVC:
43
-
44
- kubectl apply -f https://k8s.io/examples/application/mysql/mysql-pv.yaml
45
-
46
- 1 . 部署 YAML 文件中定义的 Deployment:
47
-
48
- kubectl apply -f https://k8s.io/examples/application/mysql/mysql-deployment.yaml
49
-
50
- 1 . 展示 Deployment 相关信息:
51
-
52
- kubectl describe deployment mysql
53
-
54
- Name: mysql
55
- Namespace: default
56
- CreationTimestamp: Tue, 01 Nov 2016 11:18:45 -0700
57
- Labels: app=mysql
58
- Annotations: deployment.kubernetes.io/revision=1
59
- Selector: app=mysql
60
- Replicas: 1 desired | 1 updated | 1 total | 0 available | 1 unavailable
61
- StrategyType: Recreate
62
- MinReadySeconds: 0
63
- Pod Template:
64
- Labels: app=mysql
65
- Containers:
66
- mysql:
67
- Image: mysql:5.6
68
- Port: 3306/TCP
69
- Environment:
70
- MYSQL_ROOT_PASSWORD: password
71
- Mounts:
72
- /var/lib/mysql from mysql-persistent-storage (rw)
73
- Volumes:
74
- mysql-persistent-storage:
75
- Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
76
- ClaimName: mysql-pv-claim
77
- ReadOnly: false
78
- Conditions:
79
- Type Status Reason
80
- ---- ------ ------
81
- Available False MinimumReplicasUnavailable
82
- Progressing True ReplicaSetUpdated
83
- OldReplicaSets: <none>
84
- NewReplicaSet: mysql-63082529 (1/1 replicas created)
85
- Events:
86
- FirstSeen LastSeen Count From SubobjectPath Type Reason Message
87
- --------- -------- ----- ---- ------------- -------- ------ -------
88
- 33s 33s 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica set mysql-63082529 to 1
89
-
90
-
91
- 1 . 列举出 Deployment 创建的 pods:
92
-
93
- kubectl get pods -l app=mysql
94
-
95
- NAME READY STATUS RESTARTS AGE
96
- mysql-63082529-2z3ki 1/1 Running 0 3m
97
-
98
- 1 . 查看 PersistentVolumeClaim:
99
-
100
- kubectl describe pvc mysql-pv-claim
101
-
102
- Name: mysql-pv-claim
103
- Namespace: default
104
- StorageClass:
105
- Status: Bound
106
- Volume: mysql-pv-volume
107
- Labels: <none>
108
- Annotations: pv.kubernetes.io/bind-completed=yes
109
- pv.kubernetes.io/bound-by-controller=yes
110
- Capacity: 20Gi
111
- Access Modes: RWO
112
- Events: <none>
113
-
114
- ## 访问 MySQL 实例
115
-
116
-
117
- 前面 YAML 文件中创建了一个允许集群内其他 pods 访问数据库的服务. 该服务中选项
118
- ` clusterIP: None ` 让服务 DNS 名称直接解析为 Pod 的 IP 地址. 当在一个服务下只有一个 pod
119
- 并且不打算增加 pods 的数量这是最好的.
120
-
63
+ 1 . <!-- Deploy the PV and PVC of the YAML file-->
64
+ 部署 YAML 文件中定义的 PV 和 PVC:
65
+
66
+ ``` shell
67
+ kubectl apply -f https://k8s.io/examples/application/mysql/mysql-pv.yaml
68
+ ```
69
+
70
+ 1 . <!-- Deploy the contents of the YAML file -->
71
+ 部署 YAML 文件中定义的 Deployment:
72
+
73
+ ``` shell
74
+ kubectl apply -f https://k8s.io/examples/application/mysql/mysql-deployment.yaml
75
+ ```
76
+
77
+ 1 . <!-- Display information about the Deployment -->
78
+ 展示 Deployment 相关信息:
79
+
80
+ ``` shell
81
+ kubectl describe deployment mysql
82
+ ```
83
+
84
+ ```
85
+ Name: mysql
86
+ Namespace: default
87
+ CreationTimestamp: Tue, 01 Nov 2016 11:18:45 -0700
88
+ Labels: app=mysql
89
+ Annotations: deployment.kubernetes.io/revision=1
90
+ Selector: app=mysql
91
+ Replicas: 1 desired | 1 updated | 1 total | 0 available | 1 unavailable
92
+ StrategyType: Recreate
93
+ MinReadySeconds: 0
94
+ Pod Template:
95
+ Labels: app=mysql
96
+ Containers:
97
+ mysql:
98
+ Image: mysql:5.6
99
+ Port: 3306/TCP
100
+ Environment:
101
+ MYSQL_ROOT_PASSWORD: password
102
+ Mounts:
103
+ /var/lib/mysql from mysql-persistent-storage (rw)
104
+ Volumes:
105
+ mysql-persistent-storage:
106
+ Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
107
+ ClaimName: mysql-pv-claim
108
+ ReadOnly: false
109
+ Conditions:
110
+ Type Status Reason
111
+ ---- ------ ------
112
+ Available False MinimumReplicasUnavailable
113
+ Progressing True ReplicaSetUpdated
114
+ OldReplicaSets: <none>
115
+ NewReplicaSet: mysql-63082529 (1/1 replicas created)
116
+ Events:
117
+ FirstSeen LastSeen Count From SubobjectPath Type Reason Message
118
+ --------- -------- ----- ---- ------------- -------- ------ -------
119
+ 33s 33s 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica set mysql-63082529 to 1
120
+ ```
121
+
122
+ 1 . <!-- List the pods created by the Deployment -->
123
+ 列举出 Deployment 创建的 pods:
124
+
125
+ ``` shell
126
+ kubectl get pods -l app=mysql
127
+ ```
128
+
129
+ ```
130
+ NAME READY STATUS RESTARTS AGE
131
+ mysql-63082529-2z3ki 1/1 Running 0 3m
132
+ ```
133
+
134
+ 1 . <!-- Inspect the PersistentVolumeClaim -->
135
+ 查看 PersistentVolumeClaim:
136
+
137
+ ``` shell
138
+ kubectl describe pvc mysql-pv-claim
139
+ ```
140
+
141
+ ```
142
+ Name: mysql-pv-claim
143
+ Namespace: default
144
+ StorageClass:
145
+ Status: Bound
146
+ Volume: mysql-pv-volume
147
+ Labels: <none>
148
+ Annotations: pv.kubernetes.io/bind-completed=yes
149
+ pv.kubernetes.io/bound-by-controller=yes
150
+ Capacity: 20Gi
151
+ Access Modes: RWO
152
+ Events: <none>
153
+ ```
154
+
155
+ <!--
156
+ ## Accessing the MySQL instance
157
+
158
+ The preceding YAML file creates a service that
159
+ allows other Pods in the cluster to access the database. The Service option
160
+ `clusterIP: None` lets the Service DNS name resolve directly to the
161
+ Pod's IP address. This is optimal when you have only one Pod
162
+ behind a Service and you don't intend to increase the number of Pods.
163
+
164
+ Run a MySQL client to connect to the server:
165
+ -->
166
+ ## 访问 MySQL 实例 {#accessing-the-mysql-instance}
167
+
168
+
169
+ 前面 YAML 文件中创建了一个允许集群内其他 Pod 访问的数据库服务。该服务中选项
170
+ ` clusterIP: None ` 让服务 DNS 名称直接解析为 Pod 的 IP 地址。
171
+ 当在一个服务下只有一个 Pod 并且不打算增加 Pod 的数量这是最好的.
121
172
122
173
运行 MySQL 客户端以连接到服务器:
123
174
124
175
```
125
176
kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword
126
177
```
127
178
128
- 此命令在集群内创建一个新的 Pod 并运行 MySQL 客户端,并通过 Service 将其连接到服务器.如果连接成功,你就知道有状态的 MySQL 数据库正处于运行状态.
179
+ <!--
180
+ This command creates a new Pod in the cluster running a MySQL client
181
+ and connects it to the server through the Service. If it connects, you
182
+ know your stateful MySQL database is up and running.
183
+ -->
184
+ 此命令在集群内创建一个新的 Pod 并运行 MySQL 客户端,并通过 Service 连接到服务器。
185
+ 如果连接成功,你就知道有状态的 MySQL 数据库正处于运行状态。
129
186
130
187
```
131
188
Waiting for pod default/mysql-client-274442439-zyp6i to be running, status is Pending, pod ready: false
@@ -134,20 +191,43 @@ If you don't see a command prompt, try pressing enter.
134
191
mysql>
135
192
```
136
193
137
- ## 更新
194
+ <!--
195
+ ## Updating
138
196
197
+ The image or any other part of the Deployment can be updated as usual
198
+ with the `kubectl apply` command. Here are some precautions that are
199
+ specific to stateful apps:
200
+ -->
201
+ ## 更新 {#updating}
139
202
140
- Deployment 中镜像或其他部分同往常一样可以通过 ` kubectl apply ` 命令更新. 以下是
141
- 特定于有状态应用的一些注意事项 :
203
+ Deployment 中镜像或其他部分同往常一样可以通过 ` kubectl apply ` 命令更新。
204
+ 以下是特定于有状态应用的一些注意事项 :
142
205
143
- * 不要弹性伸缩. 弹性伸缩仅适用于单实例应用. 下层的 PersistentVolume 仅只能挂载一个 pod. 对于集群级有状态应用, 请参考 StatefulSet 文档
206
+ <!--
207
+ * Don't scale the app. This setup is for single-instance apps
208
+ only. The underlying PersistentVolume can only be mounted to one
209
+ Pod. For clustered stateful apps, see the
144
210
[StatefulSet documentation](/docs/concepts/workloads/controllers/statefulset/).
145
- * 在 Deployment 的 YAML 文件中使用 ` strategy: ` ` type: Recreate ` . 该选项指示 Kubernetes 不使用滚动升级. 滚动升级将无法工作, 由于一次不能运行多个 pod. 在更新配置文件
146
- 创建一个新的 pod 前 ` Recreate ` 策略将先停止第一个 pod.
147
-
148
-
149
- ## 删除 Deployment
150
-
211
+ * Use `strategy:` `type: Recreate` in the Deployment configuration
212
+ YAML file. This instructs Kubernetes to _not_ use rolling
213
+ updates. Rolling updates will not work, as you cannot have more than
214
+ one Pod running at a time. The `Recreate` strategy will stop the
215
+ first pod before creating a new one with the updated configuration.
216
+ -->
217
+ * 不要对应用进行规模扩缩。这里的设置仅适用于单实例应用。下层的 PersistentVolume
218
+ 仅只能挂载到一个 Pod 上。对于集群级有状态应用,请参考
219
+ [ StatefulSet 文档] ( /zh/docs/concepts/workloads/controllers/statefulset/ ) .
220
+ * 在 Deployment 的 YAML 文件中使用 ` strategy: ` ` type: Recreate ` 。
221
+ 该选项指示 Kubernetes _ 不_ 使用滚动升级。滚动升级无法工作,因为这里一次不能
222
+ 运行多个 Pod。在使用更新的配置文件创建新的 Pod 前,` Recreate ` 策略将
223
+ 保证先停止第一个 Pod。
224
+
225
+ <!--
226
+ ## Deleting a deployment
227
+
228
+ Delete the deployed objects by name:
229
+ -->
230
+ ## 删除 Deployment {#deleting-a-deployment}
151
231
152
232
通过名称删除部署的对象:
153
233
@@ -157,23 +237,35 @@ kubectl delete pvc mysql-pv-claim
157
237
kubectl delete pv mysql-pv-volume
158
238
```
159
239
160
- 如果通过手动的方式分配 PersistentVolume, 那么也需要手动的删除它,以及释放下层资源.
161
- 如果是用过动态分配 PersistentVolume 的方式,在删除 PersistentVolumeClaim 后 PersistentVolume 将被自动的删除. 一些存储服务(比如 EBS 和 PD)也会在 PersistentVolume 被删除时自动回收下层资源.
162
-
163
-
164
-
240
+ <!--
241
+ If you manually provisioned a PersistentVolume, you also need to manually
242
+ delete it, as well as release the underlying resource.
243
+ If you used a dynamic provisioner, it automatically deletes the
244
+ PersistentVolume when it sees that you deleted the PersistentVolumeClaim.
245
+ Some dynamic provisioners (such as those for EBS and PD) also release the
246
+ underlying resource upon deleting the PersistentVolume.
247
+ -->
248
+ 如果通过手动的方式供应 PersistentVolume, 那么也需要手动删除它以释放下层资源。
249
+ 如果是用动态供应方式创建的 PersistentVolume,在删除 PersistentVolumeClaim 后
250
+ PersistentVolume 将被自动删除。
251
+ 一些存储服务(比如 EBS 和 PD)也会在 PersistentVolume 被删除时自动回收下层资源。
165
252
166
253
## {{% heading "whatsnext" %}}
167
254
255
+ <!--
256
+ * Learn more about [Deployment objects](/docs/concepts/workloads/controllers/deployment/).
168
257
169
- * 了解更多 Deployment 对象请参考 [ Deployment objects] ( /docs/concepts/workloads/controllers/deployment/ ) .
170
-
171
- * 了解更多 Deployment 应用请参考 [ Deploying applications] ( /docs/user-guide/deploying-applications/ )
258
+ * Learn more about [Deploying applications](/docs/tasks/run-application/run-stateless-application-deployment/)
172
259
173
- * kubectl run 文档请参考 [ kubectl run documentation] ( /docs/reference/generated/kubectl/kubectl-commands/#run )
260
+ * [kubectl run documentation](/docs/reference/generated/kubectl/kubectl-commands/#run)
174
261
175
- * 卷和持久卷请参考 [ Volumes] ( /docs/concepts/storage/volumes/ ) 和 [ Persistent Volumes] ( /docs/concepts/storage/persistent-volumes/ )
262
+ * [Volumes](/docs/concepts/storage/volumes/) and [Persistent Volumes](/docs/concepts/storage/persistent-volumes/)
263
+ -->
264
+ * 欲进一步了解 Deployment 对象,请参考 [ Deployment 对象] ( /zh/docs/concepts/workloads/controllers/deployment/ )
265
+ * 进一步了解[ 部署应用] ( /zh/docs/tasks/run-application/run-stateless-application-deployment/ )
176
266
267
+ * 参阅 [ kubectl run 文档] ( /docs/reference/generated/kubectl/kubectl-commands/#run )
177
268
269
+ * 参阅[ 卷] ( /zh/docs/concepts/storage/volumes/ ) 和[ 持久卷] ( /zh/docs/concepts/storage/persistent-volumes/ )
178
270
179
271
0 commit comments