Kubernetes simple deployment with service and configmap
Intro
This article is based on the previois installation: Install Kubernetes 1.26 on Debian 11 with Flannel, the aim is to setup:
- Simple cluster of two or more nginx pods
- Create a service on top
- Use the Configmap to create content
- Apply the new content
Deployment
deployment of 2 Nginx Pods (Replicas = 2)
- nginx-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: frontend-deployment labels: app: web tier: frontend spec: replicas: 2 selector: matchLabels: tier: frontend template: metadata: labels: app: web tier: frontend spec: containers: - image: nginx:latest name: nginx ports: - containerPort: 30080 name: nginx
- Apply
kubectl apply -f nginx-deplyoment.yaml
- List
kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES frontend-deployment-6fb5bf7f54-4l2nv 1/1 Running 0 36s 10.244.2.12 vm-c1-worker-2 <none> <none> frontend-deployment-6fb5bf7f54-mmfqb 1/1 Running 0 36s 10.244.1.10 vm-c1-worker-1 <none> <none>
Service
The service maps the label/app to web and associate the container port to port 80
- service.yaml
apiVersion: v1 kind: Service metadata: name: example-service labels: app: web spec: type: NodePort selector: app: web ports: - protocol: TCP targetPort: 80 port: 80 nodePort: 30080
- Apply
kubectl apply -f service.yaml
- List
kubectl get service -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR example-service NodePort 10.111.177.255 <none> 80:30080/TCP 8s app=web
Testing
- Apply commands to the Pod, we want to add the hostname to the nginx welcome page
kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES frontend-deployment-6fb5bf7f54-nq2bl 1/1 Running 0 36s 10.244.2.12 vm-c1-worker-2 <none> <none> frontend-deployment-6fb5bf7f54-zj44m 1/1 Running 0 36s 10.244.1.10 vm-c1-worker-1 <none> <none>
Run commands inside of the pod
- Get the hostname
kubectl exec frontend-deployment-6fb5bf7f54-nq2bl -- hostname
- Replace the message on the first Pod:
export khost=frontend-deployment-6fb5bf7f54-nq2bl kubectl exec $khost -- \ sed -ir "s/Welcome to nginx/Welcome to nginx on $HOSTNAME/gi" /usr/share/nginx/html/index.html
- Replace the message on the second Pod:
export khost=frontend-deployment-6fb5bf7f54-zj44m kubectl exec $khost -- \ sed -ir "s/Welcome to nginx/Welcome to nginx on $HOSTNAME/gi" /usr/share/nginx/html/index.html
- Use curl to alternate between the pods
curl -v --silent 10.111.177.255 2>&1 | grep '<title>'
Get own content into the Nginx Pods
Configmap
Get content to the cluster, create a simple index.html and add it to a configmap
kubectl create configmap index.html --from-file index.html kubectl get configmaps NAME DATA AGE index.html 1 5m23s
kubectl describe configmap index.html Name: index.html Namespace: default Labels: <none> Annotations: <none> Data ==== index.html: ... ...
Rewrite the deployment
- Add the volumes to nginx-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: frontend-deployment labels: app: web tier: frontend spec: replicas: 2 selector: matchLabels: tier: frontend template: metadata: labels: app: web tier: frontend spec: containers: - image: nginx:latest name: nginx ports: - containerPort: 30080 name: nginx volumeMounts: - name: htmlcontent mountPath: "/usr/share/nginx/html/" readOnly: true volumes: - name: htmlcontent configMap: name: index.html items: - key: index.html path: index.html
kubectl apply -f nginx-deplyoment.yaml