Let's say we want to run two websites in our cluster - the first one will be a simple Hello World website, and the second one will be a Daily Dog Picture website that shows a random dog picture.

Let's create the deployments for these two websites.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
  labels:
    app.kubernetes.io/name: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: hello-world
  template:
    metadata:
      labels:
        app.kubernetes.io/name: hello-world
    spec:
      containers:
        - name: hello-world-container
          image: learncloudnative/helloworld:0.1.0
          ports:
            - containerPort: 3000

Save the above to helloworld-deployment.yaml and create the deployment (kubectl apply -f helloworld-deployment.yaml)

Next, we will deploy the Daily Dog Picture website.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: dogpic-web
  labels:
    app.kubernetes.io/name: dogpic-web
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: dogpic-web
  template:
    metadata:
      labels:
        app.kubernetes.io/name: dogpic-web
    spec:
      containers:
        - name: dogpic-container
          image: learncloudnative/dogpic-service:0.1.0
          ports:
            - containerPort: 3000

Save the YAML to dogpic-deployment.yaml and run kubectl apply -f dogpic-deployment.yaml to deploy the Daily Dog Picture website. Make sure both pods from both deployments are up and running:

$ kubectl get po
NAME                           READY   STATUS    RESTARTS   AGE
dogpic-web-d565655cc-5v6jt     1/1     Running   0          12s
hello-world-55b5f85668-dp6rb   1/1     Running   0          22s

We still need to deploy the Kubernetes Services for both of these deployments. The services will be of default, ClusterIP type, so there's no need to set type field explicitly.

kind: Service
apiVersion: v1
metadata:
  name: dogpic-service
  labels:
    app.kubernetes.io/name: dogpic-web
spec:
  selector:
    app.kubernetes.io/name: dogpic-web
  ports:
    - port: 3000
      name: http
---
kind: Service
apiVersion: v1
metadata:
  name: hello-world
  labels:
    app.kubernetes.io/name: hello-world
spec:
  selector:
    app.kubernetes.io/name: hello-world
  ports:
    - port: 3000
      name: http

Save the YAML above to service.yaml and then create the services by running kubectl apply -f services.yaml.

You can use -- as a separator in YAML files to deploy multiple resources from a single file.

Installing the Ambassador API gateway

Before we create the Ingress resource, we need to deploy an Ingress controller. The job of an ingress controller is to receive the incoming traffic and route it based on the rules defined in the Ingress resource.

To deploy the Ambassador API gateway, we will start by deploying the custom resource definitions (CRDs) the gateway uses:

kubectl apply -f <https://www.getambassador.io/yaml/ambassador/ambassador-crds.yaml>