By Li Peng (Yuanyi)
Kn is a client for Knative command line operation. It allows users to operate the related resources of Knative easily.
You can download the corresponding Kn binary from the official websites below:
Kn uses the same config file as kubectl. The default file is $HOME/.kube/config
.
You can use Kn to perform operations for Knative services, including list, create, delete, apply, and update.
1. Run create:
kn service create
For example:
$ kn service create helloworld-go --env TARGET=knative --image registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56
yuncs.com/knative-sample/helloworld-go:73fbdd56
Creating service 'helloworld-go' in namespace 'default':
0.116s The Route is still working to reflect the latest desired specification.
0.216s Configuration "helloworld-go" is waiting for a Revision to become ready.
3.075s ...
3.149s Ingress has not yet been reconciled.
3.299s ...
3.762s Ready to serve.
Service 'helloworld-go' created to latest revision 'helloworld-go-pyqqz-1' is available at URL:
http://helloworld-go.default.example.com
2. Run apply:
kn service apply
For example:
# Create an initial service with using 'kn service apply', if the service has not
# been already created
kn service apply s0 --image registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56
# Apply the service again which is a no-operation if none of the options changed
kn service apply s0 --image registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56
# Add an environment variable to your service. Note, that you have to always fully
# specify all parameters (in contrast to 'kn service update')
kn service apply s0 --image registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56 --env TARGET=knative
# Read the service declaration from a file
kn service apply s0 --filename my-svc.yml
Query service lists:
kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
helloworld-go http://helloworld-go.default.example.com helloworld-go-pyqqz-1 55s 3 OK / 3 True
Query service details:
kn service describe helloworld-go
Name: helloworld-go
Namespace: default
Age: 14m
URL: http://helloworld-go.default.example.com
Revisions:
100% @latest (helloworld-go-cvclp-2) [2](9m)
Image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8 (pinned to 032b80)
Conditions:
OK TYPE AGE REASON
++ Ready 9m
++ ConfigurationsReady 9m
++ RoutesReady 9m
Details in the YAML format:
$ kn service describe helloworld-go -oyaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
annotations:
serving.knative.dev/creator: 1041208914252405-1606806762
serving.knative.dev/lastModifier: 1041208914252405-1606806762
creationTimestamp: "2020-12-09T06:07:41Z"
generation: 2
name: helloworld-go
namespace: default
resourceVersion: "39555635"
selfLink: /apis/serving.knative.dev/v1/namespaces/default/services/helloworld-go
uid: 763e8214-7c2d-476c-bd88-40b32a30597d
spec:
template:
metadata:
annotations:
client.knative.dev/user-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8
creationTimestamp: null
name: helloworld-go-cvclp-2
spec:
containerConcurrency: 0
containers:
- env:
- name: TARGET
value: knative
image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8
name: user-container
readinessProbe:
successThreshold: 1
tcpSocket:
port: 0
resources: {}
timeoutSeconds: 300
traffic:
- latestRevision: true
percent: 100
status:
address:
url: http://helloworld-go.default.svc.cluster.local
conditions:
- lastTransitionTime: "2020-12-09T06:13:33Z"
status: "True"
type: ConfigurationsReady
- lastTransitionTime: "2020-12-09T06:13:34Z"
status: "True"
type: Ready
- lastTransitionTime: "2020-12-09T06:13:34Z"
status: "True"
type: RoutesReady
latestCreatedRevisionName: helloworld-go-cvclp-2
latestReadyRevisionName: helloworld-go-cvclp-2
observedGeneration: 2
traffic:
- latestRevision: true
percent: 100
revisionName: helloworld-go-cvclp-2
url: http://helloworld-go.default.example.com
3. Run update:
kn service update NAME
For example:
$ kn service update helloworld-go --image registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8
Updating Service 'helloworld-go' in namespace 'default':
3.609s Traffic is not yet migrated to the latest revision.
3.631s Ingress has not yet been reconciled.
4.066s Ready to serve.
Service 'helloworld-go' updated to latest revision 'helloworld-go-cvclp-2' is available at URL:
http://helloworld-go.default.example.com
Run apply for service update:
kn service apply
# Add an environment variable to your service. Note, that you have to always fully
# specify all parameters (in contrast to 'kn service update')
kn service apply s0 --image registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8 --env TARGET=knative
For example:
kn service delete helloworld-go
kn revision list
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
helloworld-go-cvclp-2 helloworld-go 100% 2 40s 4 OK / 4 True
helloworld-go-pyqqz-1 helloworld-go 1 6m29s 3 OK / 4 True
$ kn revision describe helloworld-go-cvclp-2
Name: helloworld-go-cvclp-2
Namespace: default
Age: 3m
Image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8 (pinned to 032b80)
Env: TARGET=knative
Service: helloworld-go
Conditions:
OK TYPE AGE REASON
++ Ready 3m
++ ContainerHealthy 3m
++ ResourcesAvailable 3m
I Active 2m NoTraffic
$kn revision delete helloworld-go-pyqqz -1
Revision 'helloworld-go-pyqqz-1' deleted in namespace'default'.
In addition to the service and revision mentioned above, Kn also supports the following resource operations:
GitHub Actions + ACK: A Powerful Combination for Cloud-Native DevOps Implementation
The Service Discovery Principle of DNS in Kubernetes Clusters
166 posts | 30 followers
FollowAlibaba Cloud Native Community - November 15, 2023
Alibaba Cloud Native Community - November 23, 2023
Alibaba Cloud Native Community - April 9, 2024
Alibaba Container Service - February 7, 2020
Alibaba Cloud MaxCompute - February 18, 2024
Alibaba Container Service - July 22, 2021
166 posts | 30 followers
FollowAlibaba Cloud Container Service for Kubernetes is a fully managed cloud container management service that supports native Kubernetes and integrates with other Alibaba Cloud products.
Learn MoreProvides a control plane to allow users to manage Kubernetes clusters that run based on different infrastructure resources
Learn MoreAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreHigh Performance Computing (HPC) and AI technology helps scientific research institutions to perform viral gene sequencing, conduct new drug research and development, and shorten the research and development cycle.
Learn MoreMore Posts by Alibaba Container Service