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
ansible
4 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
- First, create a new linux virtual machine using
virtualbox
. In my case, I usedUbuntu 18.04 server
5 image.
- Now, we need to enable
ssh
for this machine.
- For this you need to have
openssh-server
installed on your host system.
- The best way I found for this is
port forwarding
6. - 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:
- Check the added rules:
VBoxManage modifyvm myserver --natpf1 "ssh,tcp,,3022,,22"
VBoxManage showvminfo myserver | grep 'Rule'
- Now, to ssh into the system, write:
ssh -p 3022 user@127.0.0.1
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
- Install
ansible
8 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:
- https://kubernetes.io/↩
- https://cloud.google.com/kubernetes-engine/↩
- https://aws.amazon.com/eks/↩
- https://www.ansible.com/↩
- http://releases.ubuntu.com/18.04/↩
- https://stackoverflow.com/a/10532299↩
- https://www.ssh.com/ssh/copy-id↩
- https://www.ansible.com/↩