Skip to content

Latest commit

 

History

History
576 lines (559 loc) · 17.5 KB

解放服务.md

File metadata and controls

576 lines (559 loc) · 17.5 KB

Knative

Knative组件构建于Kubernetes之上,抽象出复杂的细节,使开发人员能够专注于重要的事情。 通过编写来自现实世界成功实现的最佳实践,Knative解决了构建、部署和管理云原生服务的“无聊但困难”的部分,从而得以解脱。

  1. 安装K3S
export chifun=https://github.91chifun.workers.dev
wget ${chifun}/https://github.com//k3s-io/k3s/releases/download/v1.21.2%2Bk3s1/k3s-airgap-images-amd64.tar  
wget ${chifun}/https://github.com//k3s-io/k3s/releases/download/v1.21.2%2Bk3s1/k3s  
sudo mkdir -p /var/lib/rancher/k3s/agent/images/
sudo cp ./k3s /usr/local/bin/
sudo cp ./k3s-airgap-images-amd64.tar /var/lib/rancher/k3s/agent/images/
sudo chmod +x /usr/local/bin/k3s
curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_DOWNLOAD=true \
  K3S_KUBECONFIG_MODE=644 sh -
cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
  1. 安装Knative

安装istio

kubectl apply -l knative.dev/crd-install=true -f \
  https://github.com/knative/net-istio/releases/download/v0.24.0/istio.yaml
kubectl apply -f https://github.com/knative/net-istio/releases/download/v0.24.0/istio.yaml

安装serving

kubectl apply -f https://github.com/knative/serving/releases/download/v0.24.0/serving-crds.yaml
kubectl apply -f https://github.com/knative/serving/releases/download/v0.24.0/serving-core.yaml
kubectl apply -f https://github.com/knative/net-istio/releases/download/v0.24.0/net-istio.yaml

安装eventing

kubectl apply -f https://github.com/knative/eventing/releases/download/v0.24.0/eventing-crds.yaml
kubectl apply -f https://github.com/knative/eventing/releases/download/v0.24.0/eventing-core.yaml
kubectl apply -f https://github.com/knative/eventing/releases/download/v0.24.0/in-memory-channel.yaml

国外的镜像可以通过dockerlab提交到自己的docker.io仓库

docker pull xxx
docker tag xxx xx/knative-cmd-webhook
docker push xx/knative-cmd-webhook
  1. helloworld
apiVersion: serving.knative.dev/v1 # Current version of Knative
kind: Service
metadata:
  name: helloworld # The name of the app
  namespace: default # The namespace the app will use
spec:
  template:
    spec:
      containers:
        - image: crccheck/hello-world # Reference to the image of the app
          env:
            - name: TARGET # The environment variable printed out by the sample app
              value: "Web Sample v1"

测试访问:

curl -HHost:helloworld-go.default.example.com http://172.20.10.3:31212 
Hello Go Sample v1!

3.1 View the created Service resource

$ kubectl get ksvc helloworld-go -o yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  annotations:
    serving.knative.dev/creator: system:admin
    serving.knative.dev/lastModifier: system:admin
  creationTimestamp: "2021-08-04T02:53:57Z"
  generation: 1
  name: helloworld-go
  namespace: default
  resourceVersion: "10268"
  uid: 81d6170d-b614-47c0-bf35-961cf685ca6b
spec:
  template:
    metadata:
      creationTimestamp: null
    spec:
      containerConcurrency: 0
      containers:
      - env:
        - name: TARGET
          value: Go Sample v1
        image: zhku/knative-helloworld-go
        name: user-container
        readinessProbe:
          successThreshold: 1
          tcpSocket:
            port: 0
        resources: {}
      enableServiceLinks: false
      timeoutSeconds: 300
  traffic:
  - latestRevision: true
    percent: 100
status:
  address:
    url: http://helloworld-go.default.svc.cluster.local
  conditions:
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: ConfigurationsReady
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: Ready
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: RoutesReady
  latestCreatedRevisionName: helloworld-go-00001
  latestReadyRevisionName: helloworld-go-00001
  observedGeneration: 1
  traffic:
  - latestRevision: true
    percent: 100
    revisionName: helloworld-go-00001
  url: http://helloworld-go.default.example.com

3.2 View the created Route resource

$ kubectl get route helloworld-go -o yaml
apiVersion: serving.knative.dev/v1
kind: Route
metadata:
  annotations:
    serving.knative.dev/creator: system:admin
    serving.knative.dev/lastModifier: system:admin
  creationTimestamp: "2021-08-04T02:53:57Z"
  finalizers:
  - routes.serving.knative.dev
  generation: 1
  labels:
    serving.knative.dev/service: helloworld-go
  name: helloworld-go
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1
    blockOwnerDeletion: true
    controller: true
    kind: Service
    name: helloworld-go
    uid: 81d6170d-b614-47c0-bf35-961cf685ca6b
  resourceVersion: "10267"
  uid: c28fb5a0-7494-44f0-afd8-d5c8e289cad2
spec:
  traffic:
  - configurationName: helloworld-go
    latestRevision: true
    percent: 100
status:
  address:
    url: http://helloworld-go.default.svc.cluster.local
  conditions:
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: AllTrafficAssigned
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    message: autoTLS is not enabled
    reason: TLSNotEnabled
    status: "True"
    type: CertificateProvisioned
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: IngressReady
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: Ready
  observedGeneration: 1
  traffic:
  - latestRevision: true
    percent: 100
    revisionName: helloworld-go-00001
  url: http://helloworld-go.default.example.com

3.3 View the Kubernetes Service created by the Route

$ kubectl get service helloworld-go  -o yaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    serving.knative.dev/creator: system:admin
    serving.knative.dev/lastModifier: system:admin
  creationTimestamp: "2021-08-04T02:57:32Z"
  labels:
    serving.knative.dev/route: helloworld-go
    serving.knative.dev/service: helloworld-go
  name: helloworld-go
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1
    blockOwnerDeletion: true
    controller: true
    kind: Route
    name: helloworld-go
    uid: c28fb5a0-7494-44f0-afd8-d5c8e289cad2
  resourceVersion: "10265"
  uid: dbeecf64-3285-46b9-bd1c-72953572d25b
spec:
  externalName: knative-local-gateway.istio-system.svc.cluster.local
  ports:
  - name: http2
    port: 80
    protocol: TCP
    targetPort: 80
  sessionAffinity: None
  type: ExternalName
status:
  loadBalancer: {}

3.4 View the created Configuration resource

$ kubectl get configuration helloworld-go  -o yaml
apiVersion: serving.knative.dev/v1
kind: Configuration
metadata:
  annotations:
    serving.knative.dev/creator: system:admin
    serving.knative.dev/lastModifier: system:admin
    serving.knative.dev/routes: helloworld-go
  creationTimestamp: "2021-08-04T02:53:57Z"
  generation: 1
  labels:
    serving.knative.dev/service: helloworld-go
    serving.knative.dev/serviceUID: 81d6170d-b614-47c0-bf35-961cf685ca6b
  name: helloworld-go
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1
    blockOwnerDeletion: true
    controller: true
    kind: Service
    name: helloworld-go
    uid: 81d6170d-b614-47c0-bf35-961cf685ca6b
  resourceVersion: "10235"
  uid: 81f31a88-abfc-40c5-b7b9-babb15f81fdd
spec:
  template:
    metadata:
      creationTimestamp: null
    spec:
      containerConcurrency: 0
      containers:
      - env:
        - name: TARGET
          value: Go Sample v1
        image: zhku/knative-helloworld-go
        name: user-container
        readinessProbe:
          successThreshold: 1
          tcpSocket:
            port: 0
        resources: {}
      enableServiceLinks: false
      timeoutSeconds: 300
status:
  conditions:
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: Ready
  latestCreatedRevisionName: helloworld-go-00001
  latestReadyRevisionName: helloworld-go-00001
  observedGeneration: 1

3.5 View the Revision that was created by our Configuration

$ kubectl get revision helloworld-go-00001 -o yaml
apiVersion: serving.knative.dev/v1
kind: Revision
metadata:
  annotations:
    serving.knative.dev/creator: system:admin
    serving.knative.dev/routes: helloworld-go
    serving.knative.dev/routingStateModified: "2021-08-04T02:53:57Z"
  creationTimestamp: "2021-08-04T02:53:57Z"
  generation: 1
  labels:
    serving.knative.dev/configuration: helloworld-go
    serving.knative.dev/configurationGeneration: "1"
    serving.knative.dev/configurationUID: 81f31a88-abfc-40c5-b7b9-babb15f81fdd
    serving.knative.dev/routingState: active
    serving.knative.dev/service: helloworld-go
    serving.knative.dev/serviceUID: 81d6170d-b614-47c0-bf35-961cf685ca6b
  name: helloworld-go-00001
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1
    blockOwnerDeletion: true
    controller: true
    kind: Configuration
    name: helloworld-go
    uid: 81f31a88-abfc-40c5-b7b9-babb15f81fdd
  resourceVersion: "225515"
  uid: 6f813161-39b4-49f3-9b8e-4916b80240d4
spec:
  containerConcurrency: 0
  containers:
  - env:
    - name: TARGET
      value: Go Sample v1
    image: zhku/knative-helloworld-go
    name: user-container
    readinessProbe:
      successThreshold: 1
      tcpSocket:
        port: 0
    resources: {}
  enableServiceLinks: false
  timeoutSeconds: 300
status:
  actualReplicas: 0
  conditions:
  - lastTransitionTime: "2021-08-04T07:59:28Z"
    message: The target is not receiving traffic.
    reason: NoTraffic
    severity: Info
    status: "False"
    type: Active
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: ContainerHealthy
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: Ready
  - lastTransitionTime: "2021-08-04T02:57:32Z"
    status: "True"
    type: ResourcesAvailable
  containerStatuses:
  - imageDigest: index.docker.io/zhku/knative-helloworld-go@sha256:5ea96ba4b872685ff4ddb5cd8d1a97ec18c18fae79ee8df0d29f446c5efe5f50
    name: user-container
  imageDigest: index.docker.io/zhku/knative-helloworld-go@sha256:5ea96ba4b872685ff4ddb5cd8d1a97ec18c18fae79ee8df0d29f446c5efe5f50
  observedGeneration: 1
  serviceName: helloworld-go-00001

3.6 View the Deployment created by our Revision

$ kubectl get deployment helloworld-go-00001-deployment -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
    serving.knative.dev/creator: system:admin
  creationTimestamp: "2021-08-04T02:54:06Z"
  generation: 8
  labels:
    app: helloworld-go-00001
    service.istio.io/canonical-name: helloworld-go
    service.istio.io/canonical-revision: helloworld-go-00001
    serving.knative.dev/configuration: helloworld-go
    serving.knative.dev/configurationGeneration: "1"
    serving.knative.dev/configurationUID: 81f31a88-abfc-40c5-b7b9-babb15f81fdd
    serving.knative.dev/revision: helloworld-go-00001
    serving.knative.dev/revisionUID: 6f813161-39b4-49f3-9b8e-4916b80240d4
    serving.knative.dev/service: helloworld-go
    serving.knative.dev/serviceUID: 81d6170d-b614-47c0-bf35-961cf685ca6b
  name: helloworld-go-00001-deployment
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1
    blockOwnerDeletion: true
    controller: true
    kind: Revision
    name: helloworld-go-00001
    uid: 6f813161-39b4-49f3-9b8e-4916b80240d4
  resourceVersion: "55903"
  uid: 8c4a1767-608c-48d1-a82d-f19f48402fd0
spec:
  progressDeadlineSeconds: 600
  replicas: 0
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      serving.knative.dev/revisionUID: 6f813161-39b4-49f3-9b8e-4916b80240d4
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      annotations:
        serving.knative.dev/creator: system:admin
      creationTimestamp: null
      labels:
        app: helloworld-go-00001
        service.istio.io/canonical-name: helloworld-go
        service.istio.io/canonical-revision: helloworld-go-00001
        serving.knative.dev/configuration: helloworld-go
        serving.knative.dev/configurationGeneration: "1"
        serving.knative.dev/configurationUID: 81f31a88-abfc-40c5-b7b9-babb15f81fdd
        serving.knative.dev/revision: helloworld-go-00001
        serving.knative.dev/revisionUID: 6f813161-39b4-49f3-9b8e-4916b80240d4
        serving.knative.dev/service: helloworld-go
        serving.knative.dev/serviceUID: 81d6170d-b614-47c0-bf35-961cf685ca6b
    spec:
      containers:
      - env:
        - name: TARGET
          value: Go Sample v1
        - name: PORT
          value: "8080"
        - name: K_REVISION
          value: helloworld-go-00001
        - name: K_CONFIGURATION
          value: helloworld-go
        - name: K_SERVICE
          value: helloworld-go
        image: index.docker.io/zhku/knative-helloworld-go@sha256:5ea96ba4b872685ff4ddb5cd8d1a97ec18c18fae79ee8df0d29f446c5efe5f50
        imagePullPolicy: IfNotPresent
        lifecycle:
          preStop:
            httpGet:
              path: /wait-for-drain
              port: 8022
              scheme: HTTP
        name: user-container
        ports:
        - containerPort: 8080
          name: user-port
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: FallbackToLogsOnError
      - env:
        - name: SERVING_NAMESPACE
          value: default
        - name: SERVING_SERVICE
          value: helloworld-go
        - name: SERVING_CONFIGURATION
          value: helloworld-go
        - name: SERVING_REVISION
          value: helloworld-go-00001
        - name: QUEUE_SERVING_PORT
          value: "8012"
        - name: CONTAINER_CONCURRENCY
          value: "0"
        - name: REVISION_TIMEOUT_SECONDS
          value: "300"
        - name: SERVING_POD
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.name
        - name: SERVING_POD_IP
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: status.podIP
        - name: SERVING_LOGGING_CONFIG
        - name: SERVING_LOGGING_LEVEL
        - name: SERVING_REQUEST_LOG_TEMPLATE
          value: '{"httpRequest": {"requestMethod": "{{.Request.Method}}", "requestUrl":
            "{{js .Request.RequestURI}}", "requestSize": "{{.Request.ContentLength}}",
            "status": {{.Response.Code}}, "responseSize": "{{.Response.Size}}", "userAgent":
            "{{js .Request.UserAgent}}", "remoteIp": "{{js .Request.RemoteAddr}}",
            "serverIp": "{{.Revision.PodIP}}", "referer": "{{js .Request.Referer}}",
            "latency": "{{.Response.Latency}}s", "protocol": "{{.Request.Proto}}"},
            "traceId": "{{index .Request.Header "X-B3-Traceid"}}"}'
        - name: SERVING_ENABLE_REQUEST_LOG
          value: "false"
        - name: SERVING_REQUEST_METRICS_BACKEND
          value: prometheus
        - name: TRACING_CONFIG_BACKEND
          value: none
        - name: TRACING_CONFIG_ZIPKIN_ENDPOINT
        - name: TRACING_CONFIG_STACKDRIVER_PROJECT_ID
        - name: TRACING_CONFIG_DEBUG
          value: "false"
        - name: TRACING_CONFIG_SAMPLE_RATE
          value: "0.1"
        - name: USER_PORT
          value: "8080"
        - name: SYSTEM_NAMESPACE
          value: knative-serving
        - name: METRICS_DOMAIN
          value: knative.dev/internal/serving
        - name: SERVING_READINESS_PROBE
          value: '{"tcpSocket":{"port":8080,"host":"127.0.0.1"},"successThreshold":1}'
        - name: ENABLE_PROFILING
          value: "false"
        - name: SERVING_ENABLE_PROBE_REQUEST_LOG
          value: "false"
        - name: METRICS_COLLECTOR_ADDRESS
        image: zhku/knative-cmd-queue
        imagePullPolicy: Always
        name: queue-proxy
        ports:
        - containerPort: 8022
          name: http-queueadm
          protocol: TCP
        - containerPort: 9090
          name: http-autometric
          protocol: TCP
        - containerPort: 9091
          name: http-usermetric
          protocol: TCP
        - containerPort: 8012
          name: queue-port
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            httpHeaders:
            - name: K-Network-Probe
              value: queue
            path: /
            port: 8012
            scheme: HTTP
          periodSeconds: 1
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          requests:
            cpu: 25m
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - all
          readOnlyRootFilesystem: true
          runAsNonRoot: true
        startupProbe:
          exec:
            command:
            - /ko-app/queue
            - -probe-timeout
            - 10m0s
          failureThreshold: 1
          periodSeconds: 1
          successThreshold: 1
          timeoutSeconds: 600
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      enableServiceLinks: false
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 300
status:
  conditions:
  - lastTransitionTime: "2021-08-04T02:54:07Z"
    lastUpdateTime: "2021-08-04T02:57:32Z"
    message: ReplicaSet "helloworld-go-00001-deployment-77f646ff78" has successfully
      progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  - lastTransitionTime: "2021-08-04T07:58:28Z"
    lastUpdateTime: "2021-08-04T07:58:28Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  observedGeneration: 8