Passing Configuration Data to a Kubernetes Container
Introduction
Kubernetes has multiple options for storing and managing configuration data. This lab will focus on the process of passing that configuration data to your containers in order to configure applications. You will have the opportunity to work with application configuration in Kubernetes hands-on by passing some existing configuration data stored in Secrets and ConfigMaps to a container.
Solution
Log in to the provided lab server using the credentials provided:
ssh cloud_user@<PUBLIC_IP_ADDRESS>
Generate an htpasswd File and Store It as a Secret
Generate an
htpasswdfile:htpasswd -c .htpasswd userCreate a password you can easily remember (we’ll need it again later).
View the file’s contents:
cat .htpasswdCreate a Secret containing the
htpasswddata:kubectl create secret generic nginx-htpasswd --from-file .htpasswdDelete the
.htpasswdfile:rm .htpasswd
Create the Nginx Pod
Create
pod.yml:vi pod.ymlSet vi to ‘paste’ mode by hitting
:, and then typingset paste(ENTER). Then switch back to INSERT mode by hittingi.Enter the following to create the pod and mount the Nginx config and
htpasswdSecret data:apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - name: nginx image: nginx:1.19.1 ports: - containerPort: 80 volumeMounts: - name: config-volume mountPath: /etc/nginx - name: htpasswd-volume mountPath: /etc/nginx/conf volumes: - name: config-volume configMap: name: nginx-config - name: htpasswd-volume secret: secretName: nginx-htpasswdSave and exit the file by pressing Escape followed by
:wq.View the existing ConfigMap:
kubectl get cmGet more info about
nginx-config:kubectl describe cm nginx-configCreate the pod:
kubectl apply -f pod.ymlCheck the status of your pod and get its IP address:
kubectl get pods -o wideIts IP address will be listed once it has a
Runningstatus. We’ll use this in the final two commands.Within the existing
busyboxpod, without using credentials, verify everything is working:kubectl exec busybox -- curl <NGINX_POD_IP>We’ll see HTML for the
401 Authorization Requiredpage — but this is a good thing, as it means our setup is working as expected.Run the same command again using credentials (including the password you created at the beginning of the lab) to verify everything is working:
kubectl exec busybox -- curl -u user:<PASSWORD> <NGINX_POD_IP>This time, we’ll see the
Welcome to nginx!page HTML.