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.
ansible4 to be a great tool for this. Next I'll describe how I proceeded with using ansible to setup the environment.
Setting up a virtualbox instance for ssh
- First, create a new linux virtual machine using
virtualbox. In my case, I used
Ubuntu 18.04 server5 image.
- Now, we need to enable
sshfor this machine.
- For this you need to have
openssh-serverinstalled on your host system.
- The best way I found for this is
- 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.
- 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'
- Through the GUI:
- Now, to ssh into the system, write:
ssh -p 3022 [email protected]
Enable ssh using private key pair
- 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.
- 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
ansible4 on your host machine.
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 = 3022 ansible_ssh_use = manparvesh ansible_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.ymlwith the following contents:
--- - hosts: virtualmachine-ubuntu tasks: - name: Install Nginx apt: pkg=nginx state=present update_cache=true
- Execute the following command to install
nginxon our server:
ansible-playbook -i ./ansible-setup/server.cfg -s nginx.yml
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! 👋