For simulated Practice problems visit KillerCoda.
Setup
# copy these contents & create the pod
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
nodeName: node01
containers:
- image: nginx
name: nginx
resources: {}
ports:
- containerPort: 80
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
EOF
Solution
# create service yaml file
k create svc nodeport ng-svc --tcp=80:80 --target-port=80 --dry-run=client -o yaml > service.yaml
# update selector in service yaml file
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: ng-svc
name: ng-svc
spec:
ports:
- name: ports
nodePort: 30002
port: 80
protocol: TCP
targetPort: 80
selector:
app: myapp
type: NodePort
# create service
k create -f service.yaml
# get the node ip address
k get nodes -o wide
# make request on the node ip with port 30002
wget -qO- <node01-ip>:30002
# get the service ip
k get svc
# run
while true; do curl -s <service-ip>; sleep 1; done
# it will distribute request to the 3 created pods
Setup
# copy these contents & create the pod
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: pod-1
labels:
app: frontend
spec:
volumes:
- name: html-volume
emptyDir: {}
initContainers:
- name: init-c
image: busybox
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
command: ['sh', '-c', 'echo "responding from pod-1" > /usr/share/nginx/html/index.html']
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
---
apiVersion: v1
kind: Pod
metadata:
name: pod-2
labels:
app: frontend
spec:
volumes:
- name: html-volume
emptyDir: {}
initContainers:
- name: init-c
image: busybox
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
command: ['sh', '-c', 'echo "responding from pod-2" > /usr/share/nginx/html/index.html']
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
---
apiVersion: v1
kind: Pod
metadata:
name: pod-3
labels:
app: frontend
spec:
volumes:
- name: html-volume
emptyDir: {}
initContainers:
- name: init-c
image: busybox
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
command: ['sh', '-c', 'echo "responding from pod-3" > /usr/share/nginx/html/index.html']
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
EOF
Solution
# create service yaml file
apiVersion: v1
kind: Service
metadata:
name: frontend
spec:
selector:
app: frontend
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
# create service
k create -f service.yaml
Setup
# copy these contents & create the pod
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busyboc
name: bbox
spec:
nodeName: node01
containers:
- image: busybox
name: bbox
resources: {}
command: ["sh","-c","sleep 3600"]
ports:
- containerPort: 80
dnsPolicy: ClusterFirst
restartPolicy: Always
EOF
Solution
# create pod on port 80
k expose pod bbox --port=80 --name=bbox-svc --namespace=setup
Setup
# copy these contents & create the pod
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: backend
name: backend
namespace: setup
spec:
replicas: 3
selector:
matchLabels:
app: backend
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: backend
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
resources: {}
EOF
Solution
# create deploy on port 80
k expose deploy backend --name=dep-svc --type=ClusterIP --port=80 --namespace=setup