【问题标题】:Expose service to minikube with yaml file使用 yaml 文件向 minikube 公开服务
【发布时间】:2019-05-18 17:37:24
【问题描述】:

这是我的第一个部署 yaml 文件,我正在使用 minikube 测试 k8s,就像外部集群一样,我会将 minikube 集群的端口 80 暴露给容器(webservice)的端口 8080。这是我的 yaml:

apiVersion: v1
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
items:

############ Services ###############
- apiVersion: v1
  kind: Service
  metadata:
    name: kuard-80
    labels:
      component: webserver
      app: k8s-test
  spec:
    ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
    loadBalancerIP: 192.168.99.100 # Minikube IP from "minikube ip"
    selector:
      component: webserver
    sessionAffinity: None
    type: LoadBalancer

############ Deployments ############
- apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    name: kuard
    labels:
      component: webserver
      app: k8s-test
  spec:
    replicas: 1 # tells deployment to run 1 pod matching the template
    selector:
      matchLabels:
        component: webserver
    strategy:
      rollingUpdate:
        maxSurge: 25%
        maxUnavailable: 25%
      type: RollingUpdate
    template:
      metadata:
        labels:
          component: webserver
      spec:
        volumes:
          - name: "kuard-data"
            hostPath:
              path: "/var/lib/kuard"
        containers:
          - image: gcr.io/kuar-demo/kuard-amd64:1
            name: kuard
            volumeMounts:
              - mountPath: "/data"
                name: "kuard-data"
            livenessProbe:
              httpGet:
                path: /healthy
                port: 8080
              initialDelaySeconds: 5
              timeoutSeconds: 1
              periodSeconds: 10
              failureThreshold: 3
            ports:
              - containerPort: 8080
                protocol: TCP

    restartPolicy: Always

我希望端口 80 在 http://192.168.99.100 上回复我,错误在哪里?这是一些命令、服务和端点的结果

$ kubectl 获取服务

NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kuard-80     LoadBalancer   10.107.163.175   <pending>     80:30058/TCP   3m
kubernetes   ClusterIP      10.96.0.1        <none>        443/TCP        34d

$ kubectl 获取端点

NAME         ENDPOINTS             AGE
kuard-80     172.17.0.7:8080       10m
kubernetes   192.168.99.100:8443   34d

感谢您能给我的任何帮助,如果问题很愚蠢,请原谅...

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    您的服务是 LoadBalancer 类型,仅支持云,因此您的外部 IP 处于待处理状态。

    NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
    kuard-80     LoadBalancer   10.107.163.175   <pending>     80:30058/TCP   3m
    

    您可以在 minikube 中使用 NodePort 公开您的服务。以下是 yaml 文件:

     apiVersion: v1
       kind: Service
       metadata:
         name: kuard-80
         labels:
           component: webserver
           app: k8s-test
       spec:
         ports:
         - port: 80
           targetPort: 8080
           protocol: TCP
         selector:
           component: webserver
         type: NodePort
    

    现在,当您执行kubectl describe service kuard-80 时,您将能够看到NodePort 类型的端口,其值将在30000-32767 之间。您将能够使用以下方式访问您的应用程序:

    http://<vm_ip>:<node_port>
    

    希望对你有帮助

    【讨论】:

    • 好的,谢谢,工作,但使用 minikube 无法直接公开端口 80,而不是 NodePort?
    • 你可以使用kubectl port-forwardkubectl port-forward &lt;pod_name&gt; &lt;local-port&gt;:&lt;pod-port&gt;现在你可以使用来访问它:请找到以下有用的链接以获得更多理解:docs.giantswarm.io/guides/accessing-services-from-the-outside/…
    【解决方案2】:

    在基于 minikube 或 KinD 的集群上,您可以运行 MetalLB。它是裸机 LB 产品。要在使用 minikube 或 KinD 创建的本地集群上安装 MetalLB,您需要在 yamls 下运行。

    您需要一个名为 metal-lb.yaml 的 yaml 文件,其中包含以下内容:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      namespace: metallb-system
      name: config
    data:
      config: |
        address-pools:
        - name: default
          protocol: layer2
          addresses:
          - 192.168.1.31-192.168.1.40
    

    随意更改 ip 范围。

    kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/namespace.yaml
    kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/metallb.yaml
    kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
    kubectl apply -f ./metal-lb.yaml
    

    然后您也可以创建 LoadBalancer 类型的服务。

    【讨论】:

      猜你喜欢
      • 2019-04-15
      • 1970-01-01
      • 2020-06-17
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多