2. Configuration (18%)
1. Configuring a Pod to Use a ConfigMap
Create a new file named
config.txtwith the following environment variables as key/value pairs on each line.
DB_URL equates to localhost:3306
DB_USERNAME equates to postgresCreate a new ConfigMap named
db-configfrom that file.Create a Pod named
backendthat uses the environment variables from the ConfigMap and runs the container with the imagenginx.Shell into the Pod and print out the created environment variables. You should find
DB_URLandDB_USERNAMEwith their appropriate values.
eg: Create the environment variables in the text file.
$ echo -e "DB_URL=localhost:3306\nDB_USERNAME=postgres" > config.txtCreate the ConfigMap and point to the text file upon creation.
$ kubectl create configmap db-config --from-env-file=config.txt
configmap/db-config created
$ kubectl run backend --image=nginx --restart=Never -o yaml --dry-run > pod.yaml官网参考:configmap The final YAML file should look similar to the following code snippet.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: backend
name: backend
spec:
containers:
- image: nginx
name: backend
envFrom:
- configMapRef:
name: db-config
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}Create the Pod by pointing the create command to the YAML file.
Log into the Pod and run the env command.
2. Configuring a Pod to Use a Secret
Create a new Secret named
db-credentialswith the key/value pairdb-password=passwd.Create a Pod named
backendthat defines uses the Secret as environment variable namedDB_PASSWORDand runs the container with the imagenginx.Shell into the Pod and print out the created environment variables. You should find
DB_PASSWORDvariable.
答案: It’s easy to create the secret from the command line. Furthermore, execute the run command to generate the YAML file for the Pod. 参考官网:secret
Edit the YAML file and create an environment that reads the relevant key from the secret. 参考官网: secret
Create the Pod by pointing the create command to the YAML file.
You can find the environment variable by shelling into the container and running the env command.
3. Creating a Security Context for a Pod
Create a Pod named
securedthat uses the imagenginxfor a single container. Mount an emptyDir volume to the directory/data/app.Files created on the volume should use the filesystem group ID 3000.
Get a shell to the running container and create a new file named
logs.txtin the directory/data/app. List the contents of the directory and write them down.
答案: Start by creating the Pod definition as YAML file.
Edit the YAML file, add a volume and a volume mount. Add a security context with the relevant group ID.
Create the Pod and log into the container. Create the file in the directory of the volume mount. The group ID should be 3000 as defined by the security context.
4. Defining a Pod’s Resource Requirements
Create a resource quota named apps under the namespace rq-demo using the following YAML definition in the file rq.yaml.
Create a new Pod that exceeds the limits of the resource quota requirements. Write down the error message.
Change the request limits to fulfill the requirements to ensure that the Pod could be created successfully. Write down the output of the command that renders the used amount of resources for the namespace. 答案: First create the namespace and the resource quota in the namespace.
Next, create the YAML file named pod.yaml with more requested memory than available in the quota. 参考官网:resource-quotas
Create the Pod and observe the error message.
Lower the memory settings to less than 500m (e.g. 200m) and create the Pod.
5. Using a Service Account
Create a new service account named
backend-team.Print out the token for the service account in YAML format.
Create a Pod named
backendthat uses the imagenginxand the identity backend-team for running processes.Get a shell to the running container and print out the token of the service account.
First, create the service acccount and inspect it.
Next, you can create a new Pod and assign the service account to it.
You can print out the token from the volume source at /var/run/secrets/kubernetes.io/serviceaccount.
最后更新于