Passing Configuration Data to a Kubernetes Container
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.
Log in to the provided lab server using the credentials provided:
htpasswd File and Store It as a Secret
htpasswd -c .htpasswd user
Create a password you can easily remember (we’ll need it again later).
View the file’s contents:
Create a Secret containing the
kubectl create secret generic nginx-htpasswd --from-file .htpasswd
Create the Nginx Pod
Set vi to ‘paste’ mode by hitting
:, and then typing
set paste(ENTER). Then switch back to INSERT mode by hitting
Enter the following to create the pod and mount the Nginx config and
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-htpasswd
Save and exit the file by pressing Escape followed by
View the existing ConfigMap:
kubectl get cm
Get more info about
kubectl describe cm nginx-config
Create the pod:
kubectl apply -f pod.yml
Check the status of your pod and get its IP address:
kubectl get pods -o wide
Its 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.