Tool to check few operation on the K8s resource
This project creates a CLI tool using Golang which will be able to perform the following operations:
1.Scale the number of pods
2. Upgrade the version of the nginx server
3. Deletes the pods
4. Continuously watches for the desired number of replicas on the cluster and takes action accordingly
This project also illustrates RBAC in K8s in particaular how one can create a user, give permissions to perform all actions on the ‘pod’ and ‘deployment’ resource in the ‘test’ namespace and assign the required role. Include authentication using a kubeconfig file
Prequisite: Create the user and certs for same, the repo provides a sample certs under cert directory. Please run below commands to create an example user and certs for same.
- cd cert/
- openssl genrsa -out .key 2048
- openssl req -new -key pyn.key -out .csr -subj "/CN=/O="
- openssl x509 -req -in .csr -CA ~/.minikube/ca.crt -CAkey ~/.minikube/ca.key -CAcreateserial -out .crt -days 500
Create user context and update the minikube config
- kubectl config set-credentials --client-certificate=.crt --client-key=.key
- kubectl config set-context -context --cluster=minikube --user=
Switch back to minikube and deploy sample nginx server
- kubectl config use-context minikube
Apply role and rolebinding
- kubectl apply -f role.yaml
- kubectl apply -f role-binding.yml
NOTE: Change the user name in the role-binding.yml based on user name
USAGE: Following is the usage of the CLI
To Scale nginx deployment: kubedeploy --kubeconfig --scale 3 --name nginx
To delete the nginx deployment: kubedeploy delete –name nginx
To watch the nginx pods: kubedeploy watch –name nginx -kubeconfig string (optional) absolute path to the kubeconfig file (default "/home/edgec/.kube/config") -name string Nginx deployment name (default "nginx") -scale int Number replicas for the Nginx Pod Deployment (default 1) -upgrade float Default Nginx version deployed (default 1.14)
The application is also containerized and can be run as the pod on a K8s cluster: Command to deploy the pod:
- Switch to minikube contex: kubectl config use-context minikube
- Deploy the nginx service kubectl apply -f nginx-deploy.yml
- Switch back to the user(pyn) context kubectl config use-context pyn-context 4.Execute above commands to different operations
NOTE: All the commands are tested on minikube
Reference: https://github.com/kubernetes/client-go