Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Calling APIs to Create Workloads

Updated on 2024-10-29 GMT+08:00

CCI supports Kubernetes APIs. Compared with using the console to create workloads, calling APIs is much easier.

In Kubernetes, a pod is the minimum unit for container running and can encapsulate one or more containers, storage resources, and an independent network IP address. In practice, pods are rarely created directly. Kubernetes uses controllers such as Deployment and StatefulSet to manage pods. In addition, Kubernetes uses Services to define pods and their access policies, and uses ingresses to manage external access. For more information about Kubernetes resources, see Cloud Container Instance Developer Guide.

For the WordPress application, you can call APIs to create a series of resources, as shown in the following figure.

  • MySQL: Create a Deployment to deploy the MySQL, and create a Service to define the access policy of the MySQL.
  • WordPress: Create a Deployment to deploy the WordPress, and create a Service and an ingress to define the access policy of the WordPress.

Namespace

  1. Call the API described in Creating a Namespace to create a namespace and specify a namespace type.

    {
        "apiVersion": "v1",
        "kind": "Namespace",
        "metadata": {
            "name": "namespace-test",
            "annotations": {
                "namespace.kubernetes.io/flavor": "gpu-accelerated"
            }
        },
        "spec": {
            "finalizers": [
                "kubernetes"
            ]
        }
    }

  2. Call the API described in Creating a Network to create a network, and associate the network with a Virtual Private Cloud (VPC) and subnet.

    {
        "apiVersion": "networking.cci.io/v1beta1",
        "kind": "Network",
        "metadata": {
            "annotations": {
                "network.alpha.kubernetes.io/default-security-group": "{{security-group-id}}",
                "network.alpha.kubernetes.io/domain-id": "{{domain-id}}",
                "network.alpha.kubernetes.io/project-id": "{{project-id}}"
            },
            "name": "test-network"
        },
        "spec": {
            "availableZone": "{{zone}}",
            "cidr": "192.168.0.0/24",
            "attachedVPC": "{{vpc-id}}",
            "networkID": "{{network-id}}",
            "networkType": "underlay_neutron",
            "subnetID": "{{subnet-id}}"
        }
    }

MySQL

  1. Call the API described in Creating a Deployment to deploy the MySQL.

    • Set the Deployment name to mysql.
    • Set the pod label to app:mysql.
    • Use the mysql:5.7 image.
    • Set the value of environment variable MYSQL_ROOT_PASSWORD to ******** (replace ******** with your 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. Call the API described in Creating a Service to create a Service, and define the access policy for the pod created in 1.

    • Set the Service name to mysql.
    • Select the pod whose label is app:mysql to associate the pod created in 1.
    • Map workload access port 3306 to container port 3306.
    • The access type of the Service is ClusterIP, that is, ClusterIP is used to access the Service inside the cluster.
    {
        "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"
        }
    }

WordPress

  1. Call the API described in Creating a Deployment to deploy the WordPress.

    • Set the Deployment name to wordpress.
    • Set the value of replicas to 2, indicating that two pods are created.
    • Set the pod label to app:wordpress.
    • Use the wordpress:latest image.
    • Set the value of environment variable WORDPRESS_DB_PASSWORD to ******** (replace ******** with your password). This password must be the same as MYSQL_ROOT_PASSWORD set for the MySQL.
    {
        "apiVersion": "apps/v1",
        "kind": "Deployment",
        "metadata": {
            "name": "wordpress"
        },
        "spec": {
            "replicas": 2,
            "selector": {
                "matchLabels": {
                    "app": "wordpress"
                }
            },
            "template": {
                "metadata": {
                    "labels": {
                        "app": "wordpress"
                    }
                },
                "spec": {
                    "containers": [
                        {
                            "image": "wordpress:latest",
                            "name": "container-0",
                            "resources": {
                                "limits": {
                                    "cpu": "500m",
                                    "memory": "1024Mi"
                                },
                                "requests": {
                                    "cpu": "500m",
                                    "memory": "1024Mi"
                                }
                            },
                            "env": [
                                {
                                    "name": "WORDPRESS_DB_PASSWORD",
                                    "value": "********"
                                }
                            ]
                        }
                    ],
                    "imagePullSecrets": [
                        {
                            "name": "imagepull-secret"
                        }
                    ]
                }
            }
        }
    }

  2. Call the API described in Creating a Service to create a Service, and define the access policy for the pod created in 1.

    • Set the Service name to wordpress.
    • Select the pod whose label is app:wordpress to associate the pod created in 1.
    • Map workload access port 8080 to container port 80. For the WordPress image, port 80 is the default externally exposed port.
    • The access type of the Service is ClusterIP, that is, ClusterIP is used to access the Service inside the cluster.
    {
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {
            "name": "wordpress",
            "labels": {
                "app": "wordpress"
            }
        },
        "spec": {
            "selector": {
                "app": "wordpress"
            },
            "ports": [
                {
                    "name": "service0",
                    "targetPort": 80,
                    "port": 8080,
                    "protocol": "TCP"
                }
            ],
            "type": "ClusterIP"
        }
    }

  3. Call the API described in Creating an Ingress to create an ingress to define the external access policy of the WordPress. In this step, you need to configure a load balancer that is in the same VPC as the WordPress.

    • metadata.annotations.kubernetes.io/elb.id: ID of the load balancer
    • metadata.annotations.kubernetes.io/elb.ip: IP address of the load balancer
    • metadata.annotations.kubernetes.io/elb.port: Port configured for the load balancer
    • spec.rules: a set of rules for accessing the Service. path lists the paths for accessing the Service, for example, "/". Each path is associated with a backend (for example, wordpress:8080). A backend represents a combination of service:port. Ingress traffic will be forwarded to the corresponding backend.

    After the configuration is complete, the traffic destined for the load balancer (Load balancer IP address:Port) is transmitted to the wordpress:8080 Service. Because the Service is associated with the WordPress pod, the traffic finally accesses the WordPress container deployed in 1.

    {
        "apiVersion": "extensions/v1beta1",
        "kind": "Ingress",
        "metadata": {
            "name": "wordpress",
            "labels": {
                "app": "wordpress",
                "isExternal": "true",
                "zone": "data"
            },
            "annotations": {
                "kubernetes.io/elb.id": "2d48d034-6046-48db-8bb2-53c67e8148b5",
                "kubernetes.io/elb.ip": "10.10.10.10",
                "kubernetes.io/elb.port": "9012"
            }
        },
        "spec": {
            "rules": [
                {
                    "http": {
                        "paths": [
                            {
                                "path": "/",
                                "backend": {
                                    "serviceName": "wordpress",
                                    "servicePort": 8080
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback