【问题标题】:How to create GKE private cluster with Jinja Template?如何使用 Jinja 模板创建 GKE 私有集群?
【发布时间】:2021-08-12 10:13:41
【问题描述】:

我正在尝试编写一个 Jinja 模板和一个 bash 脚本来创建一个私有 GKE 集群,并带有一个选项(在我的 bash 脚本中),以允许它访问私有和公共端点。。 p>

所以我希望我的 bash 脚本在默认情况下使该集群具有私有端点访问权限,并且当用户使用标志 (deployment.sh --public-endpoint) 运行时使其具有公共端点访问权限。

这是我的 Jinja 模板的样子:

resources:
- name: {{ CLUSTER_NAME }}
  type: container.v1.cluster
  properties:
    zone: {{ properties["zone"] }}
    cluster:
      name: {{ CLUSTER_NAME }}
      description: Customer Stack Cluster 
      network: {{properties["network"]}}
      subnetwork: {{properties["subnetwork"]}}
      *privateClusterConfig:
        enablePrivateNodes: true
        enablePrivateEndpoint: true
        **{% if --public-endpoint flag was passed %}
             <p> Make it with a public endpoint access </p>
        {% else %}
             <p> make it with private endpoint access </p>
        {% endif %}***
        # Configure the IP range for the hosted master network
        masterIpv4CidrBlock: 172.16.0.32/28
      ipAllocationPolicy:
        useIpAliases: true
      nodePools: 
        - name: nodepool
          config: 
            oauthScopes:
            - https://www.googleapis.com/auth/compute
            - https://www.googleapis.com/auth/logging.write
            - https://www.googleapis.com/auth/monitoring
            - https://www.googleapis.com/auth/devstorage.full_control
            machineType: n1-standard-2
            # Default will be passed in 
            serviceAccount: {{ properties['computeServiceAccount'] }}
          autoscaling: 
            enabled: true 
            minNodeCount: 2
            maxNodeCount: 5
          initialNodeCount: 2

我的 bash 脚本的那部分如下所示:

CLUSTER=$(gcloud deployment-manager deployments create "${NAME}" --template deployment.jinja --properties "region:${REGION},zone:${ZONE},network:${NETWORK},subnetwork:${SUBNETWORK},computeServiceAccount:${COMPUTE_SA},k8sVersion:\"${K8S_VERSION}\"" --format=json)
if [ $? -ne 0 ]; then error "FAILED_DEPLOY_GKE"; fi

我不知道如何使这项工作以及 Jinja 中的 if 语句语法如何。

【问题讨论】:

  • 如果您想为某些私有节点提供出站互联网访问,您可以使用 Cloud NAT 或管理您自己的 NAT 网关。请检查private clusters 因此,将不同的部署分开在两个不同的文件中可能更有意义,因为如果使用公共端点,您可能还必须设置 Cloud NAT 或自己的 NAT 网关。
  • “因为在使用公共端点的情况下,您可能还必须设置 Cloud NAT 或自己的 NAT 网关”——您的意思是私有端点?
  • 是的,您是对的,将 Cloud NAT 网关设置为私有端点。您可能会在 Private Google Access Interaction 中找到更好的解释

标签: bash google-cloud-platform jinja2 google-kubernetes-engine gke-networking


【解决方案1】:

要将条件添加到您的脚本中,您可能会发现接下来的两个替代方案对私有集群的每次访问很有用,一个是没有客户端访问公共端点的权限,另一个是对公共端点的有限访问。

Creating a private cluster with no client access to the public endpoint

要创建没有可公开访问的控制平面的集群,请在 privateClusterConfig 资源中指定 enablePrivateEndpoint: true 字段。

使用此配置创建私有集群时,您可以选择使用自动生成的子网或自定义子网。

查找与Creating a private cluster with limited access to the public endpoint API相关的文档

对于自动生成的子网,请在集群 API 资源中指定 privateClusterConfig 字段:

{
  "name": "private-cluster-1",
  ...
  "ipAllocationPolicy": {
    "createSubnetwork": true,
  },
  ...
    "privateClusterConfig" {
      "enablePrivateNodes": boolean # Creates nodes with internal IP addresses only
      "enablePrivateEndpoint": boolean # false creates a cluster control plane with a publicly-reachable endpoint
      "masterIpv4CidrBlock": string # CIDR block for the cluster control plane
      "privateEndpoint": string # Output only
      "publicEndpoint": string # Output only
  }
}

或者custom subnet 可能更复杂一点

如果成功请分享脚本,对社区很有帮助。

【讨论】:

    猜你喜欢
    • 2020-01-30
    • 2019-12-24
    • 1970-01-01
    • 2021-05-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    相关资源
    最近更新 更多