且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在没有负载均衡器的情况下在Digital Ocean的托管Kubernetes上暴露端口80

更新时间:2021-08-24 21:38:38

您可以部署配置为使用主机网络和端口80/443的Ingress.

You can deploy an Ingress configured to use the host network and port 80/443.

    默认情况下,集群的
  1. DO的防火墙没有打开80/443入站.

  1. DO's firewall for your cluster doesn't have 80/443 inbound open by default.

如果您编辑自动创建的防火墙,则规则最终将重置自己.解决方案是创建一个单独的防火墙,该防火墙也指向相同的Kubernetes工作者节点:

If you edit the auto-created firewall the rules will eventually reset themselves. The solution is to create a separate firewall also pointing at the same Kubernetes worker nodes:

$ doctl compute firewall create \
--inbound-rules="protocol:tcp,ports:80,address:0.0.0.0/0,address:::/0 protocol:tcp,ports:443,address:0.0.0.0/0,address:::/0" \
--tag-names=k8s:CLUSTER_UUID \
--name=k8s-extra-mycluster

(从仪表板获取CLUSTER_UUID值,或从doctl kubernetes cluster list获取ID列)

(Get the CLUSTER_UUID value from the dashboard or the ID column from doctl kubernetes cluster list)

  1. 使用主机网络创建 nginx入口.我在下面添加了 helm chart 配置,但是您也可以通过直接安装过程来做到这一点.
  1. Create the nginx ingress using the host network. I've included the helm chart config below, but you could do it via the direct install process too.

# For Helm 2
$ helm install stable/nginx-ingress --name=myingress -f myingress.values.yml

# For Helm 3
$ helm install myingress stable/nginx-ingress -f myingress.values.yml

图表的

myingress.values.yml:

---
controller:
  kind: DaemonSet
  hostNetwork: true
  dnsPolicy: ClusterFirstWithHostNet
  daemonset:
    useHostPort: true
  service:
    type: ClusterIP
rbac:
  create: true

  1. 您应该能够通过任何辅助节点IP分别在:80和:443***问群集,它将流量路由到您的入口.

  1. you should be able to access the cluster on :80 and :443 via any worker node IP and it'll route traffic to your ingress.

因为节点IP可以&要进行更改,请查看部署外部DNS 以管理指向您的工作人员的DNS条目节点.再次使用舵图并假设您的DNS域由DigitalOcean托管(尽管任何受支持的DNS提供商都可以使用):

since node IPs can & do change, look at deploying external-dns to manage DNS entries to point to your worker nodes. Again, using the helm chart and assuming your DNS domain is hosted by DigitalOcean (though any supported DNS provider will work):

# For Helm 2
$ helm install --name=mydns -f mydns.values.yml stable/external-dns

# For Helm 3
$ helm install mydns stable/external-dns -f mydns.values.yml

图表的

mydns.values.yml:

---
provider: digitalocean
digitalocean:
  # create the API token at https://cloud.digitalocean.com/account/api/tokens
  # needs read + write
  apiToken: "DIGITALOCEAN_API_TOKEN"
domainFilters:
  # domains you want external-dns to be able to edit
  - example.com
rbac:
  create: true

  1. 创建Kubernetes 入口资源来路由请求到现有的Kubernetes服务:
  1. create a Kubernetes Ingress resource to route requests to an existing Kubernetes service:

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: testing123-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: testing123.example.com             # the domain you want associated
      http:
        paths:
          - path: /
            backend:
              serviceName: testing123-service  # existing service
              servicePort: 8000                # existing service port

  1. 大约一分钟后,您应该会看到DNS记录出现并且可以解决:

$ dig testing123.example.com             # should return worker IP address
$ curl -v http://testing123.example.com  # should send the request through the Ingress to your backend service

(编辑自动创建的防火墙规则最终会失败,请添加单独的防火墙).

( editing the automatically created firewall rules eventually breaks, add a separate firewall instead).