Get started
kubectl is a command line tool for communicating with a Kubernetes cluster’s control plane, using the Kubernetes API.
Before exploring the container’s possibilities, let’s start by deploying the default configuration:
docker run \
--interactive \
--tty \
--rm \
--name <container-name> \
dp.apps.rancher.io/containers/kubectl:1.35Check our authentication guide if you need to configure Application Collection OCI credentials in your Docker client.
Container overview
Since there is no official upstream container for kubectl, our container is built from scratch using a SUSE Linux BCI Micro base image and includes our best practices.
Apply a template from a file
To run the kubectl container, execute the following command:
docker run \
--interactive \
--tty \
--rm \
--name <container-name> \
--network host \
--entrypoint "/bin/bash" \
--volume ~/.kube/config:/.kube/config:ro \
--env KUBECONFIG=/.kube/config \
dp.apps.rancher.io/containers/kubectl:1.35We shared the host’s kubeconfig file and set the KUBECONFIG environment variable as required by the kubectl CLI to authenticate and reach
your Kubernetes clusters. We also shared the host’s network to ensure the kubectl container can communicate with the cluster’s API server.
Then, we’ll create an NGINX deployment template file to test the container’s ability to create resources in the Kubernetes cluster.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
imagePullSecrets:
- name: application-collection
containers:
- name: nginx
image: dp.apps.rancher.io/containers/nginx:1.29.3
ports:
- containerPort: 80Use the following commands to copy the template inside the kubectl container and apply it from there.
docker cp nginx-deployment.yaml <container-name>:/tmp/nginx-deployment.yamlThen, within the kubectl container, apply the NGINX deployment manifest:
kubectl apply -f /tmp/nginx-deployment.yamlRun the commands below to check that the Deployment was created and its Pod is running with:
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 5s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-69b67d89cc-279c7 1/1 Running 0 5sScale up the deployment
We deployed NGINX with one Pod. Now, we will scale up the deployment to three Pods with:
kubectl scale deployments/nginx --replicas 3Then, we can check the diff of the modified Deployment against its original template by executing:
kubectl diff -f /tmp/nginx-deployment.yamlDelete the deployment
Once we are done testing the NGINX Deployment, it can be removed with the following command:
kubectl delete -f /tmp/nginx-deployment.yamlThe kubectl container can be stopped and removed as well:
docker rm -f <container-name>