代码之家  ›  专栏  ›  技术社区  ›  nha

kubernetes-公开nginx docker映像中的内容

  •  0
  • nha  · 技术社区  · 6 年前

    我有一个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 .

    2 回复  |  直到 6 年前
        1
  •  1
  •   Rubydesic    6 年前

    你要的是服务,不是吊舱。除了本地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

        2
  •  1
  •   wineinlib    6 年前

    如何让nginx提供静态内容?

    您创建的服务 NodePort type . 使用 <NodeIP>:<NodePort> . 在你的例子中, http://<NodeIP>:31081 , https://<NodeIP>:31856 . 阿尔索 <ClusterIP>:<Port> 在集群内工作。

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