美文网首页
knative hello-world

knative hello-world

作者: myonlyzzy | 来源:发表于2019-11-28 18:19 被阅读0次

    knative helloworld

    像学习任何一门编程语言一样,先不管实现原理,基础架构.先跑起来一个helloworld,helloworld跑通之后,获的一点点成就感和一个总体的认识之后再研究基础的东西.认识knative也是如此,先把knative的helloworld 跑通之后再深揪基本原理.

    install knative

    安装knative之前必须安装好istio,关于istio的安装请参考前面的介绍.

    1. 安装knative
    kubectl apply --selector knative.dev/crd-install=true \
    --filename https://github.com/knative/serving/releases/download/v0.10.0/serving.yaml \
    --filename https://github.com/knative/eventing/releases/download/v0.8.1/release.yaml \
    --filename https://github.com/knative/serving/releases/download/v0.10.0/monitoring.yaml
    
    kubectl apply  \
    --filename https://github.com/knative/serving/releases/download/v0.10.0/serving.yaml \
    --filename https://github.com/knative/eventing/releases/download/v0.8.1/release.yaml \
    --filename https://github.com/knative/serving/releases/download/v0.10.0/monitoring.yaml
    
    1. 替换gcr.io 镜像

    由于gcr.io被gfw墙了,很多gcr.io的镜像国内拉不下来.可以使用gcr.azk8s.cn来替换

     kubectl get deployment webhook  -n knative-serving -o yaml >test.yaml && sed  "s#gcr.io#gcr.azk8s.cn#g" test.yaml  >test-new.yaml && kubectl replace -f test-new.yaml   
    

    部署第一个helloworld

    cat <<EOF > knative-helloworld.yaml
    apiVersion: serving.knative.dev/v1 # Current version of Knative
    kind: Service
    metadata:
      name: helloworld-go # The name of the app
      namespace: default # The namespace the app will use
    spec:
      template:
        spec:
          containers:
            - image: gcr.azk8s.cn/knative-samples/helloworld-go # The URL to the image of the app
              env:
                - name: TARGET # The environment variable printed out by the sample app
                  value: "Go Sample v1"
    EOF
    
    kubectl create -f knative-helloworld.yaml
    
    ➜  ~ kubectl get ksvc
    NAME            URL                                        LATESTCREATED         LATESTREADY           READY   REASON
    helloworld-go   http://helloworld-go.default.example.com   helloworld-go-c2drn   helloworld-go-c2drn   True
    

    至次我们通过上面的yaml文件向k8s提交了一个knative 自定义的crd,services.serving.knative.dev.

    kubectl get services.serving.knative.dev
    NAME            URL                                        LATESTCREATED         LATESTREADY           READY   REASON
    helloworld-go   http://helloworld-go.default.example.com   helloworld-go-c2drn   helloworld-go-c2drn   True
    

    上面的ksvc是services.serving.knative.dev的简写.

    访问 hello-world

    部署第一个knative hello-word后怎么访问呢,上面的svc的信息显示一个域名 helloworld-go.default.example.com 我们通过这个域名可以访问到 hello-world-go 应用.

    export INGRESS_HOST=10.1.1.1
    export INGRESS_PORT=31380
    curl -I -HHost:helloworld-go.default.example.com http://$INGRESS_HOST:$INGRESS_PORT
    

    上面的INGRESS_HOST是istio-ingressgateway服务的监听ip地址,INGRESS_PORT是端口,因为我的istio-ingressgateway是node—port类型.开始的时候说安装knative需要istio这就是其中一个原因,knative 通过istio-ingressgateway暴露它的服务.具体的实现后面的文章会分析.

    [root@host~]# curl -I -HHost:helloworld-go.default.example.com http://$INGRESS_HOST:$INGRESS_PORT
    HTTP/1.1 200 OK
    content-length: 20
    content-type: text/plain; charset=utf-8
    date: Thu, 28 Nov 2019 10:11:31 GMT
    x-envoy-upstream-service-time: 3309
    server: istio-envoy
    
    [root@host108752172 ~]# kubectl get  pods
    NAME                                              READY   STATUS    RESTARTS   AGE
    helloworld-go-c2drn-deployment-5ccc555d74-g6spw   2/2     Running   0          11s
    nginx-deployment-basic-5754944d6c-rg7vk           1/1     Running   0          2d22h
    prometheus-persisted-0                            0/3     Pending   0          13d
    

    curl hello-world-go 服务的时候稍微有点延迟,是因为没有request进来 helloworld-go 被auto-scaler为0. 需要拉起这个pod.

    总结

    一个简单的knative的应用跑起来了,可能会有疑惑,knative到底和传统的k8s上的web应用有啥不同,knative部署完了在k8s上到底增加了什么东西,这些下篇文章继续分析.

    相关文章

      网友评论

          本文标题:knative hello-world

          本文链接:https://www.haomeiwen.com/subject/qatiwctx.html