

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Tools for PowerShell V4 的 Amazon EKS 範例
<a name="powershell_4_eks_code_examples"></a>

下列程式碼範例示範如何使用 AWS Tools for PowerShell V4 搭配 Amazon EKS 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `Add-EKSResourceTag`
<a name="eks_TagResource_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Add-EKSResourceTag`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會將為資源指定的標籤，與指定的 resourceArn 建立關聯。**  

```
Add-EKSResourceTag -ResourceArn "arn:aws:eks:us-west-2:012345678912:cluster/PROD" -Tag @{Name = "EKSPRODCLUSTER"}
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [TagResource](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSCluster`
<a name="eks_DescribeCluster_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSCluster`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會傳回關於 Amazon EKS 叢集的相關描述性資訊。**  

```
Get-EKSCluster -Name "PROD"
```
**輸出：**  

```
Arn                  : arn:aws:eks:us-west-2:012345678912:cluster/PROD
CertificateAuthority : Amazon.EKS.Model.Certificate
ClientRequestToken   :
CreatedAt            : 12/25/2019 6:46:17 AM
Endpoint             : https://669608765450FBBE54D1D78A3D71B72C.gr8.us-west-2.eks.amazonaws.com
Identity             : Amazon.EKS.Model.Identity
Logging              : Amazon.EKS.Model.Logging
Name                 : PROD
PlatformVersion      : eks.7
ResourcesVpcConfig   : Amazon.EKS.Model.VpcConfigResponse
RoleArn              : arn:aws:iam::012345678912:role/eks-iam-role
Status               : ACTIVE
Tags                 : {}
Version              : 1.14
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeCluster](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSClusterList`
<a name="eks_ListClusters_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSClusterList`。

**Tools for PowerShell V4**  
**範例 1：此 cmdlet 列出指定區域中您 AWS 帳戶 中的 Amazon EKS 叢集。**  

```
Get-EKSClusterList
```
**輸出：**  

```
 PROD
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListClusters](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSFargateProfile`
<a name="eks_DescribeFargateProfile_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSFargateProfile`。

**Tools for PowerShell V4**  
**範例 1：此 cmdlet 會傳回 AWS Fargate 描述檔的描述性資訊。**  

```
Get-EKSFargateProfile -FargateProfileName "EKSFargate" -ClusterName "TEST"
```
**輸出：**  

```
ClusterName         : TEST
CreatedAt           : 12/26/2019 12:34:47 PM
FargateProfileArn   : arn:aws:eks:us-east-2:012345678912:fargateprofile/TEST/EKSFargate/42b7a119-e16b-a279-ce97-bdf303adec92
FargateProfileName  : EKSFargate
PodExecutionRoleArn : arn:aws:iam::012345678912:role/AmazonEKSFargatePodExecutionRole
Selectors           : {Amazon.EKS.Model.FargateProfileSelector}
Status              : ACTIVE
Subnets             : {subnet-0cd976f08d5fbfaae, subnet-02f6ff500ff2067a0}
Tags                : {}
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeFargateProfile](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSFargateProfileList`
<a name="eks_ListFargateProfiles_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSFargateProfileList`。

**Tools for PowerShell V4**  
**範例 1：此 cmdlet 列出與指定 AWS 帳戶 區域中 中指定叢集相關聯的 AWS Fargate 設定檔。**  

```
Get-EKSFargateProfileList -ClusterName "TEST"
```
**輸出：**  

```
EKSFargate
EKSFargateProfile
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListFargateProfiles](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSNodegroup`
<a name="eks_DescribeNodegroup_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSNodegroup`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會傳回 Amazon EKS 節點群組的描述性資訊。**  

```
Get-EKSNodegroup -NodegroupName "ProdEKSNodeGroup" -ClusterName "PROD"
```
**輸出：**  

```
AmiType        : AL2_x86_64
ClusterName    : PROD
CreatedAt      : 12/25/2019 10:16:45 AM
DiskSize       : 40
Health         : Amazon.EKS.Model.NodegroupHealth
InstanceTypes  : {t3.large}
Labels         : {}
ModifiedAt     : 12/25/2019 10:16:45 AM
NodegroupArn   : arn:aws:eks:us-west-2:012345678912:nodegroup/PROD/ProdEKSNodeGroup/7eb79e47-82b6-04d9-e984-95110db6fa85
NodegroupName  : ProdEKSNodeGroup
NodeRole       : arn:aws:iam::012345678912:role/NodeInstanceRole
ReleaseVersion : 1.14.7-20190927
RemoteAccess   :
Resources      :
ScalingConfig  : Amazon.EKS.Model.NodegroupScalingConfig
Status         : CREATING
Subnets        : {subnet-0d1a9fff35efa7691, subnet-0a3f4928edbc224d4}
Tags           : {}
Version        : 1.14
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeNodegroup](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSNodegroupList`
<a name="eks_ListNodegroups_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSNodegroupList`。

**Tools for PowerShell V4**  
**範例 1：此 cmdlet 列出與指定區域中 中指定叢集相關聯的 Amazon EKS AWS 帳戶 節點群組。 **  

```
Get-EKSNodegroupList -ClusterName PROD
```
**輸出：**  

```
 ProdEKSNodeGroup
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListNodegroups](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSResourceTag`
<a name="eks_ListTagsForResource_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSResourceTag`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 列出 Amazon EKS 資源的標籤。**  

```
Get-EKSResourceTag -ResourceArn "arn:aws:eks:us-west-2:012345678912:cluster/PROD"
```
**輸出：**  

```
Key  Value
---  -----
Name EKSPRODCLUSTER
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListTagsForResource](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSUpdate`
<a name="eks_DescribeUpdate_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSUpdate`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會針對您的 Amazon EKS 叢集或關聯的受管節點群組，傳回有關更新的描述性資訊。**  

```
Get-EKSUpdate -Name "PROD" -UpdateId "ee708232-7d2e-4ed7-9270-d0b5176f0726"
```
**輸出：**  

```
CreatedAt : 12/25/2019 5:03:07 PM
Errors    : {}
Id        : ee708232-7d2e-4ed7-9270-d0b5176f0726
Params    : {Amazon.EKS.Model.UpdateParam}
Status    : Successful
Type      : LoggingUpdate
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeUpdate](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-EKSUpdateList`
<a name="eks_ListUpdates_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-EKSUpdateList`。

**Tools for PowerShell V4**  
**範例 1：此 cmdlet 列出與 AWS 帳戶指定區域中 Amazon EKS 叢集或受管節點群組相關聯的更新。**  

```
Get-EKSUpdateList -Name "PROD"
```
**輸出：**  

```
ee708232-7d2e-4ed7-9270-d0b5176f0726
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListUpdates](https://docs.aws.amazon.com/powershell/v4/reference)。

### `New-EKSCluster`
<a name="eks_CreateCluster_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `New-EKSCluster`。

**Tools for PowerShell V4**  
**範例 1：此範例會建立一個稱為 'prod' 的新叢集。**  

```
New-EKSCluster -Name prod -ResourcesVpcConfig @{SubnetIds=@("subnet-0a1b2c3d","subnet-3a2b1c0d");SecurityGroupIds="sg-6979fe18"} -RoleArn "arn:aws:iam::012345678901:role/eks-service-role"
```
**輸出：**  

```
Arn                  : arn:aws:eks:us-west-2:012345678901:cluster/prod
CertificateAuthority : Amazon.EKS.Model.Certificate
ClientRequestToken   :
CreatedAt            : 12/10/2018 9:25:31 PM
Endpoint             :
Name                 : prod
PlatformVersion      : eks.3
ResourcesVpcConfig   : Amazon.EKS.Model.VpcConfigResponse
RoleArn              : arn:aws:iam::012345678901:role/eks-service-role
Status               : CREATING
Version              : 1.10
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [CreateCluster](https://docs.aws.amazon.com/powershell/v4/reference)。

### `New-EKSFargateProfile`
<a name="eks_CreateFargateProfile_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `New-EKSFargateProfile`。

**Tools for PowerShell V4**  
**範例 1：此 cmdlet 會為您的 Amazon EKS 叢集建立 AWS Fargate 設定檔。您必須在叢集中至少有一個 Fargate 設定檔，才能在 Fargate 基礎設施上排定 Pod。**  

```
New-EKSFargateProfile -FargateProfileName EKSFargateProfile -ClusterName TEST -Subnet "subnet-02f6ff500ff2067a0", "subnet-0cd976f08d5fbfaae" -PodExecutionRoleArn arn:aws:iam::012345678912:role/AmazonEKSFargatePodExecutionRole -Selector @{Namespace="default"}
```
**輸出：**  

```
ClusterName         : TEST
CreatedAt           : 12/26/2019 12:38:21 PM
FargateProfileArn   : arn:aws:eks:us-east-2:012345678912:fargateprofile/TEST/EKSFargateProfile/20b7a11b-8292-41c1-bc56-ffa5e60f6224
FargateProfileName  : EKSFargateProfile
PodExecutionRoleArn : arn:aws:iam::012345678912:role/AmazonEKSFargatePodExecutionRole
Selectors           : {Amazon.EKS.Model.FargateProfileSelector}
Status              : CREATING
Subnets             : {subnet-0cd976f08d5fbfaae, subnet-02f6ff500ff2067a0}
Tags                : {}
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [CreateFargateProfile](https://docs.aws.amazon.com/powershell/v4/reference)。

### `New-EKSNodeGroup`
<a name="eks_CreateNodegroup_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `New-EKSNodeGroup`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會為 Amazon EKS 叢集建立受管工作節點群組。您只能為叢集建立一個等於叢集目前 Kubernetes 版本的節點群組。針對叢集各自的次要 Kubernetes 版本，使用最新 AMI 發行版本建立所有節點群組。**  

```
New-EKSNodeGroup -NodeGroupName "ProdEKSNodeGroup" -AmiType "AL2_x86_64" -DiskSize 40 -ClusterName "PROD" -ScalingConfig_DesiredSize 2 -ScalingConfig_MinSize 2 -ScalingConfig_MaxSize 5 -InstanceType t3.large -NodeRole "arn:aws:iam::012345678912:role/NodeInstanceRole" -Subnet "subnet-0d1a9fff35efa7691","subnet-0a3f4928edbc224d4"
```
**輸出：**  

```
AmiType        : AL2_x86_64
ClusterName    : PROD
CreatedAt      : 12/25/2019 10:16:45 AM
DiskSize       : 40
Health         : Amazon.EKS.Model.NodegroupHealth
InstanceTypes  : {t3.large}
Labels         : {}
ModifiedAt     : 12/25/2019 10:16:45 AM
NodegroupArn   : arn:aws:eks:us-west-2:012345678912:nodegroup/PROD/ProdEKSNodeGroup/7eb79e47-82b6-04d9-e984-95110db6fa85
NodegroupName  : ProdEKSNodeGroup
NodeRole       : arn:aws:iam::012345678912:role/NodeInstanceRole
ReleaseVersion : 1.14.7-20190927
RemoteAccess   :
Resources      :
ScalingConfig  : Amazon.EKS.Model.NodegroupScalingConfig
Status         : CREATING
Subnets        : {subnet-0d1a9fff35efa7691, subnet-0a3f4928edbc224d4}
Tags           : {}
Version        : 1.14
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [CreateNodegroup](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-EKSCluster`
<a name="eks_DeleteCluster_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-EKSCluster`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會刪除 Amazon EKS 叢集控制平面。**  

```
Remove-EKSCluster -Name "DEV-KUBE-CL"
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-EKSCluster (DeleteCluster)" on target "DEV-KUBE-CL".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y

Arn                  : arn:aws:eks:us-west-2:012345678912:cluster/DEV-KUBE-CL
CertificateAuthority : Amazon.EKS.Model.Certificate
ClientRequestToken   :
CreatedAt            : 12/25/2019 9:33:25 AM
Endpoint             : https://02E6D31E3E4F8C15D7BE7F58D527776A.yl4.us-west-2.eks.amazonaws.com
Identity             : Amazon.EKS.Model.Identity
Logging              : Amazon.EKS.Model.Logging
Name                 : DEV-KUBE-CL
PlatformVersion      : eks.7
ResourcesVpcConfig   : Amazon.EKS.Model.VpcConfigResponse
RoleArn              : arn:aws:iam::012345678912:role/eks-iam-role
Status               : DELETING
Tags                 : {}
Version              : 1.14
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteCluster](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-EKSFargateProfile`
<a name="eks_DeleteFargateProfile_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-EKSFargateProfile`。

**Tools for PowerShell V4**  
**範例 1：此 cmdlet 會刪除 AWS Fargate 設定檔。當您刪除 Fargate 設定檔時，使用該設定檔建立的所有在 Fargate 上執行的 Pod，都會遭到刪除。**  

```
Remove-EKSFargateProfile -FargateProfileName "EKSFargate" -ClusterName "TEST"
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-EKSFargateProfile (DeleteFargateProfile)" on target "EKSFargate".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y

ClusterName         : TEST
CreatedAt           : 12/26/2019 12:34:47 PM
FargateProfileArn   : arn:aws:eks:us-east-2:012345678912:fargateprofile/TEST/EKSFargate/42b7a119-e16b-a279-ce97-bdf303adec92
FargateProfileName  : EKSFargate
PodExecutionRoleArn : arn:aws:iam::012345678912:role/AmazonEKSFargatePodExecutionRole
Selectors           : {Amazon.EKS.Model.FargateProfileSelector}
Status              : DELETING
Subnets             : {subnet-0cd976f08d5fbfaae, subnet-02f6ff500ff2067a0}
Tags                : {}
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteFargateProfile](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-EKSNodegroup`
<a name="eks_DeleteNodegroup_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-EKSNodegroup`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會刪除叢集的 Amazon EKS 節點群組。**  

```
Remove-EKSNodegroup -NodegroupName "ProdEKSNodeGroup" -ClusterName "PROD"
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-EKSNodegroup (DeleteNodegroup)" on target "ProdEKSNodeGroup".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y

AmiType        : AL2_x86_64
ClusterName    : PROD
CreatedAt      : 12/25/2019 10:16:45 AM
DiskSize       : 40
Health         : Amazon.EKS.Model.NodegroupHealth
InstanceTypes  : {t3.large}
Labels         : {}
ModifiedAt     : 12/25/2019 11:01:16 AM
NodegroupArn   : arn:aws:eks:us-west-2:012345678912:nodegroup/PROD/ProdEKSNodeGroup/7eb79e47-82b6-04d9-e984-95110db6fa85
NodegroupName  : ProdEKSNodeGroup
NodeRole       : arn:aws:iam::012345678912:role/NodeInstanceRole
ReleaseVersion : 1.14.7-20190927
RemoteAccess   :
Resources      : Amazon.EKS.Model.NodegroupResources
ScalingConfig  : Amazon.EKS.Model.NodegroupScalingConfig
Status         : DELETING
Subnets        : {subnet-0d1a9fff35efa7691, subnet-0a3f4928edbc224d4}
Tags           : {}
Version        : 1.14
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteNodegroup](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-EKSResourceTag`
<a name="eks_UntagResource_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-EKSResourceTag`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會從 EKS 資源刪除指定的標籤。**  

```
Remove-EKSResourceTag -ResourceArn "arn:aws:eks:us-west-2:012345678912:cluster/PROD" -TagKey "Name"
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-EKSResourceTag (UntagResource)" on target "arn:aws:eks:us-west-2:012345678912:cluster/PROD".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [UntagResource](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Update-EKSClusterConfig`
<a name="eks_UpdateClusterConfig_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Update-EKSClusterConfig`。

**Tools for PowerShell V4**  
**範例 1：更新 Amazon EKS 叢集組態。您的叢集會在更新期間繼續運作。**  

```
Update-EKSClusterConfig -Name "PROD" -Logging_ClusterLogging @{Types="api","audit","authenticator","controllerManager","scheduler",Enabled="True"}
```
**輸出：**  

```
CreatedAt : 12/25/2019 5:03:07 PM
Errors    : {}
Id        : ee708232-7d2e-4ed7-9270-d0b5176f0726
Params    : {Amazon.EKS.Model.UpdateParam}
Status    : InProgress
Type      : LoggingUpdate
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [UpdateClusterConfig](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Update-EKSClusterVersion`
<a name="eks_UpdateClusterVersion_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Update-EKSClusterVersion`。

**Tools for PowerShell V4**  
**範例 1：此 Cmdlet 會將 Amazon EKS 叢集更新為指定的 Kubernetes 版本。您的叢集會在更新期間繼續運作。**  

```
Update-EKSClusterVersion -Name "PROD-KUBE-CL" -Version 1.14
```
**輸出：**  

```
CreatedAt : 12/26/2019 9:50:37 AM
Errors    : {}
Id        : ef186eff-3b3a-4c25-bcfc-3dcdf9e898a8
Params    : {Amazon.EKS.Model.UpdateParam, Amazon.EKS.Model.UpdateParam}
Status    : InProgress
Type      : VersionUpdate
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [UpdateClusterVersion](https://docs.aws.amazon.com/powershell/v4/reference)。