如何为Kubernetes集群分配IP?

时间:2020-04-14 15:17:32

标签: kubernetes rancher

我已经通过Rancher安装了K8S集群,它已经启动并正在运行。为了进行测试,我已经部署了helloworld nginx pod:

enter image description here

要调用该服务,我必须调用NodePort IP地址,例如:

http://111.111.111.111:30359/

但是我想用一个名字来称呼它,例如:

https://helloworld.co.example.org

已安装入口控制器Nginx:

NAMESPACE       NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default         kubernetes             ClusterIP   10.43.0.1       <none>        443/TCP                  162m
dev             helloworld             ClusterIP   10.43.187.230   <none>        80/TCP                   17m
dev             helloworld-nodeport    NodePort    10.43.9.147     <none>        80:30359/TCP             17m
ingress-nginx   default-http-backend   ClusterIP   10.43.86.105    <none>        80/TCP                   161m
kube-system     kube-dns               ClusterIP   10.43.0.10      <none>        53/UDP,53/TCP,9153/TCP   161m
kube-system     metrics-server         ClusterIP   10.43.220.198   <none>        443/TCP                  161m

但是它没有EXTERNAL-IP。问题是,如何获得它。

1 个答案:

答案 0 :(得分:2)

ClusterIP类型的服务将永远不会创建EXTERNAL IP,因为它是群集内部的IP。遵循此guide,了解如何在数字海洋上使用Nginx入口通过主机名公开Hello World应用。

在安装nginx入口控制器之后,按照指南进行操作,它将由digital ocean提供的LoadBalancer公开。

如您在指南中所见,您将在进入规则中使用您的域。您需要确保通过A记录将您的域指向负载均衡器。这是通过您的DNS提供商完成的。

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: hello-kubernetes-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: helloworld.co.example.org
    http:
      paths:
      - backend:
          serviceName: hello-kubernetes
          servicePort: 80