For this project, I provisioned a Kubernetes cluster in my VM using minikube
- VirtualBox
- Minikube
- Kubectl
$ minikube start --vm-driver=virtualboxYou could ssh into the node using ssh docker@$(minikube ip)
Take note the default minikube credentials are:
- username: docker
- password: tcuser
First copy the ./k8s-web-to-nginx folder and paste in your working directory
- The index.mjs file contains a simple web server and 2 endpoints. The "/" enpoint responds with a greeting from the pod, and the "/nginx" endpoint displays the nginx homepage \
- The Dockerfile contains instructions needed to build the Docker image for our small application
Next, cd into the ./k8s-web-to-nginx folder and run the following command to build the docker image.
$ docker build . -t 4568910/k8s-web-to-nginxThe
-tflag allows us specify a tag name for this image. I used my dockerhub repo in the format {username}/{repo_name}
Next, goto hub.docker.com) and create a repo for our image k8s-web-to-nginx
The next step will be to push the image to your docker repo using:
$ docker push {docker_username}/k8s-web-to-nginx
In my case, that would be:
$ docker push 4568910/k8s-web-hello
In order to create the deployment and services, we will make use of the yaml file found at ./k8s-web-to-nginx.yaml
In this K8s manifest, the service part contains...
apiVersion: v1
kind: Service
metadata:
name: k8s-web-to-nginx
spec:
type: LoadBalancer
selector:
app: k8s-web-to-nginx
ports:
- port: 3333
targetPort: 3000In this service part of the manifest:
metadatacontains the "name" of this servicek8s-web-to-nginxspecdescribes the pod in more details
typeis LoadBalancer
selectorstates keys and values that are to be matched by pods which intend to receive traffic from this service
portstates a host port to map to thetargetPort(ports from the Express server container)
And the Deployment part contains:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-web-to-nginx
spec:
replicas: 4
selector:
matchLabels:
app: k8s-web-to-nginx
template:
metadata:
labels:
app: k8s-web-to-nginx
spec:
containers:
- name: k8s-web-to-nginx
image: 4568910/k8s-web-to-nginx
resources:
limits:
memory: "128Mi"
cpu: "250m"
ports:
- containerPort: 3000In the deployment manifest:
metadataholds thenameof htis deploymentspecgives more details about the deployment
replicasis the number of desired pods
selectoris a label selector for pods. Existing ReplicaSets whose pods are selected by this will be the ones affected by this deployment. It must match the pod template's labels.
templatedescribes the pods that will be created.
metadatacan be used to organize and categorize (scope and select) objects. May match selectors of replication controllers (deployments) and services.
specSpecification of the desired behavior of the pod.
containersdescribes resources for a list of containers belonging to the pod
The manifests for nginx are similar (but the service type is Cluster IP). Find and download it from nginx.yaml
To apply the manifests, run:
$ kubectl apply -f k8s-web-to-nginx.yaml -f nginx.yamlIn order to view our resources, run:
# to see our pods
$ kubectl get pods
# to see our service
$ kubectl get svc
# to see our deployments
$ kubectl get deployTo visit the pages being served by our web server, if you're using minikube, run:
$ minikube service k8s-web-to-nginxIt will open a page in your web browser with a greeting from our pod (showing that our "/" endpoint was hit).
To go to our nginx endpoint, simply add "/nginx" in front of the address on the browser page. You should be redirected to the Nginx home page.
If this is what you get, it means everything works fine.
Here's a diagram showing the communication between both deployments:
To remove deployments and services, run:
$ kubectl delete -f k8s-web-to-nginx.yaml -f nginx.yamlAnd to stop minikube node, run:
$ minikube stop