您如何通过使用端口8080的服务在OpenShift中部署一个应用程序?

发布于 2025-02-11 03:37:18 字数 978 浏览 2 评论 0原文

我很难理解Kubernetes网络,希望有人比文档更简单。

我的测试服务如下,包含我到目前为止有关端口和网络的所有信息。

它正在返回503。

# A Service can map any incoming port to a targetPort. 
# By default and for convenience, the `targetPort` 
# is set to the same value as the port field
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  ports:
    - name: HTTP
      # port is the stable port the Service exposes inside 
      # the cluster — other Pods in the cluster send traffic
      # to this port
      port: 5000 
      protocol: TCP
      
      # if you don’t specify a targetPort value, 
      # it defaults to the value specified in port

      # targetPort is the port that the container, 
      # within the Pod, listens on for incoming traffic.
      targetPort: 80 
      selector:
        app: my-app
route.yml - targetPort: 80
deployment.yml - containerPort: 80

I am having a hard time understanding Kubernetes networking and would like someone to explain it a little simpler than the documentation does.

My test service is below and contains all that I understand so far about ports and networking in Openshift/Kubernetes.
It is returning a 503 right now.

# A Service can map any incoming port to a targetPort. 
# By default and for convenience, the `targetPort` 
# is set to the same value as the port field
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  ports:
    - name: HTTP
      # port is the stable port the Service exposes inside 
      # the cluster — other Pods in the cluster send traffic
      # to this port
      port: 5000 
      protocol: TCP
      
      # if you don’t specify a targetPort value, 
      # it defaults to the value specified in port

      # targetPort is the port that the container, 
      # within the Pod, listens on for incoming traffic.
      targetPort: 80 
      selector:
        app: my-app
route.yml - targetPort: 80
deployment.yml - containerPort: 80

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2025-02-18 03:37:18

您可以在我的github帐户下读取微小的代码段,以熟悉Nutshell中的K8S网络: https://github.com/nirgeier/kuberneteslabs/tree/master/master/labs/05-services

实验室将详细描述您要问的内容。

  • 您还可以打印出信息(我已经知道我已经知道)
kubectl explain pod.spec.containers.ports

来回答您的问题:

  • “查找”正在发生的事情的最简单方法是使用CLI创建资源并检查输出
# Create ns
kubectl create ns codewizard

# Create the deployment
kubectl create deployment \
        nginx             \
        -n codewizard     \
        --image=nginx

# Create the service
kubectl expose deployment \
        nginx             \
        -n codewizard     \
        --port 5000       \
        --target-port 80  \
        --type ClusterIP  # default

# Check the service
kubectl get svc -n codewizard -o wide

# check to verify that the pod is connected
kubectl get      endpoints -n codewizard
kubectl describe endpoints -n codewizard


我们

# Get the service yaml 
kubectl get svc nginx -n codewizard -o yaml
### Partial...
apiVersion: v1
kind: Service
...
spec:
  ports:
  - port: 5000
    protocol: TCP
    targetPort: 80

尝试测试我们的网络

# Create the 2nd deployment for our test
kubectl create deployment \
        multitool         \
        -n codewizard     \
        --image=praqma/network-multitool

# Lets get the pod name so we can test the connection to nginx
POD_NAME=$(kubectl get pods          \
                 --no-headers      \
                 -n codewizard     \
                 -o custom-columns=":metadata.name" \
                 | grep multitool)

# Test the connection 
kubectl exec -n codewizard $POD_NAME -- curl -svL nginx:5000

”在此处输入图像说明”

You can read out the tiny code snippets under my Github account to get familiar with K8S networking in nutshell: https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/05-Services

The lab will describe in detail exactly what you are asking.

  • You can also print out the information (which you already know I assume)
kubectl explain pod.spec.containers.ports

To answer your question:

  • The easiest way to "find" out what is going on is to use CLI to create the resources and examine the output
# Create ns
kubectl create ns codewizard

# Create the deployment
kubectl create deployment \
        nginx             \
        -n codewizard     \
        --image=nginx

# Create the service
kubectl expose deployment \
        nginx             \
        -n codewizard     \
        --port 5000       \
        --target-port 80  \
        --type ClusterIP  # default

# Check the service
kubectl get svc -n codewizard -o wide

# check to verify that the pod is connected
kubectl get      endpoints -n codewizard
kubectl describe endpoints -n codewizard

enter image description here


Lets see the output

Service

# Get the service yaml 
kubectl get svc nginx -n codewizard -o yaml
### Partial...
apiVersion: v1
kind: Service
...
spec:
  ports:
  - port: 5000
    protocol: TCP
    targetPort: 80

Lets try to test our network

# Create the 2nd deployment for our test
kubectl create deployment \
        multitool         \
        -n codewizard     \
        --image=praqma/network-multitool

# Lets get the pod name so we can test the connection to nginx
POD_NAME=$(kubectl get pods          \
                 --no-headers      \
                 -n codewizard     \
                 -o custom-columns=":metadata.name" \
                 | grep multitool)

# Test the connection 
kubectl exec -n codewizard $POD_NAME -- curl -svL nginx:5000

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文