Configure a project with restrictions that prevent its users and applications from consuming all capacity of a cluster.
Outcomes
Configure project creation to use a custom project template.
Limit resource usage for new projects.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that the cluster API is reachable.
[student@workstation ~]$ lab start selfservice-review
Instructions
Log in to your OpenShift cluster as the admin user with the redhatocp password.
Design a project template with the following properties:
The user who requests the project has the default admin role binding.
The workloads in the project cannot request a total of more than 2 GiB of RAM, and they cannot use more than 4 GiB of RAM.
Each workload in the project has the following properties:
Default memory request of 256 MiB
Default memory limit of 512 MiB
Minimum memory request of 128 MiB
Maximum memory usage of 1 GiB
You can use the oc create quota command to create the resource quota without creating a YAML definition.
A template for the limit range is available at ~/DO280/labs/selfservice-review/limitrange.yaml.
You can create a template-test namespace to design your project template.
The next steps assume that you design the template in a template-test namespace.
The lab scripts clean and grade the design namespace only if you create it with the template-test name.
Use the oc command to create a template-test namespace.
[student@workstation ~]$ oc create namespace template-test
namespace/template-test createdUse the oc create quota command to create the memory quota in the template-test namespace.
[student@workstation ~]$ oc create quota memory \
--hard=requests.memory=2Gi,limits.memory=4Gi \
-n template-test
resourcequota/memory createdEdit the limit range definition at ~/DO280/labs/selfservice-review/limitrange.yaml.
Replace the CHANGE_ME text to match the following file:
apiVersion: v1 kind: LimitRange metadata: name:memorynamespace:template-testspec: limits: - min: memory:128MidefaultRequest: memory:256Midefault: memory:512Mimax: memory:1Gitype: Container
Use the oc command to create the limit range.
[student@workstation ~]$ oc create \
-f ~/DO280/labs/selfservice-review/limitrange.yaml
limitrange/memory createdVerify that the quota and limit range have the intended effect.
For example, create a deployment that uses the registry.ocp4.example.com:8443/redhattraining/hello-world-nginx:v1.0 image without resource specifications.
Verify that the limit range adds requests and limits to the pods.
Scale the deployment to 10 replicas.
Examine the deployment and the quota to verify that they have the intended effect.
If you design your template without creating a test namespace, then you must verify your design by other means.
Use the oc create deployment command to create a deployment without resource specifications.
[student@workstation ~]$ oc create deployment -n template-test test-limits \
--image registry.ocp4.example.com:8443/redhattraining/hello-world-nginx:v1.0
deployment.apps/test-limits createdUse the oc command to view the resources key of the container specification.
Optionally, use the jq command to indent the output.
[student@workstation ~]$ oc get pod -n template-test \
-o jsonpath='{.items[0].spec.containers[0].resources}'
{"limits":{"memory":"512Mi"},"requests":{"memory":"256Mi"}}Although you create the deployment without specifying resources, the limit range applies RAM requests and limits.
Use the oc scale command to scale the deployment to verify that the quota has an effect.
[student@workstation ~]$ oc scale deployment -n template-test test-limits \
--replicas=10
deployment.apps/test-limits scaledExamine the deployment and the quota.
[student@workstation ~]$oc get deployment -n template-testNAME READY UP-TO-DATE AVAILABLE AGE test-limits8/108 8 8m41s
[student@workstation ~]$oc describe resourcequota -n template-test memoryName: memory Namespace: template-test Resource Used Hard -------- ---- ----limits.memory 4Gi 4Girequests.memory 2Gi 2Gi
The deployment uses the quota completely, and scales only to eight pods. Each pod requests 256 MiB of RAM, and eight pods request 2 GiB of RAM. Each pod has a 512 MiB RAM limit, and eight pods have a 4 GiB RAM limit.
Create a project template definition with the same properties.
The solution for this step assumes that you designed your template in a template-test namespace.
If you do not create a template-test namespace to design the template, then you must create the project template by other means.
The ~/DO280/solutions/selfservice-review/template.yaml file contains a solution.
Use the oc adm create-bootstrap-project-template to print an initial project template.
Redirect the output to the template.yaml file.
[student@workstation ~]$ oc adm create-bootstrap-project-template \
-o yaml >template.yamlUse the oc command to list the limit ranges and quotas in YAML format.
Redirect the output to append to the template.yaml file.
[student@workstation ~]$ oc get limitrange,quota -n template-test \
-o yaml >>template.yamlEdit the template.yaml file to perform the following operations:
Move the limit range and quota definitions immediately after the role binding definition.
Remove any left-over content after the parameters block.
Remove the following keys from the limit range and quota definitions:
creationTimestamp
resourceVersion
uid
status
Replace the namespace: template-test text with the namespace: ${PROJECT_NAME} text.
If you use the vi editor, then you can use the following procedure to move a block of text:
Move to the beginning of the block.
Press V to enter visual line mode. This mode selects entire lines for manipulation.
Move to the end of the block. The editor highlights the selected lines.
Press d to delete the lines and to store them in a register for later usage.
Move to the destination.
Press P to insert the lines that are stored in the register.
You can also press dd to delete entire lines, and . to repeat the operation.
The resulting file should match the following content:
apiVersion: template.openshift.io/v1
kind: Template
metadata:
creationTimestamp: null
name: project-request
objects:
- apiVersion: project.openshift.io/v1
kind: Project
metadata:
annotations:
openshift.io/description: ${PROJECT_DESCRIPTION}
openshift.io/display-name: ${PROJECT_DISPLAYNAME}
openshift.io/requester: ${PROJECT_REQUESTING_USER}
creationTimestamp: null
name: ${PROJECT_NAME}
spec: {}
status: {}
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
creationTimestamp: null
name: admin
namespace: ${PROJECT_NAME}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: admin
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: ${PROJECT_ADMIN_USER}
- apiVersion: v1
kind: LimitRange
metadata:
name: memory
namespace: ${PROJECT_NAME}
spec:
limits:
- default:
memory: 512Mi
defaultRequest:
memory: 256Mi
max:
memory: 1Gi
min:
memory: 128Mi
type: Container
- apiVersion: v1
kind: ResourceQuota
metadata:
name: memory
namespace: ${PROJECT_NAME}
spec:
hard:
limits.memory: 4Gi
requests.memory: 2Gi
parameters:
- name: PROJECT_NAME
- name: PROJECT_DISPLAYNAME
- name: PROJECT_DESCRIPTION
- name: PROJECT_ADMIN_USER
- name: PROJECT_REQUESTING_USERCreate and configure the project template.
Use the oc command to create the project template.
[student@workstation ~]$ oc create -f template.yaml -n openshift-config
template.template.openshift.io/project-request createdUse the oc edit command to change the cluster project configuration.
[student@workstation ~]$ oc edit projects.config.openshift.io clusterEdit the resource to match the following content:
apiVersion: config.openshift.io/v1
kind: Project
metadata:
...output omitted...
name: cluster
spec:
projectRequestTemplate:
name: project-requestTo edit the file, you use the default vi editor.
Use the watch command to view the API server pods.
[student@workstation ~]$ watch oc get pod -n openshift-apiserver
NAME READY STATUS RESTARTS AGE
apiserver-5cfd... 2/2 Running 0 2m50sWait until new pods are rolled out.
Press Ctrl+C to exit the watch command.
Create a project to verify that the template works as intended.
The lab scripts clean up only a template-validate namespace.
Use the oc new-project command to create a template-validate project.
[student@workstation ~]$ oc new-project template-validate
Now using project "template-validate" on server "https://api.ocp4.example.com:6443".
...output omitted...Describe the quotas in the current namespace.
[student@workstation ~]$ oc describe quota
Name: memory
Namespace: template-validate
Resource Used Hard
-------- ---- ----
limits.memory 0 4Gi
requests.memory 0 2GiDescribe the limit ranges in the current namespace.
[student@workstation ~]$ oc describe limitrange
Name: memory
Namespace: template-validate
Type Resource Min Max Default Request Default Limit ...
---- -------- --- --- --------------- ------------- ...
Container memory 128Mi 1Gi 256Mi 512Mi ...Optionally, execute again the commands that you used earlier to create a deployment. Scale the deployment, and verify the limits.