• Cloud Container Engine

cce
  1. Help Center
  2. Cloud Container Engine
  3. User Guide 2.0
  4. Application Management
  5. Creating a Stateless Application

Creating a Stateless Application

Stateless application instances are independent from each other and provide the same functions. They support auto scaling and rolling upgrades. Examples of stateless applications include Nginx and WordPress.

Prerequisites

  • A cluster is available. For details on how to create a cluster, see Creating a VM Cluster.
    NOTE:

    When creating multiple containerized applications, ensure that each application has a unique port. Otherwise, application deployment will fail.

  • To enable access to an application from Internet, ensure that an elastic IP address has been bound to or ELB instances have been configured for at least one node in the cluster.

Creating a Stateless Application on the CCE Console

  1. (Optional) If you are creating an application based on your own image, upload the image to the image management service. For details about how to upload an image, see Image Repository.
  2. In the navigation pane, choose Application Management. Click Create Application, and set Select Application Type to Stateless Applications.
  3. Set basic application parameters as described in Table 1. The parameters marked with an asterisk (*) are mandatory.

    Table 1 Basic application parameters

    Parameter

    Description

    * Application Name

    Name of the containerized application to be created. The name must be unique.

    * Cluster Name

    Cluster in which the application resides.

    * Namespace

    Namespace in which the application resides. By default, this parameter is set to default.

    Application Group

    You can manage (start, stop, and delete) applications in batches by adding these applications to the same application group.

    When you enter an application group name, a user group is automatically created.

    * Instances

    Number of instances in the application. Each application has at least one instance. You can specify the number of instances as required.

    Each application instance consists of the same containers. Configuring multiple instances for an application ensures that the application can still run properly even if an instance is faulty.

    Description

    Description of the application.

  4. Click Next to add a container.

    1. Click Add Container and select the image to be deployed. Click OK.
      • The My Images tab page displays all images you created.
      • Third-party Images: CCE allows you to create an application using an image pulled from a third-party image repository, rather than a public cloud image repository or a Docker Hub image repository. When you create an application using a third-party image, ensure that the node where the application is running can access public networks. For details about how to create an application using a third-party image, see Using a Third-party Image to Create an Application.
        • If your image repository does not require authentication, set Authenticate Secret to No, specify Image Address, for example, nginx:latest, and click OK.
        • If your image repository is accessible only after being authenticated by account and password, set Authenticate Secret to Yes. You need to create a secret first and then user a third-party image to create an application.
    2. Set image parameters.
      Table 2 Image parameters

      Parameter

      Description

      Image Name

      Imported image. You can click Change Image to update it.

      * Image Version

      Version of the image to be deployed.

      * Container Name

      Name of the container. You can modify it.

      Container Resources

      For more information about Request and Limit, see Setting Container Specifications.

      • Request: the amount of resources that CCE will guarantee to a container.
      • Limit: the maximum amount of resources that CCE will allow a container to use. You can set Limit to prevent system faults caused by container overload.
    3. Set environment variables.
      Environment variables are set in the container running environment and can be modified after application deployment to ensure the flexibility of applications.
      1. Click Add.
      2. Set Type to Manual addition.
      3. Set Variable Name and Variable/Variable Reference.
    4. Set data storage.

      You can mount a host directory, EVS disk, SFS, and configuration items and secrets to the corresponding directories of a container instance. For details, see Storage Management.

      NOTE:

      If you set Allocation Mode to Automatic when adding data storage, the created storage will not be tagged.

    5. Set log policy.

      Set a policy and log directory for collecting application logs and preventing logs from exceeding size limits. For details, see Log Management.

    6. (Optional) One application instance contains one or more related containers. If your application contains multiple containers, click Add Container and then add containers.

  5. Click Next. Then, click Add Access Mode, and set the application access mode.

    To enable access to the application from other applications or the Internet, set the application access mode.

    The application access mode determines the network attributes of an application. Applications with different access modes can provide different network capabilities.

    At present, the following access modes are provided:

    • Intra-Cluster Access: An application is accessible to other applications in the same cluster by using an internal domain name.
    • Intra-VPC Access: An application is accessible to other applications in the same VPC by using the IP address of the cluster node or the ELB service address of the private network.
    • External Access - Elastic IP Address: An application is accessible to public networks by using an EIP. This access mode is applicable to services that need to be exposed to public networks. To enable access to an application from the Internet, an elastic IP address must be bound to a node in the cluster, and a mapping port number must be set. The port number ranges from 30000 to 32767, for example, the access address can be 10.117.117.117:30000.
    • External Access - Elastic Load Balancer: An application is accessible to public networks by using an ELB address. This access mode provides higher reliability than EIP-based access and is applicable to services that need to be exposed to public networks. The access address consists of the ELB service address of the public network and the configured access port, for example, 10.117.117.117:80.
    • Layer 7 Load Balancing (Ingress): Enhanced ELB is used. Compared with layer-4 ELB, layer-7 ELB newly supports Uniform Resource Identifiers(URI) configurations and distributes access traffic to the corresponding service based on the corresponding URIs. In addition, different functions are implemented based on various URIs. The access address consists of the IP address of the public network load balancer, access port, and defined URI, for example, 10.154.55.77:80/helloworld.

    For details about the access modes, see Application Access Settings.

  6. Click OK, and then click Next.
  7. (Optional) Configure advanced settings for the application.

    1. Configure the upgrade policy, as listed in Table 3.
      Table 3 Upgrade policy

      Upgrade Mode

      Description

      In-place upgrade

      In this upgrade mode, the old instance needs to be deleted before a new instance is created. Services are interrupted during the upgrade.

      Rolling upgrade

      The instance of the old version is gradually replaced with the instance of the new version. During the upgrade, service traffic is evenly distributed between new and old instances, so services are not interrupted.

    2. Migration Policy: Provides a time window for the application instances to be rescheduled to other available nodes when the node where the application instance is located is unavailable. The default value is 0s.
    3. Scheduling policy: You can combine static global scheduling policies or dynamic runtime scheduling policies as required. For details, see Affinity and Anti-Affinity Scheduling.

  8. Click Create Now. Click Back to Application List.

    In the application list, if the application status is Running, the application has been created successfully. Application status is not updated in real time. To view the application status, press F5.

  9. To access the application in a browser, go to the application list on the Application Management page. Copy the corresponding External Access Address and paste it into the address box in the browser.

    NOTE:

    External access addresses can be obtained only when the application access mode is set to Elastic IP Address or Elastic Load Balancer.

Creating a Stateless Application Using kubectl

The following procedure uses an Nginx application as an example to describe how to create an application using kubectl.

Prerequisites

You have configured the kubectl command and connected an ECS server to the cluster. For details, see Connecting to a Kubernetes Cluster Using kubectl.

Procedure

  1. Log in to the ECS server on which the kubectl commands have been configured. For details, see Logging In to a Linux ECS.
  2. Create and edit the nginx-deployment.yaml file and the nginx-elb-svc.yaml file. The file names are used as examples, and you can change them as required.

    vi nginx-deployment.yaml

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: nginx
      strategy:
        type: RollingUpdate
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - image: nginx 
            imagePullPolicy: Always
            name: nginx
          imagePullSecrets:
          - name: default-secret

    vi nginx-elb-svc.yaml

    NOTE:

    A load balancer can be automatically created when you create a service using YAML.

    • If a load balancer is available when you create a headless service, refer to the following example to edit the YAML file:
      apiVersion: v1
      kind: Service
      metadata:
        annotations:  
          kubernetes.io/elb.class: union                 
          kubernetes.io/elb.id: a172d66c-e42f-4276-aa23-9258113478f6  
          labels:
          app: nginx
        name: nginx
      spec:
        loadBalancerIP: 10.78.42.242   # IP address of the an ELB service in a public network.
        ports:
        - name: service0
          nodePort: 31540      # Access port set on the CCE console. If this parameter is not specified, the system automatically allocates an access port.
          port: 80             # Cluster virtual IP address access port, which has been registered with an ELB service.
          protocol: TCP
          targetPort: 80       # Container port set on the CCE console.
        selector:
          app: nginx
        type: LoadBalancer     # The EIP must be based on an NodePort service.
    • If a load balancer needs to be automatically created when you create a headless service, refer to the following example to edit the YAML file:
      apiVersion: v1
      kind: Service
      metadata:
        annotations:
          kubernetes.io/elb.class: union
          kubernetes.io/elb.id: 066260fe-4e6c-4553-94eb-814ad5066ebf
          service.protal.kubernetes.io/access-ip: 10.154.57.180:9999
          service.protal.kubernetes.io/type: LoadBalancer
        labels:
          app: nginx
        name: nginxsvc
      spec:
        loadBalancerIP: 10.154.57.180   # IP address of the an ELB service in a public network.
        ports:
        - name: service0
          nodePort: 31540      # Access port set on the CCE console. If this parameter is not specified, the system automatically allocates an access port.
          port: 9999             # Cluster virtual IP address access port, which has been registered with an ELB service.
          protocol: TCP
          targetPort: 80       # Container port set on the CCE console.
        selector:
          app: nginx
        type: LoadBalancer

  3. Create a containerized application.

    kubectl create -f nginx-deployment.yaml

    If the following information is displayed, the application is being created.

    deployment "nginx" created

    kubectl get po

    If the following information is displayed, the application is running.

    NAME                     READY     STATUS    RESTARTS   AGE
    icagent-m9dkt            0/0       Running   0          3d
    nginx-1212400781-qv313   1/1       Running   0          3d

  4. Create a service.

    kubectl create -f nginx-elb-svc.yaml

    If the following information is displayed, the service has been created.

    service "nginx" created

    kubectl get svc

    If the following information is displayed, the service access mode has been set successfully, and the application is accessible.

    NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
    etcd-svc     ClusterIP      None             <none>        3120/TCP       1h
    kubernetes   ClusterIP      10.247.0.1       <none>        443/TCP        3d
    nginx        LoadBalancer   10.247.130.196   10.4.10.230   80:31540/TCP   51s

  5. In the address bar of your browser, enter 10.4.10.230 and press Enter. In this example, 10.4.10.230 is the IP address of the ELB instance.

    The Nginx application is accessible.

    Figure 1 Accessing the Nginx application