Kubernetes/Kubernetes Workshop/Kubernetes Package Manager

From Wikitech

Overview

At the end of this module, you should be able to:

  • Use Kubernetes’ helm in deployment.

Step 1: Adding tags to various builds of the apache web server

Helm is a popular package manager for Kubernetes. It supports operations similar to other package managers such as apt or npm, and helm packages group files together into deployable groups. The helm package manager can install, delete, upgrade and downgrade packages, plus it maintains a history of releases. In this module, you will use helm in an application.

So far, you have not versioned your Docker images. Instead, you relied on the latest tag, which is provided automatically to the most recent build.

You need to add a tag to the Docker build command to use versions.

  • Refer to the simpleapache app you built in Module 2. Make a change to the content of the HTML file in the Dockerfile:
FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y apache2
RUN echo '<HTML><BODY>Hi World</BODY></HTML' >/var/www/html/index.html
EXPOSE 80
CMD ["apachectl","-DFOREGROUND"]
  • Login to Docker and build the image with a tag latest:
$ docker login
$ docker build -t <your-dockerhub-username>/<given-image-name>:<tag> .
  • Push the new image and its tag to Docker Hub:
$ docker push <docker_hub_username>/<given-image-name>:<tag>

Now, when you check the image on Docker Hub, it should have a different tag. When you push small changes to Docker Hub, you see how the Docker layering works in the output of the docker push command.

Since you only changed the HTML in one file and assigned the image a tag, the push is relatively quick, as most of the image is unchanged. On Docker Hub, you should be able to see both the old and new versions of your image, identifiable by their tags.

You can run these different versions of simpleapache by changing the image used in the deployment.

  • Create a deployment file for a version of the simpleapache app:

simpleapachedeployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
 name: simpleapache
 labels:
   app: simpleapache
spec:
 replicas: 1
 strategy: 
   type: RollingUpdate
 selector:
   matchLabels:
     app: simpleapache
 template:
   metadata:
     labels:
       app: simpleapache
   spec:
     containers:
      - name: simpleapache
        image: <userid>/simpleapache:1.0
        imagePullPolicy: Always
  • Create a YAML script for the service:

simpleapacheservice.yaml

kind: Service
apiVersion: v1
metadata:
 name: simpleapache
spec:
 selector:
   app: simpleapache
 type: LoadBalancer
 ports:
 - protocol: TCP
   port: 80
   targetPort: 80
  • Deploy the application:
$ minikube start
$ kubectl apply -f simpleapachedeployment.yaml
$ kubectl apply -f simpleapacheservice.yaml
$ minikube service list
  • Make a curl request to the URL listed:
$ curl <IP:port>
<HTML><BODY>Hi World</BODY></HTML

Next, deploy the simpleapache Docker image with a new tag and run. Feel free to change the contents of the HTML line:

$ kubectl apply -f simpleapachedeployment.yaml
  • Make a curl request to the URL listed:
$ curl <IP:port>
Hey World
  • Delete the application:
$ kubectl delete service simpleapache
$ kubectl delete deployment simpleapache

In the next step, you will learn how helm makes versioned deployments easier.

Step 2: Helm commands

Below are some useful helm commands:

$ helm list - lists releases
$ helm status <release> - <release> 
$ helm history <release>
$ helm delete <release>

Hands-on Demo: Using the Helm Chart in a Deployment

To use helm, you would have to install it first. A helm chart is a directory with the files helm needs to operate.

The "correct" way to use helm, is to use the helm create command. This command will create all directories needed and provide some defaults. However, this is more complicated than our current scope.

In the simpleapache directory, we need a chart.yaml file. This file contains the basic descriptions of the application.

chart.yaml

name: simpleapache
version: 1.0.0
description: a simple web server
maintainers:
     - name: A. Maintainer 
       email: simpleapache@example.com
  • It would help if you also had a templates directory that would contain the YAML files for the package.
$ mkdir templates
$ cp ../simplepache*.yaml templates
  • The structure of the simpleapache directory should be similar to the below chart:
simpleapache
|- Chart.yaml
|- templates
|---- simpleapachedeployment.yaml
|---- simpleapacheservice.yaml
  • You can now deploy the app with helm and test it.
$ cd .
$ helm install <random-name> <folder-name>/
$ kubectl get pods
$ minikube service list
$ curl URL

Next Module

Module 8: Manageability and Security

Previous Module