如何透過使用者資料自動化 EKS 工作節點之 HTTP 代理組態?
我想要透過使用者資料自動化 Amazon Elastic Kubernetes Service (Amazon EKS) 工作節點之 HTTP 代理組態。
簡短說明
若要在工作節點上設定代理,您必須設定 Amazon EKS 叢集的必要元件,從代理進行通訊。元件包含但不限於 Kubelet systemd 服務、kube-proxy、aws-node Pod 及 yum 更新。 若要透過 Docker 執行期自動化工作節點的代理組態,請執行下列操作:
**注意:**下列解析度僅會套用基礎執行期為 Docker 的節點,且不適用於使用 containerd 執行期的節點。如為使用 containerd 執行期的節點,請參閱如何自動化 EKS containerd 節點的 HTTP 代理組態?
解決方法
- 尋找叢集的 IP CIDR 區塊:
$ kubectl get service kubernetes -o jsonpath='{.spec.clusterIP}'; echo
上述指令會傳回 10.100.0.1 或 172.20.0.1。如此表示叢集 IP CIDR 區塊可能是 10.100.0.0/16 或 172.20.0.0/16。
- 根據步驟 1 的命令輸出訊息,建立名為 proxy-env-vars-config.yaml 的 ConfigMap 檔案。
如果輸出訊息具有範圍 172.20.x.x 內的 IP,請按以下方式建構 ConfigMap 檔案:
apiVersion: v1 kind: ConfigMap metadata: name: proxy-environment-variables namespace: kube-system data: HTTP_PROXY: http://customer.proxy.host:proxy_port HTTPS_PROXY: http://customer.proxy.host:proxy_port NO_PROXY: 172.20.0.0/16,localhost,127.0.0.1,VPC_CIDR_RANGE,169.254.169.254,.internal,s3.amazonaws.com,.s3.us-east-1.amazonaws.com,api.ecr.us-east-1.amazonaws.com,dkr.ecr.us-east-1.amazonaws.com,ec2.us-east-1.amazonaws.com
**注意:**使用叢集 VPC 的 IPv4 CIDR 區塊取代 VPC_CIDR_RANGE。
如果輸出訊息具有範圍 10.100.x.x 內的 IP,請按以下方式建構 ConfigMap 檔案:
apiVersion: v1 kind: ConfigMap metadata: name: proxy-environment-variables namespace: kube-system data: HTTP_PROXY: http://customer.proxy.host:proxy_port HTTPS_PROXY: http://customer.proxy.host:proxy_port NO_PROXY: 10.100.0.0/16,localhost,127.0.0.1,VPC_CIDR_RANGE,169.254.169.254,.internal,s3.amazonaws.com,.s3.us-east-1.amazonaws.com,api.ecr.us-east-1.amazonaws.com,dkr.ecr.us-east-1.amazonaws.com,ec2.us-east-1.amazonaws.com
**注意:**使用叢集 VPC 的 IPv4 CIDR 區塊取代 VPC_CIDR_RANGE。
具有私有 API 伺服器端點存取權、私有子網路及無網際網路存取權的 Amazon EKS 叢集需要額外的端點。如果您使用上述的組態建置叢集,則必須建立和新增下列服務的端點:
- Amazon Elastic Container Registry (Amazon ECR)
- Amazon Simple Storage Service (Amazon S3)
- Amazon Elastic Compute Cloud (Amazon EC2)
- Amazon Virtual Private Cloud (Amazon VPC)
例如,您可使用下列端點:api.ecr.us-east-1.amazonaws.com、dkr.ecr.us-east-1.amazonaws.com、s3.amazonaws.com、s3.us-east-1.amazonaws.com 及 ec2.us-east-1.amazonaws.com。
**重要事項:**您必須將公有端點子網域新增至 NO_PROXY 變數。例如,在 us-east-1 AWS 區域中新增適用於 Amazon S3 的 .s3.us-east-1.amazonaws.com 網域。如果您啟用 Amazon EKS 叢集的端點私有存取權,則必須將 Amazon EKS 端點新增至 NO_PROXY 變數。例如,在 us-east-1 AWS 區域中新增適用於 Amazon EKS 叢集的 .us-east-1.eks.amazonaws.com 網域。
-
確認在 configmap/proxy-environment-variables (由 kube-proxy 和 aws-node Pod 使用) 中的 NO_PROXY 變數包含 Kubernetes 叢集 IP 地址空間。例如,10.100.0.0/16 會在上述 IP 範圍位於 10.100.x.x 內之 ConfigMap 檔案的程式碼範例中使用。
-
套用 ConfigMap:
$ kubectl apply -f /path/to/yaml/proxy-env-vars-config.yaml
- 若要設定 Docker 常駐程式和 kubelet,請將使用者資料插入工作節點。例如:
Content-Type: multipart/mixed; boundary="==BOUNDARY==" MIME-Version: 1.0 --==BOUNDARY== Content-Type: text/cloud-boothook; charset="us-ascii" #Set the proxy hostname and port PROXY="proxy.local:3128" MAC=$(curl -s http://169.254.169.254/latest/meta-data/mac/) VPC_CIDR=$(curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC/vpc-ipv4-cidr-blocks | xargs | tr ' ' ',') #Create the docker systemd directory mkdir -p /etc/systemd/system/docker.service.d #Configure yum to use the proxy cloud-init-per instance yum_proxy_config cat << EOF >> /etc/yum.conf proxy=http://$PROXY EOF #Set the proxy for future processes, and use as an include file cloud-init-per instance proxy_config cat << EOF >> /etc/environment http_proxy=http://$PROXY https_proxy=http://$PROXY HTTP_PROXY=http://$PROXY HTTPS_PROXY=http://$PROXY no_proxy=$VPC_CIDR,localhost,127.0.0.1,169.254.169.254,.internal,s3.amazonaws.com,.s3.us-east-1.amazonaws.com,api.ecr.us-east-1.amazonaws.com,dkr.ecr.us-east-1.amazonaws.com,ec2.us-east-1.amazonaws.com NO_PROXY=$VPC_CIDR,localhost,127.0.0.1,169.254.169.254,.internal,s3.amazonaws.com,.s3.us-east-1.amazonaws.com,api.ecr.us-east-1.amazonaws.com,dkr.ecr.us-east-1.amazonaws.com,ec2.us-east-1.amazonaws.com EOF #Configure docker with the proxy cloud-init-per instance docker_proxy_config tee <<EOF /etc/systemd/system/docker.service.d/proxy.conf >/dev/null [Service] EnvironmentFile=/etc/environment EOF #Configure the kubelet with the proxy cloud-init-per instance kubelet_proxy_config tee <<EOF /etc/systemd/system/kubelet.service.d/proxy.conf >/dev/null [Service] EnvironmentFile=/etc/environment EOF #Reload the daemon and restart docker to reflect proxy configuration at launch of instance cloud-init-per instance reload_daemon systemctl daemon-reload cloud-init-per instance enable_docker systemctl enable --now --no-block docker --==BOUNDARY== Content-Type:text/x-shellscript; charset="us-ascii" #!/bin/bash set -o xtrace #Set the proxy variables before running the bootstrap.sh script set -a source /etc/environment /etc/eks/bootstrap.sh ${ClusterName} ${BootstrapArguments} # Use the cfn-signal only if the node is created through an AWS CloudFormation stack and needs to signal back to an AWS CloudFormation resource (CFN_RESOURCE_LOGICAL_NAME) that waits for a signal from this EC2 instance to progress through either: # - CreationPolicy https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-creationpolicy.html # - UpdatePolicy https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html # cfn-signal will signal back to AWS CloudFormation using https transport, so set the proxy for an HTTPS connection to AWS CloudFormation /opt/aws/bin/cfn-signal --exit-code $? \ --stack ${AWS::StackName} \ --resource CFN_RESOURCE_LOGICAL_NAME \ --region ${AWS::Region} \ --https-proxy $HTTPS_PROXY --==BOUNDARY==--
**重要事項:**您必須更新或建立 yum、Docker 及 kubelet 組態檔案,再啟動 Docker 常駐程式和 kubelet。
如需使用 AWS CloudFormation 範本插入工作節點之使用者資料的範例,請參閱啟動自我管理的 Amazon Linux 節點。
- 更新 aws-node 和 kube-proxy Pod:
$ kubectl patch -n kube-system -p '{ "spec": {"template": { "spec": { "containers": [ { "name": "aws-node", "envFrom": [ { "configMapRef": {"name": "proxy-environment-variables"} } ] } ] } } } }' daemonset aws-node $ kubectl patch -n kube-system -p '{ "spec": {"template":{ "spec": { "containers": [ { "name": "kube-proxy", "envFrom": [ { "configMapRef": {"name": "proxy-environment-variables"} } ] } ] } } } }' daemonset kube-proxy
如果您變更 ConfigMap,請套用更新,並在 Pod 中再次設定 ConfigMap。例如:
$ kubectl set env daemonset/kube-proxy --namespace=kube-system --from=configmap/proxy-environment-variables --containers='*' $ kubectl set env daemonset/aws-node --namespace=kube-system --from=configmap/proxy-environment-variables --containers='*'
**重要事項:**當您升級 Kubernetes 物件 kube-proxy 或 aws-node 後,您必須針對這些物件更新所有 YAML 修改。若要將 ConfigMap 更新為預設值,請使用 eksctl utils update-kube-proxy 或 eksctl utils update-aws-node 命令。
**提示:**如果代理中斷與 API 伺服器的連線,則該代理將會成為單點故障,且叢集行為可能會無法預測。若要防止代理成為單點故障,請在服務探索名稱空間或負載平衡器後方執行代理。
- 確認代理變數會在 kube-proxy 和 aws-node Pod 中使用:
$ kubectl describe pod kube-proxy-xxxx -n kube-system
輸出訊息應類似下列內容:
Environment: HTTPS_PROXY: <set to the key 'HTTPS_PROXY' of config map 'proxy-environment-variables'> Optional: false HTTP_PROXY: <set to the key 'HTTP_PROXY' of config map 'proxy-environment-variables'> Optional: false NO_PROXY: <set to the key 'NO_PROXY' of config map 'proxy-environment-variables'> Optional: false
如果您沒有使用 AWS PrivateLink,請透過適用於 Amazon EC2、Amazon ECR 及 Amazon S3 的代理伺服器驗證 API 端點的存取權。
相關內容
- 已提問 5 個月前lg...
- 已提問 2 天前lg...
- 已提問 1 年前lg...
- 已提問 1 年前lg...
- 已提問 6 個月前lg...
- AWS 官方已更新 8 個月前
- AWS 官方已更新 10 個月前
- AWS 官方已更新 10 個月前
- AWS 官方已更新 6 個月前