Skip to content

Deploy Your App

Kubernetes

Deploy an App to k8s

Create namespace

For this guide we will create the namespace lab:

kubectl create namespace lab

You can read about namespaces here:

Namespaces

Instead of running a single container as you normally would on a docker host,

In kubernetes we create a deployment.

That deployment will create the number of pods we specify, in each pod we can run at least one container.

Creating deployment

First we need to create our deployment yaml file:

Create the file deploymen.yml

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.7.9
        ports:
        - containerPort: 80

This deployment will create 3 pods of nginx.

Apply the deployment file, so kubernetes will create it.

kubectl apply -f deployment.yml -n lab

Output:

deployment.apps/nginx-deployment created

Get all deployments in namespace lab

kubectl get deployments -n lab

Output:

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           11s

Get all pods in "lab" namespace:

kubectl get pods -n lab

Output:

NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-6dd86d77d-7p7f5   1/1     Running   0          103s
nginx-deployment-6dd86d77d-ghwvf   1/1     Running   0          103s
nginx-deployment-6dd86d77d-ksjmt   1/1     Running   0          103s

Describe the nginx-deployment we just created:

kubectl describe deployment nginx-deployment -n lab

Output:

Name:                   nginx-deployment
Namespace:              lab
CreationTimestamp:      Thu, 05 Mar 2020 11:04:46 +0200
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 1
                        kubectl.kubernetes.io/last-applied-configuration:
                        {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx-deployment","namespace":"l...
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
Labels:  app=nginx
Containers:
nginx:
    Image:        nginx:1.7.9
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
Volumes:        <none>
Conditions:
Type           Status  Reason
----           ------  ------
Available      True    MinimumReplicasAvailable
Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-6dd86d77d (3/3 replicas created)
Events:          <none>

Exposing the service

Create an ELB that will expose the deployment as a service:

kubectl expose deployment nginx-deployment --port=80 --type=LoadBalancer -n lab

Output:

service/nginx-deployment exposed

Get all services in namespace "lab":

kubectl get svc -n lab -o wide

Output:

NAME               TYPE           CLUSTER-IP      EXTERNAL-IP                                                               PORT(S)        AGE   SELECTOR
nginx-deployment   LoadBalancer   172.20.29.221   a7e287f8e5ecc11eaa3410a392e66e31-2141092553.us-east-1.elb.amazonaws.com   80:32583/TCP   16m   app=nginx

Access the service we just created:

According to the output of the command above, go to your browser and access the LoadBalancer you just created:

For example:

http://a7e287f8e5ecc11eaa3410a392e66e31-2141092553.us-east-1.elb.amazonaws.com/

It could take a few minutes...

If you just created the service it should take a few minutes for the CNAME DNS Record of the LoadBalancer to become available."

so just try again in 5 minutes.

Once it is available you should see the Nginx welcome screen:

nginx

Delete the service and deployment we created

Delete the service

kubectl delete svc/nginx-deployment -n lab

Output:

service "nginx-deployment" deleted

Delete the deployment

kubectl delete deployment/nginx-deployment -n lab

Output:

deployment.extensions "nginx-deployment" deleted

Since we deployed the app as a deployment, and as a service this allows us to:

  • Update the deployment (new released container image tag)
  • Roll back the deployment
  • Scale up/down the deployment
  • Run Canary deployment

You can find information about that at the kubernetes.io deployment page:

deployments

Cleanup resources.

To clean up, delete the namespace you created.

This will also remove any resource in the namespace.

kubectl delete namespace lab

Summary

Summary of this page

We learned how to deploy an app to k8s using deployment and how to expose it externally as a service

In the next page we will deploy an app directly from Gitlab CI

Comments