Knative 快捷操作命令 Kn 介绍

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: kn 是 Knative 命令行操作客户端。 通过 kn 可以方便的操作Knative 相关的资源。

kn 是 Knative 命令行操作客户端。 通过 kn 可以方便的操作Knative 相关的资源。

安装 Kn

可以通过官方获取对应的 Kn 二进制
macOS
Linux
Windows
kn 会与 kubectl 使用同样的config 文件,默认使用 $HOME/.kube/config

服务管理

使用 Kn 命令可以执行: list, create, delete, apply, 和 update 操作Knative services.

创建服务

使用 create 命令:
kn service create
示例:

$ 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

使用 apply 命令:
kn service apply
示例:

# 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

查看服务

查询服务列表示例:

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

查询服务详情示例:

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

或者以 yaml 方式展示详情:

$ 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

更新服务(创建新版本)

使用 update 命令:
kn service update NAME
示例:

$ 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

使用 apply 也可进行更新服务

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

删除服务

示例:

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'.

其它

除了上面介绍的 service、revision。 Kn 还支持如下的资源操作, 有兴趣的可以自己试用一下。
• kn broker - 管理事件 brokers
• kn channel - 管理事件 channels
• kn route - 管理服务路由 routes
• kn source - 管理事件源 sources
• kn trigger - 管理事件 triggers

目录
相关文章
|
9月前
|
存储 Kubernetes 容器
百度搜索:蓝易云【Kubernetes使用helm部署NFS Provisioner】
现在,你已经成功使用Helm部署了NFS Provisioner,并且可以在Kubernetes中创建使用NFS存储的PersistentVolumeClaim。
220 10
|
9月前
|
Kubernetes 负载均衡 应用服务中间件
k8s学习-Ingress(安装、模板、创建、删除)
k8s学习-Ingress(安装、模板、创建、删除)
320 0
|
Kubernetes Shell Linux
1分钟配置Kubectl命令自动补全功能
1分钟配置Kubectl命令自动补全功能
249 0
|
Kubernetes Shell 容器
Kubernetes的kubectl命令补全
Kubernetes的kubectl命令补全
271 0
|
7月前
|
存储 Kubernetes API
apisix~为自定义插件设计一个configmap脚本
Kubernetes ConfigMap*是一种资源对象,用于存储非敏感配置数据,如环境变量和配置文件,解耦配置与应用,支持动态更新。它可以被挂载到Pod中,跨Namespace共享。通过`kubectl create configmap`命令创建。例如,一个apisix插件配置可能包含3个lua文件,通过`kubectl create configmap`命令将它们整合到一个ConfigMap中供apisix使用。ConfigMap不同于Secret,用于存储非敏感信息。
|
9月前
|
Java 测试技术 开发者
ChaosBlade编辑问题之编辑演练内容如何解决
ChaosBlade 是一个开源的混沌工程实验工具,旨在通过模拟各种常见的硬件、软件、网络、应用等故障,帮助开发者在测试环境中验证系统的容错和自动恢复能力。以下是关于ChaosBlade的一些常见问题合集:
|
9月前
|
Kubernetes 调度 Docker
Kubernetes常用操作命令
Kubernetes常用操作命令
136 0
|
运维 Kubernetes Shell
Kubectl命令再也不用记了,自动补全提示真好
Kubectl命令再也不用记了,自动补全提示真好
178 0
|
Ubuntu Unix Shell
Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
334 0
|
JSON Prometheus Kubernetes
k8s小工具之kubectl-mytop插件
Kubectl-mytop是一个基于metrics-server组件做指标展示的命令行工具,综合kubectl top以及kubectl describe的展示能力,可以提供Kubernetes集群中node以及pod资源请求、资源限制、资源利用率、以及pod数量做排序展示。Kubectl-mytop插件的目的在于弥补当前常用k8s监控工具的不足,便于如资源不均,调度不均,使用率不均等问题的分析定位。如指定node后,可以查看这个node上所有的pod的request,limit,util资源使用等信息并排序输出,支持指定不同namespace的相同的pod label的资源展示,以及过滤指定
410 0

热门文章

最新文章