$ kn service create hello --image docker.io/openshift/hello-openshift --env RESPONSE="Hello Serverless!"
The Knative kn
CLI extends the functionality of the oc
or kubectl
CLI tools to integrate interaction with Knative components on OpenShift Container Platform. kn
allows developers to deploy and manage applications without editing YAML files directly.
kn
The following basic workflow deploys a simple hello
service that reads the environment variable RESPONSE
and prints its output.
You can use this guide as a reference to perform create, read, update, and delete (CRUD) operations on a service.
Create a service in the default
namespace from an image:
$ kn service create hello --image docker.io/openshift/hello-openshift --env RESPONSE="Hello Serverless!"
Creating service 'hello' in namespace 'default':
0.085s The Route is still working to reflect the latest desired specification.
0.101s Configuration "hello" is waiting for a Revision to become ready.
11.590s ...
11.650s Ingress has not yet been reconciled.
11.726s Ready to serve.
service 'hello' created with latest revision 'hello-gsdks-1' and URL:
http://hello-default.apps-crc.testing
List the service:
$ kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
hello http://hello-default.apps-crc.testing hello-gsdks-1 8m35s 3 OK / 3 True
Check if the service is working by using the curl
service endpoint command:
$ curl http://hello-default.apps-crc.testing
Hello Serverless!
Update the service:
$ kn service update hello --env RESPONSE="Hello OpenShift!"
Updating service 'hello' in namespace 'default':
10.136s Traffic is not yet migrated to the latest revision.
10.175s Ingress has not yet been reconciled.
10.348s Ready to serve.
service 'hello' updated with latest revision 'hello-dghll-2' and URL:
http://hello-default.apps-crc.testing
The service’s environment variable RESPONSE
is now set to "Hello OpenShift!".
Describe the service.
$ kn service describe hello
Name: hello
Namespace: default
Age: 13m
URL: http://hello-default.apps-crc.testing
Revisions:
100% @latest (hello-dghll-2) [2] (1m)
Image: docker.io/openshift/hello-openshift (pinned to 5ea96b)
Conditions:
OK TYPE AGE REASON
++ Ready 1m
++ ConfigurationsReady 1m
++ RoutesReady 1m
Delete the service:
$ kn service delete hello
service 'hello' successfully deleted in namespace 'default'.
Verify that the hello
service is deleted by attempting to list it:
$ kn service list hello
No services found.
kn
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. |
kn
You can use kn
to control which revisions get routed traffic on your Knative service.
Knative service supports 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.
Use the kn service update
command with the --traffic
flag to update the traffic.
For example, to route 10% of traffic to a new revision before putting all traffic on:
$ kn service update svc --traffic @latest=10 --traffic svc-vwxyz=90
--traffic RevisionName=Percent
uses the following syntax:
The --traffic
flag requires two values separated by separated by an equals sign (=
).
The RevisionName
string refers to the name of the revision.
Percent
integer denotes the traffic portion assigned to the revision.
Use identifier @latest
for the RevisionName to refer to the latest ready revision of the service. You can use this identifier only once with the --traffic
flag.
If the service update
command updates the configuration values for the service along with traffic flags, the @latest
reference points to the created revision to which the updates are applied.
--traffic
flag can be specified multiple times and is valid only if the sum of the Percent
values in all flags totals 100.
A tag in a traffic block of a 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 |
Assign tag revisions by updating the service:
$ kn service update svc --tag @latest=candidate --tag svc-vwxyz=current
--tag RevisionName=Tag
uses the following syntax:
--tag
flag requires two values separated by a =
.
RevisionName
string refers to name of the Revision
.
Tag
string denotes the custom tag to be given for this Revision.
Use the identifier @latest
for the RevisionName
to refer to the latest ready revision of the service. You can use this identifier only once with the --tag
flag.
If the service update
command is updating the configuration values for the service, along with tag flags, the @latest
reference points to the created revision after applying the update.
--tag
flag can be specified multiple times.
--tag
flag may assign different tags to the same revision.
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. |
Unassign tags for revisions using the kn service update
command:
$ kn service update svc --untag candidate
--untag Tag
uses the following syntax:
The --untag
flag requires one value.
The tag
string denotes the unique tag in the traffic block of the service which needs to be unassigned. This also removes the respective custom URL.
The --untag
flag can be specified multiple times.
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.
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 |