三種方式本地執行Knative

iyacontrol發表於2020-05-21

Knative是一組構建模組,可在Kubernetes上執行無伺服器工作負載。您還可以使用這些構建塊來構建自己的無伺服器平臺。

Knative的一個問題是在本地執行它。 Kubernetes非常龐大,在它之上執行另一個平臺是一個非常困難的事情。今天,我們將介紹一些不同的方法來測試Knative,而不會過多佔用您的計算機。

一些注意事項:

  • 我將重點介紹Knative的服務部分
  • 我選擇Kourier作為Knative網路層–它比Istio更簡單,更輕便
  • Ubuntu被用於一切,減去Mac上的Docker部分
  • 您可以在此處找到安裝Knative的完整說明

k3s

k3s是Kubernetes的精簡版,專為IoT和Edge計算而設計-非常適合您的膝上型電腦。 k3s被打包為單個二進位制檔案(!!!),使其非常易於執行。它僅適用於Linux。

我們開始做吧!

export KNATIVE_VERSION="0.13.0"
export KUBECONFIG="/var/lib/rancher/k3s/server/cred/admin.kubeconfig"

# Since Knative has its own network layer, we need to disable k3s' Traefik during its installation
# to make sure Kourier proxy gets a LoadBalancer IP
curl -sfL https://get.k3s.io | sh -s - --disable traefik

# Install Knative Serving
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-crds.yaml"
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-core.yaml"

# Configure the magic xip.io DNS name
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-default-domain.yaml"

# Install and configure Kourier
kubectl apply --filename https://raw.githubusercontent.com/knative/serving/v$KNATIVE_VERSION/third_party/kourier-latest/kourier.yaml
kubectl patch configmap/config-network --namespace knative-serving --type merge --patch '{"data":{"ingress.class":"kourier.ingress.networking.knative.dev"}}'

好了,安裝完成!確保所有Pod都在執行(如果沒有執行,請稍等):

kubectl get pod -A

那個k3給Kourier一個LoadBalancer IP地址:

kubectl get svc kourier -n kourier-system

                                       # yay!
NAME      TYPE           CLUSTER-IP    EXTERNAL-IP       PORT(S)                      AGE
kourier   LoadBalancer   10.43.94.13   172.17.255.1      80:31619/TCP,443:30649/TCP   80s

讓我們部署Knative Service示例:

kubectl apply -f https://gist.githubusercontent.com/jonatasbaldin/bc04de2e376be23f75bb5815041fdd61/raw/d2345ac9aa01d0f3c771e9b3d4a1421dd766e0f9/service.yaml

service.serving.knative.dev/helloworld-go created

確保它正在執行並找到其“公共”地址:

kubectl get ksvc

NAME            URL                                                   LATESTCREATED         LATESTREADY           READY   REASON
helloworld-go   http://helloworld-go.default.172.17.255.1.xip.io      helloworld-go-2xvpx   helloworld-go-2xvpx   True    

最終:

curl http://helloworld-go.default.172.17.255.1.xip.io

Hello Go Sample v1!

kind

一種在Docker容器中執行Kubernetes。看上去如此瘋狂,它以很小的資源佔用量就可以驚人地用於測試和本地開發。

確實可以在Mac上的Docker上工作,但要獲得LoadBalancer並非易事:對於Linux,我們可以使用MetalLB獲得LB –我參照了該教程,以瞭解它。

讓我們從kind和Knative開始:

# Getting kind
curl -Lo kind https://github.com/kubernetes-sigs/kind/releases/download/v0.7.0/kind-$(uname)-amd64
chmod +x kind
mv kind /usr/local/bin

# Getting kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x kubectl
mv kubectl /usr/local/bin

# Installing Docker (just copied from the Docker docs)
apt-get update
apt-get install apt-transport-https ca-certificates  curl gnupg-agent software-properties-common -y 
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install docker-ce docker-ce-cli containerd.io -y

# Create a kind cluster
kind create cluster

# Install the Knative Serving
export KNATIVE_VERSION="0.13.0"
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-crds.yaml"
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-core.yaml"

# Configure the magic xip.io DNS name
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-default-domain.yaml"

# Install and configure Kourier
kubectl apply --filename https://raw.githubusercontent.com/knative/serving/v$KNATIVE_VERSION/third_party/kourier-latest/kourier.yaml
kubectl patch configmap/config-network --namespace knative-serving --type merge --patch '{"data":{"ingress.class":"kourier.ingress.networking.knative.dev"}}'

現在,MetalLB步驟:

# https://mauilion.dev/posts/kind-metallb/

# First, find the network CIDR used by the Docker bridge network
# MetalLB will use some IPs from there
docker inspect network bridge

[
    {
        "Name": "bridge",
        # ...
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    # this is what we want
                    # we will give the 10 addresses to MetalLB
                    # let's say, 172.17.255.1-172.17.255.250
                    "Subnet": "172.17.0.0/16"
                }
            ]
        },
        # ...
    }
]

安裝 MetalLB:

# god bless the yaml
kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/metallb.yaml
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"

# Creating a MetalLB configuration
# It states that we will use those IP addresses for the LoadBalancer
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      # the range from before!
      - 172.17.255.1-172.17.255.250
EOF

好了,安裝完成!確保所有Pod都在執行(如果沒有執行,請稍等):

kubectl get pod -A

之後,看看Kourier:

kubectl get svc kourier -n kourier-system

                                       # yes!
NAME      TYPE           CLUSTER-IP    EXTERNAL-IP    PORT(S)                      AGE
kourier   LoadBalancer   10.96.7.120   172.17.255.1   80:30201/TCP,443:30909/TCP   110s

讓我們部署Knative Service示例:

kubectl apply -f https://gist.githubusercontent.com/jonatasbaldin/bc04de2e376be23f75bb5815041fdd61/raw/d2345ac9aa01d0f3c771e9b3d4a1421dd766e0f9/service.yaml

service.serving.knative.dev/helloworld-go created

確保它正在執行並找到其“公共”地址:

kubectl get ksvc

NAME                   URL                                                       LATESTCREATED                LATESTREADY                  READY   REASON
helloworld-go          http://helloworld-go.default.172.17.255.1.xip.io          helloworld-go-7sttx          helloworld-go-7sttx          True    

最終:

curl http://helloworld-go.default.172.17.255.1.xip.io

Hello Go Sample v1!

Docker for Mac

可能是我的最愛 Docker for Mac附帶的Kubernetes是在本地使用該平臺的一種非常簡單的方法:單擊Docker配置上的按鈕,一切順利。不幸的是,對於Knative而言,這並不容易,但是讓我們對其進行修復。

首先,Knative僅支援K8S v1.15.0 +,而Mac的預設Docker隨v1.14.6一起提供,沒有升級路徑。但是,您可以透過Edge版本獲得v1.16.0 +。它具有實驗性功能,因此可能不是100%穩定的。無論如何,Edge對於實驗足夠了。在進入下載頁面之前,請閱讀“在穩定版本和Edge版本之間切換”。

安裝完成後,讓我們開始Knative:

# Install the Knative Serving
export KNATIVE_VERSION="0.13.0"
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-crds.yaml"
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-core.yaml"

# Install and configure Kourier
kubectl apply --filename https://raw.githubusercontent.com/knative/serving/v$KNATIVE_VERSION/third_party/kourier-latest/kourier.yaml
kubectl patch configmap/config-network --namespace knative-serving --type merge --patch '{"data":{"ingress.class":"kourier.ingress.networking.knative.dev"}}'

如果我們檢查Kourier LoadBalancer,它將獲得一個本地主機地址

kubectl get svc kourier -n kourier-system

NAME      TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
kourier   LoadBalancer   10.110.153.164   localhost     80:31650/TCP,443:30315/TCP   34m

LB在埠80和443上公開給我們的本地主機。這很重要,它將影響我們與Knative服務的通訊方式。

讓我們部署Knative Service示例:

kubectl apply -f https://gist.githubusercontent.com/jonatasbaldin/bc04de2e376be23f75bb5815041fdd61/raw/d2345ac9aa01d0f3c771e9b3d4a1421dd766e0f9/service.yaml

service.serving.knative.dev/helloworld-go created

確保它正在執行並找到其“公共”地址:

kubectl get ksvc

NAME            URL                                        LATESTCREATED         LATESTREADY           READY   REASON
helloworld-go   http://helloworld-go.default.example.com   helloworld-go-9qsq7   helloworld-go-9qsq7   True    

與其他步驟不同,我們沒有應用Magic http://xip.io DNS Knative檔案,因為當LoadBalancer預配置了一個域(在本例中為localhost)時,該檔案不起作用。

最後,要訪問服務,請在localhost:80上單擊LoadBalancer,並將服務域作為主機頭傳遞:

curl localhost:80 -H "Host: helloworld-go.default.example.com"

Hello Go Sample v1!

太好了,對!!

PS:我對這個解決方案沒有太多投入。本地主機LoadBalancer可能有我沒有遇到過的問題。如果發現任何可疑內容,我將保持本文的更新。

minikube

Minikube部分是Jonathan Beri的貢獻,Minikube在其GitHub儲存庫中得到了完美的描述:

minikube在macOS,Linux和Windows上實現了本地Kubernetes叢集。 minikube的主要目標是成為本地Kubernetes應用程式開發的最佳工具,並支援所有合適的Kubernetes功能。
# Get a Minikube VM up and running, with enough resources
minikube start --cpus=4 --memory=4096 --addons=ingress

# Install the Knative Serving
export KNATIVE_VERSION="0.13.0"
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-crds.yaml"
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-core.yaml"

# Configure the magic xip.io DNS name
kubectl apply --filename "https://github.com/knative/serving/releases/download/v$KNATIVE_VERSION/serving-default-domain.yaml"

# Install and configure Kourier
kubectl apply --filename https://raw.githubusercontent.com/knative/serving/v$KNATIVE_VERSION/third_party/kourier-latest/kourier.yaml
kubectl patch configmap/config-network --namespace knative-serving --type merge --patch '{"data":{"ingress.class":"kourier.ingress.networking.knative.dev"}}'

好了,安裝完成!確保所有Pod都在執行(如果沒有執行,請稍等):

kubectl get pod -A

開啟一個新的終端控制檯並啟動Minikube LoadBalancer:

minikube tunnel

Status:
        machine: minikube
        pid: 13391
        route: 10.96.0.0/12 -> 192.168.64.6
        minikube: Running
        services: [kourier]
    errors: 
                minikube: no errors
                router: no errors
                loadbalancer emulator: no errors

之後,從Kourier獲得公共IP:

kubectl get svc kourier -n kourier-system

                                        # yes!
NAME      TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)                      AGE
kourier   LoadBalancer   10.107.29.82   10.107.29.82   80:30354/TCP,443:30261/TCP   2m47s

讓我們部署Knative Service示例:

kubectl apply -f https://gist.githubusercontent.com/jonatasbaldin/bc04de2e376be23f75bb5815041fdd61/raw/d2345ac9aa01d0f3c771e9b3d4a1421dd766e0f9/service.yaml

service.serving.knative.dev/helloworld-go created

確保它正在執行並找到其“公共”地址:

kubectl get ksvc

NAME                URL                                                    LATESTCREATED             LATESTREADY               READY   REASON
helloworld-go       http://helloworld-go.default.10.107.29.82.xip.io       helloworld-go-xqlxs       helloworld-go-xqlxs       True

最終:

curl http://helloworld-go.default.10.107.29.82.xip.io

Hello Go Sample v1!

PS:本文屬於翻譯,原文

相關文章