k8s 部署实战 - 图1

    在这张架构图中,Ingress 是应用的入口,Ingress 会根据请求路径将流量分流至前后端的 Service 中,然后 Service 将请求转发给前后端 Pod 进行业务逻辑处理,后端的工作负载 Deployment 配置了 HPA 自动横向扩容。同时,Postgres 也是以 Deployment 的方式部署到集群内的。最后,所有资源都部署在 K8s 的 example 命名空间(Namespace)下。

    创建新的 K8s 集群

    创建 config.yaml
    1. kind: Cluster
    2. apiVersion: kind.x-k8s.io/v1alpha4
    3. nodes:
    4. - role: control-plane
    5. kubeadmConfigPatches:
    6. - |
    7. kind: InitConfiguration
    8. nodeRegistration:
    9. kubeletExtraArgs:
    10. node-labels: "ingress-ready=true"
    11. extraPortMappings:
    12. - containerPort: 80
    13. hostPort: 80
    14. protocol: TCP
    15. - containerPort: 443
    16. hostPort: 443
    17. protocol: TCP
    使用 kind create cluster 重新创建集群:
    1. kind create cluster --config config.yaml
    2. Creating cluster "kind" ...
    3. Ensuring node image (kindest/node:v1.23.4) 🖼
    4. Preparing nodes 📦
    5. Writing configuration 📜
    6. Starting control-plane 🕹️
    7. Installing CNI 🔌
    8. Installing StorageClass 💾
    9. Set kubectl context to "kind-kind"
    10. You can now use your cluster with:
    11. kubectl cluster-info --context kind-kind
    由于示例应用使用了 Ingress,所以我们需要为 Kind 部署 Ingress,你可以使用 kubectl apply -f 来部署 Ingress-Nginx:
    1. $ kubectl create -f https://ghproxy.com/ https://raw.githubusercontent.com/lyzhang1999/resource/main/ingress-nginx/ingress-nginx.yaml
    2. namespace/ingress-nginx created
    3. serviceaccount/ingress-nginx created
    4. serviceaccount/ingress-nginx-admission created
    5. ......

    :::color2 Ingress controller(Ingress 控制器)是面向 Kubernetes(及其他容器化)环境的专用负载均衡器。

    Kubernetes Ingress Controller 的功能如下:
    • 接受来自 Kubernetes 平台外部的流量,并将其负载均衡到 Kubernetes 平台内部运行的 pod(容器)
    • 可管理集群内需要与集群外其他服务通信的服务的出向流量
    • 使用 Kubernetes API 进行配置,以部署名为“Ingress 资源”的对象
    • 监控 Kubernetes 中运行的 pod,并在服务添加或删除 pod 后自动更新负载均衡规则

    :::