This Lab deploys a sinatra application to a K8S cluster in the cloud.
- Running the Sinatra App locally
- Running the Sinatra App in a local container
- Setting up Kubectl to connect to the cluster
- Deploy the Sinatra App image to a Kubernetes Cluster
Before you start, make sure you are using ruby version 3.1. You can check the version by running ruby -v
.
-
Clone this repository:
git clone git@github.com:HeitorMC/HandsOn-k8s.git
-
Change into the application directory:
cd HandsOn-k8s
-
install the dependencies:
bundle exec ruby app.rb
-
Run the app
bundle exec ruby app.rb
-
The web application will be availabe at http://localhost:4567/
-
Clone this repository:
git clone git@github.com:HeitorMC/HandsOn-k8s.git
-
Change into the application directory:
cd HandsOn-k8s
-
Build the app’s container image
docker build -t sinatra-app .
-
Run the container
docker run -p 4567:4567 sinatra-app
-
The web application will be availabe at http://localhost:4567/
-
Download the latest release of kubectl with the command:
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
-
Install kubectl using the following command:
chmod +x ./kubectl sudo mv ./kubectl /usr/local/bin/kubectl
-
Verify that kubectl is installed correctly by running the following command:
kubectl version --client
-
Import your kubeconfig file and after that you can connect to the cluster using the following command:
export KUBECONFIG=kubeconfig.yaml
-
Assuming your image is already available in the docker hub, you can run the following command to deploy the image to the cluster:
kubectl run <pod-name> <image-name:tag> --port=4567
-
Run the following command to see the pods running in the cluster:
kubectl get pods
The pods will be running in the cluster. However, there is an better way to deploy the image to the cluster pod.
-
We will use a K8S deployment to deploy the image to the cluster. Create a deployment file, you can use
k8s-deployment.yaml.example
as an example or check this link and build your own deployment file.
After creating the deployment file, run:
kubectl apply -f <your-deployment-file.yaml>
Now run the following command to see the pods running in the cluster:
kubectl get pods
For a more detailed view, run the following command:
kubectl get pods -o wide
- Now we'll set a K8S Service to expose the pods. Create a service file, you can use
k8s-service.yaml.example
as an example or check this link and build your own service file.
After creating the service file, run:
kubectl apply -f <your-service-file.yaml>
And run the following command to see the service running in the cluster:
kubectl describe services
Finally, copy the external IP and paste it in your browser, you'll see the web application running in the cluster. 🎉🎉🎉