Deploying and Exposing an Nginx Service on Kubernetes with YAML Generation and Scaling
This guide walks through creating an Nginx deployment on Kubernetes, exposing it via a NodePort service, generating the corresponding YAML configuration, performing dynamic scaling tests, and using various kubectl commands to manage resources and namespaces.
1. Deploy an Nginx instance
Run the following command to create a deployment named nginx using the nginx:1.20 image: kubectl create deployment nginx --image=nginx:1.20 Verify the pod is running: kubectl get pod -o wide Example output shows the pod nginx-7fb9867-cjnzj in the Running state.
2. Expose the Nginx service
Expose the deployment on port 80, targeting container port 8080, using a NodePort:
kubectl expose deployment nginx --port=80 --target-port=8080 --type=NodePortCheck the services and pods: kubectl get pod,svc -o wide The service nginx appears with a NodePort (e.g., 80:31527/TCP).
3. Generate YAML for the exposed service
Use a dry‑run to output the service definition in YAML:
kubectl expose deployment nginx --port=80 --target-port=8080 --type=NodePort --dry-run -o yamlThe resulting YAML includes apiVersion, kind, metadata, labels, selector, and the type: NodePort specification.
4. Dynamic scaling test
List the current deployment to observe replica settings:
kubectl get deployment5. View all namespaces
Show pods across all namespaces:
kubectl get pods --all-namespaces6. Generate YAML resource files directly
Create a deployment YAML without applying it:
kubectl create deployment nginx --image=nginx:1.20 --dry-run -o yaml7. Deprecated method for exporting YAML
The older --export flag is no longer supported; the command shown is:
kubectl get deploy nginx-deployment -o yaml --export > my.deploy.yamlFinally, the article includes recommended reading links to Redis tutorials and a call to like, share, and follow the author.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
