k8s Basics Flashcards
Know the definitions of different concepts
Namespaces
Namespace is kind of a virtual cluster where u can deploy your objects.
By default kubectl interacts with “default” namespace
To reference objects in other namespaces
Use : kubectl –namespace=mystuff
To list objects in all namespaces
Ex:_ kubectl get pods –all-namespaces
How to change to another namespace ?
We need to use a context.
it gets recorded in configuration file /.kube/config
$kubectl config set-context my-context –namespace=mystuff
After this to use the newly created context
$ kubectl config use-context my-context
Note: contexts can also be used to manage different clusters , users with set-context
Get detailed information on a kubernetes object
$ kubectl describe
Ex.
$kubectl describe pod nginx
Create update and destroy k8s objects
Create k8s object
$ kubectl apply -f obj.yaml
To print the object to console
$kubectl apply -f object.yaml –dry-run
To a YAML file
$kubectl apply -f object.yaml –dry-run -o yaml > object.yaml
Interactive edits instead of editing file
$ kubectl edit
View last applied configuration
$kubectl apply -f object.yaml view-last-applied
Delete object
$ kubectl delete -f object.yaml
extract a pod definition into YAML
pod : webserver
$ kubectl get po webserver -o yaml > pod.yaml
$ vi pod.yaml [edit what u want here , delete the webserver POD , then create new POD using kubectl create -f pod.yaml ]
edit a deployment
deployment name : webapp-deploy
you do not need to necessarily delete a deployment to edit it
$ kubectl edit deploy webapp-deploy
imperative commands use:
Deploy a redis pod using the redis:alpine image with the labels set to tier=db.
$ kubectl run redis –image=redis:alpine -l tier=db
Create a new pod called custom-nginx using the nginx image and expose it on container port 8080
$ kubectl run custom-nginx –image=nginx –port=8080
Create a new namespace called dev-ns
$ kubectl create namespace dev-ns
or
$ kubectl create ns dev-ns
Create a new deployment called redis-deploy in the dev-ns namespace with the redis image. It should have 2 replicas.
$ kubectl create deploy redis-deploy -n dev-ns –image=redis –replicas=2
Create a pod called httpd using the image httpd:alpine in the default namespace. Next, create a service of type ClusterIP by the same name (httpd). The target port for the service should be 80.
$ kubectl run po http –image=httpd:alpine
$ kubectl expose po httpd –port=80
How to check how many Namespaces exist on the system?
$ kubectl get namespace –no-headers | wc -l
- check pods in a namespace ‘love’
- create pod in namespace ‘love’
image: redis
name: redis-pod
1.
$ kubectl get po -n love
2.
$ k run redis-pod –images=redis -n love
how to find a POD using POD name in which namespace it exists ?
$ kubectl get po –all-namespaces
how to access a service ex: db-service
if it exists in same namespace and also suppose it exists in different namespace say namespace=dev
- If
db-service
exists in same namespace
you can access it using the service name itself
heredb-service
- If
db-service
exists in another namespace
you need to provide the FQDN(fully-qualified domain name ) of the namespace it exists
db-service.dev.svc.cluster.local
In dockerfile , how do you specify commands and arguments
CMD [“sleep” , “5”]
the first argument should be an executable
like sleep , kubectl etc…
what is ENTRYPOINT instruction in Dockerfile
it is a command which makes u to run an executable/job etc.. at the start of a container
example :
image name: ubuntu
Dockerfile:
…
ENTRYPOINT [“sleep”]
container : ubuntu-sleeper
we can run docker run ubuntu-sleeper 10
and it will take command as sleep 10
how to override entrypoint command at docker startup
$ docker run –entrypoint=<> <> <>
how do you give args in mainifest files in k8s?
let’s say a POD def file
pod.yaml
apiVersion: v1 kind:Pod metadata: name:ubuntu-sleeper spec: containers: - name: ubuntu-sleeper image: ubuntu-sleeper args: ["10"]
above will add the arg to the command in DOckerfile let’s say ENTRYPOINT [“sleep”]
then the command totally becomes sleep 10
but if you want to override the ENTRYPOINT itself in Dockerfile
utilize commands field
... spec: containers: - name: ubuntu-sleeper image: ubuntu-sleeper command: ["sleep2.0"] args: ["10"]
…
so args field in yaml overrides CMD in docker file and
command field in yaml overrides ENTRYPOINT field in Dockerfile
can we edit the running pod and deployment ?
for POD , only a few instructions can be changed
other than that , put your pod into a manifest if not present
delete the previous pod and create a new one using the new YAML manifest
for deployment , we can edit as PODs will be re-checked and changed to desired state if they are not
$kubectl edit deploy webapp-deploy
how to check what command is used in a POD
$ kubectl describe po pod_name
go to Command: field and check for it
how to set environment variables in manifest file of POD
pod. yaml
- –
... spec: containers: - name: webapp-container image: webapp ports: - containerPort: 8080 env: - name: DB_HOST #namewillAvailable to be used value: postgres
what are different types of environment variables we can set
1. plain key-value ... env: - name: DB_PASSWORD value: YPO4reh=
2. configMap ... env: - name: DB_PASSWORD valueFrom: configMapKeyRef:
3. secrets ... env: - name: DB_PASSWORD valueFrom: secretMapKeyRef: