简体   繁体   English

kubernetes pod 端口暴露/转发

[英]kubernetes pod port expose/forward

I'm trying to expose a port 8080 on a pod, so I can wget directly from server.我正在尝试在 pod 上公开端口 8080,因此我可以直接从服务器获取 wget。 With port-forward everything works fine ( kubectl --namespace jenkins port-forward pods/jenkins-6f8b486759-6vwkj 9000:8080 ), I'm able to connect to 127.0.0.1:9000使用端口转发一切正常( kubectl --namespace jenkins port-forward pods/jenkins-6f8b486759-6vwkj 9000:8080 ),我能够连接到 127.0.0.1:9000

But when I try to avoid port-forward and open ports permanently ( kubectl expose deployment jenkins --type=LoadBalancer -njenkins ): I see it in svc ( kubectl describe svc jenkins -njenkins ):但是当我尝试避免端口转发并永久打开端口时( kubectl expose deployment jenkins --type=LoadBalancer -njenkins ):我在 svc 中看到它( kubectl describe svc jenkins -njenkins ):

Name:                     jenkins
Namespace:                jenkins
Labels:                   <none>
Annotations:              <none>
Selector:                 app=jenkins
Type:                     LoadBalancer
IP Families:              <none>
IP:                       10.111.244.192
IPs:                      10.111.244.192
Port:                     port-1  8080/TCP
TargetPort:               8080/TCP
NodePort:                 port-1  31461/TCP
Endpoints:                172.17.0.2:8080
Port:                     port-2  50000/TCP
TargetPort:               50000/TCP
NodePort:                 port-2  30578/TCP
Endpoints:                172.17.0.2:50000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

but port is still not up, netstat does not show anything.但端口仍未启动,netstat 没有显示任何内容。 How it should be done correctly?应该如何正确完成?

Using minikube version: v1.20.0, pod yaml just in case:使用 minikube 版本:v1.20.0,pod yaml 以防万一:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      securityContext:

      containers:
      - name: jenkins
        image: jenkins/jenkins:lts

        ports:
          - name: http-port
            containerPort: 8080
            hostPort: 8080
          - name: jnlp-port
            containerPort: 50000
        volumeMounts:
          - name: task-pv-storage
            mountPath: /var/jenkins_home
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: task-pv-claim

What is your environment?你的环境是什么? Are you running your local k8s cluster with docker desktop/minikube/kubeadm?您是否使用 docker desktop/minikube/kubeadm 运行本地 k8s 集群?

Check that your Pods have external IPs with kubectl get pods -o=wide使用kubectl get pods -o=wide检查您的 Pod 是否具有外部 IP

Load Balancing is not supposed to be implemented on your single node machine (with Minikube), there is a somehow a "hack"负载平衡不应该在您的单节点机器(使用 Minikube)上实现,存在某种“hack”

If you are deploying your cluster on a Cloud provider, Load Balancer would be fully-managed如果您在云提供商上部署集群,负载均衡器将是完全托管的

For the "hack" i talk about, look at this tutorial video section on Ingress component explained: https://youtu.be/X48VuDVv0do?t=7312对于我所说的“hack”,请查看本教程视频部分关于 Ingress 组件的解释: https://youtu.be/X48VuDVv0do?t=7312

You are expected to place a Pod with a nginx server in front of your ingress, in front of your loadbalancer, in front of your deployment Pods您应该在入口前面、负载均衡器前面、部署 Pod 前面放置一个带有 nginx 服务器的 Pod

I see that you are running your k8s cluster locally, in this case, LoadBalancer ServiceType is not recommended as this type uses cloud providers' load balancer to expose services externally.我看到您在本地运行您的 k8s 集群,在这种情况下,不建议使用 LoadBalancer ServiceType,因为此类型使用云提供商的负载均衡器将服务暴露在外部。 You might use self-hosted or hardware load balancer but I suppose it's a bit an overkill for minikube cluster.您可能会使用自托管或硬件负载均衡器,但我认为这对于 minikube 集群来说有点过分了。

In your minikube deployment, I'd suggest using NodePort Service Type as it uses IP address of your node to expose service.在您的 minikube 部署中,我建议使用 NodePort 服务类型,因为它使用节点的 IP 地址来公开服务。 Example yaml:示例 yaml:

apiVersion: v1
kind: Service
metadata:
  name: jenkins-service
spec:
  type: NodePort
  selector:
    app: jenkins
  ports:
    - port: 8080
      targetPort: 8080
      # nodePort field is optional, Kubernetes will allocate port from a range 30000-32767, but you can choose 
      nodePort: 30007
    - port: 50000
      targetPort: 50000     
      nodePort: 30008
  

Then, you can access your app on <NodeIP>:<nodePort> .然后,您可以在<NodeIP>:<nodePort>上访问您的应用程序。 If you want to read more about k8s Services go here .如果您想在此处阅读有关 k8s 服务 go 的更多信息。

You exposed the application with a service on port 8080, but that port is not known outside of kubernetes, same as the ip address of the service or the pod.您使用端口 8080 上的服务公开了应用程序,但该端口在 kubernetes 之外未知,与服务或 pod 的 ip 地址相同。

The service opened a NodePort that is pointing at the deployments port:该服务打开了一个指向部署端口的NodePort

[...]
NodePort:                 port-1  31461/TCP
[...]

Using curl to that ip:port destination should work:使用 curl 到 ip:port 目标应该工作:

curl <cluster-node>:31461

The cluster node ip depends on how you have set up minikube.集群节点 ip 取决于您如何设置 minikube。

The issue was with a minikube itself - found it while checking kubectl get events --all-namespaces , some strange things were happening, and looks like the internal proxy component was damaged.问题出在 minikube 本身 - 在检查kubectl get events --all-namespaces时发现它,发生了一些奇怪的事情,看起来内部代理组件已损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM