I have been thinking about what should my first blog post be about. I figured since I just took the CKA (by the way, I passed!), I have kubernetes short hand commands on the brain; so I'll write about using less YAML when working with k8s.
When studying for the CKA; I came across a lot of blogs/howtos that show things like creating pods and deployments by creating a YAML file and using kubectl create -f ... or (what's worse) you'll see a cat <<EOF | kubectl create -f - to create a resource.
Now don't get me wrong; I'm not bashing using YAML. When working with kubernetes, you'll inevitably have to use YAML at some point. It's also completely valid way to do things. But when you're doing something like an exam, where time is precious. These can come in handy!
During the CKA exam, you have (if you average it out); 7 minutes per question. So time is precious and I learned how to generate resources through the kubectl command to save time.
So to create a deployment you can do the following
$ kubectl create deployment welcome-php --image=quay.io/redhatworkshops/welcome-php:latest deployment.apps/welcome-php created
This creates all my resources
$ kubectl get deploy,rs,pod NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deployment.extensions/welcome-php 1 1 1 1 7m NAME DESIRED CURRENT READY AGE replicaset.extensions/welcome-php-57db6cbb6 1 1 1 7m NAME READY STATUS RESTARTS AGE pod/welcome-php-57db6cbb6-kjtcz 1/1 Running 0 7m
Now, I can actually create a service by exposing the deplpyment
$ kubectl expose deploy welcome-php --port=8080 --target-port=8080 service/welcome-php exposed
Now I have all the resources I need for my application.
$ kubectl get deploy,svc,rs,pod NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deployment.extensions/welcome-php 1 1 1 1 15m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/welcome-php ClusterIP 100.71.31.164 <none> 8080/TCP 1m NAME DESIRED CURRENT READY AGE replicaset.extensions/welcome-php-57db6cbb6 1 1 1 15m NAME READY STATUS RESTARTS AGE pod/welcome-php-57db6cbb6-kjtcz 1/1 Running 0 15m
If you look at the kubectl create -h it'll show you what you can create via the cli. Here is a snippet.
Available Commands: clusterrole Create a ClusterRole. clusterrolebinding Create a ClusterRoleBinding for a particular ClusterRole configmap Create a configmap from a local file, directory or literal value deployment Create a deployment with the specified name. job Create a job with the specified name. namespace Create a namespace with the specified name poddisruptionbudget Create a pod disruption budget with the specified name. priorityclass Create a priorityclass with the specified name. quota Create a quota with the specified name. role Create a role with single rule. rolebinding Create a RoleBinding for a particular Role or ClusterRole secret Create a secret using specified subcommand service Create a service using specified subcommand. serviceaccount Create a service account with the specified name
So to recap...you can run the following to create and expose your application without using any YAML.
$ kubectl create deployment welcome-php --image=quay.io/redhatworkshops/welcome-php:latest $ kubectl expose deploy welcome-php --port=8080 --target-port=8080
You can even do the same with a pod and node port. (note that I named the nodeport the same as the pod)
$ kubectl run nginx --image=nginx --generator=run-pod/v1 -l app=nginx pod/nginx created $ kubectl create service nodeport nginx --node-port=32000 --tcp=80:80 service/nginx created
When working with kubernetes, you will run into lots of YAMLs that you will be copying and pasting. You can save yourself some typing if use the kubectl to create these resources for you!