gpt4 book ai didi

docker - kubernetes-暴露来自Nginx Docker镜像的内容

转载 作者:行者123 更新时间:2023-12-02 21:16:13 25 4
gpt4 key购买 nike

我有一个docker nginx容器镜像nha/my-nginx-img
我有一个安装在裸机上的单节点kubernetes集群。我正在尝试部署和公开此nginx容器(否则在本地运行良好)。

我在此文件上运行了kubectl apply -f nginx.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
labels:
app: nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nha/my-nginx-img
ports:
- containerPort: 80
- containerPort: 443
imagePullSecrets:
- name: regcred
#
# Expose Service
#
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
# I thought using NodePort here
# would expose the port on the node?
type: NodePort
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
- name: https
protocol: TCP
port: 443
targetPort: 443
selector:
run: my-nginx

我可以看到它们正在运行:
kubectl get pods -l run=my-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE
my-nginx-5ccbf78584-4lxsn 1/1 Running 0 1d 10.233.102.181 node1
my-nginx-5ccbf78584-6qkml 1/1 Running 0 1d 10.233.102.182 node1

然而:
-上面结果命令中显示的IP不是我机器的IP
-当我 curl 机器的IP时,端口80或443都未得到答复

如何获得nginx提供的静态内容?

附加信息
kubectl get services提供:
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 2d
my-nginx NodePort 10.233.16.91 <none> 80:31081/TCP,443:31856/TCP 5h

(10.233.16.91不是我的IP)

kubectl describe service my-nginx:
Name:                     my-nginx
Namespace: default
Labels: run=my-nginx
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"run":"my-nginx"},"name":"my-nginx","namespace":"default"},"spec":{"ports":[...
Selector: run=my-nginx
Type: NodePort
IP: 10.233.16.91
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 31081/TCP
Endpoints: 10.233.102.181:80,10.233.102.182:80
Port: https 443/TCP
TargetPort: 443/TCP
NodePort: https 31856/TCP
Endpoints: 10.233.102.181:443,10.233.102.182:443
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>

同样,我在任何地方都看不到我的IP。

另外,我使用 kubespray创建了集群。

最佳答案

您需要服务,而不是 pods 。除本地kubernetes网络外,该pod没有暴露于其他任何事物,这就是创建服务的原因。

apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
# I thought using NodePort here
# would expose the port on the node?
type: NodePort
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
- name: https
protocol: TCP
port: 443
targetPort: 443
selector:
run: my-nginx

这里定义了服务,并且确实公开了节点上的端口,但是您看到的IP是内部“群集IP”,只能由其他Pod访问。

所以你可以尝试 kubectl get services


这将显示暴露的端口的外部ip。

还检查 kubectl describe service yourServiceName

关于docker - kubernetes-暴露来自Nginx Docker镜像的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50779578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com