我已经部署了 Pod 及其服务,但我得到:无法连接到端口 80 [英] I have deployed pods withs its service and I am getting: failed to connect to port 80
问题描述
我在 GCP 上有一个由两个节点组成的 kubernetes 集群.我有 pod -> mycha-deploy,带有服务 -> mycha-svc,我还有带有服务 nginx-svc 的 pod nginx-controller.当我尝试卷入 Pod 或服务 ip 时,我不断收到:端口 80 连接被拒绝.当我浏览主 IP 时,我什么也没得到.配置中是否有我遗漏的东西.谢谢.
# mycha-deployapi 版本:应用程序/v1种类:部署元数据:名称: mycha-deploy标签:应用程序:mycha-app规格:复制品:1选择器:匹配标签:应用程序:mycha-app模板:元数据:标签:应用程序:mycha-app规格:容器:- 名称:mycha-容器图片:us.gcr.io/########/mycha-frontend_kubernetes_rrk8s端口:- 容器端口:80#mycha-svcapi版本:v1种类:服务元数据:名称:mycha-svc标签:应用程序:mycha-app规格:选择器:应用程序:mycha-app端口:- 端口:80目标端口:80协议:TCP名称:http#nginx-控制器api 版本:应用程序/v1种类:部署元数据:名称:nginx-控制器规格:复制品:1选择器:匹配标签:名称:nginx-ingress模板:元数据:标签:名称:nginx-ingress规格:容器:- 名称:nginx-ingress-controller图片:quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.27.0参数:-/nginx-ingress-controller- --configmap=$(POD_NAMESPACE)/nginx-configuration环境:- 名称:POD_NAME值来自:字段引用:字段路径:metadata.name- 名称:POD_NAMESPACE值来自:字段引用:字段路径:metadata.namespace端口:- 名称:http集装箱港口:80- 名称:https容器端口:443#nignx-svcapi版本:v1种类:服务元数据:名称:nginx-ingress标签:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx规格:类型:节点端口端口:- 端口:80目标端口:80协议:TCP名称:http- 端口:443目标端口:443协议:TCP名称:https选择器:名称:nginx-ingress##nginx-资源apiVersion: 扩展/v1beta1种类:入口元数据:名称:mycha-ingress注释:kubernetes.io/ingress.class: "nginx"规格:规则:- http:路径:- 小路:/后端:服务名称:mycha-svc服务端口:80-----kubectl 描述 svc nginx-ingress名称:nginx-ingress命名空间:默认标签:app.kubernetes.io/name=ingress-nginxapp.kubernetes.io/part-of=ingress-nginx注释:<none>选择器:name=nginx-ingress类型:节点端口IP:10.107.186.83端口:http 80/TCP目标端口:80/TCP节点端口:http 32606/TCP端点:10.244.1.3:80端口:https 443/TCP目标端口:443/TCP节点端口:https 31481/TCP端点:10.244.1.3:443会话亲和力:无外部流量策略:集群事件:<无>-------kubectl 获取 pods,svc名称就绪状态重新开始年龄pod/mycha-deploy-5f9b6f5c46-jjdhq 1/1 运行 0 76mpod/nginx-controller-5c45cf6d5c-dpp44 1/1 运行 0 60m名称 类型 CLUSTER-IP EXTERNAL-IP PORT(S) AGE服务/kubernetes ClusterIP 10.96.0.1 <无>443/TCP 100mservice/mycha-svc ClusterIP 10.103.188.25 <none>80/TCP 68mservice/nginx-ingress NodePort 10.107.186.83 <none>80:32606/TCP,443:31481/TCP 51m------须藤 lsof -i -P -n |听听systemd-r 890 systemd-resolve 13u IPv4 16536 0t0 TCP 127.0.0.53:53(听)splunkd 1111 根 4u IPv4 25377 0t0 TCP *:8089(听)sshd 1842 根 3u IPv4 23916 0t0 TCP *:22(听)sshd 1842 根 4u IPv6 23931 0t0 TCP *:22(听)kube-cont 22737 root 5u IPv6 116157110 0t0 TCP *:10252(听)kube-cont 22737 根 6u IPv4 116157116 0t0 TCP 127.0.0.1:10257(听)kube-prox 23291 根 8u IPv6 116256894 0t0 TCP *:31481(听)kube-prox 23291 root 11u IPv6 116256895 0t0 TCP *:32606(听)kube-prox 23291 root 16u IPv6 116164057 0t0 TCP *:10256(听)kube-prox 23291 根 17u IPv4 116164061 0t0 TCP 127.0.0.1:10249(听)etcd 23380 根 3u IPv4 116158620 0t0 TCP 10.242.6.2:2380(听)etcd 23380 根 5u IPv4 116158624 0t0 TCP 10.242.6.2:2379(听)etcd 23380 根 6u IPv4 116158625 0t0 TCP 127.0.0.1:2379(听)etcd 23380 根 11u IPv4 116157996 0t0 TCP 127.0.0.1:2381(听)kube-sche 23803 root 5u IPv6 116159474 0t0 TCP *:10251(听)kube-sche 23803 根 6u IPv4 116159480 0t0 TCP 127.0.0.1:10259(听)kube-apis 24180 根 5u IPv6 116163385 0t0 TCP *:6443(听)节点 27844 robertorios 20u IPv4 116024875 0t0 TCP 127.0.0.1:38509(听)kubelet 30601 根 10u IPv4 116038855 0t0 TCP 127.0.0.1:33119(听)kubelet 30601 根 17u IPv6 116038993 0t0 TCP *:10250(听)kubelet 30601 根 31u IPv4 116038997 0t0 TCP 127.0.0.1:10248(听)
谢谢.
在 GKE 上,您可以使用两种类型的 Ingress.一个是 Nginx Ingress
,您可能希望基于注释 kubernetes.io/ingress.class: nginx"
使用它.第二个是 GKE Ingress.
1.GKE 入口
当您想使用 GKE Ingress
时,您需要将您的服务指定为 NodePort
并应用 Ingress
.根据你的 YAML,我已经复制了它.
当你使用自己的镜像时,我使用的是 nginx 镜像.
apiVersion:apps/v1种类:部署元数据:名称: mycha-deploy标签:应用程序:mycha-app规格:复制品:1选择器:匹配标签:应用程序:mycha-app模板:元数据:标签:应用程序:mycha-app规格:容器:- 名称:mycha-容器图片:nginx端口:- 容器端口:80---#新增类型:Nodeportapi版本:v1种类:服务元数据:名称:mycha-svc标签:应用程序:mycha-app规格:类型:节点端口选择器:应用程序:mycha-app端口:- 端口:80目标端口:80协议:TCP名称:http---#removed annotation,因为这里我们使用的是 GKE IngressapiVersion: 扩展/v1beta1种类:入口元数据:名称:mycha-ingress规格:规则:- http:路径:- 小路:/后端:服务名称:mycha-svc服务端口:80deployment.apps/mycha-deploy 创建服务/mycha-svc 已创建ingress.extensions/mycha-ingress 创建
您应该能够看到如下输出:
$ kubectl get pods,svc,ing名称就绪状态重新开始年龄pod/mycha-deploy-685f894996-xbbnv 1/1 运行 0 38s名称 类型 CLUSTER-IP EXTERNAL-IP PORT(S) AGE服务/kubernetes ClusterIP 10.99.0.1 <无>443/TCP 33dservice/mycha-svc NodePort 10.99.13.51 <none>80:30808/TCP 39s名称主机地址端口时代ingress.extensions/mycha-ingress * 34.107.251.59 80 3m3s
现在您应该可以卷曲您的 svc.
$ curl 34.107.251.59...<身体><h1>欢迎使用 nginx!</h1><p>如果看到这个页面,则表示nginx web服务器安装成功,并且在职的.需要进一步配置.</p>
当您使用 GKE Ingress 时,您的 Ingress
将自动接收 EXTERNAL-IP.如果您将 service
保留为 ClusterIP
,它将不会收到任何 Address
.
$ kubectl get ing名称主机地址端口时代mycha-ingress * 80 34m
<块引用>
在服务清单中,注意类型是 NodePort.这是用于配置 HTTP(S) 负载平衡器的 Ingress 所需的类型.可以在此处找到更多详细信息.
2.GKE 上的 Nginx Ingress
当您使用 Nginx Ingress
时,您可以将您的服务指定为 ClusterIP
或 NodePort
.
为此,您需要部署适当的 Nginx Ingress
.可以在此处找到好的教程,但它有点过时了.我在下面发布更新的步骤:
添加和更新仓库:
$ helm repo 添加 ingress-nginx https://kubernetes.github.io/ingress-nginx$ helm repo 更新
- 应用您的部署网络和服务(
NodePort
或ClusterIP
,使用Nginx Ingress
两种类型都可以). - 使用
$ helm install ingress-nginx ingress-nginx/ingress-nginx
部署Nginx Ingress
.它将创建 2 个deployments
和 2 个services
.其中一项服务将被创建为LoadBalancer
. - 部署 Ingress
使用annotation.kubernetes.io/ingress.class:nginx"
apiVersion: extensions/v1beta1种类:入口元数据:名称:mycha-ingress注释:kubernetes.io/ingress.class:nginx"规格:规则:- http:路径:- 小路:/后端:服务名称:mycha-svc服务端口:80
你应该有这样的输出:
$ kk 获取 pods,svc,ing名称就绪状态重新开始年龄pod/mycha-deploy-c469dc58b-mdp6d 1/1 运行 0 2m41spod/nginx-ingress-controller-5d47f75dfc-d6xnl 1/1 运行 0 7m18spod/nginx-ingress-default-backend-f5b888f7d-rf5cx 1/1 运行 0 7m18s名称 类型 CLUSTER-IP EXTERNAL-IP PORT(S) AGE服务/kubernetes ClusterIP 10.99.0.1 <无>443/TCP 33d服务/mycha-svc ClusterIP 10.99.8.140 <无>80/TCP 2m12s服务/nginx-ingress-controller LoadBalancer 10.99.11.177 34.90.172.116 80:31593/TCP,443:30104/TCP 7m19s服务/nginx-ingress-default-backend ClusterIP 10.99.7.106 <none>80/TCP 7分19秒名称主机地址端口时代ingress.extensions/mycha-ingress * 80 17s
您的 Ingress
不会收到任何 Address
,因为 service/nginx-ingress-controller
将用作 LoadBalancer.
现在您可以使用 curl 检查是否一切正常.
$ curl 34.90.172.116<!DOCTYPE html>...<h1>欢迎使用 nginx!</h1><p>如果看到这个页面,则表示nginx web服务器安装成功,并且在职的.需要进一步配置.</p>
I have a kubernetes cluster on GCP made of two nodes. I have pod -> mycha-deploy, with service -> mycha-svc, also I have pod nginx-controller with service nginx-svc. When I try to curl into the pods or services ips I keep getting: port 80 conection refused. When I browse into the master ip I don't get anything. Is there something I am missing in the configuration. Thank you.
# mycha-deploy
apiVersion: apps/v1
kind: Deployment
metadata:
name: mycha-deploy
labels:
app: mycha-app
spec:
replicas: 1
selector:
matchLabels:
app: mycha-app
template:
metadata:
labels:
app: mycha-app
spec:
containers:
- name: mycha-container
image: us.gcr.io/########/mycha-frontend_kubernetes_rrk8s
ports:
- containerPort: 80
#mycha-svc
apiVersion: v1
kind: Service
metadata:
name: mycha-svc
labels:
app: mycha-app
spec:
selector:
app: mycha-app
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
#nginx-controller
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-controller
spec:
replicas: 1
selector:
matchLabels:
name: nginx-ingress
template:
metadata:
labels:
name: nginx-ingress
spec:
containers:
- name: nginx-ingress-controller
image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.27.0
args:
- /nginx-ingress-controller
- --configmap=$(POD_NAMESPACE)/nginx-configuration
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
#nignx-svc
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
- port: 443
targetPort: 443
protocol: TCP
name: https
selector:
name: nginx-ingress
##nginx-resource
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: mycha-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: mycha-svc
servicePort: 80
-----
kubectl describe svc nginx-ingress
Name: nginx-ingress
Namespace: default
Labels: app.kubernetes.io/name=ingress-nginx
app.kubernetes.io/part-of=ingress-nginx
Annotations: <none>
Selector: name=nginx-ingress
Type: NodePort
IP: 10.107.186.83
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 32606/TCP
Endpoints: 10.244.1.3:80
Port: https 443/TCP
TargetPort: 443/TCP
NodePort: https 31481/TCP
Endpoints: 10.244.1.3:443
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
-------
kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/mycha-deploy-5f9b6f5c46-jjdhq 1/1 Running 0 76m
pod/nginx-controller-5c45cf6d5c-dpp44 1/1 Running 0 60m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 100m
service/mycha-svc ClusterIP 10.103.188.25 <none> 80/TCP 68m
service/nginx-ingress NodePort 10.107.186.83 <none> 80:32606/TCP,443:31481/TCP 51m
------
sudo lsof -i -P -n | grep LISTEN
systemd-r 890 systemd-resolve 13u IPv4 16536 0t0 TCP 127.0.0.53:53 (LISTEN)
splunkd 1111 root 4u IPv4 25377 0t0 TCP *:8089 (LISTEN)
sshd 1842 root 3u IPv4 23916 0t0 TCP *:22 (LISTEN)
sshd 1842 root 4u IPv6 23931 0t0 TCP *:22 (LISTEN)
kube-cont 22737 root 5u IPv6 116157110 0t0 TCP *:10252 (LISTEN)
kube-cont 22737 root 6u IPv4 116157116 0t0 TCP 127.0.0.1:10257 (LISTEN)
kube-prox 23291 root 8u IPv6 116256894 0t0 TCP *:31481 (LISTEN)
kube-prox 23291 root 11u IPv6 116256895 0t0 TCP *:32606 (LISTEN)
kube-prox 23291 root 16u IPv6 116164057 0t0 TCP *:10256 (LISTEN)
kube-prox 23291 root 17u IPv4 116164061 0t0 TCP 127.0.0.1:10249 (LISTEN)
etcd 23380 root 3u IPv4 116158620 0t0 TCP 10.242.6.2:2380 (LISTEN)
etcd 23380 root 5u IPv4 116158624 0t0 TCP 10.242.6.2:2379 (LISTEN)
etcd 23380 root 6u IPv4 116158625 0t0 TCP 127.0.0.1:2379 (LISTEN)
etcd 23380 root 11u IPv4 116157996 0t0 TCP 127.0.0.1:2381 (LISTEN)
kube-sche 23803 root 5u IPv6 116159474 0t0 TCP *:10251 (LISTEN)
kube-sche 23803 root 6u IPv4 116159480 0t0 TCP 127.0.0.1:10259 (LISTEN)
kube-apis 24180 root 5u IPv6 116163385 0t0 TCP *:6443 (LISTEN)
node 27844 robertorios 20u IPv4 116024875 0t0 TCP 127.0.0.1:38509 (LISTEN)
kubelet 30601 root 10u IPv4 116038855 0t0 TCP 127.0.0.1:33119 (LISTEN)
kubelet 30601 root 17u IPv6 116038993 0t0 TCP *:10250 (LISTEN)
kubelet 30601 root 31u IPv4 116038997 0t0 TCP 127.0.0.1:10248 (LISTEN)
Thank you.
On GKE you can use two types of Ingress. One is Nginx Ingress
which you probably wanted to use based on annotation kubernetes.io/ingress.class: "nginx"
. Second one is GKE Ingress.
1. GKE Ingress
When you want to use GKE Ingress
you need to specify your service as NodePort
and apply Ingress
. Based on your YAMLs, I've reproduced it.
As you used your own image, Ive used nginx image.
apiVersion: apps/v1
kind: Deployment
metadata:
name: mycha-deploy
labels:
app: mycha-app
spec:
replicas: 1
selector:
matchLabels:
app: mycha-app
template:
metadata:
labels:
app: mycha-app
spec:
containers:
- name: mycha-container
image: nginx
ports:
- containerPort: 80
---
#added type: Nodeport
apiVersion: v1
kind: Service
metadata:
name: mycha-svc
labels:
app: mycha-app
spec:
type: NodePort
selector:
app: mycha-app
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
---
#removed annotation, as here we are using GKE Ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: mycha-ingress
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: mycha-svc
servicePort: 80
deployment.apps/mycha-deploy created
service/mycha-svc created
ingress.extensions/mycha-ingress created
You should be able to see output like below:
$ kubectl get pods,svc,ing
NAME READY STATUS RESTARTS AGE
pod/mycha-deploy-685f894996-xbbnv 1/1 Running 0 38s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 33d
service/mycha-svc NodePort 10.99.13.51 <none> 80:30808/TCP 39s
NAME HOSTS ADDRESS PORTS AGE
ingress.extensions/mycha-ingress * 34.107.251.59 80 3m3s
Now you should be able to curl your svc.
$ curl 34.107.251.59
...
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
As you are using GKE Ingress, your Ingress
will automatically received EXTERNAL-IP. If you will keep service
as ClusterIP
it won't receive any Address
.
$ kubectl get ing
NAME HOSTS ADDRESS PORTS AGE
mycha-ingress * 80 34m
In the Service manifest, notice that the type is NodePort. This is the required type for an Ingress that is used to configure an HTTP(S) load balancer. More detailed information can be found here.
2. Nginx Ingress on GKE
When you are using Nginx Ingress
you can specify your service as ClusterIP
or NodePort
.
To do that you need to deploy proper Nginx Ingress
.
Good tutorail can be found here, however it's bit outdated. I am posting below updated steps:
- Install Helm v3. This version don't require
tiller
. - Add proper repository for
Helm 3
. Details can be found here.
Adding and updating repo:
$ helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
$ helm repo update
- Apply your deploymnet and service (
NodePort
orClusterIP
, usingNginx Ingress
both types will work). - Deploy
Nginx Ingress
using$ helm install ingress-nginx ingress-nginx/ingress-nginx
. It will create 2deployments
and 2services
. One of services will be created asLoadBalancer
. - Deploy Ingress
With annotation.kubernetes.io/ingress.class: "nginx"
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: mycha-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: mycha-svc
servicePort: 80
You should have output like:
$ kk get pods,svc,ing
NAME READY STATUS RESTARTS AGE
pod/mycha-deploy-c469dc58b-mdp6d 1/1 Running 0 2m41s
pod/nginx-ingress-controller-5d47f75dfc-d6xnl 1/1 Running 0 7m18s
pod/nginx-ingress-default-backend-f5b888f7d-rf5cx 1/1 Running 0 7m18s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 33d
service/mycha-svc ClusterIP 10.99.8.140 <none> 80/TCP 2m12s
service/nginx-ingress-controller LoadBalancer 10.99.11.177 34.90.172.116 80:31593/TCP,443:30104/TCP 7m19s
service/nginx-ingress-default-backend ClusterIP 10.99.7.106 <none> 80/TCP 7m19s
NAME HOSTS ADDRESS PORTS AGE
ingress.extensions/mycha-ingress * 80 17s
Your Ingress
will not receive any Address
as service/nginx-ingress-controller
will work as LoadBalancer.
Now you can check if everything works using curl.
$ curl 34.90.172.116
<!DOCTYPE html>
...
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
这篇关于我已经部署了 Pod 及其服务,但我得到:无法连接到端口 80的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!