A set of interesting tips for making the kuberenetes usage a bit more productive.
With the --dry-run argument we can collect the object that will be sent, but without actually sending it. If combined with -o yaml we can print the yaml version of the object.
Also consider redirecting the output of the following commands to a file, like the following:
$ kubectl run nginx --image=nginx --dry-run -o yaml > nginx-deployment.yaml
kubectl run nginx --image=nginx --dry-run -o yaml
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml
kubectl run busybox --image=busybox --restart=OnFailure --dry-run -o yaml --command -- sh -c "echo 'Hello World!'"
kubectl run busybox --image=busybox --restart=OnFailure --schedule="*/15 * * * *" --dry-run -o yaml --command -- sh -c "echo 'Hello World!'"
Uning the kubectl port-forward command you can bind one resource at a time to a port on your local host.
Examples:
-
Deploy, expose and access an NGINX instance via the kubernetes proxy tunnel:
$ kubectl run nginx --image=nginx $ kubectl expose deployment nginx --port=80 $ kubectl port-forward service/nginx 8080:80
Now, on your browser go to: http://localhost:8080
-
Deploy, and access the kubernetes dashboard:
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml $ kubectl -n kube-system port-forward services/kubernetes-dashboard 8080:443
Now, on your browser go to: https://localhost:8080
Note that the URL is now using the HTTPS protocol, as the service uses port 443. Even though we have bound it to another port, like 8080 in this case, we must force the secure protocol. You may see a certificate warning, but you it should be okay to accept it.
Using the kubectl proxy command, you create a tunnel from your host to your cluster. It will allow you to expose all the services on your cluster at the same time by using the following URL format:
http://localhost:8001/api/v1/namespaces/{NAMESPACE}/services/[https]:{SERVICE_NAME}:/proxy/
NOTE: Exposing all your services at the same time may be dangerous!
- NAMESPACE: the namespace of the service that we want to access
- [https]: In case the service is served via HTTPS we MUST add this prefix to the service name
- SERVICE_NAME: the name of the service you want to access. Pay attention that the name has a colon at the beginning and another at the end of the service name in the url
Examples:
-
Deploy, expose and access an NGINX instance via the kubernetes proxy tunnel:
$ kubectl run nginx --image=nginx $ kubectl expose deployment nginx --port=80 $ kubectl proxy
Now, on your browser go to: http://localhost:8001/api/v1/namespaces/default/services/:nginx:/proxy/
-
Deploy, and access the kubernetes dashboard:
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml $ kube proxy
Now, on your browser go to: http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/