2019-11-09 11:18:49 -08:00
---
2023-06-02 21:41:36 -07:00
title_tag: Access Created Kubernetes Cluster | Crosswalk
2019-12-18 09:59:20 -08:00
meta_desc: This page provides a guide on how to try out a newly created Kubernetes cluster.
2023-05-15 15:25:28 -07:00
title: Access clusters
h1: Accessing Kubernetes clusters
2023-06-08 16:15:52 -07:00
meta_image: /images/docs/meta-images/docs-clouds-kubernetes-meta-image.png
2019-11-07 21:55:03 -08:00
menu:
2023-05-15 15:25:28 -07:00
clouds:
parent: kubernetes-guides
identifier: kubernetes-guides-access-cluster
2019-11-16 12:40:48 -08:00
weight: 4
2023-05-15 15:25:28 -07:00
aliases:
- /docs/guides/crosswalk/kubernetes/try-out-the-cluster/
2019-11-07 21:55:03 -08:00
---
2020-03-23 21:06:03 -07:00
{{< chooser cloud " aws , azure , gcp " / > }}
2019-11-07 21:55:03 -08:00
After the cluster is created with a Pulumi update, there will be
2023-05-15 15:25:28 -07:00
[outputs ](/docs/concepts/inputs-outputs/ ) with fields like the cluster's `kubeconfig` file
2019-11-07 21:55:03 -08:00
contents, and its cluster name for reference.
2020-03-20 09:32:19 -07:00
{{% choosable cloud aws %}}
2019-11-07 22:05:53 -08:00
The full code for this stack is on [GitHub][gh-repo-stack].
2019-11-13 09:53:50 -08:00
[gh-repo-stack]: https://github.com/pulumi/kubernetes-guides/tree/master/aws/03-cluster-configuration
2019-11-07 22:05:53 -08:00
2020-03-20 09:32:19 -07:00
{{% /choosable %}}
{{% choosable cloud azure %}}
2019-11-07 22:05:53 -08:00
The full code for this stack is on [GitHub][gh-repo-stack].
2019-11-13 09:53:50 -08:00
[gh-repo-stack]: https://github.com/pulumi/kubernetes-guides/tree/master/azure/03-cluster-configuration
2019-11-07 22:05:53 -08:00
2020-03-20 09:32:19 -07:00
{{% /choosable %}}
{{% choosable cloud gcp %}}
2019-11-07 22:05:53 -08:00
The full code for this stack is on [GitHub][gh-repo-stack].
2019-11-13 09:53:50 -08:00
[gh-repo-stack]: https://github.com/pulumi/kubernetes-guides/tree/master/gcp/03-cluster-configuration
2019-11-07 22:05:53 -08:00
2020-03-20 09:32:19 -07:00
{{% /choosable %}}
2019-11-07 22:05:53 -08:00
2019-11-09 10:11:59 -08:00
## Overview
We'll explore how to:
2019-12-18 09:59:20 -08:00
* [Access the Cluster ](#access-the-cluster )
* [Query the Cluster ](#query-the-cluster )
* [Deploy a Workload ](#deploy-a-workload )
* [Learn More ](#learn-more )
2019-11-09 10:11:59 -08:00
2019-11-12 11:09:27 -08:00
## Access the Cluster
2019-11-07 21:55:03 -08:00
2020-03-20 09:32:19 -07:00
{{% choosable cloud aws %}}
2019-11-07 21:55:03 -08:00
In EKS, the account caller will be placed into the
`system:masters` Kubernetes RBAC group by default. The `kubeconfig`
2019-11-12 10:30:40 -08:00
generated will be specific to this primary cluster creator use-case, and it must be
2019-11-07 21:55:03 -08:00
copied, and reconfigured to use with other IAM roles the caller assumes, as
demonstrated in [Configure Access Control][crosswalk-configure-access].
2019-12-18 09:59:20 -08:00
<!-- markdownlint - disable no - duplicate - heading -->
### As an Admin
2019-11-07 22:05:53 -08:00
2019-12-18 09:59:20 -08:00
#### Authentication
2019-11-07 21:55:03 -08:00
Authenticate as the `admins` role from the [Identity][aws-admin-identity-stack] stack.
```bash
$ aws sts assume-role --role-arn `pulumi stack output adminsIamRoleArn` --role-session-name k8s-admin
```
2019-12-18 09:59:20 -08:00
#### Kubeconfig Setup
2019-11-07 21:55:03 -08:00
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][aws-cluster-config-stack] stack.
2019-11-09 10:35:52 -08:00
Setup the kubeconfig environment variable.
```bash
$ export KUBECONFIG=`pwd` /kubeconfig-admin.json
```
Get the Admins IAM Role ARN.
2019-11-07 21:55:03 -08:00
```bash
$ pulumi stack output adminsIamRoleArn
arn:aws:iam::000000000000:role/admins-eksClusterAdmin-0627674
```
2019-11-12 10:30:40 -08:00
Make a copy of the kubeconfig file that will be edited for the `admins` to use the
2019-11-07 21:55:03 -08:00
`adminsIamRoleArn` output.
```bash
2019-11-08 01:00:31 -08:00
$ pulumi stack output kubeconfig > kubeconfig-admin.json
2019-11-07 21:55:03 -08:00
```
Edit `kubeconfig-admin.json` to use a role for authentication in the
`args` of the [`aws-iam-authenticator` ][aws-iam-auth], 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"
}
}
}
]
```
2019-12-18 09:59:20 -08:00
### As a Developer
2019-11-08 01:00:31 -08:00
2019-12-18 09:59:20 -08:00
#### Authentication
2019-11-07 21:55:03 -08:00
Authenticate as the `devs` role from the [Identity][aws-devs-identity-stack] stack.
```bash
$ aws sts assume-role --role-arn `pulumi stack output devsIamRoleArn` --role-session-name k8s-devs
```
2019-12-18 09:59:20 -08:00
#### Kubeconfig Setup
2019-11-07 21:55:03 -08:00
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][aws-cluster-config-stack] stack.
2019-11-09 10:35:52 -08:00
Setup the kubeconfig environment variable.
```bash
$ export KUBECONFIG=`pwd` /kubeconfig-devs.json
```
Get the Devs IAM Role ARN.
2019-11-07 21:55:03 -08:00
```bash
$ pulumi stack output devsIamRoleArn
arn:aws:iam::000000000000:role/devs-eksClusterDeveloper-e332028
```
2019-11-12 10:30:40 -08:00
Make a copy of the kubeconfig file that will be edited for the `devs` to use the
2019-11-07 23:06:58 -08:00
`devsIamRoleArn` output.
2019-11-07 21:55:03 -08:00
```bash
2019-11-08 01:00:31 -08:00
$ pulumi stack output kubeconfig > kubeconfig-devs.json
2019-11-07 21:55:03 -08:00
```
2019-11-07 23:06:58 -08:00
Edit `kubeconfig-devs.json` to use a role for authentication in the
2019-11-07 21:55:03 -08:00
`args` of the [`aws-iam-authenticator` ][aws-iam-auth], 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"
}
}
}
]
```
2019-12-18 09:59:20 -08:00
<!-- markdownlint - disable url -->
2019-11-07 21:55:03 -08:00
[aws-iam-auth]: https://docs.aws.amazon.com/eks/latest/userguide/install-aws-iam-authenticator.html
2023-05-15 15:25:28 -07:00
[aws-admin-identity-stack]: /docs/clouds/kubernetes/guides/identity/#create -an-iam-role-for-admins
[aws-devs-identity-stack]: /docs/clouds/kubernetes/guides/identity/#create -an-iam-role-for-developers
2019-11-13 09:53:50 -08:00
[aws-cluster-config-stack]: https://github.com/pulumi/kubernetes-guides/tree/master/aws/03-cluster-configuration
2023-05-15 15:25:28 -07:00
[crosswalk-configure-access]: /docs/clouds/kubernetes/guides/configure-access-control
2019-12-18 09:59:20 -08:00
<!-- markdownlint - enable url -->
2020-03-20 09:32:19 -07:00
{{% /choosable %}}
{{% choosable cloud azure %}}
2019-11-07 21:55:03 -08:00
In AKS, the account caller will be placed into the
`system:masters` Kubernetes RBAC group by default. Two `kubeconfig` files will
2019-11-12 10:30:40 -08:00
be generated that will be specific to the admin and cluster user [use-cases][aks-cluster-roles].
2019-11-07 21:55:03 -08:00
To configure the cluster for use with IAM roles, check out
[Configure Access Control][crosswalk-configure-access].
#### Authentication
Authenticate as the ServicePrincipal from the [Identity][azure-identity-stack] stack.
```bash
$ az login --service-principal --username $ARM_CLIENT_ID --password $ARM_CLIENT_SECRET --tenant $ARM_TENANT_ID
```
#### Admin Kubeconfig Setup
To access your new Kubernetes cluster using `kubectl` , we need to setup the
`kubeconfig` file.
```bash
$ pulumi stack output kubeconfigAdmin > kubeconfig-admin.json
$ export KUBECONFIG=`pwd` /kubeconfig-admin.json
```
#### Developers Kubeconfig Setup
To access your new Kubernetes cluster using `kubectl` , we need to setup the
`kubeconfig` file.
```bash
$ pulumi stack output kubeconfig > kubeconfig-devs.json
$ export KUBECONFIG=`pwd` /kubeconfig-devs.json
```
2019-12-18 09:59:20 -08:00
<!-- markdownlint - disable url -->
2023-05-15 15:25:28 -07:00
[azure-identity-stack]: /docs/clouds/kubernetes/guides/identity/#prerequisites
2019-11-07 21:55:03 -08:00
[aks-cluster-roles]: https://docs.microsoft.com/en-us/azure/aks/control-kubeconfig-access#available -cluster-roles-permissions
2023-05-15 15:25:28 -07:00
[crosswalk-configure-access]: /docs/clouds/kubernetes/guides/configure-access-control/
2019-12-18 09:59:20 -08:00
<!-- markdownlint - enable url -->
2020-03-20 09:32:19 -07:00
{{% /choosable %}}
{{% choosable cloud gcp %}}
2023-05-15 15:25:28 -07:00
In Google Cloud, the account caller will be placed into the
2019-11-07 21:55:03 -08:00
`system:masters` Kubernetes RBAC group by default. The `kubeconfig`
2019-11-12 10:30:40 -08:00
generated will be specific to this primary cluster creator use-case.
2019-11-07 22:05:53 -08:00
2023-05-15 15:25:28 -07:00
Google Cloud authentication will use tokens to operate as Members such as Users or ServiceAccounts,
2019-11-07 22:05:53 -08:00
and with certain permissions as detailed in [Configure Access Control][crosswalk-configure-access].
2019-11-07 21:55:03 -08:00
#### Admin Authentication
Authenticate as the `admins` ServiceAccount from the [Identity][gcp-admin-identity-stack] stack.
```bash
$ pulumi stack output adminsIamServiceAccountSecret > k8s-admin-sa-key.json
$ gcloud auth activate-service-account --key-file k8s-admin-sa-key.json
```
#### Developer Authentication
Authenticate as the `devs` ServiceAccount from the [Identity][gcp-devs-identity-stack] stack.
```bash
$ pulumi stack output devsIamServiceAccountSecret > k8s-devs-sa-key.json
$ gcloud auth activate-service-account --key-file k8s-devs-sa-key.json
```
2019-11-07 22:05:53 -08:00
#### Kubeconfig Setup
2019-11-07 21:55:03 -08:00
To access your new Kubernetes cluster using `kubectl` , we need to setup the
`kubeconfig` file, and export the environment variable for `kubectl` usage.
```bash
$ pulumi stack output --show-secrets kubeconfig > kubeconfig.json
$ export KUBECONFIG=`pwd` /kubeconfig.json
```
2023-05-15 15:25:28 -07:00
[gcp-admin-identity-stack]: /docs/clouds/kubernetes/guides/identity/#create -an-iam-role-and-serviceaccount-for-admins
[gcp-devs-identity-stack]: /docs/clouds/kubernetes/guides/identity/#create -an-iam-role-and-serviceaccount-for-developers
[crosswalk-configure-access]: /docs/clouds/kubernetes/guides/configure-access-control
2020-03-20 09:32:19 -07:00
{{% /choosable %}}
2019-12-18 09:59:20 -08:00
<!-- markdownlint - enable no - duplicate - heading -->
2019-11-07 21:55:03 -08:00
2019-11-12 11:09:27 -08:00
## Query the Cluster
2019-11-07 21:55:03 -08:00
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
```
2019-11-12 11:09:27 -08:00
## Deploy a Workload
2019-11-07 21:55:03 -08:00
2021-03-14 07:14:21 -07:00
{{< chooser k8s-language " typescript , yaml " / > }}
2020-03-20 09:32:19 -07:00
{{% choosable k8s-language yaml %}}
2019-11-07 21:55:03 -08:00
Imperatively deploy a NGINX Pod and public load-balanced service:
```bash
$ kubectl run --generator=run-pod/v1 nginx --image=nginx --port=80 --expose --service-overrides='{"spec":{"type":"LoadBalancer"}}'
```
2019-11-12 10:30:40 -08:00
After a few moments once it is deployed, visit the load balancer URL.
2019-11-07 21:55:03 -08:00
2021-03-14 07:14:21 -07:00
{{< choosable cloud aws > }}
2019-11-07 21:55:03 -08:00
```bash
$ if ING_LB=$((kubectl get svc nginx -o template --template='{{(index .status.loadBalancer.ingress 0).hostname}}') 2>& 1) ; then echo "http://$ING_LB"; else echo "LB is not ready yet."; fi
```
2021-03-14 07:14:21 -07:00
{{< / choosable > }}
2019-11-07 21:55:03 -08:00
2021-03-14 07:14:21 -07:00
{{< choosable cloud azure > }}
2019-11-07 21:55:03 -08:00
```bash
$ if ING_LB=$((kubectl get svc nginx -o template --template='{{(index .status.loadBalancer.ingress 0).ip}}') 2>& 1) ; then echo "http://$ING_LB"; else echo "LB is not ready yet."; fi
```
2021-03-14 07:14:21 -07:00
{{< / choosable > }}
2019-11-07 21:55:03 -08:00
2021-03-14 07:14:21 -07:00
{{< choosable cloud gcp > }}
2019-11-07 21:55:03 -08:00
```bash
$ if ING_LB=$((kubectl get svc nginx -o template --template='{{(index .status.loadBalancer.ingress 0).ip}}') 2>& 1) ; then echo "http://$ING_LB"; else echo "LB is not ready yet."; fi
```
2021-03-14 07:14:21 -07:00
{{< / choosable > }}
2019-11-07 21:55:03 -08:00
Delete the pod and service.
```bash
$ kubectl delete pod/nginx svc/nginx
```
2019-12-18 09:59:20 -08:00
2020-03-20 09:32:19 -07:00
{{% /choosable %}}
{{% choosable k8s-language typescript %}}
2019-11-07 21:55:03 -08:00
2019-11-08 21:31:35 -08:00
Declaratively deploy a NGINX Pod and public load-balanced service:
2019-11-07 21:55:03 -08:00
```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}
);
```
2021-03-14 07:14:21 -07:00
{{< choosable cloud aws > }}
2020-03-20 09:32:19 -07:00
2019-11-07 21:55:03 -08:00
```ts
// Export the Service name and public LoadBalancer Endpoint
2019-11-08 21:31:35 -08:00
export const serviceName = service.metadata.name;
export const serviceHostname = service.status.loadBalancer.ingress[0].hostname;
2019-11-07 21:55:03 -08:00
```
After a few moments, visit the load balancer listed in the `serviceHostname` .
```bash
$ curl `pulumi stack output serviceHostname`
```
2021-03-14 07:14:21 -07:00
{{< / choosable > }}
2020-03-20 09:32:19 -07:00
2021-03-14 07:14:21 -07:00
{{< choosable cloud azure > }}
2020-03-20 09:32:19 -07:00
2019-11-07 21:55:03 -08:00
```ts
// Export the Service name and public LoadBalancer Endpoint
2019-11-08 21:31:35 -08:00
export const serviceName = service.metadata.name;
export const serviceIp = service.status.loadBalancer.ingress[0].ip;
2019-11-07 21:55:03 -08:00
```
After a few moments, visit the load balancer listed in the `serviceIp` .
```bash
$ curl `pulumi stack output serviceIp`
```
2021-03-14 07:14:21 -07:00
{{< / choosable > }}
2020-03-20 09:32:19 -07:00
2021-03-14 07:14:21 -07:00
{{< choosable cloud gcp > }}
2020-03-20 09:32:19 -07:00
2019-11-07 21:55:03 -08:00
```ts
// Export the Service name and public LoadBalancer Endpoint
2019-11-08 21:31:35 -08:00
export const serviceName = service.metadata.name;
export const serviceIp = service.status.loadBalancer.ingress[0].ip;
2019-11-07 21:55:03 -08:00
```
After a few moments, visit the load balancer listed in the `serviceIp` .
```bash
$ curl `pulumi stack output serviceIp`
```
2021-03-14 07:14:21 -07:00
{{< / choosable > }}
2020-03-20 09:32:19 -07:00
2019-11-08 21:31:35 -08:00
To tear down NGINX, delete its definition in the Pulumi program and run a Pulumi update.
2020-03-20 09:32:19 -07:00
{{% /choosable %}}
2019-11-07 21:55:03 -08:00
2019-11-12 11:09:27 -08:00
## Learn More
2019-11-07 21:55:03 -08:00
2020-01-16 11:46:15 -05:00
See the official [Kubernetes Basics ](https://kubernetes.io/docs/tutorials/kubernetes-basics/ ) tutorial for more details.