![]() Let us create the deployment and the service: ~]# kubectl create -f nginx-deploy.yamlĭeployment.apps/nginx-deploy ~]# kubectl create -f nginx-service.yaml Here I have created a deployment using the following YAML file:Īnd we will also create a service to expose the deployment pods: There are different Service Types available but to perform port forwarding we will use NodePort. Kubernetes services can be used to expose pods to external network. Perform port-forwarding on Kubernetes Deployment Pods So we can use this port to access the nginx container: ~]# curl 127.0.0.1:40159 For example, here I have not specified any local port so kubectl has randomly selected port 40159 for the forwarding. If you don't specify a local port then kubectl will randomly select a port for forwarding. Method-3: Listen on a random port locally, forwarding to 80 in the pod Now we can access our nginx container on external network (here 192.168.0.150 is the IP of my controller node):ĪLSO READ: Steps to Install and configure Controller Node in OpenStack – Part 2 Since we are trying to access a new port, so we will need to enable this port in the firewall if we want to access this on external network: ~]# firewall-cmd -add-port 8080/tcp ~]# firewall-cmd -reload So you can use any IP address from the controller node with port 8080 to access the nginx container. In this method we will perform port forwarding to all addresses on the controller node from port 8080 to port 80 in the pod. Method-2: Listen on port 8080 on all addresses, forwarding to 80 in the pod Once done you can either kill the PID of the port-forward command or press ctrl+c on the terminal where kubectl port-forward is running. Now we attempt to access our nginx server using a different terminal: This is forwarding any and all traffic that gets created on your local machine at TCP port 8080 to TCP port 80 on the Pod nginx: ~]# kubectl port-forward pod/nginx 8080:80 In this method we will forward the traffic to port 8080 on localhost (on controller) to port 80 on worker-2 based nginx container. But is this IP reachable from the controller node? We are using weave-net CNI and with this network plugin by default the internal network of worker nodes are not accessible by the controller node so we can't connect to the container directly.Īccess nginx server with port forwarding Method-1: Listen on port 8080 locally, forwarding to port 80 in the pod Now we know that our nginx container is running on worker-2 with an IP address of 10.44.0.1. Nginx 1/1 Running 0 3m 10.44.0.1 Īccess nginx server without port forwarding NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES To get more details such IP address of the pod,worker node on which the respective pod is running etc, we will combine the above command with -o wide: ~]# kubectl get pods -o wide List the available pods: ~]# kubectl get pods Use kubectl create command to create the pod using the provided YAML file. Here I will create a simple pod with nginx server running on port 80: Once your work is done, you can go ahead and kill the PID of the background process to close the tunnel. This will print the PID of the process and then send the command to background and you will be able to use the terminal. Kubectl port-forward pods/my-pod 8080:80 & If we cancel or quit the kubectl command, typically by pressing Ctrl + C, then port forwarding will immediately end. Note that you will not have a Command Prompt back after executing these commands, which is because the command is actively running to keep this particular tunnel we've requested alive. ![]() Sample command to perform port forwarding on service: kubectl port-forward service/my-service 8080:80 Sample command to perform port forwarding on replicaset: kubectl port-forward replicaset/my-replicaset 8080:80 Sample command to perform port forwarding on deployment: kubectl port-forward deployment/my-deployment 8080:80 ![]() ![]() Sample command to perform port forwarding on pod: kubectl port-forward pods/my-pod 8080:80 The general syntax to forward ports using kubectl would be as shown below but we will explore all possible options: kubectl port-forward : This command is very useful mostly when you would want to troubleshoot a misbehaving pod. We use this command to access container content by forwarding one (or more) local ports to a pod. That means the system running it needs access to the API server, and any traffic will get tunneled over a single HTTP connection. ![]() kubectl port-forward makes a specific Kubernetes API request. This can be performed using kubectl port-forward command. We can use kubectl to set up a proxy that will forward all traffic from a local port we specify to a port associated with the Pod we determine. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |