Section 1 : Core concepts
Cluster architecture
4
● There are two types of nodes that host applications in form of
containers, in a Kubernetes cluster architecture. Master node and
worker nodes.
○ Master : Manage, plan, schedule, monitor nodes.
◆ The master nodes is responsible for :
– Managing the Kubernetes cluster.
– Storing info regarding different nodes in the cluster.
– Planning which deploy and where to deploy them.
– Monitoring the health of nodes and containers on them etc.
Master node does all this with using a set of components
3
◆
known as control plane components. They include :-
. etcd cluster : stores info about nodes in the cluster,
containers deployed on them and what time the were
deployed. This database stores info in a key-value format.
2
. kube-scheduler : identifies the right nodes to place a pod
on based on the resource requirements, node capacity,
policies such as taints and tolerations and node affinity
rules.
1
. kube-controllers : these take care of different objects in
k8s, to make sure that they are working well. They are all
managed by the controller-manager
◆ Examples :-
◆ node-controller : responsible for onboarding new
nodes to the cluster, handling nodes when they
become unavailable / destroyed.
◆ replication-controller : responsible for making
sure that the desired number of pods are available
in a replica-set / deployment, recreating pods when
they crash, scaling pod up or down as desired.
. kube-apiserver : responsible for orchestrating all
operations in the cluster.
◆ It exposes the k8s api which is used by external
◆
users to perform management operation on the
cluster.
◆ It is used by the controllers to monitor the state of
object in the cluster.
◆ It is used by the kubelet on the worker nodes to
communicate to the master node.
. kubelet : is an agent that runs on each node node in a
cluster.
◆ It listens for instructions from the kube-apiserver.
◆ It is responsible for creating / destroying pods on a
nodes as desired.
◆ It is used to send health signals of the objects on
the node back to their controllers.
. kube-proxy : ensures that the necessary networking rules
are in place to allow pods running on them to reach each
other.
○ Worker nodes : host apps as containers in pods which are
managed by kubelet agent.
Services
● Services are used to connect apps together with other apps / users.
There are 3 types of services in Kubernetes.
. nodePort : used to make the pods available to external users
through a nodePort.
. clusterIP : used to connect pods to other pods. It groups pods
together and provides a single interface to access them.
. LoadBalancer : used to distribute loads across multiple pods and
making all pods available through a single URL.
○ Kubernetes services are accessed using their names or cluster IPs.
Namespaces
● Namespaces are used to logically divide a k8s cluster.
○ You can perform the following on namespaces.
◆ List : k get namespaces
◆ Create : k create namespaces
◆ Change current namespace(context) : k config set-context $
(k config current-context) --namespace=<VALUE>
○ You can even set ResourceQuota to a namespaces so that objects
in the namespace do not use much resources.
○ To refer to a service in another namespace use :
serviceName.namespace.svc.cluster.local
○ The above is the DNS name given to the service upon creation.
◆ It can be used as follows :-
– serviceName
– serviceName.namespace
– serviceName.namespace.svc OR
– serviceName.namespace.svc.cluster.local
Imperative vs Declarative commands
● Imperative commands
○ These are used to lay out steps to take when create, updating or
deleting an object in k8s. Examples include :-
○ Creating objects
◆ k run nginx --image=nginx
◆ k create deployment nginx --image=nginx --replicas 2
◆ k expose deployment nginx --port 8080
○ Updating objects
◆ k edit deployment nginx
◆ k set image deployment nginx --image=nginx:1.8
◆ k scale deployment nginx --replicas 4
○ PODS
◆ Creating NGINX pod : k run nginx --image=nginx
◆ Generating a template file : k run nginx --image=nginx --dry-
client=client -o yaml
◆ --dry-client=client : This option just tests if the pod
creation command it syntactically correct without creating
the pod.
○ DEPLOYMENTS
◆ Creating a deployment : k create deployment nginx --
image=nginx
◆ Creating a deployment with 2 replicas : k create deployment
nginx --image=nginx --replicas=2
◆ Generating a template file : k create deployment nginx --
image=nginx --dry-client=client -o yaml
◆ Scaling up a deployment from 2 to 4 replicas : k scale
deployment nginx --replicas=4
○ SERVICES
◆ Creating a ClusterIP service to expose pod mysql to port
3306 :-
– k expose pod mysql --port=3306 --name=mysql-service
--dry-client=client -o yaml
◆ OR
– k create service clusterip mysql-service --tcp=3306:3306
--dry-run=client -o yaml
◆ The above command will assume selector labels are
app=mysql-service which might not be the case. So
after generating the service config file, you need to
set the correct labels before creating the service
with kubectl apply command.
◆ Creating a nodePort service to expose pod nginx’s port 80
on port 30080 on the nodes :-
– k expose pod nginx --type=NodePort --port=80 --
name=nginx-service --dry-run=client -o yaml.
◆ The above command will create the service with the
right labels, but will use a random port in range. So
after generating the service config file, you need to
set the correct nodePort before creating the service
with kubectl apply command.
◆ This is recommended approach as you only need to
change the nodePort.
◆ OR
– k create service nodeport nginx-service --tcp:80:80 --
node-port=30080 --dry-run=client -o yaml
◆ The above command will create the service but will
not use the pod labels as selectors. So after
generating the service config file, you need to set
the correct labels before creating the service with
kubectl apply command.
◆ Creating a NGINX pod and exposing it with a ClusteIP
service on port 80.
– k run nginx --image=nginx --port=80 --expose
● Declarative commands
○ These are used to create or update k8s objects with instructions
written in a file. This is done with kubectl apply command.
○ The apply command knows if the object is to be created, updated.
○ In the exam, it is better to use imperative commands for easy tasks
or to generate a template file quickly which can be modified to fit
your needs.