Skip to content

Latest commit

 

History

History
75 lines (65 loc) · 2.71 KB

ckad.md

File metadata and controls

75 lines (65 loc) · 2.71 KB

Objective: Passing command line arguments to a container.

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
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: {}

Task: Create a Pod and list directory inside the running container

kubectl run logger1 --image=busybox --command=true -- /bin/sh -c ls

Task: Create a Pod that writes some text to stdout every second

kubectl run logger2 --image=busybox --command -- /bin/sh -c 'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done'

Task: Create a Pod in a test namespace and expose the nginx container on port 80

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 URL http://localhost:5001 and you should able to access the nginx home page.