Deployment and Service in Kubernetes: A Practical Guide
CI/CD (Continuous Integration/Continuous Deployment) has become an essential practice for modern software development. With the growing popularity of Kubernetes as a container orchestration tool, it makes sense to become familiar with the basics of CI/CD in a Kubernetes environment.
In this tutorial, we will show you how to create Kubernetes Deployments and Services to run your applications in a Kubernetes environment and make them accessible externally. These fundamentals provide a solid foundation for building a CI/CD environment on Kubernetes, which we will explore in more detail in future articles.
Step 1: Building Container Images with Docker and Buildah
# Docker: Create a Dockerfile and build an image
mkdir demo
cd demo
nano Dockerfile
# Add Dockerfile content
sudo docker image build -t nkhare/nginx:latest .
# Buildah: Install Buildah and create an image
cd
mkdir ~/buildah
cd ~/buildah
export GOPATH=`pwd`
git clone https://github.com/containers/buildah ./src/github.com/containers/buildah
cd ./src/github.com/containers/buildah
make runc all TAGS=”apparmor seccomp”
sudo cp ~/buildah/src/github.com/opencontainers/runc/runc /usr/bin/.
sudo apt install buildah
sudo buildah build-using-dockerfile -t rsvpapp:buildah github.com/do-community/rsvpapp-webinar1
Step 2: Setting up a Kubernetes Cluster on centron with kubeadm and Terraform
# Setting up the cluster with Terraform and kubeadm
ssh-keygen -t rsa
cd k8s-cicd-webinars/webinar1/2-kubernetes/1-Terraform/
./script.sh
kubectl get nodes
Step 3: Creating Container Images with Kaniko
# Creating container images with Kaniko
sudo kubectl create configmap docker-config –from-file=$HOME/.docker/config.json
nano pod-kaniko.yml
# Add the pod definition content
kubectl apply -f pod-kaniko.yml
kubectl get pods
Step 4: Creating Kubernetes Deployments
Kubernetes Deployments allow you to run your applications and define the desired state for your Pods. In this step, we create an Nginx deployment file called `deployment.yml` in the directory `~/k8s-cicd-webinars/webinar1/2-kubernetes/1-Terraform/` to set up an Nginx Deployment.
1. First, open the file:
nano deployment.yml
2. Add the following configuration to define your Nginx Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
– name: nginx
image: nginx:1.7.9
ports:
– containerPort: 80
3. Deploy the Deployment with the following command:
kubectl apply -f deployment.yml
Step 5: Creating Kubernetes Services
Kubernetes Services allow applications to be accessible internally and externally. Each Service defines a `ServiceType` that specifies how the Service is exposed. In this example, we use a `NodePort ServiceType`, which exposes the Service on a static port on each node.
1. Create a file named `service.yml` in the directory `~/k8s-cicd-webinars/webinar1/2-kubernetes/1-Terraform/`:
nano service.yml
2. Add the following content to define your Service:
kind: Service
apiVersion: v1
metadata:
name: nginx-service
spec:
selector:
app: nginx
type: NodePort
ports:
– protocol: TCP
port: 80
targetPort: 80
nodePort: 30111
3. Deploy the Service with the following command:
kubectl apply -f service.yml
After completing these steps, your Nginx deployment is now ready and accessible via the created Service on Port 30111, both internally and externally.