Deployments and Services

A Deployment is the artefact that is used to describe the end state of a POD.  It is usually written in YAML and would look something like this

YAML Deployment File
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.9.1
        ports:
        - containerPort: 80

Key to the above YAML file

keydescription
metadata:Key/value pairs that can be used in search and configuration operation, the above example name is the label, nginx-deployment is the value.  When viewed in the dashboard nginx-deployment is a label that will be a clickable field.  From the dashboard, look at the Overview or Deployments view, you will see the deployment name "nginx-deployment", adjacent to this value is the label app:nginx.  
spec:The specification
replicas:The number of PODs in the cluster
spec:/selector:/matchLabels:

A series of labels that can be used in search and configuration operations, in the above example app is the label, nginx is the value.

From the dashboard, look at the Overview or Deployments view, you will see the deployment name "nginx-deployment", select the deployment name and the deployment details will be shown.  Within the details you will the selector value.

spec:/template:/spec:/containers:

Use this section to specify the container details

  • name - the container name
  • image - path to image (default is docker but can be url other repos such as googles)
  • ports:/containerPort - the host port the application exposes itself out to the world (docker run -p <host port>:<container port>)

Steps to deploying an application

  1. Create a deployment similar to the one shown above
  2. execute the command => kubectl apply -f <deployment yaml file>
  3. execute the command => kubectl expose deployment nginx-deployment --type=LoadBalancer --port=80

Key to the components in the steps above

componentdescription
nginx-deploymentIt should be the value of metadata:/name: in the deployment file
--type=LoadBalancerThe type of service being created
--port=80

What port the service will be exposed on.  I have had to set this to 80 in order the view the service in a browser (I cannot se another way of accessing the service when the following URL is used in the browser - http://localhost:8001/api/v1/namespaces/default/services/nginx-deployment/proxy/

In the URL shown above, http://localhost:8001 is the URL given for the minikube proxy that is invoked with the command kubectl proxy

Notice in the URL the use of the deployment value nginx-deployment for the key metadata:/name:

Notice the path to your service is always <minikube proxy IP>:<port>/api/v1/namespaces/default/services

Note on Service Port

When you execute the command kubectl expose deployment <deployment name> --type=LoadBalancer --port=<port>, you do not need to use a port value of 80 when another service is going to use this service.  The only reason we have used 80 here is because we want to view the service via a browser.

Worked Example

To create a deploy it's usually as simple as tying the following

But when check if there is a deploy you may find that it says there are no resources

It's best to work from a deployment file

Begin by creating a yaml file

helloworld.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: shw
  labels:
    run: hw  
spec:
  replicas: 1
  selector:
    matchLabels:
      run: hw
  template:
    metadata:
      labels:
        run: hw
    spec:
      containers:
      - image: kathequian/helloworld
        name: shw
        ports:
        - containerPort: 80

The service and deployment name is shw as shown on line 4

Execute the following command 

 

Now use the get pods and get deployments commands to see what has been created

Notice the name of the pod, it has a unique ID appended to the deployment name

The service is not available to the outside world until it is exposed with the kubectl expose deployment <deployment name> command

The --type=NodePort is an important switch.  It tells kubernetes to expose the service onto the Nodes Port.  We can see the exposed service using the following command

Notice shw has been exposed on port 32638 on the node, so it is now visible to the outside world.

To see the service use the kubectl service <deployment name> command

Go to your browser and enter the URL shown if the app is not visible