[TOC]

基础示例

####

2.使用port-forward访问集群中的应用程序

描述:在实际进行Debug时使用 kubectl port-forward 访问 Kubernetes 集群中的 Redis Server进行调试;

Step1.分别为Redis创建Deployment和Service
Deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cat > redis-master-deployment.yaml<<'END'
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-master-deployment
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
END


Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cat > redis-master-service.yaml<<'END'
apiVersion: v1
kind: Service
metadata:
name: redis-master-service
labels:
app: redis
role: master
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: master
tier: backend
END


Step2.执行apply命令以创建 Redis Deployment与Service:

1
2
3
4
5
kubectl apply -f redis-master-deployment.yaml
# deployment.apps/redis-master-deployment created

kubectl apply -f redis-master-service.yaml
# service/redis-master-service created


Step3.分别查看deployment与service部署情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
kubectl get pods
# NAME READY STATUS RESTARTS AGE
# redis-master-deployment-7d557b94bb-bzw2v 1/1 Running 0 3m2s

#查看 Deployment状态
kubectl get deployment
# NAME READY UP-TO-DATE AVAILABLE AGE
# redis-master-deployment 1/1 1 1 3m49s

# 查看 ReplicaSet(副本) 状态
kubectl get rs
# NAME DESIRED CURRENT READY AGE
# redis-master-deployment-7d557b94bb 1 1 1 4m46s

# 检查 Service 创建结果
kubectl get svc -o wide | grep redis
# redis-master-service ClusterIP 10.99.192.159 <none> 6379/TCP 9m27s app=redis,role=master,tier=backend

# 验证 Redis Service已经运行并监听了 6379 端口
kubectl get pods redis-master-deployment-7d557b94bb-bzw2v --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
# 6379

Step4.使用kubectl port-forward 命令转发本地端口到Pod的端口,用户可以使用资源的名称来进行端口转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#(1)下面的命令中的任意一行,都可以实现端口转发的效果:
kubectl port-forward redis-master-deployment-7d557b94bb-bzw2v 7000:6379
kubectl port-forward pods/redis-master-deployment-7d557b94bb-bzw2v 7000:6379 # kubectl get pods 获取资源名称
kubectl port-forward deployment/redis-master-deployment 7000:6379 # kubectl get deployments 获取资源名称
kubectl port-forward svc/redis-master-service 7000:6379 # kubectl get svc 获取资源名称
kubectl port-forward rs/redis-master-deployment-7d557b94bb 7000:6379 # kubectl get rs 获取资源名称

#(2)以上命令的输出结果类似:
[[email protected] ~]$ kubectl port-forward --address 127.0.0.1,10.10.107.191 redis-master-deployment-7d557b94bb-bzw2v 7000:6379
# Forwarding from 127.0.0.1:7000 -> 6379
# Handling connection for 7000 (连接到此端口反应)
redis-cli -h 10.10.107.191 -p 7000
# 10.10.107.191:7000> ping
# PONG

Step5.总结本机 7000 端口的连接被转发到集群中 Redis Server 所在 Pod 的 6379 端口。利用该命令可以方便开发或者运维人员进行Debug调试;

注意事项:

  • 1) 由于已知的限制,目前的端口转发仅适用于 TCP 协议。 在 issue 47862 中正在跟踪对 UDP 协议的支持。

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: alpine-app
spec:
containers:

  • name: alpine-app
    image: alpine
    args:

    • sleep
    • “100000”
      EOF
      pod/alpine-app created

    k get pods -o wide
    NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    alpine-app 1/1 Running 0 116s 10.100.37.194 worker-02