Tasks

Step-by-step instructions for performing operations with Kubernetes.

Documentation for Kubernetes v1.8 is no longer actively maintained. The version you are currently viewing is a static snapshot. For up-to-date documentation, see the latest version.

Tasks
Administer a Cluster
Access Clusters Using the Kubernetes API
Access Services Running on Clusters
Securing a Cluster
Encrypting data at rest
Operating etcd clusters for Kubernetes
Static Pods
Cluster Management
Cluster Management Guide for Version 1.6
Upgrading kubeadm clusters from 1.6 to 1.7
Upgrading kubeadm clusters from 1.7 to 1.8
Share a Cluster with Namespaces
Namespaces Walkthrough
Autoscale the DNS Service in a Cluster
Safely Drain a Node while Respecting Application SLOs
Configure Out Of Resource Handling
Reserve Compute Resources for System Daemons
Guaranteed Scheduling For Critical Add-On Pods
Declare Network Policy
Reconfigure a Node's Kubelet in a Live Cluster
Set Kubelet parameters via a config file
Change the Reclaim Policy of a PersistentVolume
Limit Storage Consumption
Change the default StorageClass
Kubernetes Cloud Controller Manager
Developing Cloud Controller Manager
Set up High-Availability Kubernetes Masters
Configure Multiple Schedulers
IP Masquerade Agent User Guide
Configure private DNS zones and upstream nameservers in Kubernetes
Manage GPUs
Manage HugePages
Extend kubectl with plugins

Edit This Page

Use Port Forwarding to Access Applications in a Cluster

This page shows how to use kubectl port-forward to connect to a Redis server running in a Kubernetes cluster. This type of connection can be useful for database debugging.

Before you begin

To check the version, enter kubectl version.

Creating a pod to run a Redis server

  1. Create a pod:

    kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/redis-master.yaml
    

    The output of a successful command verifies that the pod was created:

     pod "redis-master" created
    
  2. Check to see whether the pod is running and ready:

    kubectl get pods
    

    When the pod is ready, the output displays a STATUS of Running:

     NAME           READY     STATUS    RESTARTS   AGE
     redis-master   2/2       Running   0          41s
    
  3. Verify that the Redis server is running in the pod and listening on port 6379:

    kubectl get pods redis-master --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
    

    The output displays the port:

     6379
    

Forward a local port to a port on the pod

  1. Forward port 6379 on the local workstation to port 6379 of redis-master pod:

    kubectl port-forward redis-master 6379:6379
    

    The output is similar to this:

     I0710 14:43:38.274550    3655 portforward.go:225] Forwarding from 127.0.0.1:6379 -> 6379
     I0710 14:43:38.274797    3655 portforward.go:225] Forwarding from [::1]:6379 -> 6379
    
  2. Start the Redis command line interface:

    redis-cli
    
  3. At the Redis command line prompt, enter the ping command:

    127.0.0.1:6379>ping
    

    A successful ping request returns PONG.

Discussion

Connections made to local port 6379 are forwarded to port 6379 of the pod that is running the Redis server. With this connection in place you can use your local workstation to debug the database that is running in the pod.

What’s next

Learn more about kubectl port-forward.

Analytics

Create an Issue Edit this Page