Tutorials  /  Container & Cloud

Deployment and Service in Kubernetes: A Practical Guide

Ccentron Redaktion · November 2024 ·4 min read ·Container & Cloud, Tutorial

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

K8

Matching infrastructure at centron

From container to cluster: managed Kubernetes with cStack starts at €29.99 per month – control plane, autoscaler and traffic included. Explore managed Kubernetes →

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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Container & Cloud
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren