To implement the DevOps lifecycle, the following detailed step-by-step guide will cover the setup of Git workflows, AWS infrastructure with Terraform, Docker containerization, Kubernetes cluster deployment, and Jenkins pipeline creation. This guide assumes basic familiarity with these technologies.
- Initialize the Git Repository: Since the repository is already on GitHub at
https://github.com/hshar/website.git
, clone this repository to start managing the code.git clone https://github.com/hshar/website.git cd website
- Branching Strategy: Implement a branching strategy where
master
is the main branch with stable code, and development happens in feature branches. - Release Management: Setup a Git tag or release branch to handle monthly releases on the 25th. Automate this using GitHub Actions or a simple cron job to merge changes to a release branch.
- Create Terraform Configuration: Define the required AWS resources such as EC2 instances, networking components (VPC, subnets, security groups), and IAM roles.
- Terraform Initialization and Execution:
terraform init terraform apply
- Infrastructure Script: Use the provided Kubernetes installation guide in the Terraform scripts or as a startup script for EC2 instances.
- Dockerfile: Create a Dockerfile in the root of your project to containerize the application.
FROM nginx:latest COPY . /usr/share/nginx/html
- Build and Push Docker Images: Use GitHub Actions or Jenkins to automate the building and pushing of Docker images to Docker Hub upon code pushes to the master branch.
-
Kubernetes Setup: Follow the provided Kubernetes installation guide to set up the master and worker nodes using
kubeadm
. -
Deployment and Service Configuration: Here are the deployment.yaml and service.yaml files defining Kubernetes resources for deploying an application named
myapp
.
deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: myapp:latest
ports:
- containerPort: 80
Explanation:
apiVersion: apps/v1
: Specifies the API version for Deployment resource.kind: Deployment
: Defines the type of resource as Deployment.metadata.name
: Specifies the name of the Deployment.spec.replicas
: Sets the desired number of replicas (instances) of the application to 2.spec.selector.matchLabels
: Defines the label selector to match Pods controlled by this Deployment.spec.template.metadata.labels
: Labels applied to Pods created by this template.spec.template.spec.containers
: Describes the containers to be run in the Pod.name
: Name of the container.image
: Docker image to use for the container.ports
: Ports to expose from the container.
service.yaml:
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080
selector:
app: myapp
Explanation:
apiVersion: v1
: Specifies the API version for Service resource.kind: Service
: Defines the type of resource as Service.metadata.name
: Specifies the name of the Service.spec.type
: Sets the type of Service to NodePort, exposing the Service on each node’s IP at a static port.spec.ports
: Specifies the ports that the Service should expose.port
: Port on the Service (accessible internally within the cluster).targetPort
: Port to access on the Pods selected by this Service.nodePort
: If specified, this is the port where the Service is exposed externally (on each Node).
spec.selector
: Selects the Pods to expose through the Service based on labels.app: myapp
: Selects Pods with the labelapp: myapp
to route traffic to.
In this configuration:
-
The Deployment (
myapp-deployment
) will manage Pods running themyapp
container (from Docker imagemyapp:latest
), with 2 replicas, each exposing port 80. -
The Service (
myapp-service
) will expose the Deployment internally within the cluster (port: 80
) and externally (nodePort: 30080
on each Node), directing traffic to Pods labeledapp: myapp
. -
Apply Configuration:
kubectl apply -f deployment.yaml kubectl apply -f service.yaml
- Jenkins Installation and Configuration: Install Jenkins on the designated EC2 instance, install suggested plugins, and set up necessary credentials for Docker Hub and GitHub.
- Pipeline Script:
pipeline { agent none environment { DOCKERHUB_CREDENTIALS = credentials('docker-hub-credentials') } stages { stage('Checkout Code') { agent { label 'jenkins-agent' } steps { git 'https://github.com/hshar/website.git' } } stage('Build Docker Image') { agent { label 'jenkins-agent' } steps { script { sh 'docker build -t username/myapp:${BUILD_ID} .' sh 'docker login -u $DOCKERHUB_USER -p $DOCKERHUB_PASS' sh 'docker push username/myapp:${BUILD_ID}' } } } stage('Deploy to Kubernetes') { agent { label 'jenkins-agent' } steps { script { sh 'kubectl set image deployment/myapp myapp=username/myapp:${BUILD_ID}' } } } } }
- Set Up Monitoring: Use tools like Prometheus and Grafana for monitoring the Kubernetes cluster.
- Set Up Logging: Use Fluentd, Elasticsearch, and Kibana for logging.
To assist with setting up the described project involving Docker, Kubernetes, Jenkins, Terraform, and related tools for automating deployment and scaling, here are some additional resources and links that can be helpful:
- Git Documentation: Official documentation for Git, covering everything from basic usage to advanced workflows.
- Git Branching Model: A popular branching model (GitFlow) for managing feature branches and releases.
- Jenkins Documentation: Official documentation for Jenkins, covering installation, configuration, and usage.
- Jenkins Pipeline: Detailed guide on Jenkins Pipeline for defining continuous integration and delivery workflows.
- Docker Documentation: Comprehensive guide to Docker, including Dockerfile best practices and usage.
- Docker Hub: Official repository for Docker images. Learn how to push and pull images to/from Docker Hub.
- Kubernetes Documentation: Official Kubernetes documentation, covering installation, concepts, and usage.
- Kubernetes By Example: Hands-on tutorials for learning Kubernetes concepts with practical examples.
- Terraform Documentation: Official documentation for Terraform, including guides and examples for infrastructure provisioning.
- Terraform AWS Provider: Detailed documentation on using Terraform with AWS.
- Ansible Documentation: Official Ansible documentation, covering playbooks, modules, and best practices for configuration management.
- Puppet Documentation: Comprehensive guide to Puppet, including modules and configuration management concepts.
- Docker Mastery: Udemy course covering Docker fundamentals and advanced topics.
- Kubernetes Fundamentals: Udemy course for learning Kubernetes from scratch.
- Jenkins Tutorial: Tutorialspoint's Jenkins tutorial covering setup, configurations, and pipelines.
- Stack Overflow: Q&A platform for troubleshooting and asking technical questions related to DevOps tools.
- DevOps Subreddit: Reddit community discussing DevOps practices, tools, and news.
- Katacoda: Interactive learning platform with Docker and Kubernetes scenarios.
- Play with Docker/Kubernetes: Hands-on labs for experimenting with Docker and Kubernetes.
By utilizing these resources, you can deepen your understanding of the tools and concepts required to implement an effective DevOps lifecycle for containerized applications. Don't hesitate to explore tutorials, forums, and practical examples to reinforce your knowledge and troubleshoot any challenges you encounter during the project setup and deployment.
This outline provides a comprehensive approach to setting up a DevOps lifecycle for containerized applications using Docker and Kubernetes, with automation facilitated by Jenkins, all managed through Terraform on AWS.