- Prepare a cluster (Single node, kubeadm, k3s, etc)
- Open browser tabs to https://kubernetes.io/docs/, https://github.com/kubernetes/ and https://kubernetes.io/blog/ (these are permitted as per the current guidelines)
- Deploy the following manifest
- Using
kubectl, identify the Pod IP addresses - Determine the DNS name of the service.
Answer
Identify the selector for the service:
kubectl describe service nginx-service | grep -i selector
Selector: app=nginxFilter kubectl output:
kubectl get po -l app=nginx -o wideService name will be, based on the format [Service Name].[Namespace].[Type].[Base Domain Name] :
nginx-service.default.svc.cluster.localIdentify the associated Pods based on this label:
- Create three
deploymentsof your choosing - Expose one of these deployments with a service of type
ClusterIP - Expose one of these deployments with a service of type
Nodeport - Expose one of these deployments with a service of type
Loadbalancer- Note, this remains in
pendingstatus unless your cluster has integration with a cloud provider that provisions one for you (ie AWS ELB), or you have a software implementation such asmetallb
- Note, this remains in
Answer - Imperative
kubectl create deployment nginx-clusterip --image=nginx --replicas 1
kubectl create deployment nginx-nodeport --image=nginx --replicas 1
kubectl create deployment nginx-loadbalancer --image=nginx --replicas 1kubectl expose deployment nginx-clusterip --type="ClusterIP" --port="80"
kubectl expose deployment nginx-nodeport --type="NodePort" --port="80"
kubectl expose deployment nginx-loadbalancer --type="LoadBalancer" --port="80"Answer - Declarative
Apply the following:
kind: Service
apiVersion: v1
metadata:
name: nginx-clusterip
spec:
selector:
app: nginx-clusterip
type: ClusterIP
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-clusterip
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx-clusterip
template:
metadata:
labels:
app: nginx-clusterip
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
name: nginx-nodeport
spec:
selector:
app: nginx-nodeport
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-nodeport
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx-nodeport
template:
metadata:
labels:
app: nginx-nodeport
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
name: nginx-loadbalancer
spec:
selector:
app: nginx-loadbalancer
type: LoadBalancer
ports:
- port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-loadbalancer
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx-loadbalancer
template:
metadata:
labels:
app: nginx-loadbalancer
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80- Create an
ingressobject namedmyingresswith the following specification:
- Manages the host
myingress.mydomain - Traffic to the base path
/will be forwarded to aservicecalledmainon port 80 - Traffic to the path
/apiwill be forwarded to aservicecalledapion port 8080
Answer - Imperative
kubectl create ingress myingress --rule="myingress.mydomain/=main:80" --rule="myingress.mydomain/api=api:8080"Answer - Declarative
Apply the following YAML:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myingress
spec:
rules:
- host: myingress.mydomain
http:
paths:
- backend:
serviceName: main
servicePort: 80
path: /
pathType: Exact
- backend:
serviceName: api
servicePort: 8080
path: /api
pathType: Exact- Identify the configuration location of
coredns - Modify the coredns config file so DNS queries not resolved by itself are forwarded to the DNS server
8.8.8.8 - Validate the changes you have made
- Add additional configuration so that all DNS queries for
custom.localare forwarded to the resolver10.5.4.223
Answer
kubectl get cm coredns -n kube-system
NAME DATA AGE
coredns 2 94dkubectl edit cm coredns -n kube-system
replace:
forward . /etc/resolv.conf
with
forward . 8.8.8.8Add the block:
custom.local:53 {
errors
cache 30
forward . 10.5.4.223
reload
}