Create a new project:
Go to operations, kubernetes. Add a cluster:
Click add an existing cluster:
The cluster API will be on the Microk8s IP address: https://<microk8s unit external IP>:16443
.
Extract the certificate with:
juju ssh microk8s/0
kubectl get secrets
kubectl get secret <TOKEN> -o jsonpath="{['data']['ca\.crt']}" | base64 --decode
And paste it to the CA certificate field.
To extract the token create a Gitlab service account and a cluster role binding:
vim gitlab-admin-service-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: gitlab
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: gitlab-admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: gitlab
namespace: kube-system
And apply it:
kubectl apply -f gitlab-admin-service-account.yaml
Now extract the token:
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep gitlab | awk '{print $1}')
And copy the token value to the Service Token field.
Disable RBAC cluster and click “Create”.
To enable AutoDevops we need to install some apps. On the Kubernetes clusters page choose your new cluster and go to the Applications tab.
First install the Gitlab Runner, Prometheus, and a Cert Manager.
Before installing Ingress we will need also need to enable metallb add-on for the load balancing. To do it we need to choose the correct IP range for metallb to choose from:
ifconfig eth0
We will choose the range from 1 to 21:
microk8s enable metallb:172.31.16.1-172.31.31.21
And install the Ingress app:
To be able to run Auto Review and Auto Deploy stages we will need to set up a wildcard DNS pointing to the address provided by Ingress:
I will also disable the testing stage as I do not want to run the testing for this project. Go to Settings > CI/CD > Variables > Add Variable:
Then set TEST_DISABLED: true
:
Now, let’s add some code into our repository and see the pipelines running. On your local machine login and clone an existing project:
$ git config --global user.name "Administrator"
$ git config --global user.email "admin@example.com"
$ git clone https://github.com/natalytvinova/cdk-cats.git
$ cd cdk-cats/
ls -a
Our example project is just a simple Nginx app that will greet the user with a cat picture. As you can see it already has a Dockerfile that will enable the Auto Build stage. We also have a Gitlab directory with the Auto Deploy values file that changes the default application port to 80:
service:
internalPort:80
externalPort:80
Let’s push the project to the Gitlab repo:
git remote rename origin old-origin
git remote add origin git@gitlab.natalytvinova.com:root/microk8s-cats.git
git push -u origin --all
git push -u origin --tags
Now we can check if the pipeline is running by going into CI/CD > Pipelines. After some time we can see every stage being completed:
After the review stage, we can click on the production job and check how the container was deployed on our cluster. Copy the container address:
To be able to access the review page we need to sshuttle from the Microk8s machine to the Ingress address:
sshuttle -r ubuntu@<Microk8s-IP> <Ingress-IP>
Great! We have a meow from Kubernetes:
Now let’s see how Gitlab would behave if we push something to the repository. We will switch to a new branch and replace an old picture with a new one:
git checkout master
git checkout -b serious-cat
cp ~/Downloads/serious_cat.jpg html/cat.jpg
git add html/cat.jpg
git commit -m "serious cat picture change"
git push -u origin serious-cat
As we can see the new pipeline has already been started:
And after some time we can visit the review page:
And check the changes we have made: