This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.Chapter 9. Getting started with Knative Client
You are viewing documentation for a release of Red Hat OpenShift Serverless that is no longer supported. Red Hat OpenShift Serverless is currently supported on OpenShift Container Platform 4.3 and newer.
Knative Client (kn) is the Knative command line interface (CLI). The CLI exposes commands for managing your applications, as well as lower level tools to interact with components of OpenShift Container Platform. With kn, you can create applications and manage OpenShift Container Platform projects from the terminal.
9.1. Before you begin Copy linkLink copied to clipboard!
Knative Client does not have its own log in mechanism. To log in to the cluster you must install the oc CLI and use oc login.
Installation options for the oc CLI will vary depending on your operating system. For more information on installing the oc CLI for your operating system and logging in with oc, see the CLI getting started documentation.
9.2. Installing Knative Client Copy linkLink copied to clipboard!
9.2.1. Installing the kn CLI using the OpenShift Container Platform web console Copy linkLink copied to clipboard!
Once the OpenShift Serverless Operator is installed, you will see a link to download the kn CLI for Linux, macOS and Windows from the Command Line Tools page in the OpenShift Container Platform web console.
You can access the Command Line Tools page by clicking the
icon in the top right corner of the web console and selecting Command Line Tools in the drop down menu.
Procedure
-
Download the
knCLI from the Command Line Tools page. Unpack the archive:
tar -xf <file>
$ tar -xf <file>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Move the
knbinary to a directory on your PATH. To check your path, run:
echo $PATH
$ echo $PATHCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you do not use RHEL or Fedora, ensure that libc is installed in a directory on your library path. If libc is not available, you might see the following error when you run CLI commands:
kn: No such file or directory
$ kn: No such file or directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow
9.2.2. Installing the kn CLI for Linux using an RPM Copy linkLink copied to clipboard!
For Red Hat Enterprise Linux (RHEL), you can install kn as an RPM if you have an active OpenShift Container Platform subscription on your Red Hat account.
Procedure
-
Use the following command to install
kn:
subscription-manager register subscription-manager refresh subscription-manager attach --pool=<pool_id> subscription-manager repos --enable="openshift-serverless-1-for-rhel-8-x86_64-rpms" yum install openshift-serverless-clients
# subscription-manager register
# subscription-manager refresh
# subscription-manager attach --pool=<pool_id>
# subscription-manager repos --enable="openshift-serverless-1-for-rhel-8-x86_64-rpms"
# yum install openshift-serverless-clients
- 1
- Pool ID for an active OpenShift Container Platform subscription
9.2.3. Installing the kn CLI for Linux Copy linkLink copied to clipboard!
For Linux distributions, you can download the CLI directly as a tar.gz archive.
Procedure
- Download the CLI.
Unpack the archive:
tar -xf <file>
$ tar -xf <file>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Move the
knbinary to a directory on your PATH. To check your path, run:
echo $PATH
$ echo $PATHCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you do not use RHEL or Fedora, ensure that libc is installed in a directory on your library path. If libc is not available, you might see the following error when you run CLI commands:
kn: No such file or directory
$ kn: No such file or directoryCopy to Clipboard Copied! Toggle word wrap Toggle overflow
9.2.4. Installing the kn CLI for macOS Copy linkLink copied to clipboard!
kn for macOS is provided as a tar.gz archive.
Procedure
- Download the CLI.
- Unpack and unzip the archive.
-
Move the
knbinary to a directory on your PATH. To check your PATH, open a terminal window and run:
echo $PATH
$ echo $PATHCopy to Clipboard Copied! Toggle word wrap Toggle overflow
9.2.5. Installing the kn CLI for Windows Copy linkLink copied to clipboard!
The CLI for Windows is provided as a zip archive.
Procedure
- Download the CLI.
- Unzip the archive with a ZIP program.
-
Move the
knbinary to a directory on your PATH. To check your PATH, open the Command Prompt and run the command:
path
C:\> pathCopy to Clipboard Copied! Toggle word wrap Toggle overflow
9.3. Basic workflow using Knative Client Copy linkLink copied to clipboard!
Use this basic workflow to create, read, update, delete (CRUD) operations on a service. The following example deploys a simple Hello World service that reads the environment variable TARGET and prints its output.
Procedure
Create a service in the
defaultnamespace from an image.Copy to Clipboard Copied! Toggle word wrap Toggle overflow List the service.
kn service list
$ kn service list NAME URL LATEST AGE CONDITIONS READY REASON hello http://hello.default.apps-crc.testing hello-gsdks-1 8m35s 3 OK / 3 TrueCopy to Clipboard Copied! Toggle word wrap Toggle overflow Check if the service is working by using the
curlservice endpoint command:curl http://hello.default.apps-crc.testing
$ curl http://hello.default.apps-crc.testing Hello Knative!Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the service.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The service’s environment variable
TARGETis now set toKn.Describe the service.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the service.
kn service delete hello
$ kn service delete hello Service 'hello' successfully deleted in namespace 'default'.Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can then verify that the
helloservice is deleted by attempting tolistit.kn service list hello
$ kn service list hello No services found.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.4. Autoscaling workflow using Knative Client Copy linkLink copied to clipboard!
You can access autoscaling capabilities by using kn to modify Knative services without editing YAML files directly.
Use the service create and service update commands with the appropriate flags to configure the autoscaling behavior.
| Flag | Description |
|---|---|
|
| Hard limit of concurrent requests to be processed by a single replica. |
|
|
Recommendation for when to scale up based on the concurrent number of incoming requests. Defaults to |
|
| Maximum number of replicas. |
|
| Minimum number of replicas. |
9.5. Traffic splitting using Knative Client Copy linkLink copied to clipboard!
kn helps you control which revisions get routed traffic on your Knative service.
Knative service allows for traffic mapping, which is the mapping of revisions of the service to an allocated portion of traffic. It offers the option to create unique URLs for particular revisions and has the ability to assign traffic to the latest revision.
With every update to the configuration of the service, a new revision is created with the service route pointing all the traffic to the latest ready revision by default.
You can change this behavior by defining which revision gets a portion of the traffic.
Procedure
-
Use the
kn service updatecommand with the--trafficflag to update the traffic.
--traffic RevisionName=Percent uses the following syntax:
-
The
--trafficflag requires two values separated by separated by an equals sign (=). -
The
RevisionNamestring refers to the name of the revision. -
Percentinteger denotes the traffic portion assigned to the revision. -
Use identifier
@latestfor the RevisionName to refer to the latest ready revision of the service. You can use this identifier only once with the--trafficflag. -
If the
service updatecommand updates the configuration values for the service along with traffic flags, the@latestreference will point to the created revision to which the updates are applied. -
--trafficflag can be specified multiple times and is valid only if the sum of thePercentvalues in all flags totals 100.
For example, to route 10% of traffic to your new revision before putting all traffic on, use the following command:
kn service update svc --traffic @latest=10 --traffic svc-vwxyz=90
$ kn service update svc --traffic @latest=10 --traffic svc-vwxyz=90
9.5.1. Assigning tag revisions Copy linkLink copied to clipboard!
A tag in a traffic block of service creates a custom URL, which points to a referenced revision. A user can define a unique tag for an available revision of a service which creates a custom URL by using the format http(s)://TAG-SERVICE.DOMAIN.
A given tag must be unique to its traffic block of the service. kn supports assigning and unassigning custom tags for revisions of services as part of the kn service update command.
If you have assigned a tag to a particular revision, a user can reference the revision by its tag in the --traffic flag as --traffic Tag=Percent.
Procedure
Use the following command:
kn service update svc --tag @latest=candidate --tag svc-vwxyz=current
$ kn service update svc --tag @latest=candidate --tag svc-vwxyz=currentCopy to Clipboard Copied! Toggle word wrap Toggle overflow
--tag RevisionName=Tag uses the following syntax:
-
--tagflag requires two values separated by a=. -
RevisionNamestring refers to name of theRevision. -
Tagstring denotes the custom tag to be given for this Revision. -
Use the identifier
@latestfor the RevisionName to refer to the latest ready revision of the service. You can use this identifier only once with the--tagflag. -
If the
service updatecommand is updating the configuration values for the Service (along with tag flags),@latestreference will be pointed to the created Revision after applying the update. -
--tagflag can be specified multiple times. -
--tagflag may assign different tags to the same revision.
9.5.2. Unassigning tag revisions Copy linkLink copied to clipboard!
Tags assigned to revisions in a traffic block can be unassigned. Unassigning tags removes the custom URLs.
If a revision is untagged and it is assigned 0% of the traffic, it is removed from the traffic block entirely.
Procedure
A user can unassign the tags for revisions using the
kn service updatecommand:kn service update svc --untag candidate
$ kn service update svc --untag candidateCopy to Clipboard Copied! Toggle word wrap Toggle overflow
--untag Tag uses the following syntax:
-
The
--untagflag requires one value. -
The
tagstring denotes the unique tag in the traffic block of the service which needs to be unassigned. This also removes the respective custom URL. -
The
--untagflag can be specified multiple times.
9.5.3. Traffic flag operation precedence Copy linkLink copied to clipboard!
All traffic-related flags can be specified using a single kn service update command. kn defines the precedence of these flags. The order of the flags specified when using the command is not taken into account.
The precedence of the flags as they are evaluated by kn are:
-
--untag: All the referenced revisions with this flag are removed from the traffic block. -
--tag: Revisions are tagged as specified in the traffic block. -
--traffic: The referenced revisions are assigned a portion of the traffic split.
9.5.4. Traffic splitting flags Copy linkLink copied to clipboard!
kn supports traffic operations on the traffic block of a service as part of the kn service update command.
The following table displays a summary of traffic splitting flags, value formats, and the operation the flag performs. The "Repetition" column denotes whether repeating the particular value of flag is allowed in a kn service update command.
| Flag | Value(s) | Operation | Repetition |
|---|---|---|---|
|
|
|
Gives | Yes |
|
|
|
Gives | Yes |
|
|
|
Gives | No |
|
|
|
Gives | Yes |
|
|
|
Gives | No |
|
|
|
Removes | Yes |