Basic kubernetes
Setup
This section assumes you've completed the quickstart section.
You need to be aware of which namespace you’re working in, and either set it with kubectl config set-context nautilus --namespace=the_namespace
or specify in each kubectl
command by adding -n namespace
.
Explore the system
To get the list of cluster nodes (although you may not have access to all of them), type:
Right now you probably don't have anything running in the namespace, and these commands will return No resources found in ... namespace.
. There are three categories we will examine: pods, deployments and services. Later these commands will be useful to see what's running:
List all the pods in your namespace
List all the deployments in your namespace
List all the services in your namespace
Launch a simple pod
Let’s create a simple generic pod, and login into it.
You can copy-and-paste the lines below. Create the pod1.yaml
file with the following contents:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: mypod
image: ubuntu
resources:
limits:
memory: 100Mi
cpu: 100m
requests:
memory: 100Mi
cpu: 100m
command: ["sh", "-c", "echo 'Im a new pod' && sleep infinity"]
Reminder, indentation is important in YAML, just like in Python.
If you don't want to create the file and are using mac or linux, you can create yaml's dynamically like this:
Now let’s start the pod:
See if you can find it:
Note: You may see the other pods too.
If it is not yet in Running state, you can check what is going on with
Events and other useful information about the pod can be seen in describe
:
If the pod is in Running state, we can check it's logs
Let’s log into it
You are now inside the (container in the) pod!
Does it feel any different than a regular, dedicated node?
Try to create some directories and some files with content.
(Hello world will do, but feel free to be creative)
We will want to check the status of the networking.
But ifconfig is not available in the image we are using; so let’s install it.
First, let's make sure our installation tools are updated.
Now, we can use apt to install the necessary network tools. Now check the networking:Get out of the Pod (with either Control-D or exit).
You should see the same IP displayed with kubectl
We can now destroy the pod
Check that it is actually gone:
Now, let’s create it again:
Does it have the same IP?
Log back into the pod:
What does the network look like now?
What is the status of the files your created?
Finally, let’s delete explicitly the pod:
Let’s make it a deployment
You saw that when a pod was terminated, it was gone.
While above we did it by ourselves, the result would have been the same if a node died or was restarted.
In order to gain a higher availability, the use of Deployments is recommended. So, that’s what we will do next.
You can copy-and-paste the lines below.
dep1.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-dep
labels:
k8s-app: test-dep
spec:
replicas: 1
selector:
matchLabels:
k8s-app: test-dep
template:
metadata:
labels:
k8s-app: test-dep
spec:
containers:
- name: mypod
image: ubuntu
resources:
limits:
memory: 500Mi
cpu: 500m
requests:
memory: 100Mi
cpu: 50m
command: ["sh", "-c", "sleep infinity"]
Now let’s start the deployment:
See if you can find it:
The Deployment is just a conceptual service, though.
See if you can find the associated pod:
Once you have found its name, let’s log into it
You are now inside the (container in the) pod!
Create directories and files as before.
Try various commands as before.
Let’s now delete the pod!
Is it really gone?
What happened to the deployment?
Get into the new pod
Was anything preserved?
Let’s now delete the deployment:
Verify everything is gone: