我有一个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 -当我卷曲我的机器的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创建了集群。
发布于 2018-06-10 08:32:45
你需要的是服务,而不是豆荚。pod除了本地kubernetes网络之外不会公开任何内容,这就是您创建服务的原因。
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”,它只能被其他pods访问。
所以你可以试试kubectl get services
这将向您显示暴露的端口的外部ip。
另请查看kubectl describe service yourServiceName
https://stackoverflow.com/questions/50779578
复制相似问题