MySQL 在 Kubernetes 环境中运行这件事情本身并不困难,最简单的方式就是找到 MySQL 的 Docker image,跑起来就行了,但是要做到生产环境可用,还是有几个问题要解决,所以本文不对整个流程做详细的描述,而是把重点放在几个难点上。
1. Kubernetes 如何与 Ceph 联动
1.1 使用到的 Kubernetes 概念
Kubernetes 集群存储 PV 支持 Static 静态配置以及 Dynamic 动态配置,动态卷配置 (Dynamic provisioning) 可以根据需要动态的创建存储卷。我们知道,之前的静态配置方式,集群管理员必须手动调用云/存储服务提供商的接口来配置新的固定大小的 Image 存储卷,然后创建 PV 对象以在 Kubernetes 中请求分配使用它们。通过动态卷配置,能自动化完成以上两步骤,它无须集群管理员预先配置存储资源,而是使用 StorageClass 对象指定的供应商来动态配置存储资源。
1.2 Example:
cat rbd-storage-class.yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: rbd
provisioner: kubernetes.io/rbd
parameters:
monitors: 10.222.78.12:6789
adminId: admin
adminSecretName: ceph-secret-admin
adminSecretNamespace: default
pool: rbd
userId: admin
userSecretName: ceph-secret-admin
cat rbd-dyn-pv-claim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ceph-rbd-dyn-pv-claim
spec:
accessModes:
- ReadWriteOnce
storageClassName: rbd
resources:
requests:
storage: 1Gi
rbd-dyn-pvc-pod1.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: rbd-dyn-pvc-pod
name: ceph-rbd-dyn-pv-pod1
spec:
containers:
- name: ceph-rbd-dyn-pv-busybox1
image: busybox
command: ["sleep", "60000"]
volumeMounts:
- name: ceph-dyn-rbd-vol1
mountPath: /mnt/ceph-dyn-rbd-pvc/busybox
readOnly: false
volumes:
- name: ceph-dyn-rbd-vol1
persistentVolumeClaim:
claimName: ceph-rbd-dyn-pv-claim
2. 如何实现 MySQL 主从
2.1 使用到的 Kubernetes controller
Manages the deployment and scaling of a set of Pods , and provides guarantees about the ordering and uniqueness of these Pods.
2.2 解决问题的思路
使用多个 StatefulSet 运行多个 MySQL Pod ,第一个是 Master,其他是 Slave:
- 主从 MySQL 的配置文件不同,需要在启动阶段做出区分。
- 新加一个文件同步 container 来实现启动阶段 MySQL 主从文件同步。
2.3 实例
3. 如何让外网可以访问 MySQL 服务
目前 Kubernetes 将服务暴露到外网的方式主要有三种:
- NodePort:目前使用的方式,也是最简单的方式。
Node: 10.0.0.1, 10.0.0.2, 10.0.0.3
10.0.0.:30001 <-> service: mysql-1
10.0.0.:30002 <-> service: mysql-2
NodePort 的问题在于,service 如果出现问题,重新启动 port 会有变化;Node IP 变化的话,暴露服务的地址也会变;一个集群提供的端口资源限制为数千个。 - Ingress:支持如下访问方式
http://testing.example.com/mysql-1 <-> service: mysql-1
http://testing.example.com/mysql-2 <-> service: mysql-2
但问题在于 MySQL client 只支持域名,不支持 URL。 - LoadBalance
4. 该如何部署 Kubernetes
4.1 kubeadm
kubeadm 简介
A Stronger Foundation for Creating and Managing Kubernetes Clusters
What is the scope for kubeadm?
We want kubeadm to be a common set of building blocks for all Kubernetes deployments; the piece that provides secure and recommended ways to bootstrap Kubernetes. Since there is no one true way to setup Kubernetes, kubeadm will support more than one method for each phase. We want to identify the phases every deployment of Kubernetes has in common and make configurable and easy-to-use kubeadm commands for those phases. If your organization, for example, requires that you distribute the certificates in the cluster manually or in a custom way, skip using kubeadm just for that phase. We aim to keep kubeadm usable for all other phases in that case. We want you to be able to pick which things you want kubeadm to do and let you do the rest yourself.
如何使用 kubeadm 搭建一个高可用的 Kubernetes 集群
Creating HA clusters with kubeadm
- multi masters
- masters' load balance
- HA ETCD cluster
- DNS HA
Kubernetes master HA best practice
Set up High-Availability Kubernetes Masters