Kubernets Command Ckad
Kubernets Command Ckad
apiVersion: v1
kind: Service
metadata:
name: redis-service
namespace: finance
spec:
selector:
app: redis
ports:
- protocol: TCP
port: 6379
targetPort: 6379
type: ClusterIP
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-rc
namespace: finance
spec:
replicas: 3
selector:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis
ports:
- containerPort: 6379
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-deployment
namespace: finance
spec:
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis
ports:
- containerPort: 6379
Before we begin, familiarize yourself with the two options that can come in handy
while working with the below commands:
--dry-run: By default, as soon as the command is run, the resource will be created.
If you simply want to test your command, use the --dry-run=client option. This will
not create the resource. Instead, tell you whether the resource can be created and
if your command is right.
-o yaml: This will output the resource definition in YAML format on the screen.
Use the above two in combination along with Linux output redirection to generate a
resource definition file quickly, that you can then modify and create resources as
required, instead of creating the files from scratch.
POD
Create an NGINX Pod
Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)
Deployment
Create a deployment
You can also scale deployment using the kubectl scale command.
Another way to do this is to save the YAML definition to a file and modify
You can then update the YAML file with the replicas or any other field before
creating the deployment.
Service
Create a Service named redis-service of type ClusterIP to expose pod redis on port
6379
Or
Create a Service named nginx of type NodePort to expose pod nginx's port 80 on port
30080 on the nodes:
(This will automatically use the pod's labels as selectors, but you cannot specify
the node port. You have to generate a definition file and then add the node port in
manually before creating the service with the pod.)
Or
Both the above commands have their own challenges. While one of it cannot accept a
selector the other cannot accept a node port. I would recommend going with the
`kubectl expose` command. If you need to specify a node port, generate a definition
file using the same command and manually input the nodeport before creating the
service.
Reference:
https://kubernetes.io/docs/reference/kubectl/conventions/
---------------------
How do you create a Pod using kubectl run and kubectl create?
How do you configure a Pod with environment variables?
2. Configuration (18%)
Understand ConfigMaps:
How do you configure and use Ingress resources for HTTP and HTTPS routing?
Sample Questions:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
resources:
limits:
memory: "128Mi"
cpu: "500m"
requests:
memory: "64Mi"
cpu: "250m"
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
envFrom:
- configMapRef:
name: my-config
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe:
httpGet:
path: /readiness
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: example-service
port:
number: 80
apiVersion: v1
kind: Pod
metadata:
name: liveness-pod
spec:
containers:
- name: my-container
image: nginx
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 3
periodSeconds: 3
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-volume
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
apiVersion: v1
kind: Pod
metadata:
name: pv-pod
spec:
containers:
- name: my-container
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: pv-storage
volumes:
- name: pv-storage
persistentVolumeClaim:
claimName: pv-claim
apiVersion: v1
kind: Pod
metadata:
name: resource-pod
spec:
containers:
- name: my-container
image: nginx
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
envFrom:
- configMapRef:
name: my-config
apiVersion: v1
kind: Pod
metadata:
name: serviceaccount-pod
spec:
serviceAccountName: my-serviceaccount
containers:
- name: my-container
image: nginx
Create a Horizontal Pod Autoscaler (HPA):
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
apiVersion: v1
kind: Pod
metadata:
name: pvc-pod
spec:
containers:
- name: my-container
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: my-volume
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-pvc
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-namespace
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
project: my-project
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
spec:
selector:
matchLabels:
name: daemonset-example
template:
metadata:
labels:
name: daemonset-example
spec:
containers:
- name: daemonset-container
image: nginx
apiVersion: v1
kind: Pod
metadata:
name: sidecar-pod
spec:
containers:
- name: main-container
image: nginx
- name: sidecar-container
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]
apiVersion: v1
kind: Service
metadata:
name: loadbalancer-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
apiVersion: v1
kind: Pod
metadata:
name: env-printer
spec:
containers:
- name: my-container
image: busybox
command: ["sh", "-c", "printenv"]
env:
- name: MY_VAR
value: "Hello"
Create a Pod that has access to the secret data through a Volume:
apiVersion: v1
kind: Pod
metadata:
name: secret-test-pod
spec:
containers:
- name: test-container
image: nginx
volumeMounts:
# name must match the volume name below
- name: secret-volume
mountPath: /etc/secret-volume
readOnly: true
# The secret data is exposed to Containers in the Pod through a Volume.
volumes:
- name: secret-volume
secret:
secretName: test-secret
kubectl exec -i -t secret-test-pod -- /bin/bash
-------------------------
spec.containers[*].image
spec.initContainers[*].image
spec.activeDeadlineSeconds
spec.tolerations
For example you cannot edit the environment variables, service accounts, resource
limits (all of which we will discuss later) of a running pod. But if you really
want to, you have 2 options:
1. Run the kubectl edit pod <pod name> command. This will open the pod
specification in an editor (vi editor). Then edit the required properties. When you
try to save it, you will be denied. This is because you are attempting to edit a
field on the pod that is not editable.
A copy of the file with your changes is saved in a temporary location as shown
above.
You can then delete the existing pod by running the command:
Then create a new pod with your changes using the temporary file
2. The second option is to extract the pod definition in YAML format to a file
using the command
Then make the changes to the exported file using an editor (vi editor). Save the
changes
vi my-new-pod.yaml
Edit Deployments
With Deployments you can easily edit any field/property of the POD template. Since
the pod template is a child of the deployment specification, with every change the
deployment will automatically delete and create a new pod with the new changes. So
if you are asked to edit a property of a POD part of a deployment you may do that
simply by running the command
ConfigMaps
A ConfigMap is an API object used to store non-confidential data in key-value
pairs. Pods can consume ConfigMaps as environment variables, command-line
arguments, or as configuration files in a volume.