UNCLASSIFIED

You need to sign in or sign up before continuing.
README.md 6.65 KB
Newer Older
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
1 2
# k3d-dev-env

3
Deploy a multi-node k8s cluster on an AWS EC2 instance using terraform, ansible, and k3d; primarily for deploying the bigbang baseline.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
4

5
# Requirements
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
6

7
## Supported Systems
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
8 9 10 11

Other systems may be used, but documentation is written for the following:
* MacOS

12
## Install Packages
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
13 14 15 16

Install the required packages (MacOS):

```
17
brew install awscli terraform ansible kubectl
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
18 19
```

20
## AWS Configuration
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
21

Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
22 23 24 25 26 27 28 29 30 31 32 33 34
**Note**: You will need to obtain access to a supported AWS account and region to use this project.

All required AWS resources are stored in the BigBang AWS account, and shared to several other supported accounts and regions.

Using unlisted AWS accounts or regions is currently not supported.

Only the following AWS account IDs are supported:
* `927962728993`
* `792771988927`

Only the following AWS regions are supported:
* `us-gov-west-1`

Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
35
Configure your AWS credentials and region:
36
* Make sure this region equals the `aws_region` variable.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
37 38 39 40 41

```
aws configure
```

42
## SSH Key
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
43 44

Make sure to generate an SSH key:
45
* If you use something other than `<home_full_path>/.ssh/id_rsa`, make sure to update the `private_key_path` and the `public_key_path` variables.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
46 47 48 49 50

```
ssh-keygen
```

51
## Variable File Configuration
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
52

53
Set up (and optionally modify) your `terraform.tfvars.json` file:
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
54 55

```
56
cp terraform.tfvars.json.example terraform.tfvars.json
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
57 58
```

59
# Initialization
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
60

Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
61
Check out the latest stable release into a unique local dev branch you own:
62 63

```
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
64 65 66 67 68 69 70 71 72
release=$(git describe --match "release-v*" --abbrev=0 --tags master)
git fetch --tags origin
git checkout tags/$release -b <your_local_dev_branch_name>
git reset --hard $release
```

If you already have a unique local dev branch, bring in the latest release changes:

```
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
73
git fetch --all
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
74 75 76
release=$(git describe --match "release-v*" --abbrev=0 --tags master)
git checkout <your_local_dev_branch_name>
git merge $release
77 78
```

Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
79
Initialize the terraform module, this only needs to be done once:
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
80 81 82 83 84

```
terraform init
```

85
# Variables / Customization
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
86

87
All custom variables must be defined in the `terraform.tfvars.json` file.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
88

89
This file must exist, and is the only tfvars file type allowed.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
90

91
**No other method of passing variables is supported.**
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
92

93
This is due to limitations with passing terraform variables to ansible.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
94

95
# Running
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
96

97
Steps that will be done after application:
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
98 99 100 101 102
* A security group will be created that only allows your IP ingress.
* Your SSH key will be created and linked as a new AWS keypair.
* A spot EC2 instance is spawned from the `k3d-dev-env` AMI.
* A k3d cluster will be created according to set requirements.
* (Optional) Your local kubeconfig file will be overwritten.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
103 104
* Output information will be populated for debugging use.

Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
105 106 107 108 109 110
Plan the terraform module:

```
terraform plan -out=.tfplan
```

Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
111 112 113
Apply the terraform module:

```
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
114
terraform apply .tfplan
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
115 116
```

117
# Verifying
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
118

119
Check to see if your cluster is up and running:
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
120 121

```
122
kubectl get nodes -o wide
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
123 124
```

125 126 127
# Proxying

Several methods of ingress proxying can be used, here is one example:
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
128 129

```
130
sudo ssh -i $(terraform output private_key_path) -L 443:127.0.0.1:443 $(terraform output ami_user)@$(terraform output instance_ip)
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
131 132
```

133
# Re-Deploying
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
134

135
If you want to re-deploy easily, you can taint and re-apply the EC2 instance:
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
136 137

```
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
138
terraform taint aws_spot_instance_request.k3d
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
139 140 141
terraform apply --auto-approve
```

142
# Destroying
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
143 144 145 146 147 148 149

Please make sure to destroy your resources when not in use:

```
terraform destroy --auto-approve
```

150
# Other Notes
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
151 152 153

* The VPC / security group is tied to your local client public IP address, only this IP address will have access to the created infrastructure. If you need to open this up, do so using the AWS web management console or the AWS command line tool.
* Each AWS resource is tagged with a name and an owner, this allows you to more easily search for the infrastructure in the AWS console for debugging; the owner tag will be the top-level username retrieved from the configured AWS credentials.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
154
* Check to make sure you're not connected to a VPN, and that your local firewall filter is not causing issues. Both of these may conflict with some of the AWS resources, and are hard to debug. If possible, disconnect from your VPN and disable your firewall filter.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
155

156
# Cheat Sheet
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
157

158
Additionally, you can also generate an `/etc/hosts` entry with all virtual service gateways in your cluster:
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
159 160

```
161
echo "$(terraform output instance_ip) $(kubectl get vs -A -o json | jq -r '[.items[] | .spec.hosts[]] | join (" ")')"
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
162 163 164 165 166 167 168 169
```

The output should be similar to this:

```
111.1.111.111 jaeger.bigbang.dev kiali.bigbang.dev grafana.bigbang.dev prometheus.bigbang.dev alertmanager.bigbang.dev twistlock.bigbang.dev
```

170
If you use other clusters, leverage the `KUBECONFIG` environment variable along with the `kubeconfig_path` terraform variable.
Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
171 172

```
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
export KUBECONFIG="<home_full_path>/.kube/config:<home_full_path>/.kube/k3d-dev-env
```

# Notable Features

## Airgap Support

Test airgap capability is available by using the following variables:

* `k3d_upload_images` - Optionally load images from tar into k3d cluster
* `k3d_images_tarball` - Local path to the images tarball (tar.gz) to load into k3d
* `aws_airgap` - Optionally disallow any egress except to the client machine

Combining these variables allows you to test an airgap deployment of an application using a previously generated tarball of container images.

To generate this tarball, leverage the `hack/bundle_images.sh` script.

Also provided are `hack/k8s_dashboard_images.txt` and `hack/k8s_dashboard.yaml`, these files can be used to test an example deployment of the Kubernetes Dashboard in an airgapped enviornment.

An example tarball generation using this image list would be:

```
./hack/bundle_images.sh --image-list hack/k8s_dashboard_images.txt --images hack/images.tar.gz
```

With the additional variable settings in `terraform.tfvars.json` of:

```
{
    ...
    "k3d_upload_images": true,
    "k3d_images_tarball": "<project_full_path>/hack/images.tar.gz",
    "aws_airgap": true,
    ...
}
```

All images provided in the tarball are loaded before cluster creation into each node, and all baseline images required by k3d / k3s are already loaded into the cluster (regardless of airgap variable settings), so there is no need to specify any k3d / k3s cluster specific container images.

Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
212
# Contributing
213 214 215

Additional documentation can be found in several README files throughout the repository.

Zachariah Dzielinski's avatar
Zachariah Dzielinski committed
216 217 218 219 220
These files are meant to assist baseline development for this module and its components.

If you would like to request a feature or report a bug, please [open a GitLab issue](https://repo1.dso.mil/platform-one/big-bang/terraform-modules/k3d-dev-env/-/issues).

If you would like to submit a merge request, refer to the [contributor documentation](./CONTRIBUTING.md) for more information.