Book Image

Kubernetes - A Complete DevOps Cookbook

By : Murat Karslioglu
Book Image

Kubernetes - A Complete DevOps Cookbook

By: Murat Karslioglu

Overview of this book

Kubernetes is a popular open source orchestration platform for managing containers in a cluster environment. With this Kubernetes cookbook, you’ll learn how to implement Kubernetes using a recipe-based approach. The book will prepare you to create highly available Kubernetes clusters on multiple clouds such as Amazon Web Services (AWS), Google Cloud Platform (GCP), Azure, Alibaba, and on-premises data centers. Starting with recipes for installing and configuring Kubernetes instances, you’ll discover how to work with Kubernetes clients, services, and key metadata. You’ll then learn how to build continuous integration/continuous delivery (CI/CD) pipelines for your applications, and understand various methods to manage containers. As you advance, you’ll delve into Kubernetes' integration with Docker and Jenkins, and even perform a batch process and configure data volumes. You’ll get to grips with methods for scaling, security, monitoring, logging, and troubleshooting. Additionally, this book will take you through the latest updates in Kubernetes, including volume snapshots, creating high availability clusters with kops, running workload operators, new inclusions around kubectl and more. By the end of this book, you’ll have developed the skills required to implement Kubernetes in production and manage containers proficiently.
Table of Contents (12 chapters)

Configuring a Kubernetes cluster using Ansible

Powerful IT automation engines such as Ansible can be used to automate pretty much any day-to-day IT task, including the deployment of Kubernetes clusters on bare-metal clusters. In this section, we will learn how to deploy a simple Kubernetes cluster using Ansible playbooks.

Getting ready

    In this recipe, we will use an Ansible playbook. The examples that will be used in these recipes are accessible through the k8sdevopscookbook GitHub repository.

    Before you start executing the commands in this section's recipes, clone the Ansible playbook examples using the following command:

    $ git clone

    You will find the examples stored under the k8sdevopscookbook/src directory.

    How to do it…

    This section will take you through how to configure a Kubernetes cluster using Ansible. To that end, this section is further divided into the following subsections to make this process easier:

    • Installing Ansible
    • Provisioning a Kubernetes cluster using an Ansible playbook
    • Connecting to the Kubernetes cluster

    Installing Ansible

    In order to provision a Kubernetes cluster using an Ansible playbook, follow these steps:

    1. To install Ansible on your Linux workstation, first, we need to add the necessary repositories:
    $ sudo apt-get install software-properties-common
    $ sudo apt-add-repository --yes --update ppa:ansible/ansible
    1. Install Ansible using the following command:
    $ sudo apt-get update && sudo apt-get install ansible -y
    1. Verify its version and make sure Ansible is installed:
    $ ansible --version

    At the time this recipe was written, the latest Ansible version was 2.9.4.

    Provisioning a Kubernetes cluster using an Ansible playbook

    In order to provision a Kubernetes cluster using an Ansible playbook, follow these steps:

    1. Edit the hosts.ini file and replace the master and node IP addresses with your node IPs where you want Kubernetes to be configured:
    $ cd src/chapter1/ansible/ && vim hosts.ini
    1. The hosts.ini file should look as follows:
    1. Edit the groups_vars/all.yml file to customize your configuration. The following is an example of how to do this:
    kube_version: v1.14.0
    token: b0f7b8.8d1767876297d85c
    init_opts: ""
    kubeadm_opts: ""
    service_cidr: ""
    pod_network_cidr: ""
    calico_etcd_service: ""
    network: calico
    network_interface: ""
    enable_dashboard: yes
    insecure_registries: []
    systemd_dir: /lib/systemd/system
    system_env_dir: /etc/sysconfig
    network_dir: /etc/kubernetes/network
    kubeadmin_config: /etc/kubernetes/admin.conf
    kube_addon_dir: /etc/kubernetes/addon
    1. Run the site.yaml playbook to create your cluster:
    $ ansible-playbook site.yaml

    Your cluster will be deployed based on your configuration.

    Connecting to the Kubernetes cluster

    To get access to your Kubernetes cluster, you need to follow these steps:

    1. Copy the configuration file from the master1 node:
    $ scp root@master:/etc/kubernetes/admin.conf ~/.kube/config
    1. Now, use kubectl to manage your cluster.

    See also