Kubernetes For Beginners
Kubernetes For Beginners
Definition
Key Concepts
Pods
Nodes
Nodes are the machines (virtual or physical) that run the applications in
the form of Pods. Each node contains the necessary services to run
Pods and is managed by the Kubernetes master.
Clusters
Deployments
Services
Secrets
ConfigMap
Ingress
StatefulSet
Volumes in Kubernetes
Volumes in Kubernetes provide a way to store data that is accessible to
containers in a Pod. Unlike ephemeral storage, which is tied to the lifecycle
of a container, volumes can persist data beyond the lifespan of individual
containers.
Types of Volumes
hostPath
configMap
secret
persistentVolumeClaim
Temporary storage for data that does not need to persist beyond
the lifecycle of a Pod. For example, scratch space for a build
process or temporary data for processing tasks.
configMap
secret
persistentVolumeClaim
Example of a PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Kubernetes Features
High Availability
No downtime
Structure of a Node
Node ==⇒ Container=⇒Abstraction Layer=⇒Pod(contains the application
for running)
Pods
Note: Each pod has it own IP address (which gets re-assigned on restart or
for crashed pods)
This is not ideal, so K8s has a IP service which stays online all the time
Having this service is good because if the pod crashes and restarts, the
address of the pod does not change , which is ideal in web-services
Services
Services support different types of service discovery, including ClusterIP,
NodePort, and LoadBalancer.
Types of Services
ClusterIP
NodePort
<NodePort> .
LoadBalancer
NodePort
LoadBalancer
Atomicity
Definition: Atomicity ensures that each transaction is treated as a single
unit, which either completely succeeds or completely fails. There are no
partial transactions.
Consistency
Definition: Consistency ensures that a transaction can only bring the
database from one valid state to another, maintaining the predefined rules
and constraints.
Isolation
Definition: Isolation ensures that the execution of transactions concurrently
does not affect their respective outcomes. Each transaction operates
independently until it is committed.
Example: When two transactions are trying to update the same account
balance, isolation ensures that the final balance reflects both updates
correctly, without interference.
Data Security: Databases may need to comply with specific security and
regulatory requirements that are easier to enforce outside of a Kubernetes
cluster. This includes data encryption, access controls, and audit logging.
Basics of YAML
key: value
items:
- item1
- item2
parent:
child1: value1
child2: value2
multiline: |
This is a multi-line
string in YAML.
4. Literal Block Scalars: Use the greater-than ( > ) symbol to fold new lines
into spaces, creating a single-line string.
folded: >
This is a folded
dictionary:
key1: value1
key2:
subkey1: value2
subkey2: value3
6. Anchors and Aliases: YAML allows you to use anchors ( & ) and aliases ( )
to duplicate content. This is useful for reusing configuration blocks.
default: &default
key1: value1
key2: value2
custom:
<<: *default
key3: value3
Deployment Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
Service Configuration
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Summary
Understanding the basics of Kubernetes and YAML is essential for managing
containerized applications efficiently. Kubernetes provides a powerful platform
for orchestrating containers, and YAML is the de facto standard for defining
Kubernetes resources. With these tools, you can create scalable, reliable, and
maintainable applications.
Namespaces
Namespaces in Kubernetes provide a way to divide cluster resources between
multiple users. They are intended for use in environments with many users
spread across multiple teams or projects.
Creating a Namespace
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
Defining a Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: my-namespace
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
Creating a RoleBinding
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: myresources.mygroup.example.com
spec:
group: mygroup.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
Prometheus Example
Prometheus is a powerful monitoring system that collects metrics from
configured targets at given intervals, evaluates rule expressions, displays the
results, and can trigger alerts if certain conditions are observed.
Prometheus Configuration
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
Conclusion
graph TD
K[Kubernetes Objects] ---> W[Workload Resources]
K ---> S[Service Resources]
K ---> C[Config and Storage Resources]
K ---> M[Metadata Resources]
K ---> CR[Cluster Resources]
graph
W[Workload Resources]
W ---> D[Deployments]
W ---> P[Pods]
W ---> RS[ReplicaSets]
W ---> STS[StatefulSets]
W ---> DS[DaemonSets]
W ---> J[Jobs]
W ---> CJ[CronJobs]
graph
S[Service Resources]
S ---> SVC[Services]
S ---> EP[Endpoints]
S ---> I[Ingress]
graph
C[Config and Storage Resources]
C ---> CM[ConfigMaps]
C ---> SEC[Secrets]
C ---> PV[PersistentVolumes]
C ---> PVC[PersistentVolumeClaims]
graph
CR[Cluster Resources]
CR --> ND[Nodes]
CR --> CL[Cluster]
CR --> N[Namespaces]
CR --> RB[RoleBindings]
CR --> CRB[ClusterRoleBindings]
CR --> R[Roles]
CR --> CRL[ClusterRoles]
CR --> SA[ServiceAccounts]
CR --> CRS[CustomResources]