Configuration
- Container Configuration
- Resource Requirements
- ConfigMaps
- Secrets
- SecurityContexts
- Service Accounts
- Activities
Container Configuration
Command and Argument
When you create a Pod, you can define a command and arguments for the containers that run in the Pod.
The command and arguments that you define in the configuration file override the default command and arguments provided by the container image
Dockerfile vs Kubernetes Dockerfile Entrypoint -> k8s command Dockerfile CMD -> k8s args
Ports
When you create a Pod, you can specify the port number the container exposes, as best practice is good to put a name
, this way a service can specify targetport by name reference.
Environment Variable
When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the env or envFrom field in the container configuration
A Pod can use environment variables to expose information about itself to Containers running in the Pod. Environment variables can expose Pod fields and Container fields
Resources
IKS & OpenShift
References
apiVersion: v1kind: Podmetadata:name: my-cmd-podspec:containers:- name: myapp-containerimage: busyboxcommand: ['echo']
apiVersion: v1kind: Podmetadata:name: my-arg-podspec:containers:- name: myapp-containerimage: busyboxcommand: ['echo']
apiVersion: v1kind: Podmetadata:name: my-port-podspec:containers:- name: myapp-containerimage: bitnami/nginxports:
apiVersion: v1kind: Podmetadata:name: my-env-podspec:restartPolicy: Nevercontainers:- name: cimage: busybox
apiVersion: v1kind: Podmetadata:name: my-inter-podlabels:app: jedispec:restartPolicy: Nevercontainers:
Resource Requirements
When you specify a Pod, you can optionally specify how much CPU and memory (RAM) each Container needs. When Containers have resource requests specified, the scheduler can make better decisions about which nodes to place Pods on.
CPU and memory are each a resource type. A resource type has a base unit. CPU is specified in units of cores, and memory is specified in units of bytes.
Resources
IKS & OpenShift
References
apiVersion: v1kind: Podmetadata:name: my-podspec:containers:- name: my-appimage: bitnami/nginxports:
Namespaced defaults mem
apiVersion: v1kind: LimitRangemetadata:name: mem-limit-rangespec:limits:- default:memory: 512MidefaultRequest:
Namespaced defaults mem
apiVersion: v1kind: LimitRangemetadata:name: cpu-limit-rangespec:limits:- default:cpu: 1defaultRequest:
ConfigMaps
ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable.
You can data from a ConfigMap in 3 different ways.
- As a single environment variable specific to a single key
- As a set of environment variables from all keys
- As a set of files, each key represented by a file on mounted volume
Resources
OpenShift
IKS
References
apiVersion: v1kind: ConfigMapmetadata:name: my-cmdata:color: bluelocation: naboo
apiVersion: v1kind: Podmetadata:name: my-podspec:containers:- name: myappimage: busyboxcommand: ["echo"]
apiVersion: v1kind: Podmetadata:name: my-podspec:containers:- name: myappimage: busyboxcommand:
apiVersion: v1kind: Podmetadata:name: my-podspec:containers:- name: myappimage: busyboxcommand: ["/bin/sh", "-c", "env | sort"]
Secrets
Kubernetes secret objects let you store and manage sensitive information, such as passwords, OAuth tokens, and ssh keys. Putting this information in a secret is safer and more flexible than putting it verbatim in a Pod definition or in a container image.
A Secret is an object that contains a small amount of sensitive data such as a password, a token, or a key. Such information might otherwise be put in a Pod specification or in an image; putting it in a Secret object allows for more control over how it is used, and reduces the risk of accidental exposure.
Resources
OpenShift
IKS
References
apiVersion: v1kind: Secretmetadata:name: mysecrettype: Opaquedata:username: YWRtaW4=stringData:admin: administrator
apiVersion: v1kind: Secretmetadata:name: mysecret-configtype: OpaquestringData:config.yaml: |-apiUrl: "https://my.api.com/api/v1"username: token
apiVersion: v1kind: Podmetadata:name: my-podspec:containers:- name: my-appimage: bitnami/nginxports:
Create files needed for rest of example.
echo -n 'admin' > ./username.txtecho -n '1f2d1e2e67df' > ./password.txt
Creating Secret from files.
oc create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
Getting Secret
oc get secrets
Gets the Secret’s Description.
oc describe secrets/db-user-pass
Create files needed for rest of example.
echo -n 'admin' > ./username.txtecho -n '1f2d1e2e67df' > ./password.txt
Creates the Secret from the files
kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
Gets the Secret
kubectl get secrets
Gets the Secret’s Description.
kubectl describe secrets/db-user-pass
SecurityContexts
A security context defines privilege and access control settings for a Pod or Container.
To specify security settings for a Pod, include the securityContext field in the Pod specification. The securityContext field is a PodSecurityContext object. The security settings that you specify for a Pod apply to all Containers in the Pod.
Resources
OpenShift
IKS
References
Setup minikube VM with users
minikube ssh
su -
echo "container-user-0:x:2000:2000:-:/home/container-user-0:/bin/bash" >> /etc/passwdecho "container-user-1:x:2001:2001:-:/home/container-user-1:/bin/bash" >> /etc/passwdecho "container-group-0:x:3000:" >>/etc/groupecho "container-group-1:x:3001:" >>/etc/groupmkdir -p /etc/message/echo "Hello, World!" | sudo tee -a /etc/message/message.txtchown 2000:3000 /etc/message/message.txtchmod 640 /etc/message/message.txt
Using the this securityContext
the container will be able to read the file /message/message.txt
apiVersion: v1kind: Podmetadata:name: my-securitycontext-podspec:securityContext:runAsUser: 2000runAsGroup: 3000fsGroup: 3000
Using the this securityContext
the container should NOT be able to read the file /message/message.txt
apiVersion: v1kind: Podmetadata:name: my-securitycontext-podspec:securityContext:runAsUser: 2001runAsGroup: 3001fsGroup: 3001
Run to see the errors
Get Pod Logs
oc logs my-securitycontext-pod
Should return
cat: can't open '/message/message.txt': Permission denied
Get Pod Logs
kubectl logs my-securitycontext-pod
Should return
cat: can't open '/message/message.txt': Permission denied
Service Accounts
A service account provides an identity for processes that run in a Pod.
When you (a human) access the cluster (for example, using kubectl), you are authenticated by the apiserver as a particular User Account (currently this is usually admin, unless your cluster administrator has customized your cluster). Processes in containers inside pods can also contact the apiserver. When they do, they are authenticated as a particular Service Account (for example, default).
User accounts are for humans. Service accounts are for processes, which run in pods.
User accounts are intended to be global. Names must be unique across all namespaces of a cluster, future user resource will not be namespaced. Service accounts are namespaced.
Resources
OpenShift
IKS
References
apiVersion: v1kind: ServiceAccountmetadata:name: my-service-account
apiVersion: v1kind: Podmetadata:name: my-podspec:serviceAccountName: my-service-accountcontainers:- name: my-appimage: bitnami/nginx
apiVersion: v1kind: Secretmetadata:name: build-robot-secretannotations:kubernetes.io/service-account.name: my-service-accounttype: kubernetes.io/service-account-token
Creating a ServiceAccount
oc create sa <service_account_name>
View ServiceAccount Details
oc describe sa <service_account_name>
Create a ServiceAccount
kubectl create sa <service_account_name>
View ServiceAccount Details
kubectl describe sa <service_account_name>
Activities
Task | Description | Link |
---|---|---|
Try It Yourself | ||
Pod Creation | Challenge yourself to create a Pod YAML file to meet certain parameters. | Pod Creation |
Pod Configuration | Configure a pod to meet compute resource requirements. | Pod Configuration |