Example: Creating a Game Workload by Using APIs
This section describes the procedure for calling a series of CCE APIs to create a game app using the 2048 image.
The procedure for creating the 2048 game app is as follows:
- Call the token authentication API to obtain a user token. It will be used in the request header for authentication in subsequent requests.
- Create a cluster and obtain the universally unique identifier (UUID) of the cluster.
If you have created a cluster, skip this step and directly use the UUID of the created cluster.
- Call the CCE API in Creating a Cluster to create a cluster.
- Call the CCE API in Creating a Node to add a node to the cluster.
- Call the Kubernetes-native APIs to create the workload.
- Call the API in Creating a Deployment to create a workload using the 2048 image.
- Call the API in Creating a Service to create a Service. After the Service is created, the workload can be accessed from external networks.
Prerequisites
- A token has been obtained. For details, see Authentication.
- A VPC and a subnet have been created, and the VPC and subnet ID have been obtained. For details, see Creating a VPC and Subnet.
- A key pair has been created and the name of the key pair has been obtained. For details, see Creating a Key Pair.
- The endpoints of IAM and CCE have been obtained. For details, see Regions and Endpoints.
- The 2048 image used for creating the workload has been uploaded. For details, see Uploading an Image Through a Docker Client. The image address has been obtained.
Creating a Cluster
The following values are merely examples, and you need to replace them based on actual situations:
- IAM endpoint: Replace iam_endpoint with the actual IAM endpoint in the following example. For details on the endpoint of each region, see Regions and Endpoints.
- CCE endpoint: Replace cce_endpoint with the actual CCE endpoint in the following example. For details on the endpoint of each region, see Regions and Endpoints.
- VPC ID: 219ab8a0-1272-4049-a383-8ad0b770fa11
- Subnet ID: d23ef2e9-8b90-49b3-bc4a-fd7d6bea6bec
- Key pair name: keypair-cce
- Region name: cn-north-4
- Project ID: In the following example, project_id is used. For details on how to obtain project_id, see How to Obtain Parameters in the API URI.
To create a cluster with a node, perform the following steps:
- Call the token authentication API to obtain a token, and set the token as an environment variable before calling other APIs.
The information in bold needs to be replaced based on actual conditions.
- username: User account of HUAWEI CLOUD
- password: User password
- domainname: If the account is an IAM user, enter the HUAWEI CLOUD account name that creates the IAM user. If the account is not an IAM user, enter the value of username.
- cn-north-4: North China region 4 is used as an example.
curl -H "Content-Type:application/json" https://{iam_endpoint}/v3/auth/tokens -X POST -d '{ "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "name": "username", "password": "******", "domain": { "name": "domainname" } } } }, "scope": { "project": { "name": "cn-north-4" } } } }' -v
In the response header, the value of X-Subject-Token is the token.
X-Subject-Token: ******
Run the following command to set the token as an environment variable:
export Token={X-Subject-Token}
X-Subject-Token is the token obtained in the preceding step.
- Call the API in Creating a Cluster to create an empty cluster (without nodes).
curl -H "X-Auth-Token:$Token" -H "Content-Type:application/json;charset=utf-8" -X POST https://{cce_endpoint}/api/v3/projects/{project_id}/clusters -d '{ "kind": "Cluster", "apiVersion": "v3", "metadata": { "name": "cluster-sample" }, "spec": { "type": "VirtualMachine", "flavor": "cce.s1.small", "version": "v1.9.7-r0", "hostNetwork": { "vpc": "219ab8a0-1272-4049-a383-8ad0b770fa11", "subnet": "d23ef2e9-8b90-49b3-bc4a-fd7d6bea6bec" }, "containerNetwork": { "mode": "overlay_l2" } } }'The metadata.uid parameter in the items array of the response body indicates the cluster ID.
"uid":"d8cbca81-889a-11e8-88e9-0255ac10212d"
To facilitate subsequent operations, run the following command to set the cluster ID as an environment variable. In the following command, {cluster_id} indicates the cluster ID.
export cluster_id=d8cbca81-889a-11e8-88e9-0255ac10212d
- Call the API in Creating a Node to create a node for the cluster.
curl -H "X-Auth-Token:$Token" -H "Content-Type:application/json;charset=utf-8" -X POST https://{cce_endpoint}/api/v3/projects/{project_id}/clusters/{cluster_id}/nodes -d '{ "kind": "Node", "apiVersion": "v3", "metadata": { "name": "node-sample" }, "spec": { "flavor": "c3.large.2", "az": "cn-north-4a", "login": { "sshKey": "keypair-cce" }, "rootVolume": { "size": 40, "volumetype": "SATA" }, "dataVolumes": [ { "size": 100, "volumetype": "SATA" } ], "publicIP": { "count": 1, "eip": { "iptype": "5_bgp", "bandwidth": { "chargemode": "traffic", "size": 10, "sharetype": "PER" } } }, "count": 1 } }'
Creating a Workload
The following value is merely example and you need to replace it based on actual situations:
Private Docker image address: swr.cn-north-4.myhuaweicloud.com/full/2048-demo:v1.2 (For details about how to obtain this address, see Uploading an Image Through a Docker Client.)
To create a workload, perform the following steps:
- Call the API in Creating a Deployment to create a workload using the 2048 image.
Create a Deployment named deployment-test and add label name:deployment-test for it. Use image swr.cn-north-4.myhuaweicloud.com/full/2048-demo:v1.2 (this address is an example).
curl -X POST -H "Content-Type:application/json" -H "X-Cluster-ID:$UUID" -H "X-Auth-Token:$Token" -d '{ "apiVersion": "extensions/v1beta1", "kind": "Deployment", "metadata": { "labels": { "app": "deployment-test" }, "name": "deployment-test" }, "spec": { "replicas": 1, "selector": { "matchLabels": { "app": "deployment-test" } }, "template": { "metadata": { "labels": { "app": "deployment-test" } }, "spec": { "containers": [ { "image": "swr.cn-north-4.myhuaweicloud.com/full/2048-demo:v1.2", "imagePullPolicy": "IfNotPresent", "name": "deployment-test" } ], "imagePullSecrets": [{ "name": "default-secret" }] } } } }' https://{cluster ID}.{cce_endpoint}/apis/extensions/v1beta1/namespaces/default/deployments -k -v - Call the API in Reading a Specified Deployment to query whether the Deployment is successfully created in the specified namespace.
curl -X GET -H 'Content-Type:application/json;charset=utf-8' -H "X-Cluster-ID:$UUID" -H "X-Auth-Token:$Token" https://{cluster ID}.{cce_endpoint}/apis/extensions/v1beta1/namespaces/default/deployments/deployment-test -k -vIf the value of availableReplicas in the response is 1, the workload is successfully created.
"status": { "observedGeneration ": 1, "replicas ": 1, "updatedReplicas ": 1, "availableReplicas": 1 }You can also check whether the workload is successfully created using the CCE console. Specifically, log in to the CCE console and choose Workloads > Deployments from the navigation pane. The created deployment-test workload is displayed.
- After the Deployment is created, call the API in Creating a Service to create a Service, so that the pods in the Deployment can be accessed through the Service.
Set the Service name to service-test, access type to NodePort, and access protocol to TCP.
curl -X POST -H "Content-Type:application/json" -H "X-Cluster-UUID:$UUID" -H "X-Auth-Token:$Token" -d '{ "kind":"Service", "apiVersion":"v1", "metadata":{ "name":"service-test", "creationTimestamp":null, "labels":{ "app":"deployment-test" } }, "spec":{ "ports":[{ "name":"http", "nodePort":30023, "port":80, "protocol":"TCP", "targetPort":80 }], "selector":{ "app":"deployment-test" }, "type":"NodePort" } }' https://{cluster ID}.{cce_endpoint} /api/v1/namespaces/default/services -k -vIf status code 201 is returned, the command is executed successfully.
- Call the API in Obtaining a Specified Service to query the Service status and check whether the Service is successfully created.
curl -X GET -H 'Content-Type:application/json;charset=utf-8' -H "X-Cluster-ID:$UUID" -H "X-Auth-Token:$Token" https://{cluster ID}.{cce_endpoint} /api/v1/namespaces/default/services/service-test -k -vIf the following information is displayed, the Service is successfully created:
{ "kind": "Service", "apiVersion": "v1", "metadata": { "name": "service-test", "namespace": "default", "selfLink": "/api/v1/namespaces/default/services/service-test", "uid": "cb932657-214a-11e8-b486-fa163ecd089c", "resourceVersion": "340196", "creationTimestamp": "2018-03-06T14:29:36Z", "labels": { "app": "deployment-test" }, "enable": true }, "spec": { "ports": [{ "name": "http", "protocol": "TCP", "port": 80, "targetPort": 80, "nodePort": 30023 }], "selector": { "app": "deployment-test " }, "clusterIP": "10.247.90.130", "type": "NodePort", "sessionAffinity": "None", "externalTrafficPolicy": "Cluster" }, "status": { "loadBalancer": {} } }You can also view the created Service using the CCE console. Specifically, log in to the CCE console and choose Workloads > Deployments from the navigation pane. Click deployment-test (the workload name) on the displayed page to view the workload details. Click the Services tab. The access address and access port (30023) are displayed.
If the node is bound to an elastic IP address (EIP), you can use EIP:nodePort to access the workload from external networks.
Last Article: Getting Started
Next Article: APIs
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.