Kubernetes Voyage: Advanced Ship Steering
We will learn
- pods vs container
- Deployments
- Services
- Auto-healing concept
- config maps & secrets
- persistent volume
Let’s get started.
Pods vs Containers⌗
Pods | Container |
---|---|
Unit of conainers | Single container by containerization tool. |
Contains one or more Conainers | Only single container |
Crashed free | Can be crashed if some error occurs |
Can be Scheduled | Can’t be scheduled |
Configuration file⌗
To create pods we have to use a configuration file in YAML
, which is also called manifest
in k8s
We need manifest file for almost everything in kubernetes.
-
Kind:
To create maifest file for a paritcular service we have to define a
kind
keyword.kind
defines what type of manifest file is, like for Pods, Service or Deployment, etc.Example:
-
Namespace:
logical entity allow you to isolate your resources, like pods, volumne, deploymnets, etc. You can create multiple namaspcae for different resources, it is a type of group.
Default name space is already present, and everything is created under it until you specify any other namespace in manifest.
Example:
-
To apply the manifest file to your cluster command:
This will send request to api server, then it will to scheduler (resposnisble fo managing pods) and then it send back to api server and then configuration will be stored.
-
To fetch the pods information from master we can use the below command:
-n
: namespace, by default it only looks for resources in to default namespace.
Deployment⌗
Deployment is a desired state.
It is also written in manifest file and kind
will be set to Deployment
. It is used to create replicaset of templates,
It is a configuration of pods. It is a desired(required) state of your pods. You can also provide some data in pods while deployment using this manifest.
Example:
replicas
: Number of pods clone / replicationslabels
: Configuration is for, name of the replica.
Actions:
-
Apply the deployment:
-
Get the nodes with more wider output.
-
Get your deployment status.
-
Get detail information about your deployment.
-
Apply Rolling update to deployment like scaling: # No need to edit the file.
-
You can even Rollback if you made any mistake.
Services⌗
It is method used to allow outside world to access application instance running in pods deployment, some sort of proxy.
In our example, nginx is running but not accessible because we haven’t applied any service yet.
Example:
Services are of 3 types:⌗
-
NodePort:
It act as a node machine, this maps the deployment’s port to serivce’s port.
We have to provide 3 ports:
port
: Port of application running in pods, (80 incase of nginx).targetPort
: Service port, to map pods port with Service port. (incoming)nodePort
: Outgoing port, which is actual accessible port. ( ranges [30000 - 32627])
Example:
This service file will provide the access to users for our application which is nginx in this case.
-
Cluster IP:
It Exposes the Service on a cluster-internal IP, Making the Service only reachable from within the cluster not outside, And this is used by default.
-
Load balancer:
It Exposes the Service externally using an external load balancer. Kubernetes does not offer a load balancing component; you have provide one, or you can integrate your cluster with a cloud provider like AWS.
We, will use NodePort which will allow us to access out nginx outside the cluster.
-
Apply the service
-
List your Services.
Auto healing.⌗
Now, our pods are running and our application is also accessible by the user. But, what if we delete one of the pod or container?
Well, let’s do it.
-
First get list of your pods.
-
And now delete one pod.
Now, we have deleted one pod, let’s check the status of pods.
You saw that right? The
Age
of second pod is8s
.Yes, that 2nd pod got created after we deleted one, And this is what we call Auto Healing.
-
Anyway, If you want to delete you particular depolyment or manfiest configuration use this syntax:
Secrets & config map⌗
In k8s, we can’t pass environment variables through kubectl at runtime. Therefore we use secret & config map file.
-
Config map:
It is a Special kind of manifest file, if you deployment needs particular variable then you can declare that in this manifest file. All variables are passed to all pods. In manifest set kind to
kind: ConfigMap
to create a config map. It pass data as plain texts. -
Secrets:
It is a type of config which contains credentials (passwords), advantage is you can pass encoded password and it will be decrypted in pods. In manifest set kind to
kind: secrets
to create secrets.
Example:
-
Setup servers
-
Create deployment manifest.
-
You can rectify errors using dry run, it doesn’t apply the changes only show what will be happen if applied:
-
Create defined namespace:
-
Create configMaps manifest file
-
Apply config map.
-
Check your config map.
-
Now create secrests manifest.
But, before that encrypt your pass using below command.
copy output and pass in data field in secrets.yml
-
Apply secrets manifest to deployment.
-
Now, check if secret is added successfully.
-
After that, finally apply your deployment.
After your deployment, you can cross verify by logging in mysql. Run commands in worker node. Make sure you are putting correct container id.
-
You can delete your deployment if you want.
PVC & PV, Storage classes⌗
-
PV (Persistent Volume)
It creates application’s stateful location is in your disk where data is stored as snapshot.
Make sure hostPath.path directory exists in worker node.
Now apply your peristentVolume manifest
Check if persistentVolume is created.
-
PVC (Persistent Volume Claim):
After creating the volume you have to claim it to use, until now we have just created not used. You can claim how much you want from volume for your deployment.
Then, apply it
After that check if was successfull.
Now, to use volumes add these into containers object in deployment.yml.
So, that’s it.