Skip to content

Instantly share code, notes, and snippets.

@cleberjsantos
Forked from teonivalois/k8s-tips.md
Created August 6, 2019 13:42
Show Gist options
  • Select an option

  • Save cleberjsantos/acf1dc5c7864bee9cc1ea2eb49cd6dc1 to your computer and use it in GitHub Desktop.

Select an option

Save cleberjsantos/acf1dc5c7864bee9cc1ea2eb49cd6dc1 to your computer and use it in GitHub Desktop.
A few kubernetes tips for those that are still learning

K8S Tips

A set of interesting tips for making the kuberenetes usage a bit more productive.


Quickly creating resources

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

Deployment

kubectl run nginx --image=nginx --dry-run -o yaml

Pod

kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml

Job

kubectl run busybox --image=busybox  --restart=OnFailure --dry-run -o yaml --command -- sh -c "echo 'Hello World!'"

CronJob

kubectl run busybox --image=busybox  --restart=OnFailure --schedule="*/15 * * * *" --dry-run -o yaml --command -- sh -c "echo 'Hello World!'"

Accessing your resources locally

Port Forwarding

Uning the kubectl port-forward command you can bind one resource at a time to a port on your local host.

Examples:

  1. 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

  2. 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.

Kubectl Proxy

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:

  1. 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/

  2. 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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment