我已使用以下 YAML 配置实例化了 pod 和服务:
apiVersion: v1
kind: Pod
metadata:
name: jupyter-pod
labels:
app: jupyter-pod
spec:
serviceAccountName: spark-driver
containers:
- name: jupyter-pod
image: spark-k8s-jupyter:v3.5.0
imagePullPolicy: Never
command: ["jupyter", "lab", "--ip", "0.0.0.0", "--port", "9000"]
tty: true
stdin: true
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: jupyter-pod
spec:
type: NodePort
selector:
app: jupyter-pod
ports:
- protocol: TCP
port: 9000 # The port that the service will serve on.
targetPort: 9000 # The target port on the pod(s) to forward to.
当我使用
kubectl port-forward pod/jupyter-pod 9000:9000
时,我可以在浏览器中访问该页面,但不仅仅是通过该服务访问,这可能是什么原因造成的?我已经尝试了 pod 的 IP 和 localhost 以及多个端口,但没有成功。
我运行一些命令试图找出问题所在:
kubectl get endpoints -A
NAMESPACE NAME ENDPOINTS AGE
default jupyter-service 10.244.0.147:9000 4m30s
k describe service jupyter-service
Name: jupyter-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=jupyter-pod
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.106.188.89
IPs: 10.106.188.89
Port: <unset> 9000/TCP
TargetPort: 9000/TCP
NodePort: <unset> 31175/TCP
Endpoints: 10.244.0.147:9000
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
k get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
jupyter-pod NodePort 10.103.70.121 <none> 9000:30192/TCP 9s
编辑:
我通过指定
hostNetwork: true
解决了这个问题,但我不认为这是最佳实践,所以如果有人可以在不使用主机网络的情况下解决这个问题,我仍然会很感激。
我忘记在规格中设置nodePort,这里是工作版本:
apiVersion: v1
kind: Service
metadata:
labels:
app: jupyter-pod
name: jupyter-pod-svc
namespace: default
spec:
ports:
- port: 9000
protocol: TCP
targetPort: 9000
nodePort: 31200
selector:
app: jupyter-pod
type: NodePort