Dockerizing an Ansible Playbook, Part 2
In Part 2 of this two-part series, we will create a GitLab pipeline to register the Docker image of the Ansible playbook to a Docker registry and deploy it to Kubernetes.
Join the DZone community and get the full member experience.
Join For FreeThis is a two-part article series. This is part 2 of the series, where we will create a GitLab pipeline to register the Docker image of the Ansible playbook to a Docker registry and deploy it to Kubernetes.
In Part 1, we created an Ansible playbook and Dockerized it to create a Docker image and run it as a Docker container. We logged in to that container to run the ansible playbook.
Why GitLab Pipeline?
In the technology world of efficiency, we tend to automate everything possible to reduce repetitive manual tasks and effort. The GitLab pipeline plays a vital role in this journey.
At a basic level, the GitLab pipeline gets used to build, test and deploy code in stages. It helps
- In reducing the chances of human error as it is implemented as an automated scripted
- In creating faster iterations through continuous integration, delivery, deployment
- In better quality code by leveraging GitLab stages such as pre-check, testing, post-validation, logging, and tracing job execution path
Create a Gitlab Pipeline
In this article, we will create a simple GitLab pipeline with two stages, as below:
Create a Repository in Docker Hub
To create a repository in the Docker Hub, you need to log in to the Docker Hub. If you do not have an account, then sign up to create an account.
Once you log in to the Docker Hub, create a repository named "demo":
Check-In Codebase
Create a GitLab project and check in all the files created under "playbook" in folder Part 1 of this GitLab project:
Find CI/CD on the left-hand side navigation bar. The pipelines can be viewed from that menu option. At this point, there will be no pipeline displayed as we have not created any yet.
Now we will have to find a runner where we can run our pipeline.
On the right-hand panel, scroll down to find runners available. Use a runner which provides a Linux environment. If there is none exists, then create one by following Registering runners | GitLab.
Save the runner's name to refer to it later.
On the same page, scroll to find the "Variables" section.
Add two variables, UserID, and password, saving the Docker Hub account and credential.
Check-in Kubernetes config file. This file has to be retrieved from your K8s environment to log in to K8s.
Checking below code snippet as Deployment.yaml
file. This file will be executed by pipeline to deploy to K8s.
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld-ansible
labels:
app: helloworld-ansible
spec:
replicas: 1
selector:
matchLabels:
app: helloworld-ansible
template:
metadata:
labels:
app: helloworld-ansible
spec:
containers:
- name: helloworld-ansible
image: hub.docker.com/<docker-hub-user-id>/demo:hwa1.0
ports:
- containerPort: 80
Replace the <docker-hub-user-id>
with your Docker Hub UserID.
Create .gitlab_ci.yml File
This is the key file to the pipeline. It is a YAML file that needs to be created under the project's root folder. This file automatically runs whenever you push a commit to the server.
stages:
- build
- deploy
image: ubuntu:16.04
build:
stage: build
tags:
- <runner-name>
script:
# login to docker
- docker login -u $userid -p $password
# build docker image
- docker build . -t hwa
- docker tag hwa:latest hub.docker.com/<docker-hub-user-id>/demo:hwa1.0
# push docker image to docker hub
- docker push hub.docker.com/<docker-hub-user-id>/demo:hwa1.0
deploy:
stage: deploy
tags:
- <runner-name>
script:
- kubectl --kubeconfig=<kubeconfig file> apply -f deployment.yaml
Please make sure to replace <runner-name> with your runner name <docker-hub-user-id>
with your Docker Hub UserID.
Committing this file will trigger the CI/CD pipeline.
You can view and debug by accessing pipelines from CI/CD->Pipelines
Some of the common issues can be mitigated by making sure:
- The runner has access to the K8s container to deploy
- You have the correct K8s config file to log in to the K8s container
- Docker Hub repository URL is correct in your
deployment.yaml
file and GitLab config file. You may check the URL by logging in to the Docker Hub and lick on the repository. Scroll right to find the URL below "Docker commands"
Opinions expressed by DZone contributors are their own.
Comments