Kubernetes Package Toolkit
Published: 2023-12-15 10:38:30


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/draitekaixbszegxfvm5.png)
_**kpt stands for Kuberentes Package Toolkit. It is a set of tools for working with and managing Kubernetes manifests as packages. kpt helps you to organize, customize, share, and manage Kubernetes manifests in a more modular and reusable way.**_

**Key features of kpt include:**

**Package Management:** _kpt allows you to organize your Kubernetes configuration files into packages. A package is a directory containing one or more Kubernetes manifests, and it can be versioned and shared._

**Declarative Configuration:** _It encourages a declarative approach to configuration, where you describe the desired state of your Kubernetes resources, making it easier to manage configurations across different environments._

**Resource Configuration:** _kpt provides commands to work with and transform Kubernetes resources. This includes adding, updating, or removing fields from manifests._

**GitOps Workflow:** _It aligns with the GitOps approach, where changes to your infrastructure are driven by Git commits. You can use kpt to fetch, update, and apply changes to your Kubernetes manifests stored in Git repositories._

**Template Functions:** _kpt supports template functions that allow you to parameterize and customize your manifests based on different environments or requirements._

**Resource Composition:** _You can compose and customize your Kubernetes manifests by using kpt functions and tools._

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19qhkgaou2zvkl5vh8lh.png)

**System Requirements**

- KPT must be installed

```
 #wget https://github.com/GoogleContainerTools/kpt/releases/download/v1.0.0-beta.44/kpt_linux_amd64
 #chmod +x kpt_linux_amd64
 #cp kpt_linux_amd64 /usr/local/bin/kpt
 #kpt version
 1.0.0-beta.44
```


- Git must be installed
```
# git version
git version 2.40.1
```

- Kubernetes cluster

```
root@master:~/nginx# kubectl get nodes
NAME                        STATUS   ROLES           AGE   VERSION
master.homecluster.store    Ready    control-plane   38d   v1.26.0
worker1.homecluster.store   Ready    <none>          38d   v1.26.0
worker2.homecluster.store   Ready    <none>          38d   v1.26.0

```

> kpt is fully integrated with Git and enables forking, rebasing and versioning a package of configuration using the underlying Git version control system.

_First, let’s fetch the kpt package from Git to your local filesystem:_

```
# kpt pkg get https://github.com/GoogleContainerTools/kpt/package-examples/nginx@v0.9
# cd nginx
```

> kpt pkg commands provide the functionality for working with packages on Git and on your local filesystem.

```
#kpt pkg tree
Package "nginx"
├── [Kptfile]  Kptfile nginx
├── [deployment.yaml]  Deployment my-nginx
└── [svc.yaml]  Service my-nginx-svc
```

**Apply the Package**

> kpt live commands provide the functionality for deploying packages to a Kubernetes cluster.

_Initialize the kpt package:_

```
 # kpt live init
 
```
_Apply the resources to the cluster:_

```
#root@master:~/nginx/nginx# kubectl get pods
no resource found
# kpt live apply --reconcile-timeout=15m
inventory update started
inventory update finished
apply phase started
service/my-nginx-svc apply successful
deployment.apps/my-nginx apply successful
apply phase finished
reconcile phase started
service/my-nginx-svc reconcile successful
deployment.apps/my-nginx reconcile pending
deployment.apps/my-nginx reconcile successful
reconcile phase finished
inventory update started
inventory update finished
apply result: 2 attempted, 2 successful, 0 skipped, 0 failed
reconcile result: 2 attempted, 2 successful, 0 skipped, 0 failed, 0 timed out

root@master:~/nginx/nginx# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-66f8758855-57q7v   1/1     Running   0          81s
my-nginx-66f8758855-5zj88   1/1     Running   0          81s
my-nginx-66f8758855-lbpmq   1/1     Running   0          81s
my-nginx-66f8758855-zp6nm   1/1     Running   0          81s

```
_Delete the package from the cluster:_

```
# kpt live destroy
delete phase started
deployment.apps/my-nginx delete successful
service/my-nginx-svc delete successful
delete phase finished
reconcile phase started
deployment.apps/my-nginx reconcile successful
service/my-nginx-svc reconcile successful
reconcile phase finished
inventory update started
inventory update finished
delete result: 2 attempted, 2 successful, 0 skipped, 0 failed
reconcile result: 2 attempted, 2 successful, 0 skipped, 0 failed, 0 timed out

```