{"id":978,"date":"2023-08-22T11:39:02","date_gmt":"2023-08-22T06:09:02","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=978"},"modified":"2025-02-04T13:53:28","modified_gmt":"2025-02-04T08:23:28","slug":"lab-secrets-and-configmaps","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=978","title":{"rendered":"Lab: Secrets and ConfigMaps"},"content":{"rendered":"<p>This labs contain configmaps and secret labs<\/p>\n<ul>\n<li>ConfigMaps using env and volume<\/li>\n<li>Secrets using env and volume<\/li>\n<\/ul>\n<p>Lab1: Create configmap<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cm.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-980\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cm-300x177.jpg\" alt=\"\" width=\"300\" height=\"177\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cm-300x177.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cm-700x414.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cm-768x454.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cm.jpg 783w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<ol>\n<li><code>apiVersion: v1<\/code>: This indicates that the ConfigMap is using the Kubernetes API version 1.<\/li>\n<li><code>kind: ConfigMap<\/code>: This specifies the type of Kubernetes resource, which is a ConfigMap in this case.<\/li>\n<li><code>metadata<\/code>: This section contains metadata about the ConfigMap, such as its name and other optional information.\n<ul>\n<li><code>name: game-demo<\/code>: This is the name of the ConfigMap.<\/li>\n<\/ul>\n<\/li>\n<li><code>data<\/code>: This section contains the actual configuration data stored within the ConfigMap. The data is organized using key-value pairs.\n<ul>\n<li><code>player_initial_lives: \"3\"<\/code>: This sets a property-like key <code>player_initial_lives<\/code> with the value &#8220;3&#8221;.<\/li>\n<li><code>ui_properties_file_name: \"user-interface.properties\"<\/code>: This sets a property-like key <code>ui_properties_file_name<\/code> with the value &#8220;user-interface.properties&#8221;.<\/li>\n<li><code>game.properties: |<\/code>: This sets a file-like key <code>game.properties<\/code> with a multi-line value. The content following the colon (|) is treated as a block of text.<\/li>\n<li><code>user-interface.properties: |<\/code>: This sets a file-like key <code>user-interface.properties<\/code> with a multi-line value. The content following the colon (|) is treated as a block of text.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre>kubectl create -f cm.yaml\r\nkubectl get cm\r\nkubectl describe cm game-demo<\/pre>\n<p>Lab2: Map the config map to Pod<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cmpod.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-981\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cmpod-300x194.jpg\" alt=\"\" width=\"300\" height=\"194\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cmpod-300x194.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cmpod-700x452.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cmpod-768x496.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/cmpod.jpg 1057w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<ul>\n<li>\n<ul>\n<li><code>env<\/code>: Environment variables to be set within the container.\n<ul>\n<li><code>name: PLAYER_INITIAL_LIVES<\/code>: The name of the environment variable.The <code>valueFrom<\/code> field specifies that the value of this environment variable will be sourced from the &#8220;game-demo&#8221; ConfigMap, specifically from the &#8220;player_initial_lives&#8221; key.<\/li>\n<li><code>name: UI_PROPERTIES_FILE_NAME<\/code>: Another environment variable, sourced from the &#8220;ui_properties_file_name&#8221; key in the &#8220;game-demo&#8221; ConfigMap.<\/li>\n<\/ul>\n<\/li>\n<li><code>volumeMounts<\/code>: Describes how volumes are mounted into containers.\n<ul>\n<li><code>name: config<\/code>: Refers to the name of the volume defined at the Pod level.<\/li>\n<li><code>mountPath: \"\/config\"<\/code>: The path inside the container where the volume should be mounted.<\/li>\n<li><code>readOnly: true<\/code>: The mounted volume is set to read-only.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><code>volumes<\/code>: Defines volumes to be used in the Pod.\n<ul>\n<li><code>name: config<\/code>: The name of the volume.The <code>configMap<\/code> field specifies that this volume is sourced from a ConfigMap named &#8220;game-demo&#8221;.\n<ul>\n<li><code>items<\/code>: An array of keys from the ConfigMap to create as files within the volume.\n<ul>\n<li><code>key: \"game.properties\"<\/code>: The key within the ConfigMap whose value should be used to create a file.\n<ul>\n<li><code>path: \"game.properties\"<\/code>: The path where the file will be created within the volume.<\/li>\n<\/ul>\n<\/li>\n<li><code>key: \"user-interface.properties\"<\/code>: Similarly, this key&#8217;s value will be used to create a file.\n<ul>\n<li><code>path: \"user-interface.properties\"<\/code>: The path where the file will be created within the volume.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>kubectl create -f cm-pod.yaml\r\nkubectl describe pod configmap-demo-pod\r\nkubectl exec configmap-demo-pod -c demo -it -- \/bin\/sh\r\nexport\r\nls \/config\/\r\nexit<\/pre>\n<p>Lab 3: Secret (Create using command line)<\/p>\n<pre>echo -n 'root' &gt; .\/username.txt\r\necho -n 'Mq2D#(8gf09' &gt; .\/password.txt\r\nkubectl create secret generic db-cerds \\\r\n--from-file=.\/username.txt \\\r\n--from-file=.\/password.txt\r\nkubectl get secret\/db-cerds\r\nkubectl describe secret\/db-cerds<\/pre>\n<p>Lab 4: Create Secret using yaml file<\/p>\n<p>encode username and password using base64<\/p>\n<pre>echo -n 'root' | base64\r\necho -n 'Mq2D#(8gf09' | base64<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/sec.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-982\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/sec-300x156.jpg\" alt=\"\" width=\"300\" height=\"156\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/sec-300x156.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/sec.jpg 446w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>kubectl create -f sec.yaml\r\nkubectl get secret\r\nkubectl describe secret database-creds<\/pre>\n<p>Lab 5: Map secret as env in Pod<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podenv.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-983\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podenv-300x232.jpg\" alt=\"\" width=\"300\" height=\"232\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podenv-300x232.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podenv-700x542.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podenv.jpg 769w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>kubectl create -f secenvpod.yaml\r\nkubectl describe pod php-mysql-app\r\nkubectl exec php-mysql-app -c php-app -it -- \/bin\/bash\r\n#check the env value\r\nexport\r\nexit\r\n#Delete the pod\r\nkubectl delete -f secenvpod.yaml<\/pre>\n<p>Lab6: Map secret as volume<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podsec.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-984\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podsec-300x257.jpg\" alt=\"\" width=\"300\" height=\"257\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podsec-300x257.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/podsec.jpg 602w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>kubectl create -f secvolpod.yaml\r\nkubectl describe pod redis-pod\r\n#check inside the container \"\/etc\/dbcreds\"\r\nkubectl exec redis-pod -c redis-pod -it -- ls \/etc\/dbcreds\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This labs contain configmaps and secret labs ConfigMaps using env and volume Secrets using env and volume Lab1: Create configmap apiVersion: v1: This indicates that the ConfigMap is using the Kubernetes API version 1. kind: ConfigMap: This specifies the type of Kubernetes resource, which is a ConfigMap in this case. metadata: This section contains metadata [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-978","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=978"}],"version-history":[{"count":5,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/978\/revisions"}],"predecessor-version":[{"id":1484,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/978\/revisions\/1484"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}