创建应用

hello-world.yaml

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
  namespace: knative-demo
spec:
  template:
    spec:
      containers:
        - image: ghcr.dockerproxy.com/knative/helloworld-go:latest
          env:
            - name: TARGET
              value: "Go Sample v1"

创建资源

# kubectl apply -f hello-word.yaml
service.serving.knative.dev/hello created

查看资源

# kubectl get svc,rt,vs -n knative-demo
NAME                                  TYPE           CLUSTER-IP       EXTERNAL-IP                                        PORT(S)                                              AGE
service/helloworld-go                 ExternalName   <none>           knative-local-gateway.istio-system.svc.wgs.local   80/TCP                                               116s
service/helloworld-go-00001           ClusterIP      10.100.191.179   <none>                                             80/TCP,443/TCP                                       119s
service/helloworld-go-00001-private   ClusterIP      10.100.86.43     <none>                                             80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP   119s

NAME                                      URL                                               READY   REASON
route.serving.knative.dev/helloworld-go   http://helloworld-go.knative-demo.svc.wgs.local   True    

NAME                                                       GATEWAYS                                    HOSTS                                                                                                        AGE
virtualservice.networking.istio.io/helloworld-go-ingress   ["knative-serving/knative-local-gateway"]   ["helloworld-go.knative-demo","helloworld-go.knative-demo.svc","helloworld-go.knative-demo.svc.wgs.local"]   116s
virtualservice.networking.istio.io/helloworld-go-mesh      ["mesh"]                                    ["helloworld-go.knative-demo","helloworld-go.knative-demo.svc","helloworld-go.knative-demo.svc.wgs.local"]   116s

ClusterDomainClaim

要创建 DomainMapping,您必须首先拥有 ClusterDomainClaim。此 ClusterDomainClaim 将域名委托给要在其中创建 DomainMapping 的名称空间,这使得该名称空间中的 DomainMapping 能够使用该域名。

手动创建 ClusterDomainClaim

cdc.yaml

apiVersion: networking.internal.knative.dev/v1alpha1
kind: ClusterDomainClaim
metadata:
  name: helloworld.wgs.com
spec:
  namespace: knative-demo

创建 cdc 资源

# kubectl apply -f cdc.yaml
clusterdomainclaim.networking.internal.knative.dev/helloworld.wgs.com created

查看 cdc 资源

# kubectl get cdc -n knative-demo
NAME                 AGE
helloworld.wgs.com   70s

自动创建 ClusterDomainClaims

# kubectl patch configmap config-network -n knative-serving -p '{"data":{"autocreate-cluster-domain-claims":"true"}}'
configmap/config-network patched

DomainMapping

dm.yaml

apiVersion: serving.knative.dev/v1beta1
kind: DomainMapping
metadata:
  name: helloworld.wgs.com
  namespace: knative-demo
spec:
  ref:
    name: helloworld-go
    kind: Service
    apiVersion: serving.knative.dev/v1
    namespace: knative-demo

---
# 自动创建cdc
apiVersion: serving.knative.dev/v1beta1
kind: DomainMapping
metadata:
  name: helloworld-go.wgs.com
  namespace: knative-demo
spec:
  ref:
    name: helloworld-go
    kind: route
    apiVersion: serving.knative.dev/v1
    namespace: knative-demo

创建 dm

# kubectl apply -f dm.yaml
domainmapping.serving.knative.dev/helloworld.wgs.com created
domainmapping.serving.knative.dev/helloworld-go.wgs.com created

查看 dm

# kubectl get dm -n knative-demo
NAME                    URL                            READY   REASON
helloworld-go.wgs.com   http://helloworld-go.wgs.com   True    
helloworld.wgs.com      http://helloworld.wgs.com      True

查看 vs

# kubectl get vs -n knative-demo
NAME                            GATEWAYS                                      HOSTS                                                                                                        AGE
helloworld-go-ingress           ["knative-serving/knative-local-gateway"]     ["helloworld-go.knative-demo","helloworld-go.knative-demo.svc","helloworld-go.knative-demo.svc.wgs.local"]   34m
helloworld-go-mesh              ["mesh"]                                      ["helloworld-go.knative-demo","helloworld-go.knative-demo.svc","helloworld-go.knative-demo.svc.wgs.local"]   34m
helloworld-go.wgs.com-ingress   ["knative-serving/knative-ingress-gateway"]   ["helloworld-go.wgs.com"]                                                                                    90s
helloworld.wgs.com-ingress      ["knative-serving/knative-ingress-gateway"]   ["helloworld.wgs.com"]                                                                                       90s

访问测试

# curl -H "Host: helloworld.wgs.com" http://192.168.174.249
Hello Go Sample v1!
# curl -H "Host: helloworld-go.wgs.com" http://192.168.174.249
Hello Go Sample v1!

参考文档

https://knative.dev/docs/serving/services/custom-domains/