$ kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql ClusterIP None <none> 3306/TCP 4s mysql-read ClusterIP 10.105.117.44 <none> 3306/TCP 4s
$ kubectl describe pod mysql-2 ... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning FailedScheduling 3m35s default-scheduler running "VolumeBinding" filter plugin for pod "mysql-2": pod has unbound immediate PersistentVolumeClaims Warning FailedScheduling 3m35s default-scheduler persistentvolumeclaim "data-mysql-2" not found Normal Scheduled 3m32s default-scheduler Successfully assigned default/mysql-2 to node1 Normal Pulled 3m32s kubelet, node1 Container image "mysql:5.7" already present on machine Normal Created 3m32s kubelet, node1 Created container init-mysql Normal Started 3m31s kubelet, node1 Started container init-mysql Normal Started 3m30s kubelet, node1 Started container clone-mysql Normal Pulled 3m30s kubelet, node1 Container image "10.160.15.5/google_containers/xtrabackup:1.0" already present on machine Normal Created 3m30s kubelet, node1 Created container clone-mysql Normal Pulled 2m55s kubelet, node1 Container image "10.160.15.5/google_containers/xtrabackup:1.0" already present on machine Normal Created 2m55s kubelet, node1 Created container xtrabackup Normal Started 2m55s kubelet, node1 Started container xtrabackup Normal Pulled 2m54s (x2 over 2m55s) kubelet, node1 Container image "mysql:5.7" already present on machine Normal Created 2m54s (x2 over 2m55s) kubelet, node1 Created container mysql Normal Started 2m54s (x2 over 2m55s) kubelet, node1 Started container mysql
service
1 2 3 4
$ kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql ClusterIP None <none> 3306/TCP 21h mysql-read ClusterIP 10.105.117.44 <none> 3306/TCP 21h
1 2 3
$ kubectl get statefulset NAME READY AGE mysql 3/3 107m
验证集群
创建测试数据
1 2 3 4 5 6 7 8 9
$ kubectl run mysql-client --image=mysql:5.7 -i --rm --restart=Never --\ mysql -h mysql-0.mysql <<EOF CREATE DATABASE test; CREATE TABLE test.messages (message VARCHAR(250)); INSERT INTO test.messages VALUES ('hello'); EOF
If you don't see a command prompt, try pressing enter. pod "mysql-client" deleted
读取测试数据
1 2 3 4 5 6 7 8
$ kubectl run mysql-client --image=mysql:5.7 -i -t --rm --restart=Never -- mysql -h mysql-read -e "SELECT * FROM test.messages" If you don't see a command prompt, try pressing enter. +---------+ | message | +---------+ | hello | +---------+ pod "mysql-client" deleted
$ kubectl get pods -l app=mysql NAME READY STATUS RESTARTS AGE mysql-0 2/2 Running 0 4h51m mysql-1 2/2 Running 1 3h10m mysql-2 2/2 Running 1 3h10m mysql-3 2/2 Running 1 119s mysql-4 2/2 Running 0 71s
获取数据
1 2 3 4 5 6 7
$ kubectl run mysql-client --image=mysql:5.7 -it --rm --restart=Never -- mysql -h mysql-4.mysql -e "SELECT * FROM test.messages" +---------+ | message | +---------+ | hello | +---------+ pod "mysql-client" deleted
尝试写入
1 2 3 4
$ kubectl run mysql-client --image=mysql:5.7 -it --rm --restart=Never -- mysql -h mysql-4.mysql -e "INSERT INTO test.messages VALUES ('test')" ERROR 1290 (HY000) at line 1: The MySQL server is running with the --super-read-only option so it cannot execute this statement pod "mysql-client" deleted pod default/mysql-client terminated (Error)
StatefulSet 其实是一种特殊的 Deployment,只不过这个“Deployment”的每个 Pod 实例的名字里,都携带了一个唯一并且固定的编号。这个编号的顺序,固定了 Pod 的拓扑关系;这个编号对应的 DNS 记录,固定了 Pod 的访问方式;这个编号对应的 PV,绑定了 Pod 与持久化存储的关系。所以,当 Pod 被删除重建时,这些“状态”都会保持不变。