Kubernetes: Deployment, Storage, Configuration, Network, ConfigMap

Harshal Jethwa
5 min readSep 9, 2023

--

#10WeeksOfCloudOps

Introduction:

Kubernetes is a powerful tool for managing containerized applications. Here’s a simplified overview of its key concepts:
1. Deployment: Think of it as a way to tell Kubernetes how many copies of your app should always be running. It takes care of scaling and updates.
2. Storage: Kubernetes helps you manage storage for your apps. You ask for storage (Persistent Volume Claim), and it provides it (Persistent Volume).
3. Configuration: You can set up environment variables for your apps or use ConfigMaps to store configuration data separately. It makes changing settings easier.
4. Network: Kubernetes ensures that your app containers can talk to each other using their own IP addresses. Services give your app a fixed address even if containers change.
5. ConfigMap: ConfigMaps helps you store config data separately from your app code. You can change settings without changing the app itself.

Requirement:
https://github.com/piyushsachdeva/10WeeksOfCloudOps_Week7/tree/main

GitHub Repository:
https://github.com/HARSHALJETHWA19/k8-cloudops

Setups:
Cluster Provisioning:
We can use any cloud provider like AWS, GCP, or Azure to provision a Kubernetes cluster in a specific region with at least three worker nodes spread across different availability zones.

Deployment:
Create a YAML file named registry-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: 10weeksofcloudops
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: cloudopsk8
template:
metadata:
labels:
app: cloudopsk8
spec:
initContainers:
- name: init-data-dir
image: alpine:latest
command: ["sh", "-c", "mkdir -p /var/lib/10weeksofcloudops"]
volumeMounts:
- name: data-volume
mountPath: /var/lib/10weeksofcloudops
containers:
- name: cloudopsk8
image: registry:2.8.2
ports:
- containerPort: 5000
resources:
requests:
memory: "1Gi"
limits:
memory: "2Gi"
livenessProbe:
tcpSocket:
port: 5000
initialDelaySeconds: 15
periodSeconds: 30
volumes:
- name: data-volume
persistentVolumeClaim:
claimName: registry-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: 10weeksofclouops
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Gi
# Apply this configuration using 
kubectl apply -f registry-deployment.yaml

Service:
Create a YAML file named registry-service.yaml

apiVersion: v1
kind: Service
metadata:
name: cloudops-service
namespace: default
spec:
selector:
app: cloudopsk8
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer
# Apply this configuration using 
kubectl apply -f registry-network-policy.yaml

Secrets and ConfigMap:
You can create a secret for authentication credentials and a ConfigMap for configuration settings. Replace your-username and your-password with your actual credentials.

apiVersion: v1
kind: Secret
metadata:
name: registry-secret
namespace: democloudops
type: docker-registry
data:
.dockerconfigjson: <base64-encoded-docker-config-json>

for <base64-encoded-docker-config-json> write below command

# for linux user
docker login
cat ~/.docker/config.json | base64

#for windows user
[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((Get-Content -Raw -Path "<path-to-your-config-json>")))
# Apply this configuration using 
kubectl apply -f registry-secret.yaml

Create a ConfigMap YAML file named registry-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: registry-config
namespace: default
data:
config.yml: |
registry:
server: registry.example.com
port: 5000
logging:
level: info

You can change config.yml as per your requirements and configuration

# Apply this configuration using 
kubectl apply -f registry-configmap.yaml

Backup and Recovery:
For data backup, consider using Kubernetes tools like Velero to back up the Persistent Volume data. You’ll need to configure Velero for your cluster.
I have used the tool Velero. To download it you can go to the website https://velero.io/docs/v1.11/basic-install/

First, we have to create an S3 bucket on the aws or in another cloud provider if you are not using AWS.

Create Velero Backup Resources:
Create Velero backup resources to specify what to back up. You’ll typically create a Backup and BackupStorageLocation resource. Save the following YAML to a file named velero-backup.yaml

apiVersion: velero.io/v1
kind: Backup
metadata:
name: registry-backup
spec:
includedNamespaces: []

And then create a file name velero-storage-location.yaml

apiVersion: velero.io/v1
kind: BackupStorageLocation
metadata:
name: default
spec:
provider: aws # Specify your storage provider (e.g., aws, azure, gcp)
objectStorage:
bucket: valero-k8-backup # Specify your storage bucket name
prefix: velero

configure and change provider name and bucket as per your requirement and need. I have used AWS and S3 buckets for the same.

kubectl apply -f velero-backup.yaml
kubectl apply -f velero-storage-backup.yaml

Schedule Velero Backups:
Velero supports manual and scheduled backups. To schedule regular backups, you can use a Schedule resource. Save the following YAML to a file named velero-schedule.yaml

apiVersion: velero.io/v1
kind: Schedule
metadata:
name: registry-backup-schedule
spec:
schedule: "0 0 * * *" # Schedule a daily backup at midnight UTC
template:
includedNamespaces:
- cloudopsk8 # Namespace where your Docker registry is deployed
kubectl apply -f velero-schedule.yaml

Restore Data:
To restore data in case of data loss or pod failures, follow Velero's documentation on how to restore backups: https://velero.io/docs/main/restore-tutorial/

To see data backups you can check it by going to the s3 bucket you have created or by writing below command

velero backup get

Perform Cluster Upgrade:
Performing a Kubernetes cluster upgrade is a separate process. You’ll need to follow your cloud provider’s or Kubernetes distribution’s documentation on how to upgrade your cluster to the desired major version. After the upgrade, ensure that your cluster is healthy, including the workload.

If you are using AWS EKS just go to the EKS cluster and by going to edit you can upgrade it.
https://docs.aws.amazon.com/eks/latest/userguide/update-cluster.html

To represent your Kubernetes deployment using Prometheus, Kubernetes Dashboard, Rancher or Portainer, and KubeView

GitHub Repository:
https://github.com/HARSHALJETHWA19/k8-cloudops

Reference :
https://aws.amazon.com/eks/
https://velero.io/docs/v1.11/

Follow me:

Linkedin: https://www.linkedin.com/in/harshaljethwa/

GitHub: https://github.com/HARSHALJETHWA19/

Twitter: https://twitter.com/harshaljethwaa

Thank You!

--

--

Harshal Jethwa

DevOps | Docker | Linux | Jenkins | AWS | Git | Terraform | Technical Blogger