Kubernetes YAML
This article explains about what is Kubernetes YAML, its structure, references for different activities. Talks about YAML editors & validator and different techniques to generate YAML file from existing system. It contains 4 main parts
- apiVersion: Version of API to create or modify the object. [v1 | v2]
- kind:
- Represent Kubernetes objects and state of your cluster.
- [Deployment | Pod | ConfigMap | Replicaset | Screte | LimitRanges DaemonSet | ReplicationController | Job].
- The first character of 'kind' should always be capital as per naming conventions of Kubernetes.
- Is a “record of intent”–once you create the object, the Kubernetes system will constantly work to ensure that the object exists.
- Ensure cluster desired state
- metadata:
- Uniquely identify the object using the name, UID or namespace.
- [Meta information about kind, usually it contains key-value pair]
- Each object in the cluster has a NAME that is unique for that type of resource.
- Kubernetes auto-assign a unique ID to each object in the cluster. Universally unique.
- You can apply labels to object to define its characteristics, like
metadata: name: label-demo labels: environment: production app: nginx - spec:
- Desired state of the object
- Specification of a kind
Specification
- Deployment
- Upgrade instance [Tolling (default), blue-green, canery]
- Run multiple instances
- Rollback changes
- Apply changes with pause
- Replicaset - Ensure desire state configuration for pod
- Deployment is a higher-level concept that manages ReplicaSets
- Use Deployments instead of directly using ReplicaSets
- Pod
- ConfigMap
- Allow you to decouple configuration artifacts from image content to keep containerized applications portable.
- Secrete
- Store and manage sensitive information, such as passwords, OAuth tokens, and ssh keys.
- contains a small amount of sensitive data
- To use a secret, a pod needs to reference the secret. A secret can be used with a Pod in two ways:
- As files in a volume mounted on one or more of its containers.
- By the kubelet when pulling images for the Pod.
- Built-in secrete - automatically creates secrets that contain credentials for accessing the API and automatically modifies your pods to use this type of secret.
- Custom Secrete - for example, user credentials required by Pods to access a database
- Secrets are not secure as they are base64 encoded strings only.
- LimitRange
- Assign a resource request and a resource limit to a Container.
- ReplicaSet
- Ensure a stable set of replica pods at a given point of time.
- Guarantee availability of a specified number of identical pods
- Use Deployments instead of directly using ReplicaSets as Deployment is the higher-level component that manages REplicaSets
- Alternative to ReplicaSet
- Deployment (Recommended)
- Job
- DaemonSet
- ReplicationController
- BarePod
- Job
- creates one or more Pods and ensures that a specified number of them successfully terminate.
- Deleting a job will clean up its pods
- The job object will create a new pod if the first pod fails of deleted.
- Reliably run a pod to its completion
- Use a job to run multiple pods in parallel
- DaemonSet
- Ensure all or some nodes runs a copy of a pod.
- Deleting a DaemonSet will cleanup the pod created by DaemonSet.
- Use case
- Storage daemon
- Logs collection
- Monitoring
- Scheduling
- Scheduled by the default scheduler
- DaemonSet pods are created and scheduled by the DaemonSet controller
- DaemonSet pods are not created in the Pending state.
- Communicating with daemon pod
- Push -
- Node IP & known port
- DNS
- Service
- Alternative to DaemonSet
- Bare Pods
- Static Pods
- Deployments
- ReplicationController
- Ensures a homogeneous set of pods is always up and available.
Examples
Pod
YAMLapiVersion: v1
kind: Pod
metadata:
name: rss-site
labels:
app: web
spec:
containers:
- name: front-end
image: nginx
ports:
- containerPort: 80
CLI
kubectl run --generator=run-pod/v1 --image=nginx
#Only generate Yaml file
kubectl run --generator=run-pod/v1 --image=nginx --dry-run -o yaml
Referencehttps://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/
Deployment Yaml
YAML apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
CLI kubectl run --generator=run-pod/v1 --image=nginx
#Only generate Yaml file
kubectl run --generator=run-pod/v1 --image=nginx --dry-run -o yaml
Referencehttps://kubernetes.io/docs/concepts/workloads/controllers/deployment/
ConfigMap Yaml - kubectl get configmaps app-config -o yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
queue.properties:
maxlength:10
topic: abx
sql.properties:
server: .
database: app-db
CLI
kubectl create configmap kubectl describle configmap app-config # Create the configmap from directories #command packages each file, in /configmap directory into the game-config ConfigMap kubectl create configmap game-config --from-file=configure-pod-container/configmap/
Reference
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
ReplicaSet YAML
apiVersion: v1
kind: LimitRange
metadata:
name: mem-limit-range
spec:
limits:
- default:
memory: 512Mi
defaultRequest:
memory: 256Mi
type: Container
CLI
kubectl describe replicaset kubectl create replicasetReference
https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/
Secrete YAML
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
----------------------------------------------------------------
Below is an example of a Pod that mounts a Secret in a volume:
----------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: redis
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
CLI
kubectl describe secerete kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txtReference
https://kubernetes.io/docs/concepts/configuration/secret/
LimitRange YAML
apiVersion: v1
kind: LimitRange
metadata:
name: mem-limit-range
spec:
limits:
- default:
memory: 512Mi
defaultRequest:
memory: 256Mi
type: Container
----------------------
Limiting pod compute resources
---------------------
apiVersion:v1
kind:LimitRange
metadata:
name:limit-cpu-per-pod
spec:
limits:
- max:
cpu:"2"
memory:"2Gi"
type:pod
CLIkubectl create limitrange/mem-limit-range
kubectl describe limitrange/mem-limit-range
Referencehttps://kubernetes.io/docs/tasks/configure-pod-container/
DaemonSet
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
CLIkubectl apply -f https://k8s.io/examples/controllers/daemonset.yaml
kubectl describe daemonset/fluentd-elasticsearch
Referencehttps://kubernetes.io/docs/concepts/workloads/controllers/daemonset/
Job
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
CLIkubectl apply -f https://kubernetes.io/examples/controllers/job.yaml
kubectl describe job/pi
Referencehttps://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
Replication Controller
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
CLIkubectl apply -f https://k8s.io/examples/controllers/replication.yaml
kubectl describe replicationcontroller/nginx
Referencehttps://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/