iT邦幫忙

2021 iThome 鐵人賽

DAY 20
0
自我挑戰組

從雲端開始的菜鳥任務系列 第 20

Day 20 Knative Serving DNS 測試(二)

  • 分享至 

  • xImage
  •  

設定 Networking Layer

參考: https://knative.dev/docs/install/any-kubernetes-cluster/

  • Ambassador
  • Contour
  • Gloo
  • Istio
  • Kong
  • Kourier

Kourier

  1. 安裝 Knative Kourier controller
kubectl apply --filename https://github.com/knative/net-kourier/releases/download/v0.20.0/kourier.yaml
namespace/kourier-system created
serviceaccount/3scale-kourier created
clusterrole.rbac.authorization.k8s.io/3scale-kourier created
clusterrolebinding.rbac.authorization.k8s.io/3scale-kourier created
deployment.apps/3scale-kourier-control created
service/kourier-control created
deployment.apps/3scale-kourier-gateway created
service/kourier created
service/kourier-internal created
configmap/kourier-bootstrap created
  1. 將Knative Serving配置為默認使用Kourier
kubectl patch configmap/config-network \
  --namespace knative-serving \
  --type merge \
  --patch '{"data":{"ingress.class":"kourier.ingress.networking.knative.dev"}}'
configmap/config-network patched
  1. 提取外部IP或CNAME
kubectl --namespace kourier-system get service kourier
NAME      TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
kourier   LoadBalancer   10.102.77.91   <pending>     80:30794/TCP,443:31790/TCP   22s

部屬服務

參考: https://knative.dev/docs/serving/getting-started-knative-app/

service.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.io/knative-samples/helloworld-go # Reference to the image of the app
          env:
            - name: TARGET # The environment variable printed out by the sample app
              value: "Go Sample v1"
kubectl apply --filename service.yaml

可以看到

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

查看服務

查看服務是否成功部屬

kubectl get ksvc helloworld-go
NAME            URL                                        LATESTCREATED         LATESTREADY           READY   REASON
helloworld-go   http://helloworld-go.default.example.com   helloworld-go-00001   helloworld-go-00001   True

配置 DNS

  • Magic DNS (xip.io)
    This will only work if the cluster LoadBalancer service exposes an IPv4 address or hostname, so it will not work with IPv6 clusters or local setups like Minikube.
  • Real DNS
  • Temporary DNS

Magic DNS 僅在集群 LoadBalancer 服務公開了 IPv4 地址的情況下才有效,因此不適用於 IPv6 集群,AWS 或 Minikube之類的本地設置。

生產環境的話,應該考慮使用Real DNS。

Temporary DNS

If you are using curl to access the sample applications, or your own Knative app, and are unable to use the “Magic DNS (xip.io)” or “Real DNS” methods, there is a temporary approach. This is useful for those who wish to evaluate Knative without altering their DNS configuration, as per the “Real DNS” method, or cannot use the “Magic DNS” method due to using, for example, minikube locally or IPv6 clusters.

取得部屬服務的 URL

kubectl get ksvc
NAME            URL                                        LATESTCREATED         LATESTREADY           READY   REASON
helloworld-go   http://helloworld-go.default.example.com   helloworld-go-00001   helloworld-go-00001   True

Instruct curl to connect to the External IP or CNAME defined by the networking layer in section 3 above, and use the -H "Host:" command-line option to specify the Knative application’s host name. For example, if the networking layer defines your External IP and port to be http://192.168.39.228:32198 and you wish to access the above helloworld-go application, use:

curl -H "Host: helloworld-go.default.example.com" http://10.20.1.142:30794
Hello Go Sample v1!

設置自定義 Domain

參考: https://knative.dev/docs/serving/using-a-custom-domain/

先確認目前服務 route

kubectl get route helloworld-go
NAME            URL                                        READY   REASON
helloworld-go   http://helloworld-go.default.example.com   True

By default, Knative Serving routes use example.com as the default domain. The fully qualified domain name for a route by default is {route}.{namespace}.{default-domain}.

To change the {default-domain} value there are a few steps involved:

  • Edit using kubectl
  • Apply from a file

Apply from a file

config-domain.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-domain
  namespace: knative-serving
data:
  # These are example settings of domain.
  # example.org will be used for routes having app=prod.
  example.org: |
    selector:
      app: prod
  # Default value for domain, for routes that does not have app=prod labels.
  # Although it will match all routes, it is the least-specific rule so it
  # will only be used if no other domain matches.
  mydomain.com: ""

更新 Domain Configuration

kubectl apply --filename config-domain.yaml

查看服務的 Domain

kubectl get route helloworld-go
NAME            URL                                         READY   REASON
helloworld-go   http://helloworld-go.default.mydomain.com   True

重新拜訪更新過 Domain 的服務

curl -H "Host: helloworld-go.default.mydomain.com" http://10.20.1.142:30794
Hello Go Sample v1!

上一篇
Day 19 Knative Serving DNS 測試
下一篇
Day 21 菜鳥的 helm 紀錄 - 介紹篇
系列文
從雲端開始的菜鳥任務30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言