Skip to content

Commit

Permalink
update github-ci to deploy to amazon EKS (techschool#22)
Browse files Browse the repository at this point in the history
* update github-ci to deploy to amazon EKS

* add image pull policy always

Co-authored-by: phamlequang <phamlequang@gmail.com>
  • Loading branch information
techschool and phamlequang authored Jul 17, 2021
1 parent 25d22b9 commit 3dda2e2
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 9 deletions.
20 changes: 17 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ on:

jobs:

build:
deploy:
name: Build image
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Install kubectl
uses: azure/setup-kubectl@v1
with:
version: 'v1.21.3'
id: install

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
Expand All @@ -34,5 +40,13 @@ jobs:
ECR_REPOSITORY: simplebank
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t latest .
docker push -a $ECR_REGISTRY/$ECR_REPOSITORY
- name: Deploy image to Amazon EKS
run: |
kubectl apply -f eks/aws-auth.yaml
kubectl apply -f eks/deployment.yaml
kubectl apply -f eks/service.yaml
kubectl apply -f eks/issuer.yaml
kubectl apply -f eks/ingress.yaml
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ In this backend master class, we’re going to learn everything about how to des
- Lecture #23: [Build a minimal Golang Docker image with a multistage Dockerfile](https://www.youtube.com/watch?v=p1dwLKAxUxA&list=PLy_6D98if3ULEtXtNSY_2qN21VCKgoQAE&index=23)
- Lecture #24: [How to use docker network to connect 2 stand-alone containers](https://www.youtube.com/watch?v=VcFnqQarpjI&list=PLy_6D98if3ULEtXtNSY_2qN21VCKgoQAE&index=24)
- Lecture #25: [How to write docker-compose file and control service start-up orders with wait-for.sh](https://www.youtube.com/watch?v=jf6sQsz0M1M&list=PLy_6D98if3ULEtXtNSY_2qN21VCKgoQAE&index=25)
- Lecture #26: [How to create a free tier AWS account](https://www.youtube.com/watch?v=4UqN1P8pIkM&list=PLy_6D98if3ULEtXtNSY_2qN21VCKgoQAE&index=26)
- Lecture #27: [Auto build & push docker image to AWS ECR with Github Actions](https://www.youtube.com/watch?v=3M4MPmSWt9E&list=PLy_6D98if3ULEtXtNSY_2qN21VCKgoQAE&index=27)
- Lecture #28: [How to create a production DB on AWS RDS](https://www.youtube.com/watch?v=0EaG3T4Q5fQ&list=PLy_6D98if3ULEtXtNSY_2qN21VCKgoQAE&index=28)
- Lecture #29: [Store & retrieve production secrets with AWS secrets manager](https://www.youtube.com/watch?v=3i1mQ_Ye8jE&list=PLy_6D98if3ULEtXtNSY_2qN21VCKgoQAE&index=29)

## Simple bank service

Expand Down Expand Up @@ -64,17 +68,17 @@ The service that we’re going to build is a simple bank. It will provide APIs f

- [Gomock](https://github.com/golang/mock)

``` bash
go install github.com/golang/mock/mockgen@v1.6.0
```
``` bash
go install github.com/golang/mock/mockgen@v1.6.0
```

### Setup infrastructure

- Create the bank-network

``` bash
make network
```
``` bash
make network
```

- Start postgres container:

Expand Down Expand Up @@ -145,3 +149,17 @@ The service that we’re going to build is a simple bank. It will provide APIs f
```bash
make test
```

## Deploy to kubernetes cluster

- [Install nginx ingress controller](https://kubernetes.github.io/ingress-nginx/deploy/#aws):

```bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.48.1/deploy/static/provider/aws/deploy.yaml
```

- [Install cert-manager](https://cert-manager.io/docs/installation/kubernetes/):

```bash
kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.4.0/cert-manager.yaml
```
11 changes: 11 additions & 0 deletions eks/aws-auth.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: aws-auth
namespace: kube-system
data:
mapUsers: |
- userarn: arn:aws:iam::095420225348:user/github-ci
username: github-ci
groups:
- system:masters
22 changes: 22 additions & 0 deletions eks/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: simple-bank-api-deployment
labels:
app: simple-bank-api
spec:
replicas: 2
selector:
matchLabels:
app: simple-bank-api
template:
metadata:
labels:
app: simple-bank-api
spec:
containers:
- name: simple-bank-api
image: 095420225348.dkr.ecr.eu-west-1.amazonaws.com/simplebank:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
30 changes: 30 additions & 0 deletions eks/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: nginx
spec:
controller: k8s.io/ingress-nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: simple-bank-ingress
annotations:
cert-manager.io/cluster-issuer: letsencrypt
spec:
ingressClassName: nginx
rules:
- host: "api.simple-bank.org"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: simple-bank-api-service
port:
number: 80
tls:
- hosts:
- api.simple-bank.org
secretName: simple-bank-api-cert
16 changes: 16 additions & 0 deletions eks/issuer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt
spec:
acme:
email: techschool.guru@gmail.com
server: https://acme-v02.api.letsencrypt.org/directory
privateKeySecretRef:
# Secret resource that will be used to store the account's private key.
name: letsencrypt-account-private-key
# Add a single challenge solver, HTTP01 using nginx
solvers:
- http01:
ingress:
class: nginx
12 changes: 12 additions & 0 deletions eks/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: simple-bank-api-service
spec:
selector:
app: simple-bank-api
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP

0 comments on commit 3dda2e2

Please sign in to comment.