How to Create Update Scale List Get and Delete a Deployment using Kubernetes Golang SDK
Introduction
This article will walk the user through the process of Creating, Updating, Scaling, Listing, Getting and Deleting Deployment in a Kubernetes cluster with the help of Kubernetes golang client SDK.
Table of Contents:
- Creating a client to communicate with the Kubernetes API Server
- Creating Deployment
- Get Deployment
- Listing Deployment
- Updating the Image of a Container in Deployment
- Scaling Deployment
- Deleting Deployment
- Assembling the Pieces
- Installing the Dependencies and Testing
- Conclusion
Creating a client to communicate with the Kubernetes API Server
Kubernetes provides a golang SDK to access the API Server programmatically. We can learn more about communicating with the Kubernetes API Server using go-client SDK in this article.
To perform the operations on Kubernetes Deployments, we need to create a client that will interact with the API Server. We will use the Kubernetes SDK to interact with the kubeconfig
file and exposes functionality to create a client. Let’s dive right into the code. We will explain the stuff as we go ahead.
In the code snippet, we have created a function getK8sClient()
, which returns a pointer to the K8sClient
struct. The K8sClient
struct has a Client
of type kubernetes.Interface
, which is the type of the Kubernetes client. We can use this to create method sets when performing operations on the Deployment.
In the code snippet, we get the user’s home directory using the function os.UserHomeDir()
and then acquire the path to the ~/.kube/config
file. We then pass the kubeConfigPath
to the clientcmd.BuildConfigFromFlags()
function, which returns a kubeconfig
object. Next, we pass the kubeconfig
to the kubernetes.NewForConfig()
function to obtain the kubernetes client
. Finally, we return the K8sClient
with the kubernetes client we will use to perform operations on Deployments.
Creating Deployment
In the previous section, we created a Kubernetes client. Now we will be using it to perform actions on the Kubernetes resources. For example, this section will use the client to develop Kubernetes Deployment. But first, let’s review the Deployment object.
Walk through the Deployment object
The Kubernetes go client SDK provides a Create
function to create Deployment. The procedure takes multiple parameters, one of which we will explain in this section: the Deployment object. First, let’s walk through the Deployment object.
Here in the &appsv1.Deployment{...}
struct, we pass the intended name of the Deployment in the metav1.ObjectMeta
struct. Under the appsv1.DeploymentSpec
struct, we provide the Replicas
count and Selector
map. The Template
uses the &corev1.PodTemplateSpec
struct defined in the Kubernetes k8s.io/api/core/v1
. It has a Label map which should be the same as the Selector
mentioned earlier. The corev1.PodSpec
struct contains the []corev1.Container
struct, which is an array as we can have multiple containers running inside a Pod. Further, we have the Name
, Image
and Ports
related to a container. The []corev1.ContainerPort
struct is an array as we can expose multiple ports in our Pod.
Using the Deployment object to create a Deployment
Now that we have deconstructed the Deployment struct let’s go through the CreateDeployment()
function.
The CreateDeploymentConfig()
function receives parameters like the name
of the Deployment and the namespace
we need to create the Deployment. The container image
to be used for the Container, the number of replicas
that the Deployment should create. The Kubernetes client
is passed in as the method set (c *K8sClient)
for interacting with the Kubernetes cluster. Finally, the function returns an error
, which is nil
in case the DeploymentConfig gets created.
The Create()
function takes in a context
, the *v1.DeploymentConfig
struct and the metav1.CreateOptions{}
. It returns an error and a *v1.DeploymentConfig
struct for the created DeploymentConfig. We will return with an error
if there is any error while creating the DeploymentConfig. Else, we return with a nil
.
Using the code below, we can call the CreateDeployment()
function from the main()
.
Get Deployment
Now that we can create a Deployment using the SDK let’s try to get the Deployment we just deployed. This section will use the Kubernetes client to run the Deployment in a namespace. So first, let’s go through the function GetDeployment()
.
The ListDeployment()
receives one argument, the namespace
, and a pointer to the Kubernetes client
is passed in the method set. The function returns two parameters *appsv1.DeploymentList
and error
.
We can call the GetDeployment()
function from the main()
and get the *appsv1.Deployment
using the following code.
Listing Deployment
Now that we can create a Deployment using the SDK, let’s try to list them for a given namespace. This section will use the Kubernetes client to list Deployment running in a namespace. So first, let’s go through the function ListDeployment()
.
The ListDeployment()
receives one argument, the namespace
, and a pointer to the Kubernetes client
is passed in the method set. The function returns two parameters *appsv1.DeploymentList
and error
.
We can call the ListDeployment()
function from the main()
and iterate over the *appsv1.DeploymentList
array using the following code.
Using a for
loop, we can iterate over the deployment
variable returned from the ListDeployment()
function. In the above example, we are printing out the Deployment name using the d.ObjectMeta.Name
. The item has all the information related to Deployment.
Updating the Image of a Container in Deployment
We saw that using the Kubernetes SDK; we can easily create and list Deployment. Next up, we will update the image of a container in Deployment. Previously we created a Deployment with the image docker.io/httpd:latest
. We will change that to docker.io/nginx:latest
, an open-source web server. Finally, we will update the image with the UpdateDeployment()
function. Let’s dive right into the code.
The UpdateDeployment()
function receives the Deployment name
, namespace
, and image
to be updated. The stringPatch
struct defines the payload
. It consists of an Op
referring to the Operation
, which in this case is replace
. The Path
in the JSON body is where we need to perform the replace operation. The Value
is the image name the image
variable provides.
The payload
is then converted into an array of bytes payloadBytes
using the json.Marshall()
function. Finally, the Patch()
function uses the patch payload payloadBytes
along with the types.JSONPatchType
, which signifies our patch operation. The UpdateDeployment()
is a simple function call without any return value. We can call the above function from the main()
.
Scaling Deployment
Scaling the Deployment is done through a Patch()
function call. In addition, the SDK has a GetScale()
and UpdateScale()
function for getting and updating the scale of the Deployment. So let’s walk through the ScaleDeployment()
function.
This function receives a Deployment name
, namespace
and the replica count denoted by the variable of replicas
. Here the GetScale()
function provides us with the scale object. The scale object is updated to scale the Deployment using the sd.Spec.Replicas
. Finally, we call the UpdateScale()
function with the revised scale object. The procedure does not return any value. We can call the above function from the main()
.
Deleting Deployment
Now that we have performed all the operations in the scope of this article, we can go ahead and delete the Deployment from using the Kubernetes SDK. We have created a function DeleteDeployment()
to perform the cleanup task. Let’s review the code for this function.
The DeleteDeployment()
function receives a Deployment name
and namespace
and returns an error
. The Delete()
function call of the Kubernetes SDK does the work of deleting the Deployment. We can call the DeleteDeployment()
function from the main()
.
Assembling the Pieces
After putting all the pieces together, we have a main.go
file containing all the code we discussed previously. Secondly, we have a go.mod
file containing the dependencies.
Installing the Dependencies and Testing
We are using the go version 1.19
. To install the dependencies, we need to run the following command:
We can run the code using the below command:
Before running the code, ensure you can access the target Kubernetes cluster using kubectl
. The kubeconfig
file should be in the user’s home .kube
folder for the setup to work as expected.
Conclusion
The Kubernetes SDK allows us to extend Kubernetes based on our use case. We can leverage the power of the Kubernetes SDK by in-cluster client communication. This way, the code is executed inside the Kubernetes cluster in a Pod where we can provide a granular level of access using a service account with proper permissions. The level of automation we can create using the Kubernetes SDK is limited only by our imagination.
Subscribe to Faizan Bashir
Get the latest posts delivered right to your inbox