[Serverless Knative] Knative Docs - Deploying your first Knative Service

Deploying your first Knative Service


In this tutorial, you will deploy a “Hello world” service.


This service will accept an environment variable, TARGET, and print “Hello ${TARGET}!.

Since our “Hello world” Service is being deployed as a Knative Service, not a Kubernetes Service, it gets some super powers out of the box 🚀.

Knative Service: “Hello world!”

kn

1
2
3
4
5
$ kn service create hello \
--image gcr.io/knative-samples/helloworld-go \
--port 8080 \
--env TARGET=World \
--revision-name=world

Expected output:

1
2
Service hello created to latest revision 'hello-world' is available at URL:
http://hello.default.127.0.0.1.nip.io

Why did I pass in revision-name?

Note the name “world” which you passed in as “revision-name,” naming your Revisions will help you to more easily identify them, but don’t worry, you’ll learn more about Revisions later.


YAML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# hello.yaml

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
spec:
template:
metadata:
# This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name}
name: hello-world
spec:
containers:
- image: gcr.io/knative-samples/helloworld-go
ports:
- containerPort: 8080
env:
- name: TARGET
value: "World"

Once you’ve created your YAML file (named something like “hello.yaml”):

1
$ kubectl apply -f hello.yaml

Expected output:

1
service.serving.knative.dev/hello created

To see the URL where your Knative Service is hosted, leverage the kubectl CLI:

1
$ kubectl get ksvc

Expected output:

1
2
NAME             URL                                LATESTCREATED          LATESTREADY            READY   REASON
hello http://hello.default.127.0.0.1.nip.io hello-0001 hello-0001 True

Or the kn CLI:

1
$ kn service list

Expected output:

1
2
NAME             URL                                LATEST                 AGE     CONDITIONS   READY   REASON
hello http://hello.default.127.0.0.1.nip.io hello-0001 1min 1 OK / 1 True

Ping your Knative Service

Ping your Knative Service by opening http://hello.default.127.0.0.1.nip.io in your browser of choice or by running the command:

1
$ curl http://hello.default.127.0.0.1.nip.io

Expected output:

1
Hello World!

Are you seeing curl: (6) Could not resolve host: hello.default.127.0.0.1.nip.io?

In some cases your DNS server may be set up not to resolve *.nip.io addresses. If you encounter this problem, it can be fixed by using a different nameserver to resolve these addresses.

The exact steps will differ according to your distribution. For example, with Ubuntu derived systems which use systemd-resolved, you can add the following entry to the /etc/systemd/resolved.conf:

1
2
3
[Resolve]
DNS=8.8.8.8
Domains=~nip.io.

Then simply restart the service with sudo service systemd-resolved restart.

For MacOS users, you can add the DNS and domain using the network settings as explained here - https://support.apple.com/en-gb/guide/mac-help/mh14127/mac.


Congratulations 🎉, you’ve just created your first Knative Service. Up next, Autoscaling!

References

[1] First Knative Service - Knative - https://knative.dev/docs/getting-started/first-service/

[2] Home - Knative - https://knative.dev/docs/

[3] Installing Guide - https://knative.dev/docs/install/