docs: kubernetes: support kind/gke/eks

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-06-03 20:34:18 -07:00 committed by Sam Alba
parent 4221011064
commit 4554a216f1
2 changed files with 208 additions and 14 deletions

View File

@ -3,3 +3,6 @@ default: true
# MD013/line-length - Line length # MD013/line-length - Line length
MD013: false MD013: false
# MD033 - Inline HTML. Needed for tabs in docusaurus
MD033: false

View File

@ -1,15 +1,27 @@
# Kubernetes # Kubernetes
This example illustrates how to use `dagger` to build, push and deploy Docker This tutorial illustrates how to use `dagger` to build, push and deploy Docker
images to Kubernetes. images to Kubernetes.
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
## Prerequisites ## Prerequisites
### Setup a local Kubernetes cluster For this tutorial, you will need a Kubernetes cluster.
While dagger supports GKE and EKS, for the purpose of this example, we'll be <Tabs
using [kind](https://kind.sigs.k8s.io/) to install a local Kubernetes cluster defaultValue="kind"
in addition to a local container registry, no cloud account required. groupId="provider"
values={[
{label: 'kind', value: 'kind'},
{label: 'GKE', value: 'gke'},
{label: 'EKS', value: 'eks'},
]}>
<TabItem value="kind">
[Kind](https://kind.sigs.k8s.io/docs/user/quick-start) is a tool for running local Kubernetes clusters using Docker.
1\. Install kind 1\. Install kind
@ -47,6 +59,23 @@ EOF
docker network connect kind registry docker network connect kind registry
``` ```
</TabItem>
<TabItem value="gke">
This tutorial can be run against a [GCP GKE](https://cloud.google.com/kubernetes-engine) cluster and [GCR](https://cloud.google.com/container-registry
)
</TabItem>
<TabItem value="eks">
This tutorial can be run against a [AWS EKS](https://aws.amazon.com/eks/
) cluster and [ECR](https://aws.amazon.com/ecr/)
</TabItem>
</Tabs>
## Initialize a Dagger Workspace and Environment ## Initialize a Dagger Workspace and Environment
```shell ```shell
@ -57,13 +86,6 @@ dagger new default
## Create a basic plan ## Create a basic plan
<!-- `.dagger/env/default/plan`
- `manifest.cue`:
- `deploy.cue`:
[Stateless Application](https://kubernetes.io/docs/tasks/run-application/run-stateless-application-deployment/) -->
Create a file named `.dagger/env/default/plan/manifest.cue` and add the Create a file named `.dagger/env/default/plan/manifest.cue` and add the
following configuration to it. following configuration to it.
@ -101,6 +123,17 @@ used to create a _nginx_ deployment.
Next, create `.dagger/env/default/plan/main.cue`. Next, create `.dagger/env/default/plan/main.cue`.
<Tabs
defaultValue="kind"
groupId="provider"
values={[
{label: 'kind', value: 'kind'},
{label: 'GKE', value: 'gke'},
{label: 'EKS', value: 'eks'},
]}>
<TabItem value="kind">
```cue title=".dagger/env/default/plan/main.cue" ```cue title=".dagger/env/default/plan/main.cue"
package main package main
@ -114,7 +147,7 @@ kubeconfig: string @dagger(input)
// deploy uses the `dagger.io/kubernetes` package to apply a manifest to a // deploy uses the `dagger.io/kubernetes` package to apply a manifest to a
// Kubernetes cluster. // Kubernetes cluster.
deploy: kubernetes.#Apply & { deploy: kubernetes.#Resources & {
// reference the `kubeconfig` input above // reference the `kubeconfig` input above
"kubeconfig": kubeconfig "kubeconfig": kubeconfig
@ -123,6 +156,65 @@ deploy: kubernetes.#Apply & {
} }
``` ```
</TabItem>
<TabItem value="gke">
```cue title=".dagger/env/default/plan/main.cue"
package main
import (
"dagger.io/kubernetes"
"dagger.io/gcp/gke"
)
// gkeConfig used for deployment
gkeConfig: gke.#KubeConfig @dagger(input)
kubeconfig: gkeConfig.kubeconfig
// deploy uses the `dagger.io/kubernetes` package to apply a manifest to a
// Kubernetes cluster.
deploy: kubernetes.#Resources & {
// reference the `kubeconfig` input above
"kubeconfig": kubeconfig
// reference to the manifest defined in `manifest.cue`
"manifest": manifest
}
```
</TabItem>
<TabItem value="eks">
```cue title=".dagger/env/default/plan/main.cue"
package main
import (
"dagger.io/kubernetes"
"dagger.io/aws/eks"
)
// eksConfig used for deployment
eksConfig: eks.#KubeConfig @dagger(input)
kubeconfig: eksConfig.kubeconfig
// deploy uses the `dagger.io/kubernetes` package to apply a manifest to a
// Kubernetes cluster.
deploy: kubernetes.#Resources & {
// reference the `kubeconfig` input above
"kubeconfig": kubeconfig
// reference to the manifest defined in `manifest.cue`
"manifest": manifest
}
```
</TabItem>
</Tabs>
This defines: This defines:
- `kubeconfig` a _string_ **input**: kubernetes configuration (`~/.kube/config`) - `kubeconfig` a _string_ **input**: kubernetes configuration (`~/.kube/config`)
@ -142,6 +234,38 @@ $ dagger up
You can inspect the list of inputs (both required and optional) using `dagger input list`: You can inspect the list of inputs (both required and optional) using `dagger input list`:
<!--
<Tabs
defaultValue="kind"
groupId="provider"
values={[
{label: 'kind', value: 'kind'},
{label: 'GKE', value: 'gke'},
{label: 'EKS', value: 'eks'},
]}>
<TabItem value="kind">
</TabItem>
<TabItem value="gke">
</TabItem>
<TabItem value="eks">
</TabItem>
</Tabs>
-->
<Tabs
defaultValue="kind"
groupId="provider"
values={[
{label: 'kind', value: 'kind'},
{label: 'GKE', value: 'gke'},
{label: 'EKS', value: 'eks'},
]}>
<TabItem value="kind">
```shell ```shell
$ dagger input list $ dagger input list
Input Type Description Input Type Description
@ -149,13 +273,80 @@ kubeconfig string ~/.kube/config file used for deployment
deploy.namespace string Kubernetes Namespace to deploy to deploy.namespace string Kubernetes Namespace to deploy to
``` ```
Let's provide the missing input: </TabItem>
<TabItem value="gke">
```shell
$ dagger input list
Input Type Description
deploy.namespace string Kubernetes Namespace to deploy to
gkeConfig.config.region string GCP region
gkeConfig.config.project string GCP project
gkeConfig.config.serviceKey dagger.#Secret GCP service key
gkeConfig.clusterName string GKE cluster name
```
</TabItem>
<TabItem value="eks">
```shell
$ dagger input list
Input Type Description
deploy.namespace string Kubernetes Namespace to deploy to
eksConfig.config.region string AWS region
eksConfig.config.accessKey dagger.#Secret AWS access key
eksConfig.config.secretKey dagger.#Secret AWS secret key
eksConfig.clusterName string EKS cluster name
```
</TabItem>
</Tabs>
Let's provide the missing inputs:
<Tabs
defaultValue="kind"
groupId="provider"
values={[
{label: 'kind', value: 'kind'},
{label: 'GKE', value: 'gke'},
{label: 'EKS', value: 'eks'},
]}>
<TabItem value="kind">
```shell ```shell
# we'll use the ~/.kube/config created by `kind` # we'll use the ~/.kube/config created by `kind`
dagger input text kubeconfig -f ~/.kube/config dagger input text kubeconfig -f ~/.kube/config
``` ```
</TabItem>
<TabItem value="gke">
```shell
dagger input text gkeConfig.config.project <PROJECT>
dagger input text gkeConfig.config.region <REGION>
dagger input text gkeConfig.clusterName <GKE CLUSTER NAME>
dagger input secret gkeConfig.config.serviceKey -f <PATH TO THE SERVICEKEY.json>
```
</TabItem>
<TabItem value="eks">
```shell
dagger input text eksConfig.config.region <REGION>
dagger input text eksConfig.clusterName <EKS CLUSTER NAME>
dagger input secret eksConfig.config.accessKey <ACCESS KEY>
dagger input secret eksConfig.config.secretKey <SECRET KEY>
```
</TabItem>
</Tabs>
### Deploying ### Deploying
Now is time to deploy to kubernetes. Now is time to deploy to kubernetes.