Kubernetes Deployments
100Days Resources
- Video by Anais Urlichs
- Add your blog posts, videos etc. related to the topic here!
Learning Resources
Example Notes
This little exercise will be based on the following application: https://github.com/anais-codefresh/react-article-display
Then we will create a deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: react-application
spec:
replicas: 2
selector:
matchLabels:
run: react-application
template:
metadata:
labels:
run: react-application
spec:
containers:
- name: react-application
image: anaisurlichs/react-article-display:master
ports:
- containerPort: 80
imagePullPolicy: Always
More information on Kubernetes deployments
- A deployment is a Kubernetes object that makes it possible to manage multiple, identical pods
- Using deployments, it is possible to automate the process of creating, modifying and deleting pods — it basically manages the lifecycle of your application
- Whenever a new object is created, Kubernetes will ensure that this object exist
- If you try to set-up pods manually, it can lead to human error. On the other hand, using deployments is a better way to prevent human errors.
- The difference between a deployment and a service is that a deployment ensures that a set of pods keeps running by creating pods and replacing broken prods with the resource defined in the template. In comparison, a service is used to allow a network to access the running pods.
- Deploy a replica set or pod
- Update pods and replica sets
- Rollback to previous deployment versions
- Scale a deployment
- Pause or continue a deployment
Create deployment
kubectl create -f deployment.yaml
Access more information on the deployment
kubectl describe deployment <deployment name>
Create the service yml
apiVersion: v1
kind: Service
metadata:
name: react-application
labels:
run: react-application
spec:
type: NodePort
ports:
- port: 8080
targetPort: 80
protocol: TCP
name: http
selector:
run: react-application
Creating the service with kubectl expose
kubectl expose deployment/my-nginx
This will create a service that is highly similar to our in yaml defined service. However, if we want to create the service based on our yaml instead, we can run:
kubectl create -f my-pod-service.yml
How is the Service and the Deployment linked?
The targetPort in the service yaml links to the container port in the deployment. Thus, both have to be, for example, 80.
We can then create the deployment and service based on the yaml, when you look for "kubectl get service", you will see the created service including the Cluster-IP. Take that cluster IP and the port that you have defined in the service e.g. 10.152.183.79:8080 basically
kubectl get service
Alternatively, for more information of the service
kubectl get svc <service name> -o yaml
-o yaml: the data should be displayed in yaml format
Delete the resources by
kubectl delete service react-application
kubectl delete deployment react-application
// in this case, your pods are still running, so you would have to remove them individually
Note: replace react-application with the name of your service.