Start EKS cluster
Create EKS cluster IAM role
Using CloudFormation
EKS_CLUSTER_ROLE_STACK_NAME="<stack name>"
EKS_CLUSTER_ROLE_NAME="<role name>"
PROJECT_NAME="<project name>"
REGION="<region code>"
curl -LO https://raw.githubusercontent.com/marcus16-kang/aws-resources-example/main/scripts/eks/cluster-role-cfn.yaml
# Deploy stack
aws cloudformation deploy \
--template-file ./cluster-role-cfn.yaml \
--stack-name $EKS_CLUSTER_ROLE_STACK_NAME \
--capabilities CAPABILITY_NAMED_IAM \
--parameter-overrides RoleName=$EKS_CLUSTER_ROLE_NAME ProjectName=$PROJECT_NAME \
--tags project=$PROJECT_NAME \
--disable-rollback \
--region $REGION
# Get IAM role arn
aws cloudformation describe-stacks \
--stack-name $EKS_CLUSTER_ROLE_STACK_NAME \
--query "Stacks[0].Outputs[0].OutputValue" \
--output text \
--region $REGION
$EKS_CLUSTER_ROLE_STACK_NAME="<stack name>"
$EKS_CLUSTER_ROLE_NAME="<role name>"
$PROJECT_NAME="<project name>"
$REGION="<region code>"
curl.exe -LO https://raw.githubusercontent.com/marcus16-kang/aws-resources-example/main/scripts/eks/cluster-role-cfn.yaml
# Deploy stack
aws cloudformation deploy `
--template-file ./cluster-role-cfn.yaml `
--stack-name $EKS_CLUSTER_ROLE_STACK_NAME `
--capabilities CAPABILITY_NAMED_IAM `
--parameter-overrides RoleName=$EKS_CLUSTER_ROLE_NAME ProjectName=$PROJECT_NAME `
--tags project=$PROJECT_NAME `
--disable-rollback `
--region $REGION
# Get IAM role arn
aws cloudformation describe-stacks `
--stack-name $EKS_CLUSTER_ROLE_STACK_NAME `
--query "Stacks[0].Outputs[0].OutputValue" `
--output text `
--region $REGION
Using AWS CLI
Create cluster trust policy file
Create the cluster role
aws iam create-role \
--role-name <role name> \
--assume-role-policy-document file://cluster-trust-policy.json
Note
If you want to create tag, use this parameter.
Attach the required IAM policy to the role
aws iam attach-role-policy \
--policy-arn arn:aws:iam::aws:policy/AmazonEKSClusterPolicy \
--role-name <role name>
Create EKS cluster
Please use AWS Management Console to create EKS cluster.
Using eksctl
Create IAM OIDC provider
Create kubeconfig
for EKS cluster
Install AWS Authenticator Config Map
curl -LO aws-auth-cm.yaml https://s3.us-west-2.amazonaws.com/amazon-eks/cloudformation/2020-10-29/aws-auth-cm.yaml
You should open file and change to IAM role arn(not instance profile).
Using kubectl
with IAM role
cat << EOF >> cluster-role-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: iam-role-binding
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: User
name: <role name>
apiGroup: rbac.authorization.k8s.io
EOF
# Delete aws configure files
rm -rf ~/.aws
aws sts get-caller-identity
Create IAM Identity Mapping
Create IRSAs for Addons
irsa.yaml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
Encrypt secrets using KMS
Create CMK
CLUSTER_ROLE=<cluster role arn>
KMS_ALIAS=<KMS key alias(name)>
REGION=<region code>
cat << EOF > eks-kms.yaml
Parameters:
ClusterRole:
Description: "The arn of cluster's IAM role."
Type: String
AliasName:
Description: "The name of KMS key."
Type: String
Resources:
Key:
Type: 'AWS::KMS::Key'
Properties:
Description: CMK for EKS secrets
KeyPolicy:
Version: 2012-10-17
Id: key-default-1
Statement:
- Sid: Enable IAM User Permissions.
Effect: Allow
Principal:
AWS: !Sub arn:aws:iam::\${AWS::AccountId}:root
Action: 'kms:*'
Resource: '*'
- Sid: Enable IAM Role at EKS Cluster.
Effect: Allow
Principal:
AWS: !Ref ClusterRole
Action: 'kms:*'
Resource: '*'
Alias:
Type: 'AWS::KMS::Alias'
Properties:
AliasName: !Sub 'alias/\${AliasName}'
TargetKeyId: !Ref Key
EOF
aws cloudformation deploy \
--stack-name eks-kms-stack \
--template-file ./eks-kms.yaml \
--parameter-overrides ClusterRole=$CLUSTER_ROLE AliasName=$KMS_ALIAS \
--region $REGION
Limit IAM role to access kubernetes resource by namespace
Create a role
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: <role name>
namespace: <namespace name>
rules:
- apiGroups:
- ""
- "apps"
- "batch"
- "extensions"
# - "autoscaling"
resources:
- "configmaps"
- "cronjobs"
- "deployments"
- "events"
- "ingresses"
- "jobs"
- "pods"
- "pods/attach"
- "pods/exec"
- "pods/log"
- "pods/portforward"
- "secrets"
- "services"
# - "horizontalpodautoscalers"
verbs:
- "create"
- "delete"
- "describe"
- "get"
- "list"
- "patch"
- "update"
Create a role binding
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: <role binding name>
namespace: <namespace name>
subjects:
- kind: User
name: <user name>
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: <role name>
apiGroup: rbac.authorization.k8s.io