aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
Laptop | Tools
  • Computing
  • Engineering
  • Practices

Best Practices For Securing Kubernetes Deployments

  • aster.cloud
  • December 22, 2023
  • 4 minute read

Although Kubernetes is a powerful container orchestration platform, its complexity and its adoption makes it a prime target for security attacks. We’ll go over some of the best practices for securing the Kubernetes deployments and keeping applications and data safe in this article.

This article is only about pods or deployments; I intend to cover other security related topics in subsequent articles.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

Below is a list of settings and configurations that can be implemented to accomplish the intended goal.

  • securityContext.allowPrivilegeEscalation
    • The securityContext.allowPrivilegeEscalation setting determines whether a container’s privileges can be escalated. When true, it grants a container additional privileges beyond those granted by default.
    • Setting allowPrivilegeEscalation to false can help reduce the risk of privilege escalation attacks.
  containers:
  - name: webapp
    image: nginx:1.17
    securityContext:
      allowPrivilegeEscalation: false

  # Output trimmed

  • securityContext.runAsNonRoot
    • The securityContext.runAsNonRoot setting is used to prevent containers from being run as the root user, which can be dangerous.
    • When runAsNonRoot is set to true, the container is started with a non-root user ID (UID) instead of the default root UID of 0.
    • The securityContext section is used in the following example to set runAsNonRoot to true. This means the container will be started with a non-root UID.
    • It’s generally recommended to run containers as non-root whenever possible to reduce the risk of privilege escalation attacks. Keep in mind, however, that some applications may require root access to function properly.
  containers:
  - name: webapp
    image: nginx:1.17
    securityContext:
      runAsNonRoot: true

  # Output trimmed

  • securityContext.readOnlyRootFilesystem
    • The securityContext.readOnlyRootFilesystem setting is used to prevent write access to a container’s root filesystem.
    • When this setting is enabled and set to true, the container’s root filesystem is mounted as read-only, resulting in a runtime error if any attempt to write to the root filesystem fails.
    • Enabling this will for sure reduce the attack surface, However, do keep in mind that this setting may not be appropriate for all containers/applications, particularly those that require write access to the root filesystem to function properly.
  containers:
  - name: webapp
    image: nginx:1.17
    securityContext:
      readOnlyRootFilesystem: true

  # Output trimmed

  • securityContext.runAsUser
    • The securityContext.runAsUser setting in Kubernetes is used to specify the user ID that should be used to run a container.
    • Containers are run as the root user by default, which can pose a security risk if an attacker gains access to the container.
    • To reduce the risk of privilege escalation attacks, containers should be run as non-root users whenever possible.
    • This configuration can be used at the pod and/or container levels; if set at the container level, it will override the pod’s configuration.
  securityContext:
    runAsUser: 1000
  containers:
  - name: webapp
    image: nginx:1.17

  # Output trimmed

  • securityContext.runAsGroup
    • The securityContext.runAsGroup setting specifies the group ID under which the container’s main process should run.
    • This configuration too can be used at the pod and/or container levels; if set at the container level, it will override the pod’s configuration.
  containers:
  - name: webapp
    image: nginx:1.17
    securityContext:
      runAsGroup: 1000

  # Output trimmed

  • securityContext.capabilities
    • It is recommended that containers drop all capabilities, and only authorized or permitted ones should be added if necessary.
    • This helps to mitigate the risk of potential privilege escalation attacks on the containers.
    • Set the capabilities field to an empty object {} to remove all Linux capabilities from the container.
  containers:
  - name: webapp
    image: nginx:1.17
    securityContext:
      capabilities: {}

  # Output trimmed

  • securityContext.capabilities.add
    • If required you can use add to specify specific capabilities.
  containers:
  - name: webapp
    image: nginx:1.17
    securityContext:
      capabilities:
        add:
        - SYS_TIME

  # Output trimmed

  • securityContext.capabilities.drop
    • If required you can use drop to remove specific capabilities.
  containers:
  - name: webapp
    image: nginx:1.17
    securityContext:
      capabilities:
        drop:
        - SYS_ADMIN

  # Output trimmed

NOTE:

  • For more information around capabilities, fire man capabilities.
  • resources.limits.cpu
  • resources.limits.memory
    • The settings resources.limits.cpu and resources.limits.memory specifies the maximum amount of CPU/Memory that a container can use.
    • It’s used to restrict the amount of CPU/Memory resources that a container can use.
  containers:
  - name: webapp
    image: nginx:1.17
    resources:
      limits:
        cpu: "1"
        memory: "512Mi"

  # Output trimmed

  • resources.requests.cpu
  • resources.requests.memory
    • The settings resources.requests.cpu and resources.requests.memory specifies the minimum amount of CPU/Memory that a container should use.
    • It’s used to allocate the amount of CPU/Memory resources on the node for container.
  containers:
  - name: webapp
    image: nginx:1.17
    resources:
      requests:
        cpu: "0.5"
        memory: "256Mi"

  • replicas
    • A replica is a duplicate of a pod that runs a single application. When you deploy an application in Kubernetes, you can use the replicas key to specify the number of replicas you want.
    • This instructs Kubernetes on how many instances of the pod should be running at any given time.
    • In below example we are specifying replicas as 3, which mean 3 identical pods will run on the cluster.
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: webapp
  spec:
    replicas: 3

  # Output trimmed

  • image
    • When deploying applications in production, the deployment or the pods should specify an image tag. It is best to avoid using the :latest image tag or no tag.
    • By doing this, it becomes difficult to determine which version of the image is in use and to roll back the version.
    • In below example we are specifying the tag as 1.17 for nginx image.
  containers:
  - name: webapp
    image: nginx:1.17

  # Output trimmed

  • namespace
    • Deployments should not be configured with the ‘default’ namespace; ensure that the default namespace is not used.
    • In below example we are specifying namespace as frontend, where the application webapp will be deployed.
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: webapp
    namespace: frontend
  spec:

  # Output trimmed

NOTE:

  • There are many other securityContext options available, but these are the ones that are most commonly used.
Read More  Extending Anthos To Manage On-Premises Edge VMs: Now Generally Available

Just for reference here is the list of options which are accepted on pod layer (to know more about it fire kubectl explain pod.spec.securityContext):

  • fsGroup
  • fsGroupChangePolicy
  • runAsGroup
  • runAsNonRoot
  • runAsUser
  • seLinuxOptions
  • seccompProfile
  • supplementalGroups
  • sysctls
  • windowsOptions

And below are the ones which are accepted on container layer (to know more about it fire kubectl explain pod.spec.containers.securityContext):

  • allowPrivilegeEscalation
  • capabilities
  • privileged
  • procMount
  • readOnlyRootFilesystem
  • runAsGroup
  • runAsNonRoot
  • runAsUser
  • seLinuxOptions
  • seccompProfile
  • windowsOptions

If there are any important configurations or use cases that I may have missed from deployments perspective, please feel free to add them.

References:

  • https://kubernetes.io/docs/home/
  • Linux man pages.
  • Documentation from kubectl explain command.

Community post originally published on DEV.to by Sunny Bhambhani

By: Sunny Bhambhani
Published at: Cloud Native Computing Foundation

Source: cyberpogo.com


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • Best Practices
  • Cloud
  • Containers
  • Kubernetes
  • Security
You May Also Like
Getting things done makes her feel amazing
View Post
  • Computing
  • Data
  • Featured
  • Learning
  • Tech
  • Technology

Nurturing Minds in the Digital Revolution

  • April 25, 2025
View Post
  • Computing
  • Public Cloud
  • Technology

United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services

  • April 15, 2025
View Post
  • Engineering
  • Technology

Guide: Our top four AI Hypercomputer use cases, reference architectures and tutorials

  • March 9, 2025
Microsoft’s Majorana 1 chip carves new path for quantum computing
View Post
  • Computing
  • Technology

Microsoft’s Majorana 1 chip carves new path for quantum computing

  • February 19, 2025
View Post
  • Computing
  • Engineering

Why a decades old architecture decision is impeding the power of AI computing

  • February 19, 2025
CES 2025: Intel Shows Off Its AI Tech
View Post
  • Computing
  • Technology

CES 2025: Intel Shows Off Its AI Tech

  • January 23, 2025
View Post
  • Engineering
  • Software Engineering

This Month in Julia World

  • January 17, 2025
View Post
  • Engineering
  • Software Engineering

Google Summer of Code 2025 is here!

  • January 17, 2025

Stay Connected!
LATEST
  • notta-ai-header 1
    Notta vs Otter: Which AI Transcription Tool Has the Edge in 2025? (HackerNoon)
    • May 16, 2025
  • college-of-cardinals-2025 2
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 3
    The World Is Revalidating Itself
    • May 6, 2025
  • oracle-ibm 4
    IBM and Oracle Expand Partnership to Advance Agentic AI and Hybrid Cloud
    • May 6, 2025
  • 5
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 6
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 7
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 8
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 9
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 10
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 2
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
  • 3
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 4
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 5
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.