Kubernetes For World PDF
Kubernetes For World PDF
● Master Node is responsible for managing the Kubernetes cluster and consists of these:
1. Kube API Server: Allows users and other components to interact with the cluster. All
communications internal or external go through it. Port 443. API talks to worker kubelet.
2. ETCD: Used for storing the current state of the cluster and its configuration data.
3. Kube controller Manager: Runs various controllers that handle tasks, such as node
management and replication. Always watching-out for changes then applies accordingly
4. Cloud controller Manager: Responsible for interacting with the cloud provider.
5. Scheduler: Responsible for assigning pods to nodes based on resource requirements.
● Worker Node (minion node) is a member of a cluster where the containers run and it has:
1. Kubelet: An agent that runs on a worker which communicates with the master (scheduler). It
manages the containers, ensures they’re running correctly. Reports node and pod state.
2. Container Runtime: Responsible for pulling container images and running containers on the
node. Docker, containerd, ark , and CRI-O are common containers used. Runtime abstracts
container management for Kubernetes. Containers don’t have permanent IP addresses.
3. Kube Proxy: Responsible for local cluster networking and load balancing between services
across the cluster. If you want to send a request to a cluster, you go through it.
● Pod is the basic unit of deployment in Kubernetes. It can consist of one or more containers that share
the same resources. It is an abstraction over a container. Pods get IP addresses. They’re ephemeral.
● ReplicaSet ensures that a specified number of identical pods are running at all times. It provides scaling
and high availability by creating or removing pods as needed.
● A deployment is a higher-level abstraction that manages ReplicaSets. It’s a blueprint for pods. It
enables rolling updates, rollbacks, and scaling of application. It ensures that the desired number of
replicas are running and monitors their health. You’ll never create pods. You’ll create deployments.
● StatefullSets: Are like deployments, but used for databases. You should create databases this way.
● A service provides a permanent IP for accessing a group of pods. It abstracts the dynamic IP
addresses of the pods and provides load balancing and service discovery.
● A namespace is a logical boundary within a cluster that allows multiple virtual clusters to coexist.
Docker is deprecated. Now Kubernetes works with Containerd. Concept and commands are the same
Minikube
One node K8 cluster. Both master and worker
processes run on one node (machine)
Also, the node comes with Docker runtime
pre-installed on it. Minicube will create a virtual
environment on your laptop. You can use K8 this
way for testing and learning.
Kubectl
To create components and configure them, you need a way to interact with Minikube. Kubectl (which is a CLI
tool) is your gateway to connect to the Minikube and do the configurations. The Kubectl will communicate with
the API server. There are 3 ways/clients to contact the API server and they are: UI, API and CLI. Kubectl is the
most powerful client of them all. You can use Kubectl for any type of cluster, for example cloud cluster, or Hybrid
cluster.
Installation of Minikube and Kubectl (L-122)
Install virtualization environment first:
brew update
brew install hyperkit
brew install minikube
Install Minikube (it comes with Kubectl (also known as Kubernetes-cli), so no need to install it separately)
How do you check the status of the nodes: kubectl get nodes
How do you check the status of our minikube: minikube status
What version of Kubernetes have you installed ? kubectl version (check both server & client)
This command “minikube start --vm-driver=hyperkit” is only for starting the cluster. We will be
using kubectl to interact with our minikube cluster from here onwards.
kubectl get nodes (We already know that we have 1 node that is acting as a worker and master)
kubectl get pod (checks what pods are available, but we don’t have any pods yet)
kubectl get services (Will check what IP and port are available)
kubectl get deployment
kubectl get replicaset
Let us create an nginx pod (We don’t create pods. We create deployment. Deployment then creates the pod)
kubectl create deployment nginx-depl --image=nginx
kubectl get deployment Checks the deployment we’ve just created.
Remember, a deployment is just a blueprint for a pod. Between a deployment and a pod, there is another layer
called replicaset, that kubernets manages for you. Let’s check it: kubectl get replicaset
Your pod inherits the hash and image name from the replicaset, then adds its own ID at the end and that how it's
made. You don’t update, create or delete a replicaset. You ONLY deal with deployments.
Layers of abstraction: (Remember, anything below them is managed by kubernetes so you don’t have to worry!
Deployment manages a replicaset. Replicaset manages a pod. A pod is an abstraction of a container.
If you want to edit the name of an image, you do it in the deployment, and NOT in the pod itself. Let’s do it.
kubectl edit deployment nginx-depl add :1.0 after the name under “containers” (editor is VI)
Let’s delete our pods (Let’s find out our blueprint first: kubectl get deployment)Then delete if you want.
kubectl delete deployment <deployment name> This will delete the pod made by that deployment.
Check your the replicaset and pods are both gone. This is your challenge!
Kubernetes Configuration File (L-124)
Typing these lines (kubectl create deployment nginx-depl --image=nginx) is tedious and that’s
why we need configuration files. We type all the commands in the file, the execute the file with this command:
kubectl apply -f <file-name.yml>
● You can store your K8 Config file in any location but it is better practice to keep it with the rest of the
code files.
Ports and Port-forwarding: When external networks want to communicate with the pods, they contact the
Services first through port 80. See the black box on top right. The ‘Services’ then forwards all legitimate
communications to the pods through port 8080 in this case. This means, external entities don’t have direct
access to the pods which is good for security.
There is a file for the status of your deployment which is available in ETCD. To see this file, type this:
kubectl get deployment nginx-deployment -o yaml
You can redirect the output to another file if you like to save it and investigate more (the red bit)
kubectl get deployment nginx-deployment -o yaml > nginx-deployment.yaml
Open the output file side by side with the deployment and concentrate on the status part. Useful for debugging.
Things we need:
1. We will need a database URL of MongoDB so Mongo-express can connect to it. So, we will create a
ConfigMap. This file will contain the database URL
2. We will need credentials. Those are the username and password of the database so they can
authenticate to DB. We will create a Secret. This will contain the credentials. We can pass these credentials to
Mongo-express through its deployment file using Environmental Variables.
We will reference both in our deployment file. Once we finish, we can access the Mongo-express through the
browser.
The steps:
1. Let’s check and see everything in our cluster: kubectl get all (mine is completely empty)
2. First, create a file that will contain our credentials. Call this file mongo-secret.yaml (this secret file has to
be applied first before pods deployment, otherwise your pods will throw errors)
3. Apply the secret file kubectl apply -f mongo-secret.yaml (you can see it with kubectl get secret)
4. Create a mongoDB deployment and call it mongo.yaml
apiVersion: apps/v1
kind: Deployment Those 3 dashes (---) in the script on the left are officially known in the YML format as a
metadata: syntax for document separation. This means you can put multiple deployments in a
name: mongodb-deployment
labels: single file. YML will understand this and create the deployments separated by these
app: mongodb
spec:
dashes.
replicas: 1
selector:
matchLabels: The pink part at the bottom creates a service, while the rest at the top creates a
app: mongodb
template:
mongoDB. You can make separate files if you want.
metadata:
labels:
app: mongodb We will combine these 2 deployments in the same file as they belong together.
spec:
containers:
- name: mongodb The service part has a key-value pair (app: mongodb underlined) and that’s how your
image: mongo
ports:
service knows what to connect to.
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME Keep an eye on these parts:
valueFrom:
secretKeyRef:
1. -kind “Service”
name: mongodb-secret 2. -metadata / name random name
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD 3. -selector connection to pods through a label
valueFrom: 4. -ports
secretKeyRef:
name: mongodb-secret ports Service port
key: mongo-root-password
---
apiVersion: v1
kind: Service
metadata: Here, for the internal service, we don’t mention the type. That way, this service
name: mongodb-service
spec: automatically becomes for internal use. Later, in the next script for the Mongo
selector: Express, we will put the type as LoadBalancer. That will make the next Service an
app: mongodb
ports: external one.
- protocol: TCP
port: 27017
targetPort: 27017
Create components in a NS (remember, if we don’t mention a NS, components get created in the default)
You can change the system’s behaviour of creating components in the default NS by using kubens.
Install it on a Mac with brew install kubectx Once it is installed, type kubens which will show all NSs
while also highlighting the active one. Typing kubens my-namespace will make my-namespace the default.
4 Services - Connecting to Applications inside cluster (L-127)
1) ClusterIP Services, 2) Headless Services, 3) NodePort Services, and 4) LoadBalancer Services
A Pod is ephemeral. When it dies, it loses everything including IP. A new Pod will get a new IP and that is not
good because you’ll have to adjust your environment each time this happens. With Services however, pods get
static IPs which will stay even when pods die. Service also provides load-balancing. If you have 3 application
replicas, your Service will get the request from your clients then it will forward the traffic to any of the pods that’s
available. Here are the types:
1. ClusterIP Services. If you don’t specify the type of Service in your Service file, you get ClusterIP by default .
2. Headless Service.
In Headless Service, clusterIP value in the spec is set to “None”
Headless is used when a client wants to communicate with a specific pod
and not any pod. Also, it is used when pods are communicating with one
another for data syncing without the need to go through the K8’s Service first.
If you set the clusterIP to “None”, then the DNS will return an IP for the Pod.
This way, clients can use the IP to get to the Pod. Headless is used for
stateful applications (like databases).
3. NodePort Service. The ‘type’ attribute can be one of three (ClusterIP, NodePort or LoadBalancer)
Unlike ClusterIP (which was accessible ONLY from within the cluster), NodePort will let external traffic into the
cluster using a static fixed port. This means, traffic from outside will reach the worker node via ports, without
using ingress. This port (range has to be between 30000 and 32767) is defined in the Service file, at the
nodePort attribute. This type Service is neither efficient nor secure because external traffic can reach your pods
directly.
4. LoadBalancer Service External traffic can reach the Service through LoadBalancer from Cloud Provider.
When LoadBalancer gets created, ClusterIP and NodePort also get created automatically. This way, LoadBalancer will
forward traffic from outside to the NodePort. This means, NodePort cannot be reached directly. LoadBalance type is an
extension of NodePort type, which itself is an extension of the ClusterIP type. In a real K8 production, you may use
NodePort for quick testing but not for external connection. If you have an application that will be used through browser,
you’ll either configure ingress which will forward traffic to your ClusterIP, or you’ll use the LoadBalancer from your cloud
provider.
Ingress - Connecting to Applications outside cluster (L-128)
For External Services, you’ll have to visit your
app through an IP and port which is not the
way a company app would serve clients. A
better way is to use an Ingress Service.
When the clients visit from browser, request
first reaches the Ingress Service, then the
Ingress will redirect traffic to the Internal
Service (See diagram)
Type this: kubectl get ingress -n kubernetes-dashboard into your terminal. It will assign an IP to the
dashboard. Take that IP and map it to dashboard.com in your hosts file in your local machine (/etc/hosts)
Now, visit dashboard.com through your browser.
Extra commands=