2026-09-07 12:05
Status:
Tags: IBM IBM Full Stack Software Developer Professional Certificate IBM - Introduction to Containers with Docker, Kubernetes & OpenShift Docker Kubernetes OpenShift Virtual Machines The Cloud DevOps Deployment
CDKO - Module 3 - Managing Applications with Kubernetes
==Managing Applications with Kubernetes
-
ReplicaSet:
- Basically, these are great for things like scaling, because if you only have a singular Pod, then the Pod will be unable to perform certain actions if requests increase, or if an outage occur
- They cannot accommodate growing demands, handle outages, and minimize downtime
- The best way to go around this is using a ReplicaSet
- This ensures the right number of pods are always running it always tries to match the actual state of he replicas to the desired state
- It can do things like add or delete pods for scaling and redundancy, replace failing pods to main desired state, & supersede ReplicaControllers
- It is good for whenever you have a Deployment inside of your cluster, as it is exactly these deployments that manage different replica sets, and you should never only have a replica set created outright without a deployment
- Basically as said in the image above, you have Kubernetes that tries to keep object types independent therefore, there is not a concept of nested objects inside of objects
- So, replica sets only refer to the Pods it controls using Pod Labels
- The image above also shows a deployment table, where its basically a markup file where you connect yourself over to the given API, specify that this config file is going to create a Deployment Object
- This Deployment object is going to host a few replica sets, which are defined using the template: section of the table
- You essentially specify the given metadata, labels, and the name for the project this cluster belongs to
- The ReplicaSet is actually automatically created for you whenever you create a deployment
- You can check this out using
kubectl get deploymentwhich will generally give you thinks like the name, ready status, if it is up to date, if it is available, and it’s age - Then use the
kubectl get replicaset, this will then show you the inner replica set inside of the deployment that you just got, with much similar information - If you want to create a replica set from scratch, you can do so by just making a
YAMLfile, with thekind: ReplicaSetattribute - This then as you see can do a lot of things like set the number of pods inside of the replica set, with the
replicas: 1attribute, which can obviously be changed as well - To create this replica set from the given YAML file you then run
kubectl create -f replicaset.yamlwhich is the specification for your given replica set - Then
kubectl get podswhich would get the inner pods of that replica set, be it the one or multiple you have based on your specification - Lastly, you would run
kubectl get rswhich in turn then basically gets the actual replica set object - The above-hand should not be done though, ONLY CREATE REPLICA SETS WITH DEPLOYMENTS
- Before you can actually scale out your deployment for whatever purposes you have, you must ensure there is AT LEAST ONE pod
- You can create a deployment using the given
kubectl create -f deployment.yaml - The deployment creates a given pod by default, and you can confirm this using the
kubectl get podscommand, which will show you the pod in the deployment - So now that you actually have this deployment and what not you can actually scale it
- This is done using the
kubectl scale deploy hello-kubernetes --replicas=3command for example, and what this does is actually change the given number of pods in your replica set by whatever you have - This can also say generally be done inside of the deployment config I would assume, where you can just change that
replcias=xmetadata tag - Now that you have these three pods, you can use the replica set to manage them, and this can be done using something like
kubectl delete pod <podname>and then this actually deletes the replica pod with whatever containers it would have had inside - This will actually cause for the desired state to be INCORRECT, meaning the deleted pod gets REPLACED WITH A NEW POD IMMEDIATELY
- This would also do the inverse when a new pod is created, so if you run something like
kubectl creaete pod <podname>, it would create a new pod inside of the deployment - The pod is deployment and would run and what not, but as soon as the controller realizes that something is out order with the desired state of the deployment, and it would delete and remove one of the running containers
-
Autoscaling:
- A ReplicaSet are a good starting point for autoscaling, but at times does not meet the required amounts
- Basically the issue with them is that you need to have this fixed number of instances running inside of your deployment
- This would then generally mean you do not have a dynamic form of load balancing that would scale up or down as you need it to
- There are THREE types of autoscalers inside of kubernetes
- Horizontal Pod Autoscaler (HPA)- Adjusts the number of replicas of an application by increasing or decreasing the number of pods
- Vertical Pod Autoscaler (VPA)- Adjusts the resource requests and limits of a container by increasing or decreasing the resource size or speed of the pods, so it just upgrades them instead of making more
- Cluster Autoscaler (CA)- Adjusts the number of nodes in the cluster when pods fail to schedule, or when demand increases or decreases in relation to the capacity of existing nodes
- Set up autoscaling with a given deployment is actually pretty simple, you can really just do it with running
kubectl get pods,kubectl get rsto find out the data on your replica set and pods - After having done so, you can then do
kubernetes autoscale deploy <names> --min=x --max=x --cpu-percent=x - What this does is set the range of pods that a given replica set or deployment can have, and then also sets the maximum amount of hardware resources it can end up using too, before it has to create a new pod to load balance
- Horizontal Pod Autoscalers automatically update the workload resource by making more pods in a given deployment in order to load balance across the new mods, deleting unnecessary pods when demand is not needed
- You can run the
kubectl get hpacommand to see the status of a given HPA auto-scale - It knows to scale the deployment based on things like the maximum amount of ram usage, or even things like maximum CPU usage as well
- You can specify a bunch of this stuff inside of the
autoscaling YAMLfile which would have the config needed to mark up the specifications for your given HPA instance, generally it is best practice to use theautoscalecommand instead - Now while horizontal scaling is ideal in most situations, sometimes it is not possible due to whatever restrictions you might have, which is where Vertical Pod Autoscaler (VPA) comes in
- VPA adds in more resources to a given pod instead of just creating new ones outright, boosting the already existing ones
- You do things like set metrics for cpu or Ram utilization, and then the service reconciles the size of the given Pods based on the usage of this percentage
- A Cluster Autoscaler (CA) actually auto-scales the cluster itself, increasing and decreasing the number of available nodes that pods can run on
- This for example is really good whenever you might have something like HPA and your nodes are absolutely filled with way too many pods, this way you can scale them outward even more with increasing of nodes in the cluster
- Allowing you to outsource even more resources from other machines that might have lower resources
-
Deployment Strategies:
Kubernetes deployment strategies are used to:
- Deploy, update, or rollback Replica-sets, Pods, Services, and Applications
- Pause/Resume Deployments
- Scale Deployments manually or automatically
- Types of deployment strategies include Recreate, Rolling, Blue/Green, Canary, A/B testing, Shadow
- RECREATE
- In this one, you basically just shutdown all of the Pods running the live version of the application at the same time. Then recreating a new version of them to get it all back up and running
- This is the simplest deployment strat, but is also plagued by a bit of downtime
- Rolling (ramped)
- This one is a bit more complicated, basically, you go with this sort of a one by one scenario, where you basically delete a single pod, create a new one, and then replace the one you deleted
- You do this over and over again until all different pods are replaced with the newer version
- This is easy to set up as well, and good for stateful applications needed re-balancing of data, but it also takes a good while to complete
- In this one you basically have two live environments, the blue one being the one that all of the user traffic is currently going to, the green one being an exact copy of that environment but with all of the updated deployments
- The green area is heavily tested to make sure it works, and then you redirect all of the user network traffic over to the green area so it becomes the blue area
- It has no downtime, but it does require two times the resources and lots of testing
- This is basically just a smaller version of the above hand strategy. You have a small subset of users that are basically sent over to a new updated application of the code. This is then tested and updated for any bugs, and then when it is all good, you basically roll it out to your users
- This is good for making sure that there are no errors in your code and application, but the roll-out is slow and gradual as you slowly expose more uses to the new applications
- This one is good for whenever you don’t really know what your users might find better or your service has a split user-base of people
- In essence, you have two different versions of your application where you then have one set of people try out one version and then the other try out the other.
- Then based on their feedback on what they liked more, that is the one that generally becomes the main application
- This is create because it allows for multiple app versions to run in parallel, but is difficult for issues like troubleshooting
- So what this one does is basically do something similar to send over the user data to both the live API and the Shadow API, which both forward it to the model they have respectively
- The only thing is, that the Live API is the only one that would send any data back
- This is good for things like seeing how your given API performs next to real life data without any actual disruption to the user experience
- The only issue is that it does take double the resources to run of course
-
Rolling Updates:
- Rolling Updates are basically automated updates the occur on a scheduled basis that you can manage
- These are good for rolling out automated and controlled app changes to pods, and they work with pod templates like deployments, allowing for easy rollback as needed
- To incorporate the ability to have rolling updates, you first need to have
liveness and readiness probesinside of your deployments, which can be seen in the left-hand side of the screen - You then need to add in the rolling update strategy to your YAML file that houses your deployment object
- This is done by updating the
strategymetadata as is seen inside of the given object, the things withmaxUnavailable: 50%means that only about half of your given pods can be unavailable given an update a time time - With only 2 new ones being able to be created at a time as well
- The changing of these two variables is how you implement the different deployment strategies mentioned above
- In this given scenario, you have this application deployed that basically just hows
hello worldbut then you want to change it so that way whenever a user sees it, it would sayhello world v2 - In order to actually get this update going, and without downtime you first need to build, tag, and upload this new image to Docker Hub
- This here basically lets you have this image as an existing thing that you can pull from inside of your configuration files
- At this point, you need to apply this new image to your deployment, which is done with the
kubectl set imagecommand, with all of the extra things like the deployment and the new image - You can then see the status of this deployment using the
kubectl rollout status <name>command, and this will then show you if the setting of the given image you tried to do above hand actually worked or not - There can be issues with the new rollout though, and you might want to do something called a rollback, which is where you undo the changes that were done in your original update
- This is done in kubernetes using the
kubectl rollout undo <deploymentname>, which, if it does work correctly, will then restore you back to your older image - This can then be checked by seeing your mods, you will see the three pods with the new version be deleted, and then the three pods with the older version will be created
- There are also different times of roll-outs, with things like a all at once rollout, where basically all of the given pods are terminated at once, and then replaced with the new version of the application (which results in a bit of down)
- Or, you can also do a one by one, where each pod is replaced by the new one at a time until all are sequentially updated, this is good because it means no downtime, but also means that it takes a lot longer
- These things then also have the inverse, of an all at once rollback & one by one rollback, where they functionally work the same, just go back instead of going forward
-
Config-Maps and Secrets:
- ConfigMap basically, a good dev practice is to keep your configuration settings for your deployment to actually be separate from the codebase itself, which means that whenever you make a change to the configuration settings, it does not require code changes
- These ConfigMaps are basically an API object that stores key-value pairs of NON-CONFIDENTIAL DATA
- They are not secret at all, but do provide configuration data to pods and deployments, so that way it is not hard-coded inside of the application code itself
- The issue is it has to be less than 1 mb of data, and for larger amounts it is generally recommended to mount a separate volume or database
- There is also no spec field in the given template, and the name that it uses MUST be a valid DNS subdomain name
- The config maps are very portable and reusable across different deployments in a cluster
- They can be created in multiple ways include string literals, an existing “key”=“value” file, and a ConfigMap YAML description file, with the 2 former being able to get used to create the ladder
- You can reference them in multiple ways in each pod or deployment, including things like a environment variable or mounted as a volume
- The environment variable is used directly inside of the YAML file as can be seen in the
env:portion of the markup - You can then basically set things up from this file as you would in Javascript or something like that, where you reference the file, and then the name of whatever attribute it is that you want to have involved in there
- In the images above are also then the different examples of using either string literals in the command line, passing in a given key=value file as an option, and then finally the YAML file as well as a means of source for the pod implementation inside of your given deployment
- Including all of the commands and different flags for adding them in
- You create a secret using
kubectl create secret generaic api-creds --from-literal=key=yoursecretapikey - You can then check it exists using
kubectl get secret, and thekubectl describe sercet api-credsto make sure that none of the actually secret information is visible - A secret is very similar to that of the ConfigMap, with the main difference being that the key=value paired nature of it is basically just hidden away so that way someone cannot just easily view
- This is good for things like tokens, or passwords, or API keys and what not
-
Service Binding:
-
Service Binding is well, the binding of outside services to your given application. It is a process to consume external services or backing services, so things like databases or event buses in your application
-
It manages configuration and credentials for back-end services while protecting your sensitive data
-
In tandem, as a bonus, it actually creates those credentials for you as one of the
secretobjects inside of Kubernetes -
-
It basically binds the third party service over to a deployment, and then your own application code uses the credentials from the binding and calls the corresponding service so it can use the needed items from it
-
-
You basically first need to have an instance of the third-party service, and then you bind over the service to your cluster as a means of creating service credentials
-
After these credentials have been created, what you do is store them as a Kubernetes Secret
-
When they are then this secret, you can have your application access the given service credentials inside of the Kubernetes deployment, or the deployment hosting the pod hosting the container hosting the code
-
==Summary
- A ReplicaSet enables scaling by creating or deleting pods.
- A ReplicaSet always tries to match the actual state to the desired state.
- Autoscaling enables scaling as needed at the cluster or node level, and the pod level.
- Autoscaler types include horizontal pod (HPA), vertical pod (VPA), and cluster (CA).
- Rolling updates roll out app changes in a controlled and automated way.
- Rolling updates and rollback can be performed using all-at-once and one-at-a-time strategies.
- ConfigMaps are used to provide variables for your application.
- Secrets are used to provide sensitive information to your application.
- Binding an external Service to your deployment automatically provides the credentials to use the Service inside the code.
- Binding manages configuration and credentials for back-end Services while protecting sensitive data.
==Cheat Sheet
| Command | Description |
|---|---|
| kubectl autoscale deployment | Autoscales a Kubernetes Deployment. |
| kubectl create configmap | Creates a ConfigMap resource. |
| kubectl get deployments -o wide | Lists deployments with details. |
| kubectl get hpa | Lists Horizontal Pod Autoscalers (hpa) |
| kubectl scale deployment | Scales a deployment. |
| kubectl set image deployment | Updates the current deployment. |
| kubectl rollout | Manages the rollout of a resource. |
| kubectl rollout restart | Restarts the resource so that the containers restart. |
| kubectl rollout undo | Rollbacks the resource. |
==Glossary
| Term | Definition |
|---|---|
| Cluster Autoscaler | Also known as CA. An API resource that autoscales the cluster itself, increasing and decreasing the number of available nodes that pods can run on. |
| Config Map | An API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume. |
| Horizontal Pod Autoscaler | Also known as:HPA An API resource that automatically scales the number of Pod replicas based on targeted CPU utilization or custom metric targets. |
| Linguistic Analysis | Detects the tone in a given text. |
| IBM Cloud catalog | provides various Services that range from visual recognition to natural language processing and creating chatbots. |
| Persistent Volume | An API object that represents a piece of storage in the cluster. Available as a general, pluggable resource that persists beyond the lifecycle of any individual Pod. |
| Persistent Volume Claim | Claims storage resources defined in a PersistentVolume so that it can be mounted as a volume in a container. |
| Rolling Updates | Provide a way to roll out application changes in an automated and controlled fashion throughout your pods. Rolling updates work with pod templates such as deployments. Rolling updates allow for rollback if something goes wrong. |
| Secrets | Stores sensitive information, such as passwords, OAuth tokens, and ssh keys. |
| Service binding | is the process needed to consume external Services or backing Services, including REST APIs, databases, and event buses in your applications. |
| Tone Analyzer Service | is used for explaining service binding. This IBM Cloud Service uses linguistic analysis to detect tone in a given text. |
| Vertical Pod Autoscaler also known as VPA | An API resource that adds resources to an existing machine. A VPA lets you scale a service vertically within a cluster. |
| Volume | A directory containing data, accessible to multiple containers in a Pod. |
| Volume Mount | entails mounting of the declared volume into a container in the same Pod. |
| Volume Plugin | A Volume Plugin enables integration of storage within a Pod. |
































