问题描述
我无法使用该服务的外部IP.我的Kubernetes集群已部署在GKE上.
I am unable to curl to the external IP of the service. My Kubernetes cluster is deployed on GKE.
$ kubectl run kubia-container --image=australia/kubia_py --port=8080 --generator=run/v1
kubectl run --generator=run/v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
replicationcontroller/kubia-container created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubia-container-6b2cs 1/1 Running 0 25s
$ kubectl expose rc kubia-container --type=LoadBalancer --name kubia-http
service/kubia-http exposed
$ kubectl get pods,svc,rc
NAME READY STATUS RESTARTS AGE
pod/kubia-container-6b2cs 1/1 Running 0 93m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 6h28m
service/kubia-http LoadBalancer 10.0.4.238 35.188.42.26 8080:30030/TCP 91m
$ curl 35.188.42.26:8080
curl: (7) Failed to connect to 35.188.42.26 port 8080: Connection refused
推荐答案
我已经尝试了多种方法来实现所需的目标.我认为这张图片 australia/kubia_py 是您的自定义图片.我认为您无法像在文件nodeapp.py
中的该图像内那样卷曲此服务,您已将条目放置为:
I've tried many ways to achieve what you want. I assume this image australia/kubia_py it's your custom image. I think you are not able to curl this service as inside this image in file nodeapp.py
you have put entry like:
server = HTTPServer(('localhost', 8080), GetHandler)
print ("Starting server, use <Ctrl-C> to stop")
server.serve_forever()
此外,如果您要检查容器内的netstat
,还会看到它在127.0.0.1:8080
Also if you would check netstat
inside container you would see that its listening on 127.0.0.1:8080
# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 1/python3
我认为您应该指定0.0.0.0
而不是'localhost'来监听所有接口.
I think that instead of 'localhost' you should specify 0.0.0.0
to listen on all interfaces.
我发现只有一种方法可以实现此 replicacontroller 应用的输出(我建议以后再尝试使用部署)) href ="https://cloud.google.com/shell/docs/using-web-preview" rel ="nofollow noreferrer">网络预览.为此,您需要port-forward
您的广告连播.
I found only one way to achieve output of this replicacontroller app (I suggest to try using deployments in the future) was using web-preview. To do that you would need to port-forward
your pod.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubia-container-95rrq 1/1 Running 0 44m
user@cloudshell:~ (project-name)$ kubectl port-forward kubia-container-95rrq 8888:8080
Forwarding from 127.0.0.1:8888 -> 8080
Handling connection for 8888
Handling connection for 8888
端口8888
只是我选择的默认端口之一.菜单上有几个.
Port 8888
was just one from default I choose. You have a few in menu.
并使用WebPreview
(使用云外壳中的按钮).能够实现如下输出:
And using WebPreview
(using button from cloud shell). was able to achieve output like below:
CLIENT VALUES:
client_address=('127.0.0.1', 46696) (127.0.0.1)
command=GET
path=/?authuser=0
real path=/
query=authuser=0
request_version=HTTP/1.1
SERVER VALUES:
server_version=BaseHTTP/0.6
sys_version=Python/3.6.9
protocol_version=HTTP/1.0
host_name=kubia-container-95rrq
host_ip=10.44.1.26
此外,如果要公开replicacontroller
或deployment
,请记住是否只设置--port=8080
,它也会自动将--port-target
设置为8080
.如果您使用kubectl expose
来使用标志--port=XX
和--target-port=xxxx
,则是首选.
In addition, if you are exposing replicacontroller
or deployment
keep in mind if you will just set --port=8080
it will also automatically set --port-target
as 8080
. It's preferred if you are using kubectl expose
to use flags --port=XX
and --target-port=xxxx
.
如果您想检查kubia
replicacontroller
和service
,则可以使用此示例,如下所示:
If you would like to check kubia
replicacontroller
and service
you can use this example, like below:
$ kubectl apply -f kubia-rc-and-service-v1.yaml
replicationcontroller/kubia-v1 created
service/kubia created
user@cloudshell:~ (your-project)$ kubectl get rc,svc
NAME DESIRED CURRENT READY AGE
replicationcontroller/kubia-v1 3 3 3 40s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.48.1 <none> 443/TCP 8h
service/kubia LoadBalancer 10.0.51.154 35.240.23.237 80:30785/TCP 40s
$ curl 35.240.23.237:80
This is v1 running in pod kubia-v1-zpj6s
另一个公开服务并访问服务的好例子,您可以在 GKE官方文档.
Another good example to expose service and access it you can find on Official GKE docs.
这篇关于无法卷曲到外部IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!