Calling APIs to Create Workloads

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 CCI Development 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 in Creating a Namespace to create a namespace and specify a namespace type.

    curl POST https://cci.cn-north-1.myhuaweicloud.com/api/v1/namespaces -H 'content-type: application/json' -H "x-auth-token: $Token" -v -k -d '{
        "apiVersion": "v1",
        "kind": "Namespace",
        "metadata": {
            "name": "namespace-test",
            "annotations": {
                "namespace.kubernetes.io/flavor": "gpu-accelerated"
            }
        },
        "spec": {
            "finalizers": [
                "kubernetes"
            ]
        }
    }'

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

    curl POST https://cci.cn-north-1.myhuaweicloud.com/apis/networking.cci.io/v1beta1/namespaces/namespace-test/networks -H 'content-type: application/json' -H "x-auth-token: $Token" -v -k -d '{
        "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": "cnnorth1a",
            "cidr": "192.168.0.0/24",
            "attachedVPC": "vpc-id",
            "networkID": "network-id",
            "networkType": "underlay_neutron",
            "subnetID": "subnet-id"
        }
    }

MySQL

  1. Call the API 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 the environment variable MYSQL_ROOT_PASSWORD to ******** (replace ******** with an actual 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 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, that is, associate the pod created in 1.
    • Map workload access port 3306 to container port 3306.
    • Set the access type of the service to NodePort. That is, resources are accessed through the port of the node where the pod is located.
    {
        "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 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 the environment variable WORDPRESS_DB_PASSWORD to ******** (replace ******** with an actual 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 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, that is, 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.
    • Set the access type of the service to NodePort. That is, resources are accessed through the port of the node where the pod is located.
    {
        "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 in Creating an Ingress to create an ingress to define the external access policy of the WordPress. That is, associate a load balancer (it must be in the same VPC as the WordPress workload).

    • metadata.annotations.kubernetes.io/elb.id: must be data.
    • metadata.annotations.kubernetes.io/elb.ip: IP address of the load balancer.
    • metadata.annotations.kubernetes.io/elb.port: port of the load balancer.
    • spec.rules: set of rules for accessing the service. A path list is in the format like "/". 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 ELB 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
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }