This Jenkinsfile will deploy Kubernetes cluster on Amazon AWS including the Load Balancer.
The cluster is deployed via eksctl
command line utility that is calling CloudFormation. It is very easy way how to deploy Kubernetes cluster on AWS.
Prerequisites, Jenkins server has to have these installed:
- docker
- eksctl
- kubectl
- hadolint
- awscli
- pipeline-aws Jenkins plugin to authenticate with AWS Cloud
pipeline {
agent any
stages {
stage('Cluster creation') {
steps {
withAWS(credentials:'aws-static') {
sh '''
eksctl create cluster \
--name analytics-cluster \
--version 1.18 \
--region us-east-2 \
--nodegroup-name linux-nodes \
--node-type t2.small \
--nodes 3 \
--nodes-min 1 \
--nodes-max 4 \
--with-oidc \
--managed \
'''
}
}
}
stage('LB IAM OIDC provider and cluster pairing') {
steps {
withAWS(credentials:'aws-static') {
sh '''
eksctl utils associate-iam-oidc-provider \
--region us-east-2 \
--cluster analytics-cluster \
--approve \
'''
}
}
}
stage('LB Role and service account creation') {
steps {
withAWS(credentials:'aws-static') {
sh '''
eksctl create iamserviceaccount \
--cluster=analytics-cluster \
--namespace=kube-system \
--region us-east-2 \
--name=aws-load-balancer-controller \
--attach-policy-arn=arn:aws:iam::667684686916:policy/AWSLoadBalancerControllerIAMPolicy \
--override-existing-serviceaccounts \
--approve \
'''
}
}
}
stage('LB controller installation') {
steps {
withAWS(region:'us-east-2', credentials:'aws-static') {
sh 'kubectl apply -k "github.com/aws/eks-charts/stable/aws-load-balancer-controller//crds?ref=master"'
sh 'helm repo add eks https://aws.github.io/eks-charts'
sh '''
helm upgrade -i aws-load-balancer-controller eks/aws-load-balancer-controller \
--set clusterName=analytics-cluster \
--set serviceAccount.create=false \
--set serviceAccount.name=aws-load-balancer-controller \
-n kube-system \
'''
sh 'kubectl get deployment -n kube-system aws-load-balancer-controller'
}
}
}
}
}
Sources:
creating Kubernetes cluster on AWS:
- https://logz.io/blog/amazon-eks-cluster/
- https://docs.aws.amazon.com/eks/latest/userguide/create-cluster.html
- https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-cluster.html
- https://docs.aws.amazon.com/eks/latest/userguide/clusters.html
- https://www.ironin.it/blog/a-simple-kubernetes-cluster-on-aws.html
- https://docs.aws.amazon.com/eks/latest/APIReference/API_CreateCluster.html
- https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html
- https://rtfm.co.ua/en/aws-elastic-kubernetes-service-a-cluster-creation-automation-part-1-cloudformation/
- https://www.bluematador.com/blog/my-first-kubernetes-cluster-a-review-of-amazon-eks
- https://docs.aws.amazon.com/eks/latest/userguide/cluster-endpoint.html
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-nodegroup.html
- https://aws.amazon.com/blogs/storage/deploying-jenkins-on-amazon-eks-with-amazon-efs/