You can use a master instance on Container Service for Kubernetes (ACK) One to deploy an application across multiple clusters that are associated with the master instance. To use the preceding method, you must create Kubernetes resources, such as Deployments, Services, and Ingresses, for the application. Then, you must specify the clusters in which you want to deploy the application and create the application. You can view the deployment progress of the application in each cluster. This topic describes how to use a master instance on ACK One to deploy an application across multiple clusters.

Prerequisites

  • The kubeconfig file of the master instance is obtained from the ACK One console by using an account with the developer role. A kubectl client is connected to the master instance.

  • Namespaces and resource quotas are configured by the master instance administrator. For more information, see Manage namespaces and resource quotas.

  • Multiple ACK clusters are associated with the master instance. For more information, see Associate clusters with a master instance.
  • The AMC command-line tool is installed. For more information, see Use AMC.

Create an application

  1. Run the following command to create a namespace.
    Replace <your-namespace> with the name of the namespace that you want to create.
    kubectl create namespace <your-namespace>
  2. Create a file named app-meta.yaml with the following content. The file defines the Kubernetes resources that are required to deploy an application.
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: demo
      name: demo
      namespace: demo
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: demo
      template:
        metadata:
          labels:
            app: demo
        spec:
          containers:
          - image: registry.cn-hangzhou.aliyuncs.com/acs/rollouts-demo:red
            name: demo
    ---
    apiVersion: v1
    kind: Secret
    metadata:
      name: demo
      namespace: demo
      labels:
        app: demo
    type: Opaque
    data:
      version: MQ==
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: demo-1
      namespace: demo
      labels:
        app: demo
    data:
      version: "1"
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: demo
      namespace: demo
      labels:
        app: demo
    spec:
      selector:
        app: demo
      ports:
        - protocol: TCP
          port: 80
          targetPort: 8080
    ---
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: demo
      namespace: demo
      labels:
        app: demo
    spec:
      rules:
        - host: app.demo.example.com
          http:
            paths:
            - path: /
              pathType: Prefix
              backend:
                service:
                  name: demo
                  port:
                    number: 80
  3. Run the following command to deploy an application named demo on the master instance:
    kubectl apply -f app-meta.yaml
  4. Create a file named app.yaml with the following content. The file defines a KubeVela application named demo.
    1. Run the following command to query the clusters that are associated with the master instance:
      kubectl get managedclusters

      Expected output:

      NAME                     HUB ACCEPTED   MANAGED CLUSTER URLS   JOINED   AVAILABLE   AGE
      managedcluster-c5***z9   true                                  True     True        12d
      managedcluster-c1***e5   true                                  True     True        12d
    2. Replace <clusterid1> and <clusterid2> with the clusters id in which you want to deploy the application.
      apiVersion: core.oam.dev/v1beta1
      kind: Application
      metadata:
        name: demo
        namespace: demo
        annotations:
          app.oam.dev/publishVersion: version1
      spec:
        components:
          - name: demo
            type: ref-objects
            properties:
              objects:
                - apiVersion: apps/v1
                  kind: Deployment
                  name: demo
                - apiVersion: v1
                  kind: ConfigMap
                  selector:
                    app: demo
                - apiVersion: v1
                  kind: Secret
                  name: demo
                - apiVersion: v1
                  kind: Service
                  name: demo
                - apiVersion: networking.k8s.io/v1
                  kind: Ingress
                  name: demo
        policies:
          - type: topology
            name: beijing-clusters
            properties:
              clusters: ["<clusterid1>", "<clusterid2>"]  # Specify the cluster id in which you want to deploy the application. You can add cluster names or modify the existing cluster names. 
                              
      Parameter Description
      annotations. app.oam.dev/publishVersion The version of the application. If an application or the Kubernetes resources that are used by an application are updated, you must change the value of this parameter to trigger the master instance to synchronize the updates to the clusters.
      components. type: ref-objects The apiVersion, kind, and name parameters are used to reference Kubernetes resources in the same namespace. The master instance deploys these resources in the specified clusters.
      • ACK One supports the following Kubernetes resources: Deployment/apps/v1, Secret/v1, ConfigMap/v1, and Ingress/networking.k8s.io/v1.
      • If you require other resources, Submit a ticket to contact the ACK One team.
      policies. type: topology Set the type of the policy to topology. A topology policy specifies the destinations to which Kubernetes resources are deployed.
      policies. properties. clusters You can specify destinations only by cluster id. Each destination can contain one or more clusters that are associated with the master instance.
  5. Run the following command to deploy the application demo on the master instance:
    kubectl apply -f app.yaml
  6. Run the following command to query the deployment progress of the application:
    kubectl get app demo -n demo

    Expected output:

    NAME   COMPONENT   TYPE          PHASE     HEALTHY   STATUS   AGE
    demo   demo        ref-objects   running   true               24m
  7. Query the deployment progress of the Kubernetes resources in each destination cluster.
    kubectl get app demo -n demo -o yaml
    ......
    status:
      appliedResources:
      - apiVersion: apps/v1
        cluster: <clusterid1>
        creator: workflow
        kind: Deployment
        name: demo
        namespace: demo
      - apiVersion: v1
        cluster: <clusterid1>
        creator: workflow
        kind: ConfigMap
        name: demo-1
        namespace: demo
      - apiVersion: v1
        cluster: <clusterid1>
        creator: workflow
        kind: Secret
        name: demo
        namespace: demo
      - apiVersion: networking.k8s.io/v1
        cluster: <clusterid1>
        creator: workflow
        kind: Ingress
        name: demo
        namespace: demo
      - apiVersion: apps/v1
        cluster: <clusterid2>
        creator: workflow
        kind: Deployment
        name: demo
        namespace: demo
      - apiVersion: v1
        cluster: <clusterid2>
        creator: workflow
        kind: ConfigMap
        name: demo-1
        namespace: demo
      - apiVersion: v1
        cluster: <clusterid2>
        creator: workflow
        kind: Secret
        name: demo
        namespace: demo
      - apiVersion: networking.k8s.io/v1
        cluster: <clusterid2>
        creator: workflow
        kind: Ingress
        name: demo
        namespace: demo
  8. Run the following command to query the status of Kubernetes resources in a specified destination cluster:
    Replace <clusterid1> with the clusters id in which you want to query.
    kubectl amc get deployment demo -n demo -m <clusterid1>

    Expected output:

    Run on ManagedCluster <clusterid1>
    NAME   READY   UP-TO-DATE   AVAILABLE   AGE
    demo   1/1     1            1           28m
    kubectl amc get pod -n demo -m <clusterid1>

    Expected output:

    Run on ManagedCluster <clusterid1>
    NAME                    READY   STATUS      RESTARTS   AGE
    demo-7c96b5bffd-****   1/1     Running     0          30m

Update the Kubernetes resources that are used by an application

To update the Kubernetes resources that are used by an application, perform the following steps:

  1. Run the kubectl apply command to modify the Kubernetes resources that are deployed in Step 2.
    You can modify the Kubernetes resources based on your business requirements. For example, you can modify the OS image in a Deployment, the port of a Service, or the Secret in a ConfigMap. The updates are automatically synchronized to the destination clusters.
  2. Run the kubectl apply command to modify the Kubernetes resources that are deployed for the application demo in Step 4. You must change the value of annotations: app.oam.dev/publishVersion: version1 to trigger the master instance to deploy the updated Kubernetes resources in the destination clusters.
    For example, you can change the value of app.oam.dev/publishVersion to version2.
  3. Repeat Steps 6, 7, and 8 to query the status of the application and resources.

Delete an application

Run the following command to delete an application. After you delete an application, the Kubernetes resources that are deployed in the destination clusters for the application are also deleted.

kubectl delete app demo -n demo