kubernetes - 公开来自 nginx docker 图像的内容

kubernetes - expose content from nginx docker image

我有一个 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 创建了集群。

您需要的是服务,而不是吊舱。 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是内部"cluster IP",只能被其他pods访问。

所以你可以试试 kubectl get services

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

另请查看 kubectl describe service yourServiceName

How do I get this static content to be served by nginx?

您创建了 NodePort type 的服务。使用 <NodeIP>:<NodePort>。在您的示例中,http://<NodeIP>:31081https://<NodeIP>:31856<ClusterIP>:<Port> 也在集群内工作。

强烈推荐的文档:https://kubernetes.io/docs/concepts/services-networking/service/