Kubernetes is a powerful container orchestration platform that uses various objects to manage the deployment, scaling, and operation of application containers. Here’s a step-by-step guide to some of the key Kubernetes objects with examples:
1. Pod
A Pod is the smallest and simplest Kubernetes object. It represents a single instance of a running process in your cluster.
Example:
Create a file named pod-example.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
Commands:
kubectl apply -f pod-example.yaml
kubectl get pods
kubectl describe pod my-pod
2. Service
A Service is an abstraction that defines a logical set of Pods and a policy by which to access them. This can be used to expose your application.
Example:
Create a file named service-example.yaml
:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
Commands:
kubectl apply -f service-example.yaml
kubectl get services
kubectl describe service my-service
3. Deployment
A Deployment provides declarative updates to Pods and ReplicaSets. It manages the deployment of Pods and ensures the desired state is maintained.
Example:
Create a file named deployment-example.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
Commands:
kubectl apply -f deployment-example.yaml
kubectl get deployments
kubectl describe deployment my-deployment
4. ReplicaSet
A ReplicaSet ensures that a specified number of pod replicas are running at any given time. Deployments manage ReplicaSets and Pods.
Example:
Create a file named replicaset-example.yaml
:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
Commands:
kubectl apply -f replicaset-example.yaml
kubectl get replicasets
kubectl describe replicaset my-replicaset
5. StatefulSet
A StatefulSet is used for managing stateful applications. It provides guarantees about the ordering and uniqueness of Pods.
Example:
Create a file named statefulset-example.yaml
:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
serviceName: "my-service"
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
Commands:
kubectl apply -f statefulset-example.yaml
kubectl get statefulsets
kubectl describe statefulset my-statefulset
6. ConfigMap
A ConfigMap allows you to separate configuration artifacts from image content to keep containerized applications portable.
Example:
Create a file named configmap-example.yaml
:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
my-key: my-value
Commands:
kubectl apply -f configmap-example.yaml
kubectl get configmaps
kubectl describe configmap my-configmap
7. Secret
A Secret is used to store and manage sensitive information, such as passwords, OAuth tokens, and ssh keys.
Example:
Create a file named secret-example.yaml
:
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
my-key: bXktdmFsdWU=
Commands:
kubectl apply -f secret-example.yaml
kubectl get secrets
kubectl describe secret my-secret
8. Namespace
A Namespace provides a mechanism for isolating groups of resources within a single cluster.
Example:
Create a file named namespace-example.yaml
:
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
Commands:
kubectl apply -f namespace-example.yaml
kubectl get namespaces
kubectl describe namespace my-namespace
9. Ingress
An Ingress manages external access to services, typically HTTP. It provides load balancing, SSL termination, and name-based virtual hosting.
Example:
Create a file named ingress-example.yaml
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: my-app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 80
Commands:
kubectl apply -f ingress-example.yaml
kubectl get ingress
kubectl describe ingress my-ingress
10. Job
A Job creates one or more Pods and ensures that a specified number of them successfully terminate.
Example:
Create a file named job-example.yaml
:
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
template:
spec:
containers:
- name: my-container
image: busybox
command: ["sh", "-c", "echo Hello Kubernetes! && sleep 30"]
restartPolicy: OnFailure
Commands:
kubectl apply -f job-example.yaml
kubectl get jobs
kubectl describe job my-job
11. CronJob
A CronJob creates Jobs on a scheduled time, similar to cron in Linux.
Example:
Create a file named cronjob-example.yaml
:
apiVersion: batch/v1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-container
image: busybox
command: ["sh", "-c", "echo Hello Kubernetes! && sleep 30"]
restartPolicy: OnFailure
Commands:
kubectl apply -f cronjob-example.yaml
kubectl get cronjobs
kubectl describe cronjob my-cronjob