Setting up a local cloud environment

Tags
Ansible
Published
2019-01-19
Author
Man Parvesh Singh Randhawa

The plot

I have recently been reading a lot about kubernetes and how it has changed the way we deploy our applications. Kubernetes1 helps developers automate deployment, scale and manage their containerized applications.
While it is now becoming easier than ever to try k8s (short for kubernetes) using several managed platforms like Google’s GKE2, Azure, Amazon’s EKS3, and many more, I thought that there must be a way to try these things in a local development environment as well. From this thought, I came across solutions like minikube, which could create a new k8s cluster in your machine using virtualbox in the background.
Such solutions made testing on local easy, but I decided to go into some more depth by trying and setting up everything myself once, so that it would help me understand how it is setup, what is important, and all that.
I found ansible4 to be a great tool for this. Next I’ll describe how I proceeded with using ansible to setup the environment.

The Story

Setting up a virtualbox instance for ssh

  1. First, create a new linux virtual machine using virtualbox. In my case, I used Ubuntu 18.04 server5 image.
  1. Now, we need to enable ssh for this machine.
  1. For this you need to have openssh-server installed on your host system.
  1. The best way I found for this is port forwarding6.
    1. Through the GUI:
        • Go to the Network settings and click the Port Forwarding button.
        • Add a new Rule.
        • As the rule name, insert “ssh”. As “Host port”, insert 3022.
        • As “Guest port”, insert 22.
        • Everything else of the rule can be left blank.
    2. Through the command line:
        • If ‘myserver’ is the name of the created VM:
          • VBoxManage modifyvm myserver --natpf1 "ssh,tcp,,3022,,22"
        • Check the added rules:
          • VBoxManage showvminfo myserver | grep 'Rule'
  1. Now, to ssh into the system, write:
ssh -p 3022 user@127.0.0.1

Enable ssh using private key pair

  1. Until now, you were able to login to the virtual machine using ssh but it always asked for a password to the machine. Now, we aim to create a private key so that we can ssh into the machine without having to enter the password.
  1. For this, we create an ssh key in the host machine and export to virtual machine using ssh-copy-id7

Setting up ansible and accessing the machine

  • Install ansible8 on your host machine.
  • Create server.cfg. This contains the ansible configuration of the server we want to connect to, and how. An example configuration file can be found below:
[virtualmachine]virtualmachine-ubuntu[virtualmachine:vars]remote_port = 3022ansible_ssh_use = manparveshansible_ssh_common_args='-F /home/manparvesh/.ssh/ubuntu_virtualbox/ssh1.cfg'
  • Create ssh1.cfg. This contains the ssh config for our server
Host virtualmachine-ubuntu HostName 127.0.0.1 User manparvesh Port 3022 IdentityFile /home/manparvesh/.ssh/ubuntu_virtualbox/key
  • Run the following command to ping our server:
ansible -i ./ansible-setup/server.cfg all -m ping

Installing a sample program on our server

Now let’s install nginx on our server using ansible.
  • Create a new file nginx.yml with the following contents:
---- hosts: virtualmachine-ubuntu tasks: - name: Install Nginx apt: pkg=nginx state=present update_cache=true
  • Execute the following command to install nginx on our server:
ansible-playbook -i ./ansible-setup/server.cfg -s nginx.yml

Conclusion

Now, we have an almost complete setup for a local cloud. We can use it to perform different experiments with various tools available online.
In the future posts, I will write about my progress in installing a complete kubernetes cluster using virtualbox on my local and ansible. Stay tuned! :wave:

  1. https://kubernetes.io/
  1. https://cloud.google.com/kubernetes-engine/
  1. https://aws.amazon.com/eks/
  1. https://www.ansible.com/
  1. http://releases.ubuntu.com/18.04/
  1. https://stackoverflow.com/a/10532299
  1. https://www.ssh.com/ssh/copy-id
  1. https://www.ansible.com/