Namespaces
- k8s uses namespaces to organize objects in the cluster.
- Namespace can be thought as a virtual cluster.
- By default kubectl command line interacts with
defaultnamespace - If you want to get the objects from all namespaces
kubectl get <object> --all-namespaces
- If you want to get the object from a specific namespace other than default
kubectl get <object> --namespace <name>
K8s objects
- k8s objects are persistent entities in k8s system.
- k8s uses these objects to represent state of the cluster
- what containerized applications are running
- Resources available to those applications
- To work with k8s objects whether to create, modify, delete, you will need to use the Kubernetes API
- Kubernetes API is HTTP API that API-Server exposes and lets end users or different parts of k8s cluster communicate with one another
- For us we will be using Kubernetes API through the kubectl command-line
- Object Spec and Status
- Every k8s object includes two nested object fields
- object spec
- object status
- Every k8s object includes two nested object fields
Pods
-
Pod is the smallest deployable unit in k8s.
-
A Pod is group of one or more containers with shared storage and network resources and a specification for how to run containers
-
Each container in the Pod runs in its own cgroup, but they share a number of Linux Namespaces
-
Applications running in the Pod share same IP address, port space (network namespace) , have the same hostname.
-
However, containers running in different Pods are isolated from each other
-
Lets try to write our first Pod nginx pod
-
To write the Pod we need to create a manifest yaml file
-
Any manifest in k8s will have the following sections
apiVersion:
kind:
metadata:
spec:
-
To understand API version we need to know about API groups and versioning
- API Groups make it easier to extend k8s API. API group is specified in a REST path and in the
apiVersionfield of the object - There are several api groups in k8s
- The
coregroup: This is legacy group.- if the object belongs to core group and has version v1
apiVersion: v1
- if the object belongs to core group and has version v1
- The named groups
- if the object belongs to apps group and has version v1
apiVersion: apps/v1
- if the object belongs to apps group and has version v1
- The
- K8s will have multiple api versions such v1, v1beta1, v1alpha1
- API Groups make it easier to extend k8s API. API group is specified in a REST path and in the
-
To write the Pod manifest Refer Here
---
apiVersion: v1
kind: Pod
metadata:
name: my-first-pod
spec:
containers:
- name: nginx-container
image: nginx:1.21.1
- Create a file called as nginx-pod.yaml
- Execute the following commands
kubectl apply -f nginx-pod.yaml
kubectl get pods
kubectl get pods -o wide
kubectl describe pods my-first-pod
kubectl delete -f nginx-pod.yaml
#or
kubectl delete pods my-first-pod
- Now lets look some common commands
- Viewing logs:
kubectl logs <pod-name> - Executing commands inside a container with in a pod
kubectl exec <pod-name> -- <command> - To login into the container in a terminal
kubectl exec -it <pod-name> -- bash
- Viewing logs:
- Lets write one-more manifest for jenkins
---
apiVersion: v1
kind: Pod
metadata:
name: my-jenkins-pod
spec:
containers:
- name: jenkins
image: jenkins/jenkins:jdk11
- Exercise: Create one pod which runs alpine image
