跳到正文

Try Out the Cluster ​

After the cluster is created with a Pulumi update, there will be outputs with fields like the cluster's kubeconfig file contents, and its cluster name for reference.

The full code for this stack is on GitHub.

Overview ​

We'll explore how to:

Access the Cluster ​

In EKS, the account caller will be placed into the system:masters Kubernetes RBAC group by default. The kubeconfig generated will be specific to this primary cluster creator use-case, and it must be copied, and reconfigured to use with other IAM roles the caller assumes, as demonstrated in Configure Access Control.

As an Admin ​

Authentication ​

Authenticate as the admins role from the Identity stack.

bash
$ aws sts assume-role --role-arn `pulumi stack output adminsIamRoleArn` --role-session-name k8s-admin

Kubeconfig Setup ​

To access your new Kubernetes cluster using kubectl, we need to setup the kubeconfig file, and export the environment variable for kubectl usage from the Cluster Configuration stack.

Setup the kubeconfig environment variable.

bash
$ export KUBECONFIG=`pwd`/kubeconfig-admin.json

Get the Admins IAM Role ARN.

bash
$ pulumi stack output adminsIamRoleArn
arn:aws:iam::000000000000:role/admins-eksClusterAdmin-0627674

Make a copy of the kubeconfig file that will be edited for the admins to use the adminsIamRoleArn output.

bash
$ pulumi stack output kubeconfig > kubeconfig-admin.json

Edit kubeconfig-admin.json to use a role for authentication in the args of the aws-iam-authenticator, e.g.

bash
...
"users": [
  {
    "name": "aws",
    "user": {
      "exec": {
        "apiVersion": "client.authentication.k8s.io/v1alpha1",
        "args": [
          "token",
          "-i",
          "k8s-aws-cluster-eksCluster-1ef1afe",
          "-r",
          "arn:aws:iam::000000000000:role/admins-eksClusterAdmin-0627674"
        ],
        "command": "aws-iam-authenticator"
      }
    }
  }
]

As a Developer ​

Authentication ​

Authenticate as the devs role from the Identity stack.

bash
$ aws sts assume-role --role-arn `pulumi stack output devsIamRoleArn` --role-session-name k8s-devs

Kubeconfig Setup ​

To access your new Kubernetes cluster using kubectl, we need to setup the kubeconfig file, and export the environment variable for kubectl usage from the Cluster Configuration stack.

Setup the kubeconfig environment variable.

bash
$ export KUBECONFIG=`pwd`/kubeconfig-devs.json

Get the Devs IAM Role ARN.

bash
$ pulumi stack output devsIamRoleArn
arn:aws:iam::000000000000:role/devs-eksClusterDeveloper-e332028

Make a copy of the kubeconfig file that will be edited for the devs to use the devsIamRoleArn output.

bash
$ pulumi stack output kubeconfig > kubeconfig-devs.json

Edit kubeconfig-devs.json to use a role for authentication in the args of the aws-iam-authenticator, e.g.

bash
...
"users": [
  {
    "name": "aws",
    "user": {
      "exec": {
        "apiVersion": "client.authentication.k8s.io/v1alpha1",
        "args": [
          "token",
          "-i",
          "k8s-aws-cluster-eksCluster-1ef1afe",
          "-r",
          "arn:aws:iam::000000000000:role/devs-eksClusterDeveloper-e332028"
        ],
        "command": "aws-iam-authenticator"
      }
    }
  }
]

Query the Cluster ​

Get cluster information.

bash
$ kubectl version
$ kubectl cluster-info

Get the Nodes.

bash
$ kubectl get nodes -o wide --show-labels

Get all Pods in the cluster, and show output attributes.

bash
$ kubectl get pods --all-namespaces -o wide --show-labels

Get all Pods in the designated developer Namespace, and show output attributes.

bash
$ kubectl get pods -n `pulumi stack output appsNamespaceName` -o wide --show-labels

Get the ConfigMaps of the kube-system Namespace.

bash
$ kubectl get cm -n kube-system

Deploy a Workload ​

Declaratively deploy a NGINX Pod and public load-balanced service:

ts
import * as k8s from "@pulumi/kubernetes";

// Expose a k8s provider instance of the cluster.
const provider = new k8s.Provider("provider", {kubeconfig: kubeconfig });

// Create a NGINX Pod
const nginx = new k8s.core.v1.Pod(name,
    {
        metadata: {labels: {app: "nginx"}},
        spec: {
            containers: [
                {
                    name: name,
                    image: "nginx:latest",
                    ports: [{ name: "http", containerPort: 80 }]
                }
            ],
        }
    }, {provider: provider}
);

// Create a LoadBalancer Service for the NGINX Deployment
const service = new k8s.core.v1.Service(name,
    {
        metadata: {labels: {app: "nginx"}},
        spec: {
            type: "LoadBalancer",
            ports: [{ port: 80, targetPort: "http" }],
            selector: {app: "nginx"},
        },
    }, {provider: provider}
);
ts
// Export the Service name and public LoadBalancer Endpoint
export const serviceName = service.metadata.name;
export const serviceHostname = service.status.loadBalancer.ingress[0].hostname;

After a few moments, visit the load balancer listed in the serviceHostname.

bash
$ curl `pulumi stack output serviceHostname`

To tear down NGINX, delete its definition in the Pulumi program and run a Pulumi update.

Learn More ​

See the official Kubernetes Basics tutorial for more details.

文档内容翻译自 Pulumi 官方文档。