2017年1月24日星期二

OpenShift_036:一键部署 mysql 主从集群

环境:OCP 3.4

本实验全部材料来自我的同事陈耿和王洪涛,在此向他们表示感谢! 
  
1. 创建 NFS Server(在 Registry 机器上操作)

yum install –y nfs-utils

export volname=mysql-vol
mkdir -p /srv/nfs/${volname}
chown nfsnobody:nfsnobody /srv/nfs/${volname}
chmod 700 /srv/nfs/${volname}
echo "/srv/nfs/${volname} *(rw,sync,all_squash)" >> /etc/exports
systemctl enable nfs-server
systemctl restart rpcbind
systemctl restart nfs-server nfs-lock nfs-idmap

2. 测试 NFS Server 工作是否正常 (在 Node1/Node2 机器上操作)
export volname=mysql-vol
mkdir -p /mnt/nfs
mount -t nfs registry.example.com:/srv/nfs/${volname} /mnt/nfs
umount /mnt/nfs

3. 创建 PV(在 Master 机器上操作)
echo '{
  "apiVersion": "v1",
  "kind": "PersistentVolume",
  "metadata": {
    "name": "mysql-volume"
  },
  "spec": {
    "capacity": {
        "storage": "1Gi"
    },
    "accessModes": [ "ReadWriteMany" ],
    "nfs": {
        "path": "/srv/nfs/mysql-vol",
        "server": "registry.example.com"
    },
    "persistentVolumeReclaimPolicy": "Retain"
  }
}' | oc create -f -

oc get pv
输出如下:
NAME           CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM     REASON    AGE
mysql-volume   1Gi        RWX           Retain          Available                       4s

4. 创建 mysql-replication tempalte (在 Master 机器上操作)
oc create -f mysql-replication-template.json -n openshift

mysql-replication-template.json 文件内容如下:
{
  "kind": "Template",
  "apiVersion": "v1",
  "metadata": {
    "name": "mysql-replication-example",
    "creationTimestamp": null,
    "annotations": {
      "description": "MySQL Replication Example",
      "iconClass": "icon-database",
      "tags": "database,mysql,replication"
    }
  },
  "parameters": [
    {
      "name": "MYSQL_MASTER_SERVICE_NAME",
      "description": "Service name for MySQL master service",
      "value": "mysql-master"
    },
    {
      "name": "MYSQL_MASTER_USER",
      "description": "The username used for master-slave replication",
      "value": "master"
    },
    {
      "name": "MYSQL_MASTER_PASSWORD",
      "description": "The password for the MySQL master user",
      "generate": "expression",
      "from": "[a-zA-Z0-9]{12}"
    },
    {
      "name": "MYSQL_USER",
      "description": "The username that clients will use to connect to MySQL server",
      "value": "user"
    },
    {
      "name": "MYSQL_PASSWORD",
      "description": "The password for the MySQL master user",
      "generate": "expression",
      "from": "[a-zA-Z0-9]{12}"
    },
    {
      "name": "MYSQL_DATABASE",
      "description": "The name of the database that will be created",
      "value": "userdb"
    },
    {
      "name": "MYSQL_ROOT_PASSWORD",
      "description": "The password for the MySQL adminitrator",
      "generate": "expression",
      "from": "[a-zA-Z0-9]{12}"
    }
  ],
  "objects": [
    {
      "kind": "PersistentVolumeClaim",
      "apiVersion": "v1",
      "metadata": {
        "name": "mysql-master"
      },
      "spec": {
        "accessModes": [
          "ReadWriteMany"
        ],
        "resources": {
          "requests": {
            "storage": "1Gi"
          }
        }
      }
    },
    {
      "kind": "Service",
      "apiVersion": "v1",
      "metadata": {
        "name": "${MYSQL_MASTER_SERVICE_NAME}",
        "labels": {
          "name": "mysql-master"
        }
      },
      "spec": {
        "ports": [
          {
            "protocol": "TCP",
            "port": 3306,
            "targetPort": 3306,
            "nodePort": 0
          }
        ],
        "selector": {
          "name": "mysql-master"
        },
        "portalIP": "",
        "type": "ClusterIP",
        "sessionAffinity": "None"
      },
      "status": {
        "loadBalancer": {}
      }
    },
    {
      "kind": "Service",
      "apiVersion": "v1",
      "metadata": {
        "name": "mysql-slave",
        "labels": {
          "name": "mysql-slave"
        }
      },
      "spec": {
        "ports": [
          {
            "protocol": "TCP",
            "port": 3306,
            "targetPort": 3306,
            "nodePort": 0
          }
        ],
        "selector": {
          "name": "mysql-slave"
        },
        "portalIP": "",
        "type": "ClusterIP",
        "sessionAffinity": "None"
      },
      "status": {
        "loadBalancer": {}
      }
    },
    {
      "kind": "DeploymentConfig",
      "apiVersion": "v1",
      "metadata": {
        "name": "mysql-master",
        "creationTimestamp": null
      },
      "spec": {
        "strategy": {
          "type": "Recreate",
          "resources": {}
        },
        "triggers": [
          {
            "type": "ConfigChange"
          }
        ],
        "replicas": 1,
        "selector": {
          "name": "mysql-master"
        },
        "template": {
          "metadata": {
            "creationTimestamp": null,
            "labels": {
              "name": "mysql-master"
            }
          },
          "spec": {
            "volumes": [
              {
                "name": "mysql-master-data",
                "persistentVolumeClaim": {
                  "claimName": "mysql-master"
                }
              }
            ],
            "containers": [
              {
                "name": "server",
                "image": "registry.example.com:5000/rhscl/mysql-56-rhel7",
                "args": [
                  "run-mysqld-master"
                ],
                "ports": [
                  {
                    "containerPort": 3306,
                    "protocol": "TCP"
                  }
                ],
                "env": [
                  {
                    "name": "MYSQL_MASTER_USER",
                    "value": "${MYSQL_MASTER_USER}"
                  },
                  {
                    "name": "MYSQL_MASTER_PASSWORD",
                    "value": "${MYSQL_MASTER_PASSWORD}"
                  },
                  {
                    "name": "MYSQL_USER",
                    "value": "${MYSQL_USER}"
                  },
                  {
                    "name": "MYSQL_PASSWORD",
                    "value": "${MYSQL_PASSWORD}"
                  },
                  {
                    "name": "MYSQL_DATABASE",
                    "value": "${MYSQL_DATABASE}"
                  },
                  {
                    "name": "MYSQL_ROOT_PASSWORD",
                    "value": "${MYSQL_ROOT_PASSWORD}"
                  }
                ],
                "volumeMounts": [
                  {
                    "name": "mysql-master-data",
                    "mountPath": "/var/lib/mysql/data"
                  }
                ],
                "resources": {},
                "terminationMessagePath": "/dev/termination-log",
                "imagePullPolicy": "IfNotPresent",
                "securityContext": {
                  "capabilities": {},
                  "privileged": false
                }
              }
            ],
            "restartPolicy": "Always",
            "dnsPolicy": "ClusterFirst"
          }
        }
      },
      "status": {}
    },
    {
      "kind": "DeploymentConfig",
      "apiVersion": "v1",
      "metadata": {
        "name": "mysql-slave",
        "creationTimestamp": null
      },
      "spec": {
        "strategy": {
          "type": "Recreate",
          "resources": {}
        },
        "triggers": [
          {
            "type": "ConfigChange"
          }
        ],
        "replicas": 1,
        "selector": {
          "name": "mysql-slave"
        },
        "template": {
          "metadata": {
            "creationTimestamp": null,
            "labels": {
              "name": "mysql-slave"
            }
          },
          "spec": {
            "containers": [
              {
                "name": "server",
                "image": "registry.example.com:5000/rhscl/mysql-56-rhel7",
                "args": [
                  "run-mysqld-slave"
                ],
                "ports": [
                  {
                    "containerPort": 3306,
                    "protocol": "TCP"
                  }
                ],
                "env": [
                  {
                    "name": "MYSQL_MASTER_SERVICE_NAME",
                    "value": "${MYSQL_MASTER_SERVICE_NAME}"
                  },
                  {
                    "name": "MYSQL_MASTER_USER",
                    "value": "${MYSQL_MASTER_USER}"
                  },
                  {
                    "name": "MYSQL_MASTER_PASSWORD",
                    "value": "${MYSQL_MASTER_PASSWORD}"
                  },
                  {
                    "name": "MYSQL_DATABASE",
                    "value": "${MYSQL_DATABASE}"
                  }
                ],
                "resources": {},
                "terminationMessagePath": "/dev/termination-log",
                "imagePullPolicy": "IfNotPresent",
                "securityContext": {
                  "capabilities": {},
                  "privileged": false
                }
              }
            ],
            "restartPolicy": "Always",
            "dnsPolicy": "ClusterFirst"
          }
        }
      },
      "status": {}
    }
  ]
}

4. 在 OpenShift 控制台创建应用
选择 mysql-replication-example 模板,

mysql-master 和 mysql-slave pod 启动成功:

5. 使用 mywebsql 分别连接 mysql-master 和 mysql-slave
在 mysql-master 上创建 table,插入数据,在 mysql-slave 上应该能看到。

6.  清除 mysql 主从集群

6.1 在 Master 机器上操作
oc delete svc $(oc get svc | grep mysql |awk '{print $1}')
oc delete dc $(oc get dc | grep mysql |awk '{print $1}')
oc delete rc $(oc get rc | grep mysql |awk '{print $1}')
oc delete routes $(oc get routes | grep mysql |awk '{print $1}')
oc delete pod $(oc get pod | grep mysql |awk '{print $1}')
oc delete pvc mysql-master
oc delete pv mysql-volume

6.2 在 Registry 机器上操作
cd /srv/nfs/mysql-vol
rm -rf *

没有评论: