Showing posts with label Ansible. Show all posts
Showing posts with label Ansible. Show all posts

1/07/2020

Setup Kubernetes Cluster with Ansible and Virtualbox VMs (Redhat)




This is an improvement for previous blog. In here we are going to use Redhat 7.6 has virtual machines and Ansible as K8s setup automation tool.


Steps:


1. You need to create three Redhat virtual machines in virtualbox. You can follow below article for this.


user: dhanuka
user_password: root

root_password: root

https://developers.redhat.com/products/rhel/hello-world#fndtn-rhel



After successfully setup virtual machine, you need to install guest additions as described below.
https://www.virtualbox.org/manual/ch04.html

2. Once you setup one virtual machine you need to configure network and other OS configuration as we did in previous blog post.



  • Please note that, we install Docker with Ansible, so you don't have to install it manually. 
  • Also we are using host-only-adapter and Net adapter for networking.









3. Disable SWAP


vim /etc/fstab

comment out lines which indicate about swap
example :

#/dev/mapper/rhel-swap   swap                    swap    defaults        0 0


4. Create a static IP


Type below command and select enp0s9 entry

ifconfig -a


enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.3.15  netmask 255.255.255.0  broadcast 10.0.3.255
        inet6 fe80::1a2e:75d3:a38b:aac6  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:76:52:cd  txqueuelen 1000  (Ethernet)
        RX packets 73480  bytes 96132054 (91.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11196  bytes 2431057 (2.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s17: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.20  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::64f6:2e06:bb4a:ee00  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:af:7a:5f  txqueuelen 1000  (Ethernet)
        RX packets 88747  bytes 51715147 (49.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 67547  bytes 7154603 (6.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


sudo vim /etc/sysconfig/network-scripts/ifcfg-enp0s17

insert below code block to above file: example k8s-worker-node1


HWADDR=08:00:27:AF:7A:5F
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.56.20
PREFIX=24
GATEWAY=192.168.56.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s17
UUID=1ab88d1d-3f78-47ab-b516-394c288a3f6f
ONBOOT=yes



Change /etc/hosts file with below IP entries

sudo vim /etc/hosts

192.168.56.10   k8s-worker-node2
192.168.56.20   k8s-worker-node1
192.168.56.30   k8s-master1 

Restart the VM

5. Now you can clone the VM as explain in previous blog post and repeat Steps 3 & 4 accordingly


6. Copy the ssh key to virtual machine


ssh-copy-id -i ~/.ssh/id_rsa root@192.168.56.20
ssh-copy-id -i ~/.ssh/id_rsa root@192.168.56.10
ssh-copy-id -i ~/.ssh/id_rsa root@192.168.56.30


6. Clone this github project and go to that directory



7. In here I am using Python pip virtual environment. My host machine is Ubuntu, so following commands will setup virtual environment.


sudo apt install python-pip

python -m pip install --upgrade pip setuptools wheel

sudo apt-get install python3-venv

python3 -m venv k8s_env

source k8s_env/bin/activate





8. Install Ansible 2.8.7


pip install 'ansible==2.8.7'


9. Install Docker and Setup Kubernetes : Run bellow command


ansible-playbook -i inventory/virthost/virthost.inventory playbooks/kube-install.yml -vvvvv


10. Uninstall Kubernetes


ansible-playbook -i inventory/virthost/virthost.inventory playbooks/kube-teardown.yml  -vvv







12/06/2019

Working with Ansible and Docker




As shown in the diagram, we are trying to provision, docker container in remote host using a intermediate docker container (Docker-Ansible) which has been installed Ansible.

So Ansible playbook will run inside the docker container which is in local host.

By this way you don't have to install Ansible in local machine.

Also we can execute multiple playbook at the same time.

Please note, for this demonstration I am using single machine. You can use two virtual machines instead of single machine with different IP address.


Steps.


1. Install Open-SSH server in Ubuntu.

Please follow below article for this.

https://www.cyberciti.biz/faq/ubuntu-linux-install-openssh-server/

2. Setup SSH keys in Ubuntu.

Please follow below article for this

https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1604

https://www.ssh.com/ssh/copy-id

3. Install Docker

https://dhanuka84.blogspot.com/2019/02/install-docker-in-ubuntu.html

4. Install Python

https://tecadmin.net/install-python-2-7-on-ubuntu-and-linuxmint/

5. Clone below two github projects

git clone https://github.com/dhanuka84/docker-ansible.git
git clone https://github.com/dhanuka84/docker-ansible-playbook.git


6. Build each project

cd   docker-ansible/master-ubuntu16.04
docker-ansible/master-ubuntu16.04$   docker build -t ansible-docker:master-ubuntu16.04 .
cd   docker-ansible-playbook
docker-ansible-playbook$   docker build -t docker-ansible-intermediate:latest .

7. Provisioning the container using intermediate container

cd   docker-ansible-playbook
docker-ansible-playbook$  ./start.sh















Explanation


1. Docker image ansible-docker:master-ubuntu16.04


If you look at the Dockerfile, you can see it has setup up all the things mentioned in the diagram as Docker-Ansible. This will be the generic Ubuntu based image.


2. docker-ansible-intermediate:latest



We use previous image as parent image in here. Mainly what we do with this image is setup SSH keys and hosts

Though define ssh-keyscan 192.168.0.114  > /root/.ssh/known_hosts in the image level is not the best approach for this just for demo purpose we did like this.

Best approach is do the same in container level  (start.sh) instead of image level.



3. docker-ansible-playbook$ vim docker.yml  playbook


---
- hosts: docker
#  gather_facts: no
  tasks:
  - name: Create container
    docker_container:
     name: docker-test
    # docker_host: "tcp://localhost:22"
    # This is a pre-built ubuntu based image. Also it has been installed python-pip as explain in the #diagram.
   #We use this image as provisioned container
     image: nitincypher/docker-ubuntu-python-pip:latest
    # image: ansible:ubuntu16.04
     command: sleep 1d
     detach: true
     interactive: true
     tty: true
    # tls_hostname: localhost
     tls_verify: yes



References:


https://developer.ibm.com/tutorials/cl-provision-docker-containers-ansible/