Part 1: Using Port-Forward
This is Part 1 of a multi-part, self-paced quick start exercise.
Note
This exercise requires a Rafay managed cluster. We will be using Rafay's Zero Trust KubeCTL Access.
What Will You Do¶
In part 1, you will:
- Download Kubeconfig
- Deploy a Nginx Web Server to our K8s cluster
- Verify we can access the web page from our localhost
Estimated Time
Estimated time for this exercise is 5 minutes.
Using Port Forward¶
Kubectl port-forward is a tool that allows users to directly access pods without having to be exposed. There are times when application owners need to test or debug an application using their local environment. Port-forward allows you to create the network connection from your localhost to the pod's exposed service through the cluster's API server. This is very useful if hooking up a tool like jconsole and monitoring application metrics exposed with JMX.
Port Forward YAML file¶
Create a deployment using a YAML file, which is a configuration file. You could create a YAML file from the command line, but for this exercise, you can just use a text editor. Or you can download the service YAML file from this public Git repository
- Open the Terminal.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
touch port-forward.yaml
- Use the nano text editor in the Terminal.
nano port-forward.yaml
- Copy and paste the configuration below into the text editor.
apiVersion: apps/v1 kind: Deployment metadata: labels: app: web name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - image: nginx imagePullPolicy: Always name: nginx restartPolicy: Always
- Press Cmd + X, then type Y and press Return to save the port-forward.yaml file.
- Open the command prompt.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
copy NUL port-forward.yaml
- Open the port-forward.yaml file with a text editor. For example, use Notepad++ to edit the YAML file.
-
Copy and paste the configuration below into the text editor.
apiVersion: apps/v1 kind: Deployment metadata: labels: app: web name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - image: nginx imagePullPolicy: Always name: nginx restartPolicy: Always
-
Save the port-forward.yaml file.
- Open the Terminal.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
touch port-forward.yaml
- Use the nano text editor in the Terminal.
nano port-forward.yaml
-
Copy and paste the configuration below into the text editor.
apiVersion: apps/v1 kind: Deployment metadata: labels: app: web name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - image: nginx imagePullPolicy: Always name: nginx restartPolicy: Always
-
Press Ctrl + X, then type Y and press Enter to save the port-forward.yaml file.
Add the Deployment¶
- Download the cluster's kubeconfig from the Rafay console.
- Set your KUBECONFIG environment variable to your Rafay downloaded kubeconfig.
$ export KUBECONFIG=/my-kubeconfig.yaml
- In the Terminal or Command Prompt, add the Deployment to your environment using a YAML file.
$ kubectl apply -f port-forward.yaml deployment.apps/nginx-deployment created
- List the pods.
$ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-76b56fd968-6q795 1/1 Running 0 35s nginx-deployment-76b56fd968-7k8t8 1/1 Running 0 35s nginx-deployment-76b56fd968-ntkvg 1/1 Running 0 35s
- Create a port-forward, we will be mapping our local 8080 to port 80 on the pod
$ kubectl port-forward nginx-deployment-76b56fd968-6q795 8080:80 Forwarding from 127.0.0.1:8080 -> 80
- Open a new terminal
- Test that the web page is available using the localhost port we setup. Example: curl http://127.0.0.1:8080. A Welcome to nginx! page should appear.
$ curl http://127.0.0.1:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>