Task: Create a pod that runs a printenv command inside the container and passes command line arguments to print hostname and kubernetes port to the stdout.
- Option 1: Use kubectl run
kubectl run container-with-args --image=debian --command=true printenv HOSTNAME KUBERNETES_PORT
kubectl logs <<name-of-pod>>
t2-6c85b5c6b5-6rg98
tcp://10.96.0.1:443
- Option 2: Use the YAML from: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#define-a-command-and-arguments-when-you-create-a-pod
kind: Pod
metadata:
name: command-demo
labels:
purpose: demonstrate-command
spec:
containers:
- name: command-demo-container
image: debian
command: ["printenv"]
args: ["HOSTNAME", "KUBERNETES_PORT"]
restartPolicy: OnFailure
NOTE The YAML generated by kuebctl run in the option 1 is funtionally equivalent of option 2. However, if you take a look at its contents it differs on how the command and arguments are defined in the YAML.
You can run a kubectl run command with --dry-run
switch and view the YAML.
kubectl run container-with-args --image=debian --command=true printenv HOSTNAME KUBERNETES_PORT --dry-run=true -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: container-with-args
name: container-with-args
spec:
replicas: 1
selector:
matchLabels:
run: container-with-args
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: container-with-args
spec:
containers:
- command:
- printenv
- HOSTNAME
- KUBERNETES_PORT
image: debian
name: container-with-args
resources: {}
status: {}
kubectl run logger1 --image=busybox --command=true -- /bin/sh -c ls
kubectl run logger2 --image=busybox --command -- /bin/sh -c 'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done'
kubectl run ng --image=nginx --port=80 --namespace=test
NOTE By using port switch you are making the container available within the Pod network but not externally. If you want to expose it externally you should use kubectl expose command. However you can still access the Pod from local machine by using portforwarding command
kubectl port-forward <<pod-name>> 80:5001
. Now access URLhttp://localhost:5001
and you should able to access the nginx home page.