kubernetes 搭建单节点mysql服务

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

参考链接:https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/

一、创建service

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
  namespace: admin-d2069c
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql

二、创建StatefulSet

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: mysql
  namespace: admin-d2069c
spec:
  serviceName: mysql
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
      namespace: admin-d2069c
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123"
        ports:
        - name: mysql
          containerPort: 3306
        volumeMounts:
        - name: lihaile
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 300m
            memory: 200M
        livenessProbe:
          exec:
            command: ["mysqladmin", "-h", "127.0.0.1", "-uroot", "-p123", "ping"]
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          exec:
            # Check we can execute queries over TCP (skip-networking is off).
            command: ["mysql", "-h", "127.0.0.1", "-uroot", "-p123", "-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1
      volumes:
      - name: conf
        emptyDir: {}
      - name: config-map
        configMap:
          name: mysql
  volumeClaimTemplates:
  - metadata:
      name: lihaile
      annotations:
        volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
      namespace: admin-d2069c
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 2Gi

三、创建访问Service

apiVersion: v1
kind: Service
metadata:
  name: mysql-access
  labels:
    app: mysql
    namespace: admin-d2069c
spec:
  ports:
  - name: mysql
    port: 3306
  selector:
    app: mysql

查看Mysql

root@node4:~# kubectl -n admin-d2069c get pvc,pv,statefulset,pod,service |grep mysql
pvc/storage-mysql-0                    Bound     pvc-a6c63604-c2ee-11e8-b599-0050568eef9f   512M       RWX            managed-nfs-storage   18s

pv/pvc-a6c63604-c2ee-11e8-b599-0050568eef9f   512M       RWX            Delete           Bound     admin-d2069c/storage-mysql-0                   managed-nfs-storage             18s

statefulsets/mysql                      1         1         18s

po/mysql-0                                   1/1       Running   0          18s

svc/mysql                       ClusterIP   None            <none>        3306/TCP                          18s
svc/mysql-access                ClusterIP   10.68.31.80     <none>        3306/TCP                          18s


ing/mysql-access                jekens.com               80        18s

五、测试

1、查看集群详情和状态

root@node4:~# mysql -h 10.68.31.80 -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 124
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> 
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
10天前
|
关系型数据库 MySQL Apache
mysql5.7 本地计算机上的mysql 服务启动后停止 的问题解决
mysql5.7 本地计算机上的mysql 服务启动后停止 的问题解决
10 0
|
18天前
|
存储 Java 关系型数据库
社区医院管理服务系统【GUI/Swing+MySQL】(Java课设)
社区医院管理服务系统【GUI/Swing+MySQL】(Java课设)
24 1
|
2月前
|
监控 安全 关系型数据库
在规划阿里云RDS跨区迁移资源和服务可用性
在规划阿里云RDS跨区迁移资源和服务可用性
260 4
|
3月前
|
Kubernetes Linux 应用服务中间件
Kubernetes—集群环境搭建
Kubernetes—集群环境搭建
78 0
|
3月前
|
关系型数据库 MySQL 网络安全
主机无法访问远程mysql服务
主机无法访问远程mysql服务
48 0
|
2月前
|
Prometheus 监控 Kubernetes
如何用 Prometheus Operator 监控 K8s 集群外服务?
如何用 Prometheus Operator 监控 K8s 集群外服务?
|
4月前
|
关系型数据库 MySQL
cmd中输入net start mysql 提示:服务名无效或者MySQL正在启动 MySQL无法启动
cmd中输入net start mysql 提示:服务名无效或者MySQL正在启动 MySQL无法启动
|
1月前
|
弹性计算 关系型数据库 MySQL
阿里云ECS使用docker搭建mysql服务
阿里云ECS使用docker搭建mysql服务
152 1
|
20天前
|
人工智能 监控 Serverless
如何基于ACK Serverless快速部署AI推理服务
通过上述步骤,可以在ACK Serverless上快速部署AI推理服务,实现高可用、弹性扩展的服务架构。
19 1
|
21天前
|
Kubernetes 网络协议 Docker
K8S核心插件-coredns服务
K8S核心插件-coredns服务
15 0

推荐镜像

更多