For this I loaded Docker Desktop to walk through the different steps.
Command Line Method
From the command line create a NGINX pod named nginx-pod in the namespace of default (-n default) and if the job stops or restarts do not restart this pod (—restart=Never) —OneTime job
kubectl run nginx-pod --image=nginx --restart=Never --port=80 -n default
Verify the pod you just created (nginx-pod) is running
kubectl get pods
Now you need to expose the nginx-pod outside of Kubernetes over a specific port (in this case port 80)
kubectl expose pod nginx-pod --type=NodePort --port=80 --name=nginx-service
Verify the service you just created (nginx-service) is running
kubectl get svc
So this is a screenshot of me running it on my laptop with Docker for Mac
So if you want to access this pod that is now externally facing you see the 80:31613 above? This means if you open a browser and go to http://127.0.0.1:31613 you will be accessing the kubernetes pod.
Now let’s say you prefer to do ALL of this using a declarative programming way so let’s get started
Declarative YAML Method
Create a YAML file for nginx pod (nginx-pod.yaml):
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
Apply the YAML file using below command:
kubectl apply -f nginx-pod.yaml -n default
This command tells Kubernetes to create or update the resource described in the YAML file. Which means it will create a nginx pod in default namespace
Verify the Pod is Running using below command:
kubectl get pods
Create a YAML file for nginx service (nginx-service.yaml):
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
labels:
app: nginx
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
Apply the YAML file using below command:
kubectl apply -f nginx-service.yaml -n default
Verify the service is created using below command:
kubectl get svc
ROLLBACK/CLEANUP
COMMAND LINE
# Delete the service
kubectl delete service nginx-service
# Delete the pod
kubectl delete pod nginx-pod
YAML WAY
# Delete the service using the YAML file
kubectl delete -f nginx-service.yaml
# Verify that the service is deleted
kubectl get services
# Delete the pod using the YAML file
kubectl delete -f nginx-pod.yaml
# Verify that the pod is deleted
kubectl get pods