Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

Chapter 1. Knative Serving CLI commands


1.1. kn service commands

You can use the following commands to create and manage Knative services.

1.1.1. Creating serverless applications by using the Knative CLI

Using the Knative (kn) CLI to create serverless applications provides a more streamlined and intuitive user interface over modifying YAML files directly. You can use the kn service create command to create a basic serverless application.

Prerequisites

  • You have installed OpenShift Serverless Operator and Knative Serving on your cluster.
  • You have installed the Knative (kn) CLI.
  • You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in OpenShift Container Platform.

Procedure

  • Create a Knative service:

    $ kn service create <service_name> --image <image> --tag <tag_value>

    Where:

    • --image is the URI of the image for the application.
    • --tag is an optional flag that you can use to add a tag to the initial revision that Knative creates with the service.

      You get an output similar to the following example command:

      $ kn service create showcase \
          --image quay.io/openshift-knative/showcase

      You get an output similar to the following example:

      Creating service 'showcase' in namespace 'default':
      
        0.271s The Route is still working to reflect the latest desired specification.
        0.580s Configuration "showcase" is waiting for a Revision to become ready.
        3.857s ...
        3.861s Ingress has not yet been reconciled.
        4.270s Ready to serve.
      
      Service 'showcase' created with latest revision 'showcase-00001' and URL:
      http://showcase-default.apps-crc.testing

1.1.2. Updating serverless applications by using the Knative CLI

You can use the kn service update command for interactive sessions on the command line as you build up a service incrementally. In contrast to the kn service apply command, when using the kn service update command you only have to specify the changes that you want to update, rather than the full configuration for the Knative service.

Procedure

  1. Update a service by adding a new environment variable by running the command:

    $ kn service update <service_name> --env <key>=<value>
  2. Update a service by adding a new port by running the command:

    $ kn service update <service_name> --port 80
  3. Update a service by adding new request and limit parameters by running the command:

    $ kn service update <service_name> --request cpu=500m --limit memory=1024Mi --limit cpu=1000m
  4. Assign the latest tag to a revision by running the command:

    $ kn service update <service_name> --tag <revision_name>=latest
  5. Update a tag from testing to staging for the latest READY revision of a service by running the command:

    $ kn service update <service_name> --untag testing --tag @latest=staging
  6. Add the test tag to a revision that receives 10% of traffic, and send the rest of the traffic to the latest READY revision of a service:

    $ kn service update <service_name> --tag <revision_name>=test --traffic test=10,@latest=90

1.1.3. Applying service declarations

Use the kn service apply command to declaratively configure a Knative service. If the service does not exist, the command creates it. If it exists, the command updates the service with the specified options.

The kn service apply command works well in shell scripts or CI pipelines. In these environments, users often specify the complete service state in a single command. When you use kn service apply, give the full configuration for the Knative service. The kn service update command only requires the options that you want to change.

Procedure

  1. Create a service by running the command:

    $ kn service apply <service_name> --image <image>
  2. Add an environment variable to a service by running the command:

    $ kn service apply <service_name> --image <image> --env <key>=<value>
  3. Read the service declaration from a JSON or YAML file by running the command:

    $ kn service apply <service_name> -f <filename>

1.1.4. Describing serverless applications by using the Knative CLI

You can describe a Knative service by using the kn service describe command.

Procedure

  1. Describe a service by running the command:

    $ kn service describe --verbose <service_name>

    The --verbose flag is optional and provides a more detailed description. The following examples show the difference between regular and verbose output:

    You get an output similar to the following example:

    Name:       showcase
    Namespace:  default
    Age:        2m
    URL:        http://showcase-default.apps.ocp.example.com
    
    Revisions:
      100%  @latest (showcase-00001) [1] (2m)
            Image:  quay.io/openshift-knative/showcase (pinned to aaea76)
    
    Conditions:
      OK TYPE                   AGE REASON
      ++ Ready                   1m
      ++ ConfigurationsReady     1m
      ++ RoutesReady             1m

    You get an output similar to the following example:

    Name:         showcase
    Namespace:    default
    Annotations:  serving.knative.dev/creator=system:admin
                  serving.knative.dev/lastModifier=system:admin
    Age:          3m
    URL:          http://showcase-default.apps.ocp.example.com
    Cluster:      http://showcase.default.svc.cluster.local
    
    Revisions:
      100%  @latest (showcase-00001) [1] (3m)
            Image:  quay.io/openshift-knative/showcase (pinned to aaea76)
            Env:    GREET=Bonjour
    
    Conditions:
      OK TYPE                   AGE REASON
      ++ Ready                   3m
      ++ ConfigurationsReady     3m
      ++ RoutesReady             3m
  2. Describe a service in YAML format by running the command:

    $ kn service describe <service_name> -o yaml
  3. Describe a service in JSON format by running the command:

    $ kn service describe <service_name> -o json
  4. Print the service URL only by running the command:

    $ kn service describe <service_name> -o url

1.2. kn service commands in offline mode

Overview of kn service commands in offline mode for managing service configurations without cluster deployment.

1.2.1. About the Knative CLI offline mode

When you run kn service commands, the changes immediately propagate to the cluster or you can run kn service commands in offline mode. When you create a service in offline mode, the command does not change the cluster and creates the service descriptor file on your local machine.

Important

The offline mode of the Knative CLI is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.

For more information about the support scope of Red Hat Technology Preview features, see Technology Preview Features Support Scope.

After you create the descriptor file, you can manually change it and track it in a version control system. You can also propagate changes to the cluster by running kn service create -f, kn service apply -f, or oc apply -f on the descriptor file.

The offline mode has several uses:

  • You can manually change the descriptor file before using it to make changes on the cluster.
  • You can track the service descriptor file locally in a version control system. You can then reuse the descriptor file in other environments, such as continuous integration (CI) pipelines, development environments, or demos.
  • You can examine the descriptor files to learn about Knative services. You can also see how different arguments passed to the kn command influence the resulting service.

Offline mode has several advantages. It is fast and does not require a connection to the cluster. However, offline mode lacks server-side validation. For example, you cannot verify that the service name is unique or that the cluster can pull the specified image.

1.2.2. Creating a service using offline mode

You can run kn service commands in offline mode so the command does not change the cluster and instead creates a service descriptor file on your local machine. After you create the descriptor file, you can change it before propagating changes to the cluster.

Important

The offline mode of the Knative CLI is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.

For more information about the support scope of Red Hat Technology Preview features, see Technology Preview Features Support Scope.

Prerequisites

  • You have installed OpenShift Serverless Operator and Knative Serving on your cluster.
  • You have installed the Knative (kn) CLI.

Procedure

  1. In offline mode, create a local Knative service descriptor file:

    $ kn service create showcase \
        --image quay.io/openshift-knative/showcase \
        --target ./ \
        --namespace test

    You get an output similar to the following example:

    Service 'showcase' created in namespace 'test'.
    • The --target ./ flag enables offline mode and specifies ./ as the directory for storing the new directory tree.

      If you do not specify an existing directory but use a filename, such as --target my-service.yaml, the command does not create a directory tree. Instead, the command creates the service descriptor file my-service.yaml in the current directory.

      The filename can have the .yaml, .yml, or .json extension. Choosing .json creates the service descriptor file in the JSON format.

    • The --namespace test option places the new service in the test namespace.

      If you do not use --namespace and you log in to an OpenShift Container Platform cluster, the command creates the descriptor file in the current namespace. If you do not log in to a cluster, the command creates the descriptor file in the default namespace.

  2. Examine the created directory structure:

    $ tree ./

    You get an output similar to the following example:

    ./
    └── test
        └── ksvc
            └── showcase.yaml
    
    2 directories, 1 file
    • The ./ directory specified with --target now has a test/ directory named after the specified namespace.
    • The test/ directory has the ksvc directory, named after the resource type.
    • The ksvc directory has the descriptor file showcase.yaml, named according to the specified service name.
  3. Examine the generated service descriptor file:

    $ cat test/ksvc/showcase.yaml

    You get an output similar to the following example:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      creationTimestamp: null
      name: showcase
      namespace: test
    spec:
      template:
        metadata:
          annotations:
            client.knative.dev/user-image: quay.io/openshift-knative/showcase
          creationTimestamp: null
        spec:
          containers:
          - image: quay.io/openshift-knative/showcase
            name: ""
            resources: {}
    status: {}
  4. List information about the new service:

    $ kn service describe showcase --target ./ --namespace test

    You get an output similar to the following example:

    Name:       showcase
    Namespace:  test
    Age:
    URL:
    
    Revisions:
    
    Conditions:
      OK TYPE    AGE REASON
    • The --target ./ option specifies the root directory for the directory structure containing namespace subdirectories.

      You can also directly specify a YAML or JSON filename with the --target option. The accepted file extensions are .yaml, .yml, and .json.

    • The --namespace option specifies the namespace, which communicates to kn the subdirectory that has the necessary service descriptor file.

      If you do not use --namespace and you log in to an OpenShift Container Platform cluster, kn searches for the service in the subdirectory that matches the current namespace. Otherwise, kn searches in the default/ subdirectory.

  5. Use the service descriptor file to create the service on the cluster:

    $ kn service create -f test/ksvc/showcase.yaml

    You get an output similar to the following example:

    Creating service 'showcase' in namespace 'test':
    
      0.058s The Route is still working to reflect the latest desired specification.
      0.098s ...
      0.168s Configuration "showcase" is waiting for a Revision to become ready.
     23.377s ...
     23.419s Ingress has not yet been reconciled.
     23.534s Waiting for load balancer to be ready
     23.723s Ready to serve.
    
    Service 'showcase' created to latest revision 'showcase-00001' is available at URL:
    http://showcase-test.apps.example.com

1.3. kn container commands

You can use the following commands to create and manage many containers in a Knative service spec.

1.3.1. Knative client multi-container support

You can use the kn container add command to print the YAML container specification to standard output. Use this command for multi-container use cases and combine it with other kn flags to create definitions.

The kn container add command accepts all container-related flags that the kn service create command supports. You can also chain the kn container add command by using UNIX pipes (|) to create many container definitions.

Example commands
  • You can add a container from an image and print it to standard output:

    $ kn container add <container_name> --image <image_uri>

    You get an output similar to the following example command:

    $ kn container add sidecar --image docker.io/example/sidecar

    You get an output similar to the following example:

    containers:
    - image: docker.io/example/sidecar
      name: sidecar
      resources: {}
  • You can chain two kn container add commands together, and then pass them to a kn service create command to create a Knative service with two containers:

    $ kn container add <first_container_name> --image <image_uri> | \
    kn container add <second_container_name> --image <image_uri> | \
    kn service create <service_name> --image <image_uri> --extra-containers -

    --extra-containers - specifies a special case where kn reads the pipe input instead of a YAML file.

    You get an output similar to the following example command:

    $ kn container add sidecar --image docker.io/example/sidecar:first | \
    kn container add second --image docker.io/example/sidecar:second | \
    kn service create my-service --image docker.io/example/my-app:latest --extra-containers -

    The --extra-containers flag can also accept a path to a YAML file:

    $ kn service create <service_name> --image <image_uri> --extra-containers <filename>

    You get an output similar to the following example command:

    $ kn service create my-service --image docker.io/example/my-app:latest --extra-containers my-extra-containers.yaml

1.4. kn domain commands

You can use the following commands to create and manage domain mappings.

1.4.1. Creating a custom domain mapping by using the Knative CLI

You can use Knative CLI to map custom domains to Knative services or routes and assign branded or user-friendly URLs to your applications. Use your own domain names instead of the default cluster-generated URLs.

Prerequisites

  • You have installed the OpenShift Serverless Operator and Knative Serving on your cluster.
  • You have created a Knative service or route, and control a custom domain that you want to map to that CR.
  • Your custom domain must point to the DNS of the OpenShift Container Platform cluster.
  • You have installed the Knative (kn) CLI.
  • You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in OpenShift Container Platform.

Procedure

  • Map a domain to a CR in the current namespace:

    $ kn domain create <domain_mapping_name> --ref <target_name>

    You get an output similar to the following example command:

    $ kn domain create example.com --ref showcase

    The --ref flag specifies an Addressable target CR for domain mapping.

    If you do not give a prefix when using the --ref flag, the system treats the target as a Knative service in the current namespace.

  • Map a domain to a Knative service in a specified namespace:

    $ kn domain create <domain_mapping_name> --ref <ksvc:service_name:service_namespace>

    You get an output similar to the following example command:

    $ kn domain create example.com --ref ksvc:showcase:example-namespace
  • Map a domain to a Knative route:

    $ kn domain create <domain_mapping_name> --ref <kroute:route_name>

    You get an output similar to the following example command:

    $ kn domain create example.com --ref kroute:example-route

1.4.2. Managing custom domain mappings by using the Knative CLI

After you have created a DomainMapping custom resource (CR), you can list existing CRs, view information about an existing CR, update CRs, or delete CRs by using the Knative (kn) CLI.

Prerequisites

  • You have installed the OpenShift Serverless Operator and Knative Serving on your cluster.
  • You have created at least one DomainMapping CR.
  • You have installed the Knative (kn) CLI tool.
  • You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in OpenShift Container Platform.

Procedure

  1. List existing DomainMapping CRs:

    $ kn domain list -n <domain_mapping_namespace>
  2. View details of an existing DomainMapping CR:

    $ kn domain describe <domain_mapping_name>
  3. Update a DomainMapping CR to point to a new target:

    $ kn domain update --ref <target>
  4. Delete a DomainMapping CR:

    $ kn domain delete <domain_mapping_name>
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2026 Red Hat
Nach oben