Simple example using Git actions + Argo CD + K8S + Docker and GO lang
- Have an ArgoCD account and Installed.
- Docker installed.
- Have k3D intalled.
- Kubectl installed.
- Have a Github account.
- Unlimited of curiosity to learn new things.
- Create a K8S cluster
k3d cluster create {clustername}
(It is in your preference host, in this case are local with K3D). - To list all contexts
kubectl config get-contexts
. - Enter in K8S cluster's context
kubectl cluster-info --context {clustername}
. - See if nodes are ok in cluster with
kubectl get nodes
. - Create a app that you need, in this case a simple webserver in go.
- Make a Dockerfile using multistage build.
- Build image and send to registry if is necessary (in this case I will maintain local)
docker build -t {myname}/{imagename}:{tag} .
. - Run image with command
docker run --rm -p 9090:9090
. - Push image in your DockerHub account (You can use any registry that you wish), first make login in your account using
docker login
, second run this command:docker push {myname}/{imagename}:{tag}
. - Insert in line 16 of file
k8s/deployment.yaml
your image address and in line 22 the port of application. - To run deployment in your kubernetes cluster use the command:
kubectl apply -f k8s/deployment.yaml
. - To see if deployment is running use
kubectl get pods
, if you not add -n and namespace name kubernetes show only for default namespace. - Make a service in
k8s/service.yaml
with data about deployments, addin a port and name for your app, in my case i use goaap. - Run command
kubectl apply -f k8s/service.yaml
. - To show services you can use command
kubectl get svc
. - To test if service are running use command:
kubectl port-forward svc/goapp 9090:9090
, after this you can access in your localhost:9090 at webrowser. Nice you have a application running in kubernetes.
In real world we have a necessity to use a versioning of images, to use it with more easy way, kustomize are tool that help you to make it.
- Make a
kustomization.yaml
ink8s
folder. - Add parameters that you need in kustomize file.
- To build and update file you can use command into k8s folder:
kustomize build
- Catch a token in dockerhub and add in github repository
- Make a push and see in git actions your CD running.
For Now Update a file and add a Deploy stage in workflow yaml.
- To delete cluster
k3d cluster delete {clustername}
.
Little explain about dockerfile for generating go image using multistage build.
# Multistage build
# First image compile a binary in GO
FROM golang:1.17 as build
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -o server main.go
# Second image catch a binary from first and generate a simple and light image to use in webserver
FROM alpine:3.12
WORKDIR /app
COPY --from=build /app/server .
CMD ["./server"]
Materials used to reference:
FullCycle - Deploy contínuo com GitOps e ArgoCD FullCycle - Reference repo