Skip to content

Commit b2aae26

Browse files
add files
1 parent 09223e2 commit b2aae26

11 files changed

+633
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,8 @@ Video: https://youtu.be/xhva6DeKqVU <br/>
6464
Part #7 Kubernetes ingress | the basics <br/>
6565
Video: https://youtu.be/izWCkcJAzBw <br/>
6666

67+
Kubernetes in the Cloud
68+
69+
Checkout my series on running Kubernetes in the Cloud [here](./kubernetes/cloud/readme.md) <br/>
6770

6871
More details coming soon!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Principal": {
7+
"Service": "ec2.amazonaws.com"
8+
},
9+
"Action": "sts:AssumeRole"
10+
}
11+
]
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Principal": {
7+
"Service": "eks.amazonaws.com"
8+
},
9+
"Action": "sts:AssumeRole"
10+
}
11+
]
12+
}
+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Getting Started with EKS
2+
3+
## Amazon CLI
4+
5+
```
6+
7+
# Run Amazon CLI
8+
docker run -it --rm -v ${PWD}:/work -w /work --entrypoint /bin/sh amazon/aws-cli:2.0.17
9+
10+
cd ./kubernetes/cloud/amazon
11+
12+
yum install jq
13+
```
14+
15+
## Login to AWS
16+
17+
https://docs.aws.amazon.com/eks/latest/userguide/getting-started-console.html
18+
19+
```
20+
# Access your "My Security Credentials" section in your profile.
21+
# Create an access key
22+
23+
aws configure
24+
25+
# Regions
26+
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html
27+
28+
```
29+
30+
31+
# Deploy Cluster with AWS CLI
32+
33+
You can deploy a cluster using multiple ways. </br>
34+
We will cover the two fundamental ways.
35+
36+
1) AWS CLI https://docs.aws.amazon.com/eks/latest/userguide/getting-started-console.html
37+
2) EKS CLI (newer) https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html
38+
39+
40+
## AWS CLI
41+
42+
Kubernetes needs a service account to manage our Kubernetes cluster <br/>
43+
In AWS this is an IAM role <br/>
44+
Lets create one! <br/>
45+
46+
Follow "Create your Amazon EKS cluster IAM role" [here](https://docs.aws.amazon.com/eks/latest/userguide/getting-started-console.html) <br/>
47+
48+
```
49+
50+
# create our role for EKS
51+
role_arn=$(aws iam create-role --role-name getting-started-eks-role --assume-role-policy-document file://assume-policy.json | jq .Role.Arn | sed s/\"//g)
52+
aws iam attach-role-policy --role-name getting-started-eks-role --policy-arn arn:aws:iam::aws:policy/AmazonEKSClusterPolicy
53+
54+
# create the cluster VPC
55+
56+
curl https://amazon-eks.s3.us-west-2.amazonaws.com/cloudformation/2020-05-08/amazon-eks-vpc-sample.yaml -o vpc.yaml
57+
aws cloudformation deploy --template-file vpc.yaml --stack-name getting-started-eks
58+
59+
# grab your stack details
60+
aws cloudformation list-stack-resources --stack-name getting-started-eks > stack.json
61+
62+
# create our cluster
63+
64+
aws eks create-cluster \
65+
--name getting-started-eks \
66+
--role-arn $role_arn \
67+
--resources-vpc-config subnetIds=subnet-063efe1fa0c5d4913,subnet-06f91e563755e2077,subnet-0824d16f8536b3681,securityGroupIds=sg-0960d3a116ba912e1,endpointPublicAccess=true,endpointPrivateAccess=false
68+
69+
aws eks list-clusters
70+
aws eks describe-cluster --name getting-started-eks
71+
```
72+
73+
74+
## Get a kubeconfig for our cluster
75+
76+
```
77+
78+
aws eks update-kubeconfig --name getting-started-eks --region ap-southeast-2
79+
80+
#grab the config if you want it
81+
cp ~/.kube/config .
82+
83+
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
84+
chmod +x ./kubectl && mv ./kubectl /usr/local/bin/kubectl
85+
86+
```
87+
88+
## Add nodes to our cluster
89+
90+
```
91+
92+
# create our role for nodes
93+
role_arn=$(aws iam create-role --role-name getting-started-eks-role-nodes --assume-role-policy-document file://assume-node-policy.json | jq .Role.Arn | sed s/\"//g)
94+
95+
aws iam attach-role-policy --role-name getting-started-eks-role-nodes --policy-arn arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy
96+
aws iam attach-role-policy --role-name getting-started-eks-role-nodes --policy-arn arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy
97+
aws iam attach-role-policy --role-name getting-started-eks-role-nodes --policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
98+
99+
```
100+
More details on node permissions [here](https://docs.aws.amazon.com/eks/latest/userguide/worker_node_IAM_role.html)
101+
102+
103+
More details on instance types to choose from [here](https://aws.amazon.com/ec2/instance-types/)
104+
105+
```
106+
aws eks create-nodegroup \
107+
--cluster-name getting-started-eks \
108+
--nodegroup-name test \
109+
--node-role $role_arn \
110+
--subnets subnet-0ec47e6ae964a233f \
111+
--disk-size 200 \
112+
--scaling-config minSize=1,maxSize=2,desiredSize=1 \
113+
--instance-types t2.small
114+
```
115+
116+
## EKS CTL example
117+
118+
```
119+
eksctl create cluster --name getting-started-eks-1 \
120+
--region ap-southeast-2 \
121+
--version 1.16 \
122+
--managed \
123+
--node-type t2.small \
124+
--nodes 1 \
125+
--node-volume-size 200
126+
127+
```
128+
## Create some sample containers
129+
130+
```
131+
cd ../..
132+
133+
kubectl create ns example-app
134+
135+
# lets create some resources.
136+
kubectl apply -n example-app -f secrets/secret.yaml
137+
kubectl apply -n example-app -f configmaps/configmap.yaml
138+
kubectl apply -n example-app -f deployments/deployment.yaml
139+
140+
# remember to change the `type: LoadBalancer`
141+
kubectl apply -n example-app -f services/service.yaml
142+
143+
```
144+
## Cleanup
145+
146+
```
147+
148+
eksctl delete cluster --name getting-started-eks-1
149+
150+
aws eks delete-nodegroup --cluster-name getting-started-eks --nodegroup-name test
151+
aws eks delete-cluster --name getting-started-eks
152+
153+
aws iam detach-role-policy --role-name getting-started-eks-role --policy-arn arn:aws:iam::aws:policy/AmazonEKSClusterPolicy
154+
aws iam delete-role --role-name getting-started-eks-role
155+
156+
aws iam detach-role-policy --role-name getting-started-eks-role-nodes --policy-arn arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy
157+
aws iam detach-role-policy --role-name getting-started-eks-role-nodes --policy-arn arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy
158+
aws iam detach-role-policy --role-name getting-started-eks-role-nodes --policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
159+
160+
aws iam delete-role --role-name getting-started-eks-role-nodes
161+
162+
aws cloudformation delete-stack --stack-name getting-started-eks
163+
```
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Getting Started with AKS
2+
3+
## Azure CLI
4+
5+
```
6+
# Run Azure CLI
7+
docker run -it --rm -v ${PWD}:/work -w /work --entrypoint /bin/sh mcr.microsoft.com/azure-cli:2.6.0
8+
9+
cd ./kubernetes/cloud/azure
10+
11+
```
12+
13+
## Login to Azure
14+
15+
```
16+
#login and follow prompts
17+
az login
18+
19+
# view and select your subscription account
20+
21+
az account list -o table
22+
SUBSCRIPTION=<id>
23+
az account set --subscription <SubscriptionId-id-here>
24+
25+
```
26+
27+
## Create our Resource Group
28+
29+
```
30+
RESOURCEGROUP=aks-getting-started
31+
az group create -n $RESOURCEGROUP -l australiaeast
32+
33+
```
34+
## Create Service Principal
35+
36+
Kubernetes needs a service account to manage our Kubernetes cluster </br>
37+
Lets create one! </br>
38+
39+
```
40+
41+
SERVICE_PRINCIPAL_JSON=$(az ad sp create-for-rbac --skip-assignment --name aks-getting-started-sp -o json)
42+
43+
#Keep the `appId` and `password` for later use!
44+
45+
SERVICE_PRINCIPAL=$(echo $SERVICE_PRINCIPAL_JSON | jq -r '.appId')
46+
SERVICE_PRINCIPAL_SECRET=$(echo $SERVICE_PRINCIPAL_JSON | jq -r '.password')
47+
48+
#grant contributor role over the resource group to our service principal
49+
50+
az role assignment create --assignee $SERVICE_PRINCIPAL \
51+
--scope "/subscriptions/$SUBSCRIPTION/resourceGroups/$RESOURCEGROUP" \
52+
--role Contributor
53+
54+
```
55+
For extra reference you can also take a look at the Microsoft Docs: [here](https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/aks/kubernetes-service-principal.md) </br>
56+
57+
## Create our cluster
58+
59+
```
60+
#full list of options
61+
62+
az aks create --help
63+
az aks get-versions --location australiaeast -o table
64+
65+
#generate SSH key
66+
67+
ssh-keygen -t rsa -b 4096 -N "VeryStrongSecret123!" -C "[email protected]" -q -f ~/.ssh/id_rsa
68+
cp ~/.ssh/id_rsa* .
69+
70+
az aks create -n aks-getting-started \
71+
--resource-group $RESOURCEGROUP \
72+
--location australiaeast \
73+
--kubernetes-version 1.16.9 \
74+
--load-balancer-sku standard \
75+
--nodepool-name default \
76+
--node-count 1 \
77+
--node-vm-size Standard_E4s_v3 \
78+
--node-osdisk-size 250 \
79+
--ssh-key-value ./id_rsa.pub \
80+
--network-plugin kubenet \
81+
--service-principal $SERVICE_PRINCIPAL \
82+
--client-secret $SERVICE_PRINCIPAL_SECRET \
83+
--output none
84+
85+
# if your SP key is invalid, generate a new one:
86+
SERVICE_PRINCIPAL_SECRET=(az ad sp credential reset --name $SERVICE_PRINCIPAL | jq -r '.password')
87+
```
88+
89+
## Get a kubeconfig for our cluster
90+
91+
```
92+
# use --admin for admin credentials
93+
# use without `--admin` to get no priviledged user.
94+
95+
az aks get-credentials -n aks-getting-started \
96+
--resource-group $RESOURCEGROUP
97+
98+
#grab the config if you want it
99+
cp ~/.kube/config .
100+
101+
```
102+
103+
## Get kubectl
104+
105+
```
106+
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
107+
chmod +x ./kubectl
108+
mv ./kubectl /usr/local/bin/kubectl
109+
110+
cd ../..
111+
112+
kubectl create ns example-app
113+
114+
# lets create some resources.
115+
kubectl apply -n example-app -f secrets/secret.yaml
116+
kubectl apply -n example-app -f configmaps/configmap.yaml
117+
kubectl apply -n example-app -f deployments/deployment.yaml
118+
119+
# remember to change the `type: LoadBalancer`
120+
kubectl apply -n example-app -f services/service.yaml
121+
122+
```
123+
124+
## Clean up
125+
126+
```
127+
az group delete -n $RESOURCEGROUP
128+
az ad sp delete --id $SERVICE_PRINCIPAL
129+
```

0 commit comments

Comments
 (0)