Skip to content

ababhilash/K8s-Ansible

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Objective: Kubernetes cluster setup using Ansible

Build Status

Description:

I used an EC2 instance (CentOS) and installed Ansible on it, also used CentOS for the Kube servers. Used passwordless authentication between the Ansible server and the K8’s servers. I have created a script to avoid configuring the Ansible hosts manually and it will also verify the connection to the Kube servers are faultless. The script will ask for some details such as the public IP of K8’s Master, no. of K8’s Nodes for the cluster, and the public IPs of worker nodes. After that, I have initialized the Ansible roles and to set up the Kube cluster proper YAML files are placed under the respective roles. The YAML file consists of installing services and dependencies, also to satisfy the Kubernetes requirements I have used “Flannel” as an Overlay Network. The interesting fact is that I’m not touching the K8’s master and its nodes for configuration. The execution of the playbook will results in the K8’s cluster formation within 10 mins (PS: If you have a good internet connection).

Prerequisite:

  • A server with Ansible installed.
  • A server for K8’s Master and ‘n’ no.of nodes (You can specify any no.of K8’s nodes)
  • Public IP address of K8’s Master and ‘n’ nodes

Steps:

  • Step 1: Installed Ansible on a CentOS server.
  • Step 2: Made passwordless authentication with the Ansible server and the K8’s servers.
  • Step 3: Run the script configure.sh which is present under the root directory.
  • Step 4: The script will ask for the details such as:
    1. The public IP address of K8’s master
    2. No.of Kube nodes which you want to use for the cluster.
    3. The public IP address of “n” no. of nodes
  • Step 5: If the connections are perfect the script will show the ping result to the K8’s master and its nodes as an output.
  • Step 6: Initialized the Ansible roles.
  • Step 7: Run the setup.yml file with the Ansible playbook command.

Installation:

Installing Ansible on CentOS server:

$ sudo yum update -y
$ sudo yum install ansible -y

This would successfully install the Ansible.
Also recommended to do an update after the installation of ansible

File Locations:

The Shell Script and Yaml file are present under the directory named “cluster”.

[root@ip-172-31-86-154 cluster]# pwd
/root/cluster
[root@ip-172-31-86-154 cluster]# ll
total 8
-rwx------. 1 root root 1143 Jun 11 11:02 configure.sh
-rw-r--r--. 1 root root  109 Jun 11 06:30 setup.yml

Output of the Shell script:

Initializing Ansible roles:

Ansible-galaxy is the command to be used to re-define the Ansible roles
I have initialized two roles, one for K8’s Master and one for K8’s nodes.

# ansible-galaxy init /etc/ansible/roles/kube_deploy_all --offline
- Role /etc/ansible/roles/kube_deploy_all was created successfully
# ansible-galaxy init /etc/ansible/roles/kube_deploy_master --offline
- Role /etc/ansible/roles/kube_deploy_master was created successfully

Ansible roles:

Role 1: kube_deploy_all
  • The configure.yml file consists of disabling SELinux, disabling swap, adding Kubernetes yum repositories, etc on all the K8’s servers.
  • The install.yml file consists of installing docker service, kubectl, kubeadm, etc on all the K8’s servers.
  • The service.yml file consists of starting the docker and kubelet service.
[root@ip-172-31-86-154 roles]# tree kube_deploy_all/
kube_deploy_all/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── configure.yml
│   ├── install.yml
│   ├── main.yml
│   └── service.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml
Role 2: kube_deploy_master
  • The configure.yml file consists of initializing the cluster, copying admin.conf contents to the user’s Kube configuration, installing the Pod overlay network (Flannel) on K8’s master.
  • The install.yml file consists of installing the kubectl service on K8’s master.
[root@ip-172-31-86-154 roles]# tree kube_deploy_master/
kube_deploy_master/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── configure.yml
│   ├── install.yml
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

Ports to be allowed on the Kube servers:

Ports Description Server
6443/tcp Kubernetes API port All k8s servers
2379-2380/tcp etcd server client API All k8s servers
10250/tcp API which allows full node access All k8s servers
10251/tcp kube-scheduler All k8s servers
10252/tcp kube-controller-manager All k8s servers
10255/tcp HTTP read-only port All k8s servers
30000-32767/tcp NodePort Services Only on worker nodes
6783-6784/tcp CNI ports Only on worker nodes

Ansible playbook

Run the ansible playbook named “setup.yml”

# ansible-playbook setup.yml

By running this file will install all the services, dependencies, Overlay Network, etc for the Kubernetes cluster setup. Upon completion of all the above steps, the Kubernetes cluster should be up and running. We can login to the master or worker nodes. The result of the successfull installation of the Kubernetes are stored on a file named “cluster_initialized.txt” on the root directory. On the same file there consists of the joining token for the worker nodes to join with the Kubernetes master. Executing the token on the worker nodes will finish the job.

Outputs:

Token for joining the worker nodes

All the pods are up and running

Details of created Overlay Network (Flannel)

Conclusion:

As we can see the master and worker nodes are in ready status. This concludes that kubernetes has been installed successfully and also successfully joined two worker nodes. Now its possible to create pods and services on this Kube Cluster.

Thank you

About

Kubernetes cluster using Ansible Roles

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages