Hello community,
I recently deployed an nginx application (ckad15-depl-svcn
) in Kubernetes with two replicas and exposed it using a NodePort service (ckad15-service-svcn
). The service is configured to use port 30085
for both internal and external access. this is part of ckad mock series questions
For this question, please set the context to
cluster3
by running:kubectl config use-context cluster3
Create a Deployment named
ckad15-depl-svcn
with “two replicas” ofnginx
image and expose it using a service namedckad15-service-svcn
.Please be noted that service needs to be accessed from both inside and outside the cluster (use port
30085
).Create the service in the
default
namespace.
solution by kodekloud:
- The following manifest can be used to create a deployment
ckad15-depl-svcn
withnginx
image and2
replicas.
apiVersion: apps/v1
kind: Deployment
metadata:
name: ckad15-depl-svcn
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- To access from outside the cluster, we use nodeport type of service.
apiVersion: v1
kind: Service
metadata:
name: ckad15-service-svcn
spec:
selector:
app: nginx
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
nodePort: 30085
Details
Is deployment ckad15-depl-svcn
created?
Is service ckad15-service-svcn
created?
Is service accessible from outside the cluster?
but when I checked
curl http://cluster3-node01:30085
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
even though I created deploy and service is with correct named
this is my answer
k get deploy ckad15-depl-svcn -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"creationTimestamp":null,"labels":{"app":"ckad15-depl-svcn"},"name":"ckad15-depl-svcn","namespace":"default"},"spec":{"replicas":2,"selector":{"matchLabels":{"app":"ckad15-depl-svcn"}},"strategy":{},"template":{"metadata":{"creationTimestamp":null,"labels":{"app":"ckad15-depl-svcn"}},"spec":{"containers":[{"image":"nginx","name":"nginx","resources":{}}]}}},"status":{}}
creationTimestamp: "2024-05-28T03:43:43Z"
generation: 1
labels:
app: ckad15-depl-svcn
name: ckad15-depl-svcn
namespace: default
resourceVersion: "14277"
uid: e71310ac-6000-467b-9666-ab016816dcc1
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: ckad15-depl-svcn
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: ckad15-depl-svcn
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
k get svc ckad15-service-svcn -o yaml
apiVersion: v1
kind: Service
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"creationTimestamp":"2024-05-28T03:46:35Z","name":"ckad15-service-svcn","namespace":"default","resourceVersion":"14519","uid":"2cb772ab-f953-4880-811d-5a6eaed949a6"},"spec":{"clusterIP":"10.96.110.140","clusterIPs":["10.96.110.140"],"externalTrafficPolicy":"Cluster","internalTrafficPolicy":"Cluster","ipFamilies":["IPv4"],"ipFamilyPolicy":"SingleStack","ports":[{"nodePort":30085,"port":80,"protocol":"TCP","targetPort":80}],"selector":{"app":"ckad15-depl-svcn"},"sessionAffinity":"None","type":"NodePort"},"status":{"loadBalancer":{}}}
creationTimestamp: "2024-05-28T03:47:27Z"
name: ckad15-service-svcn
namespace: default
resourceVersion: "14601"
uid: b7889ca6-6043-45d2-a1ae-a8fd602f78bc
spec:
clusterIP: 10.96.110.140
clusterIPs:
- 10.96.110.140
externalTrafficPolicy: Cluster
internalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- nodePort: 30085
port: 80
protocol: TCP
targetPort: 80
selector:
app: ckad15-depl-svcn
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
Despite achieving the expected result, this approach was marked as incorrect. Why?