Kubernetes / Kubernetes Configuration Management

Managing Secrets for Sensitive Data

This tutorial will guide you on how to manage sensitive data using Secrets in Kubernetes. Secrets provide a more secure way of storing and managing sensitive data like passwords a…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explains how to manage application configurations in Kubernetes.

Introduction

This tutorial aims to guide you on how to manage sensitive data using Secrets in Kubernetes. Secrets offer a secure method to handle sensitive information such as passwords, tokens, or keys, ensuring they aren't exposed in your application code.

By the end of this tutorial, you'll learn:

  • What Kubernetes Secrets are and why they're essential.
  • How to create and use Secrets in Kubernetes.
  • Best practices for managing sensitive data with Kubernetes Secrets.

Prerequisites:

Before starting this tutorial, you should have:

  • Basic knowledge of Kubernetes
  • Access to a Kubernetes cluster for practical exercises

Step-by-Step Guide

Kubernetes Secrets are objects that contain small amounts of sensitive data like passwords, OAuth tokens, and ssh keys. They are used to store non-public information, allowing you to manage sensitive data.

Creating a Secret:

You can create a secret using kubectl create secret command. For example, to create a secret named my-secret with the key my-key and value my-value, use the following command:

kubectl create secret generic my-secret --from-literal=my-key=my-value

Using a Secret:

You can use secrets in pods either as files from a volume mounted on one or more of its containers, or by the kubelet pulling images for the pod.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
  volumes:
  - name: foo
    secret:
      secretName: my-secret

In the above example, the secret my-secret is mounted on a volume foo and the Pod my-pod has access to this secret.

Code Examples

Example 1:

Create a Secret:

kubectl create secret generic my-secret --from-literal=username=my-username --from-literal=password=my-password

This command creates a secret named my-secret with two keys username and password.

Example 2:

Use the secret in a Pod:

apiVersion: v1
kind: Pod
metadata:
  name: secret-pod
spec:
  containers:
  - name: test-container
    image: test-image
    volumeMounts:
    - name: my-volume
      mountPath: "/etc/secrets"
  volumes:
  - name: my-volume
    secret:
      secretName: my-secret

In this example, the secret my-secret is mounted on a volume my-volume. The test-container in the secret-pod Pod can use this secret.

Summary

In this tutorial, we've learned how to use Kubernetes Secrets to manage sensitive data. We've learned how to create a secret and use it in a Pod. To explore further, you can look at how to use Secrets for environment variables and how to use Secrets with a service account.

Practice Exercises

Exercise 1: Create a secret named test-secret with the key api-key and value 123456.

Solution:

kubectl create secret generic test-secret --from-literal=api-key=123456

Exercise 2: Create a Pod that uses the test-secret in a volume.

Solution:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: test-container
    image: test-image
    volumeMounts:
    - name: test-volume
      mountPath: "/etc/secrets"
  volumes:
  - name: test-volume
    secret:
      secretName: test-secret

Exercise 3: Extend the Pod created in Exercise 2 to read the api-key from the secret and print it out.

Solution:

This exercise depends on your application in the test-image having the ability to read a file and print its contents. Here's an example if you're using a bash script:

#!/bin/bash
api_key=$(cat /etc/secrets/api-key)
echo "API Key: $api_key"

This script reads the api-key from the mounted secret and prints it.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Date Difference Calculator

Calculate days between two dates.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

Unit Converter

Convert between different measurement units.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help