Use port forward to access the applications in the cluster, taking Redis as an example

Create Deployment and Service for Redis

  • Create Redis Deployment. YAML file is as follows:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis-master
      labels:
        app: redis
    spec:
      selector:
        matchLabels:
          app: redis
          role: master
          tier: backend
      replicas: 1
      template:
        metadata:
          labels:
            app: redis
            role: master
            tier: backend
        spec:
          containers:
          - name: master
            image: redis
            resources:
              requests:
                cpu: 100m
                memory: 100Mi
            ports:
            - containerPort: 6379
    

Execute command to create Redis Deployment

kubectl apply -f https://kuboard.cn/statics/learning/ptc/port-forward/redis-master-deployment.yaml

Execute the command to view the Pod status:

kubectl get pods

The output results are as follows:

NAME                            READY     STATUS    RESTARTS   AGE
redis-master-765d459796-258hz   1/1       Running   0          50s

Execute the command to view the Deployment status:

kubectl get deployment    

The output is as follows

NAME         READY   UP-TO-DATE   AVAILABLE   AGE
redis-master 1/1     1            1           55s    

Execute the command to view the ReplicaSet status

kubectl get rs

The output results are as follows:

NAME                      DESIRED   CURRENT   READY     AGE
redis-master-765d459796   1         1         1         1m    

Create Redis service. YAML file is as follows:

apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    app: redis
    role: master
    tier: backend
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
    role: master
    tier: backend

Execute command, create Service

kubectl apply -f https://kuboard.cn/statics/learning/ptc/port-forward/redis-master-service.yaml    

Execute the command to check the Service creation result

kubectl get svc | grep redis   

The output is as follows:

NAME           TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
redis-master   ClusterIP   10.100.0.213   <none>        6379/TCP   27s    

Verify that Redis Service is running and listen to port 6379

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

Please replace the redis-master-765d459796-258hz with your actual redis name output as follows:

6379    

# Forward local port to Pod port

  • With the kubectl port forward command, the user can use the name of the resource to forward the port. Any line in the following command can achieve the effect of port forwarding:

    # Just execute any of these commands
    kubectl port-forward redis-master-765d459796-258hz 7000:6379
    kubectl port-forward pods/redis-master-765d459796-258hz 7000:6379
    kubectl port-forward deployment/redis-master 7000:6379
    kubectl port-forward rs/redis-master 7000:6379
    kubectl port-forward svc/redis-master 7000:6379
    

The output of the above command is similar:

I0710 14:43:38.274550    3655 portforward.go:225] Forwarding from 127.0.0.1:7000 -> 6379
I0710 14:43:38.274797    3655 portforward.go:225] Forwarding from [::1]:7000 -> 6379    

Start Redis command line:

redis-cli -p 7000

At the Redis command line tool prompt, enter the ping command as follows:

127.0.0.1:7000>ping   
  • Redis server will return PONG

# summary

The connection of the native 7000 port is forwarded to port 6379 of the Pod where the Redis Server is located in the cluster. When this connection exists, you can use the client on your own machine to Debug the Redis Server deployed in the cluster.

Tips

  • Due to some limitations, port forward currently only supports TCP protocol, issue 47862

Used to follow up support for UDP protocol.

MySQL database and other servers deployed in K8S cluster using TCP protocol can use this method to DEBUG

Tags: Redis MySQL Database

Posted on Sat, 30 May 2020 02:08:17 -0400 by gsv2com