克隆项目

## 克隆项目
#
[root@ec-k8s-m1 ~]# git clone https://github.com/storageos/deploy.git ~/storageos-deploy
## 
#
[root@ec-k8s-m1 ~]# cd ~/storageos-deploy/k8s/examples/mysql/
## 
#
[root@ec-k8s-m1 mysql]# ll
total 24
-rw-r--r-- 1 root root   60 Oct 20 20:43 00-service-account.yaml
-rw-r--r-- 1 root root  329 Oct 20 20:43 10-service.yaml
-rw-r--r-- 1 root root  290 Oct 20 20:43 15-mysqld-configmap.yaml
-rw-r--r-- 1 root root 1135 Oct 20 20:43 20-statefulset.yaml
-rw-r--r-- 1 root root  171 Oct 24 11:48 30-mysql-client-pod.yaml
-rw-r--r-- 1 root root  201 Oct 20 20:43 example.sql
## 00-service-account.yaml
#
apiVersion: v1
kind: ServiceAccount
metadata:
  name: mysql

## 10-service.yaml
#
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-read
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  selector:
    app: mysql

## 15-mysqld-configmap.yaml
#
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
    env: prod
data:
  mysqld.cnf: |
    [mysqld]
    pid-file = /var/run/mysqld/mysqld.pid
    #socket = /var/run/mysqld/mysqld.sock
    datadir = /var/lib/mysql
    bind-address = 0.0.0.0
    symbolic-links=0
## 20-statefulset.yaml
#
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
      env: prod
  serviceName: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
        env: prod
    spec:
      serviceAccountName: mysql
      securityContext:
        runAsUser: 0
        fsGroup: 999
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ALLOW_EMPTY_PASSWORD # 密码为空
          value: "1"
        ports:
        - name: mysql
          containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf # 配置文件
          mountPath: /etc/mysql/mysql.conf.d
        resources: # [计][环境] 资源
          requests:
            cpu: 500m
            memory: 1Gi
      volumes:
      - name: conf
        configMap:
          name: mysql
  volumeClaimTemplates:
  - metadata:
      name: data
      labels:
        env: prod
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "fast" # StorageOS storageClass 
      resources:
        requests:
          storage: 5Gi
## 30-mysql-client-pod.yaml
#
apiVersion: v1
kind: Pod
metadata:
  name: mysql-client
spec:
  containers:
    - name: mysql-client
      image: mysql:5.7.14
      command: ["/bin/sleep"]
      args: [ "3600" ]


部署

## create 部署
#
[root@ec-k8s-m1 mysql]# kubectl create -f 00-service-account.yaml -f 10-service.yaml -f 15-mysqld-configmap.yaml -f 20-statefulset.yaml -f 30-mysql-client-pod.yaml 
serviceaccount/mysql created
service/mysql created
service/mysql-read created
configmap/mysql created
statefulset.apps/mysql created
pod/mysql-client created

 

## Display detailed information on one or more volumes
#
[root@ec-k8s-m1 mysql]# storageos volume inspect default/pvc-560694d1-d7ab-11e8-8ba8-080027ceb424
[
    {
        "id": "2f726515-2206-dee7-af31-f93390546702",
        "inode": 108723,
        "name": "pvc-560694d1-d7ab-11e8-8ba8-080027ceb424",
        "size": 5,
        "pool": "default",
        "fsType": "ext4",
        "description": "Kubernetes volume",
        "labels": {
            "app": "mysql",
            "env": "prod"
        },
        "namespace": "default",
        "nodeSelector": "",
        "master": {
            "id": "92b7554e-f8b1-12a4-a651-379fe855ec55",
            "inode": 181608,
            "node": "60ebc601-7fae-8b8d-fd71-0788ae3b4d20",
            "nodeName": "ec-k8s-n1",
            "health": "healthy",
            "status": "active",
            "createdAt": "2018-10-24T16:39:17.676826131Z"
        },
        "mounted": true,
        "mountDevice": "/var/lib/storageos/volumes/2f726515-2206-dee7-af31-f93390546702",
        "mountpoint": "/var/lib/kubelet/plugins/kubernetes.io~storageos/mounts/pvc-560694d1-d7ab-11e8-8ba8-080027ceb424.default.pvc-560694d1-d7ab-11e8-8ba8-080027ceb424",
        "mountedAt": "2018-10-24T16:39:25.211891937Z",
        "mountedBy": "ec-k8s-n3",
        "replicas": [],
        "health": "healthy",
        "status": "active",
        "statusMessage": "",
        "mkfsDone": true,
        "mkfsDoneAt": "2018-10-24T16:39:24.541369078Z",
        "createdAt": "2018-10-24T16:39:17.556728815Z",
        "createdBy": "storageos"
    }
]

## ec-k8s-n1 HostPath
#
[root@ec-k8s-n1 ~]# ll -h /var/lib/storageos/volumes
total 1.3G
brw-rw---- 1 root disk 8, 32 Oct 25 00:47 2f726515-2206-dee7-af31-f93390546702
-rw-rw---- 1 root disk  5.0G Oct 25 00:47 92b7554e-f8b1-12a4-a651-379fe855ec55
-rw-rw---- 1 root disk  5.0G Oct 25 00:47 bst-108723

测试

## 查看 Service
#
[root@ec-k8s-m1 mysql]# kubectl get svc
NAME              TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
mysql             ClusterIP      None            <none>        3306/TCP       5m
mysql-read        ClusterIP      10.101.70.7     <none>        3306/TCP       5m

## K8S集群内可用 Host:mysql、mysql-read
#
[root@ec-k8s-m1 mysql]# kubectl exec -it mysql-client /bin/bash
root@mysql-client:/# mysql -h mysql-read -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
mysql> create database shop;
Query OK, 1 row affected (0.01 sec)

mysql> use shop;
Database changed

mysql> create table books (id int NOT NULL AUTO_INCREMENT, title VARCHAR(256), price decimal(4,2), creation DATE, primary key(id));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into books value (1,'Gates of Fire', 13.99, CURDATE());
Query OK, 1 row affected (0.01 sec)

mysql> select * from books;
+----+---------------+-------+------------+
| id | title         | price | creation   |
+----+---------------+-------+------------+
|  1 | Gates of Fire | 13.99 | 2018-10-25 |
+----+---------------+-------+------------+
1 row in set (0.00 sec)

mysql> exit
Bye

移除

## delete 移除K8S的部署
#
[root@ec-k8s-m1 mysql]# kubectl delete -f 20-statefulset.yaml -f 15-mysqld-configmap.yaml -f 10-service.yaml -f 00-service-account.yaml 
statefulset.apps "mysql" deleted
configmap "mysql" deleted
service "mysql" deleted
service "mysql-read" deleted
serviceaccount "mysql" deleted
## remove storageos volume 
## 最好手动先移除 storageos volume,再移除K8S的PVC、PV
## 直接移除K8S的PVC后,storageos 也会移除相应volume,但出现过Bug。
#
[root@ec-k8s-m1 ~]# storageos volume rm default/pvc-560694d1-d7ab-11e8-8ba8-080027ceb424