4 - Configuration Flashcards
Create a configmap named config with values foo=lala,foo2=lolo
kubectl create configmap config –from-literal=foo=lala –from-literal=foo2=lolo
Display its values
kubectl get cm config -o yaml
# or
kubectl describe cm config
Create and display a configmap from a file
echo -e “foo3=lili\nfoo4=lele” > config.txt
kubectl create cm configmap2 –from-file=config.txt
kubectl get cm configmap2 -o yaml
Create and display a configmap from a .env file
Create the file with the command
echo -e “var1=val1\n# this is a comment\n\nvar2=val2\n#anothercomment” > config.env
kubectl create cm configmap3 –from-env-file=config.env
kubectl get cm configmap3 -o yaml
Create and display a configmap from a file, giving the key ‘special’
Create the file with
echo -e “var3=val3\nvar4=val4” > config4.txt
kubectl create cm configmap4 –from-file=special=config4.txt
kubectl describe cm configmap4
kubectl get cm configmap4 -o yaml
Create a configMap called ‘options’ with the value var5=val5. Create a new nginx pod that loads the value from variable ‘var5’ in an env variable called ‘option’
kubectl create cm options –from-literal=var5=val5
kubectl run nginx –image=nginx –restart=Never –dry-run=client -o yaml > pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
env:
- name: option # name of the env variable
valueFrom:
configMapKeyRef:
name: options # name of config map
key: var5 # name of the entity in config map
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
kubectl create -f pod.yaml
kubectl exec -it nginx – env | grep option # will show ‘option=val5’
Create a configMap ‘anotherone’ with values ‘var6=val6’, ‘var7=val7’. Load this configMap as env variables into a new nginx pod
kubectl create configmap anotherone –from-literal=var6=val6 –from-literal=var7=val7
kubectl run –restart=Never nginx –image=nginx -o yaml –dry-run=client > pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
envFrom: # different than previous one, that was ‘env’
- configMapRef: # different from the previous one, was ‘configMapKeyRef’
name: anotherone # the name of the config map
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
kubectl create -f pod.yaml
kubectl exec -it nginx – env
Create a configMap ‘cmvolume’ with values ‘var8=val8’, ‘var9=val9’. Load this as a volume inside an nginx pod on path ‘/etc/lala’. Create the pod and ‘ls’ into the ‘/etc/lala’ directory.
kubectl create configmap cmvolume –from-literal=var8=val8 –from-literal=var9=val9
kubectl run nginx –image=nginx –restart=Never -o yaml –dry-run=client > pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
volumes: # add a volumes list
- name: myvolume # just a name, you’ll reference this in the pods
configMap:
name: cmvolume # name of your configmap
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
volumeMounts: # your volume mounts are listed here
- name: myvolume # the name that you specified in pod.spec.volumes.name
mountPath: /etc/lala # the path inside your container
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
kubectl create -f pod.yaml
kubectl exec -it nginx – /bin/sh
cd /etc/lala
ls # will show var8 var9
cat var8 # will show val8
Create the YAML for an nginx pod that runs with the user ID 101. No need to create the pod
kubectl run nginx –image=nginx –restart=Never –dry-run=client -o yaml > pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
securityContext: # insert this line
runAsUser: 101 # UID for the user
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
Create the YAML for an nginx pod that has the capabilities “NET_ADMIN”, “SYS_TIME” added to its single container
kubectl run nginx –image=nginx –restart=Never –dry-run=client -o yaml > pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
securityContext: # insert this line
capabilities: # and this
add: [“NET_ADMIN”, “SYS_TIME”] # this as well
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
Create an nginx pod with requests cpu=100m,memory=256Mi and limits cpu=200m,memory=512Mi
kubectl run nginx –image=nginx –dry-run=client -o yaml > pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources:
requests:
memory: “256Mi”
cpu: “100m”
limits:
memory: “512Mi”
cpu: “200m”
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
Create a namespace named limitrange with a LimitRange that limits pod memory to a max of 500Mi and min of 100Mi
kubectl create ns one
vi 1.yaml
apiVersion: v1
kind: LimitRange
metadata:
name: ns-memory-limit
namespace: one
spec:
limits:
- max: # max and min define the limit range
memory: “500Mi”
min:
memory: “100Mi”
type: Container
kubectl apply -f 1.yaml
Describe the namespace limitrange
kubectl describe limitrange ns-memory-limit -n one
Create an nginx pod that requests 250Mi of memory in the limitrange namespace
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
namespace: one
spec:
containers:
- image: nginx
name: nginx
resources:
requests:
memory: “250Mi”
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
Create ResourceQuota in namespace one with hard requests cpu=1, memory=1Gi and hard limits cpu=2, memory=2Gi.
apiVersion: v1
kind: ResourceQuota
metadata:
name: my-rq
namespace: one
spec:
hard:
requests.cpu: “1”
requests.memory: 1Gi
limits.cpu: “2”
limits.memory: 2Gi
kubectl apply -f rq-one.yaml
or
kubectl create quota my-rq –namespace=one –hard=requests.cpu=1,requests.memory=1Gi,limits.cpu=2,limits.memory=2Gi