- https://qiita.com/miekoma/items/e559c065ccbeb2ba1572
- https://knative.dev/docs/getting-started/first-service/#knative-service-hello-world
以下の手順は全てamd64なUbuntu Serverで実行することを前提にしている。 ほぼ参考サイトままだが、Knative 1.3.0ベースに書き換えている。
Ubuntu 20.04.4を入れてアップデート、再起動
$ sudo apt update && sudo apt install -y docker.io dbus-user-session && sudo usermod -aG docker $USER && sudo reboot
kind v0.12.0(2022/04/04時点で最新)を入れる
$ curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/v0.12.0/kind-linux-amd64
$ chmod +x ./kind
$ sudo mv ./kind /usr/local/bin/kind
kn CLIを入れる
$ curl -Lo ./kn https://github.com/knative/client/releases/download/knative-v1.3.1/kn-linux-amd64
$ chmod +x ./kn
$ sudo mv ./kn /usr/local/bin/kn
kubectl CLIを入れる
$ sudo snap install kubectl --channel=1.23/stable --classic
クラスタ作成用のYAMLを作成 バージョンを指定したい場合はこの辺をみる。
$ cat > clusterconfig.yaml <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
## expose port 31080 of the node to port 80 on the host
- containerPort: 31080
hostPort: 80
## expose port 31443 of the node to port 443 on the host
- containerPort: 31443
hostPort: 443
EOF
クラスター作成(このコマンドでkindでクラスター作成、Knative Servingのセットアップまで行われる)
$ kind create cluster --name knative --config clusterconfig.yaml
kind v0.12.0ではKubernretsの1.23.4がデフォルトで構築される。
$ kubectl get no
NAME STATUS ROLES AGE VERSION
knative-control-plane Ready control-plane,master 5m8s v1.23.4
Knative Servingのインストール
$ kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.3.0/serving-crds.yaml
Knative Serving Core Componentのインストール
$ kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.3.0/serving-core.yaml
ネットワーク・レイヤーのインストール KnativeのネットワークレイヤはIstioだったが、Kourierという軽量なIngressが使えるらしい。今回はこれを使ってみる。
$ curl -Lo kourier.yaml https://github.com/knative/net-kourier/releases/download/knative-v1.3.0/kourier.yaml
以下の設定部分を書き換えてapply
...
apiVersion: v1
kind: Service
metadata:
name: kourier
namespace: kourier-system
labels:
networking.knative.dev/ingress-provider: kourier
app.kubernetes.io/component: net-kourier
app.kubernetes.io/version: "1.3.0"
app.kubernetes.io/name: knative-serving
serving.knative.dev/release: "v1.3.0"
spec:
ports:
- name: http2
port: 80
protocol: TCP
targetPort: 8080
nodePort: 31080 # 追加
- name: https
port: 443
protocol: TCP
targetPort: 8443
nodePort: 31443 # 追加
selector:
app: 3scale-kourier-gateway
type: NodePort # LoadBalancerから変更
...
設定を適用
$ kubectl apply --filename kourier.yaml
Knative ServingがKourierを使用するように構成
$ kubectl patch configmap/config-network --namespace knative-serving --type merge --patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}'
DNSの構成 ここではワイルドカードDNSサービスであるsslip.ioを使用するよう構成します(ファイル内に「args: ["-magic-dns=sslip.io"]」みたいな記述がある)。
$ wget https://github.com/knative/serving/releases/download/knative-v1.3.0/serving-default-domain.yaml
$ kubectl apply -f serving-default-domain.yaml
config-domainに以下を追加して、カスタム・ドメイン"*.127.0.0.1.sslip.io"を127.0.0.1に解決します。
$ kubectl patch configmap/config-domain --namespace knative-serving --type merge --patch '{"data":{"127.0.0.1.sslip.io":""}}'
試しにこれを作ってみよう
$ kn service create hello --image gcr.io/knative-samples/helloworld-go --port 8080 --env TARGET=World
Creating service 'hello' in namespace 'default':
0.010s The Route is still working to reflect the latest desired specification.
0.080s ...
0.096s Configuration "hello" is waiting for a Revision to become ready.
167.698s ...
167.970s Ingress has not yet been reconciled.
168.128s Waiting for load balancer to be ready
168.287s Ready to serve.
Service 'hello' created to latest revision 'hello-00001' is available at URL:
http://hello.default.127.0.0.1.sslip.io
$ curl http://hello.default.127.0.0.1.sslip.io
Hello World!
KnativeにはKnative EventingというEvent-Driven用のコンポーネントもありますが、 とりあえずRequest-Replyモデルのアプリケーションが動く、Knative Servingだけ動くのを確認しました。
$ kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
hello http://hello.default.127.0.0.1.sslip.io hello-00001 26m 3 OK / 3 True
そして、2分経過したら縮退するのを確認
$ kubectl get pod -l serving.knative.dev/service=hello -w
NAME READY STATUS RESTARTS AGE
hello-00001-deployment-5f8fbcf77-llqv8 2/2 Running 0 25s
hello-00001-deployment-5f8fbcf77-llqv8 2/2 Terminating 0 70s
hello-00001-deployment-5f8fbcf77-llqv8 1/2 Terminating 0 73s
hello-00001-deployment-5f8fbcf77-llqv8 0/2 Terminating 0 102s
hello-00001-deployment-5f8fbcf77-llqv8 0/2 Terminating 0 108s
hello-00001-deployment-5f8fbcf77-llqv8 0/2 Terminating 0 108
縮退するけど、アクセスするとPodが再作成され復活する
$ curl hello.default.127.0.0.1.sslip.io
Hello World!
$ kubectl get pod -l serving.knative.dev/service=hello
NAME READY STATUS RESTARTS AGE
hello-00001-deployment-5f8fbcf77-nj92h 2/2 Running 0 24s
現在のサービスの状態を確認
$ kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
hello http://hello.default.127.0.0.1.sslip.io hello-00001 47m 3 OK / 3 True
KnativeServiceの更新されたバージョンをデプロイします。
$ kn service update hello --env TARGET=Knative
Updating Service 'hello' in namespace 'default':
0.067s The Configuration is still working to reflect the latest desired specification.
9.259s Traffic is not yet migrated to the latest revision.
9.461s Ingress has not yet been reconciled.
9.603s Waiting for load balancer to be ready
9.736s Ready to serve.
Service 'hello' updated to latest revision 'hello-00002' is available at URL:
http://hello.default.127.0.0.1.sslip.io
リビジョンが変わったことが確認できる
$ kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
hello http://hello.default.127.0.0.1.sslip.io hello-00002 48m 3 OK / 3 True
新しいリビジョンにアクセスしてみる(普通にcurlでアクセスしても良い)
$ echo "Accessing URL $(kn service describe hello -o url)"
$ curl "$(kn service describe hello -o url)"
Hello Knative!
リビジョンを確認 現在はhello-00002のサービスに100%アクセスが振り分けられている
$ kn revisions list
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-00002 hello 100% 2 2m59s 3 OK / 4 True
hello-00001 hello 1 51m 3 OK / 4 True
リビジョン間でトラフィックを分割する
$ kn service update hello --traffic hello-00001=50 --traffic @latest=50
Updating Service 'hello' in namespace 'default':
0.059s The Route is still working to reflect the latest desired specification.
0.204s Ingress has not yet been reconciled.
0.335s Waiting for load balancer to be ready
0.432s Ready to serve.
Service 'hello' with latest revision 'hello-00002' (unchanged) is available at URL:
http://hello.default.127.0.0.1.sslip.io
$ kn revisions list
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-00002 hello 50% 2 4m58s 3 OK / 4 True
hello-00001 hello 50% 1 53m 3 OK / 4 True
アクセスすると大体半分の確率でhello-00001とhello-00002のサービスに振り分けられた
$ curl http://hello.default.127.0.0.1.sslip.io
Hello World!
$ curl http://hello.default.127.0.0.1.sslip.io
Hello World!
$ curl http://hello.default.127.0.0.1.sslip.io
Hello Knative!
$ curl http://hello.default.127.0.0.1.sslip.io
Hello World!
$ curl http://hello.default.127.0.0.1.sslip.io
Hello World!
$ curl http://hello.default.127.0.0.1.sslip.io
Hello World!
$ curl http://hello.default.127.0.0.1.sslip.io
Hello Knative!
$ kubectl get all -A
NAMESPACE NAME READY STATUS RESTARTS AGE
knative-serving pod/activator-6fb68fff7b-n9swg 1/1 Running 4 112m
knative-serving pod/autoscaler-54f48f5bb7-p578z 1/1 Running 0 112m
knative-serving pod/controller-66cb4b556b-sqwvv 1/1 Running 0 112m
knative-serving pod/default-domain-6zljr 0/1 Error 0 104m
knative-serving pod/default-domain-l5wfz 0/1 Completed 0 84m
knative-serving pod/domain-mapping-66f8d5bc4c-7sf8p 1/1 Running 0 112m
knative-serving pod/domainmapping-webhook-55bdf595dd-859c2 1/1 Running 0 112m
knative-serving pod/net-kourier-controller-7df5866d8d-fvxpt 1/1 Running 0 107m
knative-serving pod/webhook-6d5c77f989-h8w2m 1/1 Running 0 112m
kourier-system pod/3scale-kourier-gateway-58856c6cc7-xtgdt 1/1 Running 0 107m
kube-system pod/coredns-558bd4d5db-cbq9z 1/1 Running 0 118m
kube-system pod/coredns-558bd4d5db-h95qv 1/1 Running 0 118m
kube-system pod/etcd-knative-control-plane 1/1 Running 0 118m
kube-system pod/kindnet-9t7gh 1/1 Running 0 118m
kube-system pod/kube-apiserver-knative-control-plane 1/1 Running 0 118m
kube-system pod/kube-controller-manager-knative-control-plane 1/1 Running 0 118m
kube-system pod/kube-proxy-gpv4p 1/1 Running 0 118m
kube-system pod/kube-scheduler-knative-control-plane 1/1 Running 0 118m
local-path-storage pod/local-path-provisioner-547f784dff-qldbl 1/1 Running 0 118m
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/hello ExternalName <none> kourier-internal.kourier-system.svc.cluster.local 80/TCP 96m
default service/hello-00001 ClusterIP 10.96.167.103 <none> 80/TCP 98m
default service/hello-00001-private ClusterIP 10.96.54.106 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 98m
default service/hello-00002 ClusterIP 10.96.43.125 <none> 80/TCP 50m
default service/hello-00002-private ClusterIP 10.96.69.118 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 50m
default service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 118m
knative-serving service/activator-service ClusterIP 10.96.124.221 <none> 9090/TCP,8008/TCP,80/TCP,81/TCP 112m
knative-serving service/autoscaler ClusterIP 10.96.179.96 <none> 9090/TCP,8008/TCP,8080/TCP 112m
knative-serving service/autoscaler-bucket-00-of-01 ClusterIP 10.96.17.155 <none> 8080/TCP 112m
knative-serving service/controller ClusterIP 10.96.177.44 <none> 9090/TCP,8008/TCP 112m
knative-serving service/default-domain-service ClusterIP 10.96.133.70 <none> 80/TCP 104m
knative-serving service/domainmapping-webhook ClusterIP 10.96.208.93 <none> 9090/TCP,8008/TCP,443/TCP 112m
knative-serving service/net-kourier-controller ClusterIP 10.96.84.122 <none> 18000/TCP 107m
knative-serving service/webhook ClusterIP 10.96.140.126 <none> 9090/TCP,8008/TCP,443/TCP 112m
kourier-system service/kourier NodePort 10.96.252.95 <none> 80:31080/TCP,443:31443/TCP 107m
kourier-system service/kourier-internal ClusterIP 10.96.60.48 <none> 80/TCP 107m
kube-system service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 118m
NAMESPACE NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
kube-system daemonset.apps/kindnet 1 1 1 1 1 <none> 118m
kube-system daemonset.apps/kube-proxy 1 1 1 1 1 kubernetes.io/os=linux 118m
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
default deployment.apps/hello-00001-deployment 0/0 0 0 98m
default deployment.apps/hello-00002-deployment 0/0 0 0 50m
knative-serving deployment.apps/activator 1/1 1 1 112m
knative-serving deployment.apps/autoscaler 1/1 1 1 112m
knative-serving deployment.apps/controller 1/1 1 1 112m
knative-serving deployment.apps/domain-mapping 1/1 1 1 112m
knative-serving deployment.apps/domainmapping-webhook 1/1 1 1 112m
knative-serving deployment.apps/net-kourier-controller 1/1 1 1 107m
knative-serving deployment.apps/webhook 1/1 1 1 112m
kourier-system deployment.apps/3scale-kourier-gateway 1/1 1 1 107m
kube-system deployment.apps/coredns 2/2 2 2 118m
local-path-storage deployment.apps/local-path-provisioner 1/1 1 1 118m
NAMESPACE NAME DESIRED CURRENT READY AGE
default replicaset.apps/hello-00001-deployment-5f8fbcf77 0 0 0 98m
default replicaset.apps/hello-00002-deployment-67bf5948d7 0 0 0 50m
knative-serving replicaset.apps/activator-6fb68fff7b 1 1 1 112m
knative-serving replicaset.apps/autoscaler-54f48f5bb7 1 1 1 112m
knative-serving replicaset.apps/controller-66cb4b556b 1 1 1 112m
knative-serving replicaset.apps/domain-mapping-66f8d5bc4c 1 1 1 112m
knative-serving replicaset.apps/domainmapping-webhook-55bdf595dd 1 1 1 112m
knative-serving replicaset.apps/net-kourier-controller-7df5866d8d 1 1 1 107m
knative-serving replicaset.apps/webhook-6d5c77f989 1 1 1 112m
kourier-system replicaset.apps/3scale-kourier-gateway-58856c6cc7 1 1 1 107m
kube-system replicaset.apps/coredns-558bd4d5db 2 2 2 118m
local-path-storage replicaset.apps/local-path-provisioner-547f784dff 1 1 1 118m
NAMESPACE NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
knative-serving horizontalpodautoscaler.autoscaling/activator Deployment/activator <unknown>/100% 1 20 1 112m
knative-serving horizontalpodautoscaler.autoscaling/webhook Deployment/webhook <unknown>/100% 1 5 1 112m
NAMESPACE NAME COMPLETIONS DURATION AGE
knative-serving job.batch/default-domain 1/1 20m 104m
NAMESPACE NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON ACTUAL REPLICAS DESIRED REPLICAS
default revision.serving.knative.dev/hello-00001 hello 1 True 0 0
default revision.serving.knative.dev/hello-00002 hello 2 True 0 0
NAMESPACE NAME LATESTCREATED LATESTREADY READY REASON
default configuration.serving.knative.dev/hello hello-00002 hello-00002 True
NAMESPACE NAME URL READY REASON
default route.serving.knative.dev/hello http://hello.default.127.0.0.1.sslip.io True
NAMESPACE NAME URL LATESTCREATED LATESTREADY READY REASON
default service.serving.knative.dev/hello http://hello.default.127.0.0.1.sslip.io hello-00002 hello-00002 True