Saturday, April 29, 2023

Using kubectl to Create a Deployment

Once you have a running Kubernetes cluster, you can deploy your containerized applications on top of it. To do so, you create a Kubernetes Deployment. The Deployment instructs Kubernetes how to create and update instances of your application. Once you've created a Deployment, the Kubernetes control plane schedules the application instances included in that Deployment to run on individual Nodes in the cluster.


Once the application instances are created, a Kubernetes Deployment controller continuously monitors those instances. If the Node hosting an instance goes down or is deleted, the Deployment controller replaces the instance with an instance on another Node in the cluster. This provides a self-healing mechanism to address machine failure or maintenance.


You can create and manage a Deployment by using the Kubernetes command line interface, kubectl. Kubectl uses the Kubernetes API to interact with the cluster. 



When you create a Deployment, you'll need to specify the container image for your application and the number of replicas that you want to run. You can change that information later by updating your Deployment;


The common format of a kub ectl command is: kubectl action resource



This performs the specified action (like create, describe or delete) on the specified resource (like node or deployment). You can use --help after the subcommand to get additional info about possible parameters (for example: kub ectl get nodes --help).


Check that ku bectl is configured to talk to your cluster, by running the kubectl version command.



Check that kub ectl is installed and you can see both the client and the server versions.



To view the nodes in the cluster, run the kubectl get nodes command.


You see the available nodes. Later, Kubernetes will choose where to deploy our application based on Node available resources.


Let’s deploy our first app on Kubernetes with the kubectl create deployment command. We need to provide the deployment name and app image location (include the full repository url for images hosted outside Docker hub).



ku bectl cr eate deployment kub ernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1


Great! You just deployed your first application by creating a deployment. This performed a few things for you:


searched for a suitable node where an instance of the application could be run (we have only 1 available node)

scheduled the application to run on that Node

configured the cluster to reschedule the instance on a new Node when needed


To list your deployments use the kubectl get deployments command:


k ub ectl get deployments


We see that there is 1 deployment running a single instance of your app. The instance is running inside a container on your node.


View the app

Pods that are running inside Kubernetes are running on a private, isolated network. By default they are visible from other pods and services within the same kubernetes cluster, but not outside that network. When we use kubectl, we're interacting through an API endpoint to communicate with our application.



The kubectl command can create a proxy that will forward communications into the cluster-wide, private network. The proxy can be terminated by pressing control-C and won't show any output while its running.


You need to open a second terminal window to run the proxy.


k u b ectl pr ox y


We now have a connection between our host (the online terminal) and the Kubernetes cluster. The proxy enables direct access to the API from these terminals.


You can see all those APIs hosted through the proxy endpoint. For example, we can query the version directly through the API using the curl command:


cu rl http://localhost:8001/version


The API server will automatically create an endpoint for each pod, based on the pod name, that is also accessible through the proxy.


First we need to get the Pod name, and we'll store in the environment variable POD_NAME:


exp ort POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')

ec ho Name of the Pod: $POD_NAME


You can access the Pod through the proxied API, by running:


curl h ttp://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/


In order for the new Deployment to be accessible without using the proxy, a Service is required which will be explained in the next modules.


references:

https://kubernetes.io/docs/tutorials/kubernetes-basics/deploy-app/deploy-intro/

No comments:

Post a Comment