云容器实例 CCI-调用API创建负载:MySQL

时间:2025-02-12 14:56:00

MySQL

  1. 调用创建Deployment接口部署MySQL。

    • Deployment名称为mysql。
    • 设置Pod的标签为app:mysql。
    • 使用mysql:5.7镜像。
    • 设置容器环境变量MYSQL_ROOT_PASSWORD为“********”,请替换为您设置的密码。
    {    "apiVersion": "apps/v1",    "kind": "Deployment",    "metadata": {        "name": "mysql"    },    "spec": {        "replicas": 1,        "selector": {            "matchLabels": {                "app": "mysql"            }        },        "template": {            "metadata": {                "labels": {                    "app": "mysql"                }            },            "spec": {                "containers": [                    {                        "image": "mysql:5.7",                        "name": "container-0",                        "resources": {                            "limits": {                                "cpu": "500m",                                "memory": "1024Mi"                            },                            "requests": {                                "cpu": "500m",                                "memory": "1024Mi"                            }                        },                        "env": [                            {                                "name": "MYSQL_ROOT_PASSWORD",                                "value": "********"                            }                        ]                    }                ],                "imagePullSecrets": [                    {                        "name": "imagepull-secret"                    }                ]            }        }    }}

  2. 调用创建Service接口创建一个Service,定义1中创建的Pod的访问策略。

    • Service名称为mysql。
    • 选择标签为app:mysql的Pod,即关联1中创建的Pod。
    • 负载访问端口3306映射到容器的3306端口。
    • Service的访问类型为ClusterIP,即使用ClusterIP在内部访问Service。
    {    "apiVersion": "v1",    "kind": "Service",    "metadata": {        "name": "mysql",        "labels": {            "app": "mysql"        }    },    "spec": {        "selector": {            "app": "mysql"        },        "ports": [            {                "name": "service0",                "targetPort": 3306,                "port": 3306,                "protocol": "TCP"            }        ],        "type": "ClusterIP"    }}

support.huaweicloud.com/bestpractice-cci/cci_04_0005.html