BookmarkSubscribeRSS Feed

kubectl for the SAS administrator

Started ‎04-29-2024 by
Modified ‎04-29-2024 by
Views 892

As a SAS Administrator for SAS Viya  two command line tools are essential: sas-viya and the kubectl. Both commands help you to do common tasks. This post will be about using kubectl for some common tasks.

 

The article only introduces commands and options as they are used for the examples shown. Consult the kubectl documentation for all the available commands and options.

 

The following topics are covered:

  • Basics on kubectl
  • List all pods running a SAS compute server
  • List all pods running either as compute server, connect server or batch job
  • List log from a pod

Basics on kubectl

 

The kubectl command is your interface to the Kubernetes cluster's control plane, it uses the the Kubernetes API for communication.

 

In order to connect you will need a profile. By default the profile is stored at this location: ~/.kube in the file named config. Ask your Kubernetes administrator for the profile. You can use the kubectl config view command to see the current config.

 

To get a list of all Kubernetes  resources that you can manage use the command kubectl api-resources. The output looks like this:

 

brm-1-kubectl-api-resources-1024x186.png

Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.

 

To get a list of resources of a specific kind you can use kubectl get NAME|SHORTNAMES where NAME|SHORTNAMES is the value under the respective column headings in the output above.

 

Setting a default namespace

 

A SAS Viya environment is contained within a Kubernetes namespace. To get a list of all namespaces use the command kubectl get namespaces or kubectl get ns.

 

As we can see in the image above many resources need a namespace to be specified (column NAMESPACED is true). You can specify the namespace name with each command like so: kubectl get pods -n edu, or you can set a default namespace with this command kubectl config set-context --current --namespace edu,to avoid typing the -n namespace option with every command.

 

List all pods running a SAS compute server

 

SAS Studio users use a compute server to execute the SAS program code. The compute server is running within a pod in the Kubernetes cluster. In this example we will look how we can list all the compute server pods and which user is using it.

 

First we need to get some information on a compute server pod. Make sure you have a compute server running. Luckily for us all the compute server pods follow this naming convention: sas-compute-server-<some-key>. So we can use the command kubectl get pods | grep sas-compute-server to get a list. The list will look similar to this:

 

sas-compute-server-1c685360-a73c-4d16-874e-253267158530-1997 2/2 Running 0 37m
sas-compute-server-4156241b-ff03-44f6-a413-208678c8166a-2002 2/2 Running 0 20s

 

Each pod has some labels attached that we can use for selecting specific pods and/or display the label information. Lets see what labels are available for a compute server pod. For that we use the command kubectl describe pod <name-from-list-above>. The result will look like this (only the top portion is displayed):

 

brm-2-kubectl-describe-pod.png

 

Some labels of interest are: launcher.sas.com/job-type telling us the type of server and launcher.sas.com/username giving us the username. An alternative to display labels of pods is by using the --show-labels option with the kubectl get pods command.

 

The kubectl get pods command supports two options that help us:

 

Option name Description
-l, --selector filter pods that have a given label, we can filter on more than one label, all conditions must be true
-L, --label-columns define a comma separated list of labels to display as additional columns

 

Example:

 

$ kubectl get pods -l sas.com/created-by=sas-launcher -L launcher.sas.com/username

NAME                                                         READY STATUS  RESTARTS AGE   USERNAME

sas-compute-server-4156241b-ff03-44f6-a413-208678c8166a-2002 2/2   Running 0        6h26m eric

sas-compute-server-d944163a-dafe-4109-a397-2e39ae843af2-2043 2/2   Running 0        92m   christine

 

You can add the option -o wide to the above command to see on which node the pod is executing on. If you want to sort the listing by username, you can use the --sort-by option. You must write the field to sort by as a JSONPath expression. Example:

 

$ kubectl get pods -l sas.com/created-by=sas-launcher -L launcher.sas.com/username --sort-by '.metadata.labels.launcher\.sas\.com/username'

NAME                                                         READY STATUS  RESTARTS AGE   USERNAME

sas-compute-server-d944163a-dafe-4109-a397-2e39ae843af2-2043 2/2   Running 0        94m   christine

sas-compute-server-4156241b-ff03-44f6-a413-208678c8166a-2002 2/2   Running 0        6h29m eric

 

Note the the JSONPath must be specified in single quotes. the period in the label name must be escaped. 


List all pods running either as compute server, connect server or batch job


Beside a compute server pod there may be other types of SAS servers running, to get a list of all servers we can use a similar command as before with a different -l option and displaying two labels: 

$ kubectl get pods -l 'sas.com/created-by=sas-launcher' -L launcher.sas.com/username,launcher.sas.com/job-type

NAME                                                      READY STATUS  RESTARTS AGE   USERNAME JOB-TYPE

sas-compute-server-659c96bf-8ceb-4fc2-a1e1-f97d2f4b16d2-5 2/2   Running 0        6m56s geladm   compute-server

sas-connect-server-9b2aa9bb-dc88-47a8-881f-e54d6e3d6c0d-6 2/2   Running 0        6m35s geladm   sas-connect-server

sas-batch-server-e2015140-ce29-41c0-bec9-b33a6a309117-7   2/2   Running 0        17s   Frank    sas-batch-job

 

As we can see, beside the compute server we also have a pod running that was created by a SAS/CONNECT Server as well as a pod create by running a SAS program in batch using the sas-viya batch jobs submit-pgm command.

 

List log from a pod

 

Another typical operation might be to look at the log of a given service or pod running.

 

The command to get the log of a specific container in a pod looks like this:

 

kubectl logs <pod-name> <container-name>

 

This command will return all the log entries available. The container name is optional.

 

A service of interest to the administrator is the sas-readiness service. It keeps track of the services not ready. During the start of the SAS Viya environment we can easily follow the log of this service using this command:

 

kubectl logs -l app=sas-readiness --tail -1 | jq

 

The kubectl logs command can read the logs of a container within a pod. Options used explained:

 

Option Description
-l app=... We only want to list the log of the sas-readiness service. Using this option we do not need to specify the pod name.
--tail -1 Since we use the -l to select a specific service only the last 10 lines are returned by default. So we use the --tail -1 to get all log lines
-f Optionally you can specify this option to "follow" the log as new log entries are created.

 

The jq command will display the log lines, they are JSON format, in a more readable form. The image below shows the last two log entries from the sas-readiness service.

 

brm-3-kubectl-logs-sas-readiness-1-1024x460.png

 

As we can see from the image above a log entries contains a lot of information which we might not always be interested in. We can use functionality of the jq command to only display certain fields from the JSON. The example below shows the log of the default CAS server.

 

kubectl -n edu logs sas-cas-server-default-controller | jq -R -r '. as $line | try (fromjson| "\(.timeStamp) \(.level | ascii_upcase) \(.message)" ) catch $line'

 

Since sometimes there are plain text messages produced by a command we use the try, catch functionality of the jq command to either test for a valid JSON or just print the plain text message.

 

brm-4-kubectl-logs-cas-1024x63.png

 

As we can see from the image above, since we did not specify a container name, the kubectl logs command choose on. Since we have the try/catch in our jq command this line is printed as is. For the JSON based log lines only the fields we asked for are printed.

 

Summary

 

This article described three use cases for the kubectl command. We learned about the possibility to use the -l option to subset the listing of pods based on a label assigned to the pod. Likewise we learned about the -L option to display additional information. Finally we have seen some examples to list the log of a pod in different ways.

 

The kubectl command has many more options and commands to do many more things. So be curious and start exploring.

Version history
Last update:
‎04-29-2024 10:11 AM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags