BreakingExpress

Automate your container orchestration with Ansible modules for Kubernetes

Ansible is among the greatest instruments for automating your work. Kubernetes is among the greatest instruments for orchestrating containers. What occurs once you mix the 2? As you may count on, Ansible mixed with Kubernetes permits you to automate your container orchestration. 

Ansible modules

On its personal, Ansible is mainly only a framework for decoding YAML recordsdata. Its true energy comes from its many modules. Modules are what allow you to invoke exterior purposes with just some easy configuration settings in a playbook.

There are a couple of modules that deal straight with Kubernetes, and some that deal with associated know-how like Docker and Podman. Learning a brand new module is commonly just like studying a brand new terminal command or a brand new API. You get accustomed to a module from its documentation, you study what arguments it accepts, and also you equate its choices to the way you may use the applying it interfaces with.

Access a Kubernetes cluster

To check out Kubernetes modules in Ansible, you need to have entry to a Kubernetes cluster. If you do not have that, then you definately may attempt to open a trial account on-line, however most of these are brief time period. Instead, you possibly can set up Minikube, as described on the Kubernetes web site or in Bryant Son’s glorious article on getting started with Minikube. Minikube gives a neighborhood occasion of a single-node Kubernetes set up, permitting you to configure and work together with it as you’ll a full cluster.

[Download the Ansible k8s cheat sheet]

Before putting in Minikube, you need to make sure that your atmosphere is able to function a virtualization backend. You may have to put in libvirt and grant your self permission to the libvirt group:

$ sudo dnf set up libvirt
$ sudo systemctl begin libvirtd
$ sudo usermod --append --groups libvirt `whoami`
$ newgrp libvirt

Install Python modules

To put together for utilizing Kubernetes-related Ansible modules, you also needs to set up a couple of helper Python modules:

$ pip3.6 set up kubernetes --user
$ pip3.6 set up openshift --user

Start Kubernetes

If you are utilizing Minikube as a substitute of a Kubernetes cluster, use the minikube command to start out up a neighborhood, miniaturized Kubernetes occasion in your laptop:

$ minikube begin --driver=kvm2 --kvm-network default

Wait for Minikube to initialize. Depending in your web connection, this might take a number of minutes.

Get details about your cluster

Once you’ve got began your cluster efficiently, you will get details about it with the cluster-info possibility:

$ kubectl cluster-info
Kubernetes grasp is operating at https://192.168.39.190:8443
KubeDNS is operating at https://192.168.39.190:8443/api/v1/namespaces/kube-system/providers/kube-dns:dns/proxy

To additional debug and diagnose cluster issues, use 'kubectl cluster-info dump'.

Use the k8s module

The entry level for utilizing Kubernetes by Ansible is the k8s module, which lets you handle Kubernetes objects out of your playbooks. This module describes states ensuing from kubectl directions. For occasion, this is how you’ll create a brand new namespace with kubectl:

$ kubectl create namespace my-namespace

It’s a easy motion, and the YAML illustration of the identical result’s equally terse:

- hosts: localhost
  duties
:
    - identify
: create namespace
      k8s
:
        identify
: my-namespace
        api_version
: v1
        variety
: Namespace
        state
: current

In this case, the host is outlined as localhost, below the belief that you just’re operating this towards Minikube. Notice that the module in use defines the syntax of the parameters out there (resembling api_version and variety).

Before utilizing this playbook, confirm it with yamllint:

$ yamllint instance.yaml

Correct any errors, after which run the playbook:

$ ansible-playbook ./instance.yaml

Verify that the brand new namespace has been created:

$ kubectl get namespaces
NAME              STATUS   AGE
default           Active   37h
kube-node-lease   Active   37h
kube-public       Active   37h
kube-system       Active   37h
demo              Active   11h
my-namespace      Active   3s

Pull a container picture with Podman

Containers are Linux programs, nearly impossibly minimal in scope, that may be managed by Kubernetes. Much of the container specs have been outlined by the LXC project and Docker. A latest addition to the container toolset is Podman, which is widespread as a result of it runs with out requiring a daemon.

With Podman, you possibly can pull a container picture from a repository, resembling Docker Hub or Quay.io. The Ansible syntax for that is easy, and all you must know is the situation of the container, which is obtainable from the repository’s web site:

   - identify: pull a picture
      podman_image
:
        identify
: quay.io/jitesoft/nginx

Verify it with yamllint:

$ yamllint instance.yaml

And then run the playbook:

$ ansible-playbook ./instance.yaml
[WARNING]: offered hosts listing is empty, solely localhost is obtainable.
Note that the implicit localhost doesn't match 'all'

PLAY [localhost] ************************

TASK [Gathering Facts] ************************
okay: [localhost]

TASK [create k8s namespace] ************************
okay: [localhost]

TASK [pull a picture] ************************
modified: [localhost]

PLAY RECAP ************************
localhost: okay=three modified=1 unreachable=zero failed=zero
           skipped=zero rescued=zero ignored=zero

Deploy with Ansible

You’re not restricted to small upkeep duties with Ansible. Your playbook can work together with Ansible in a lot the identical means a configuration file does with kubectl. In truth, in some ways, the YAML you already know through the use of Kubernetes interprets to your Ansible performs. Here’s a configuration you may move on to kubectl to deploy a picture (on this instance, an internet server):

apiVersion: apps/v1
variety
: Deployment
metadata
:
  identify
: my-webserver
spec
:
  selector
:
    matchLabels
:
      run
: my-webserver
  replicas
: 1
  template
:
    metadata
:
      labels
:
        run
: my-webserver
    spec
:
      containers
:
      - identify
: my-webserver
        picture
: nginx
        ports
:
        - containerPort
: 80

If you already know these parameters, then you definately principally know the parameters required to perform the identical with Ansible. You can, with little or no modification, transfer that YAML right into a definition component in your Ansible playbook:

   - identify: deploy an internet server
      k8s
:
        api_version
: v1
        namespace
: my-namespace
        definition
:
          variety
: Deployment
          metadata
:
            labels
:
              app
: nginx
            identify
: nginx-deploy
          spec
:
            replicas
: 1
            selector
:
              matchLabels
:
                app
: nginx
            template
:
              metadata
:
                labels
:
                  app
: nginx
              spec
:
                containers
:
                  - identify
: my-webserver
                    picture
: quay.io/jitesoft/nginx
                    ports
:
                      - containerPort
: 80
                        protocol
: TCP

After operating this, you possibly can see the deployment with kubectl, as typical:

$ kubectl -n my-namespace get pods
NAME                      READY  STATUS
nginx-deploy-7fdc9-t9wc2  1/1    Running

Modules for the cloud

As extra growth and deployments transfer to the cloud, it is necessary to grasp automate the necessary features of your cloud. The k8s and podman_image modules are solely two examples of modules associated to Kubernetes and a mere fraction of modules developed for the cloud. Take a take a look at your workflow, discover the duties you need to monitor and automate, and see how Ansible will help you do extra by doing much less.

Exit mobile version