1.Core Concepts
Create a namespace called 'mynamespace' and a pod with image nginx called nginx on this namespace
$ kubectl create namespace mynamespace
$ kubectl get ns
NAME STATUS AGE
mynamespace Active 6m13s
$ kubectl run nginx --image=nginx --restart=Never -n mynamespace
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 5m2s
Create the pod that was just described using YAML
kubectl run nginx --image=nginx --restart=Never --dry-run=client -n mynamespace -o yaml > pod.yaml
cat pod.yaml
apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: nginx name: nginx spec: containers: - image: nginx name: nginx resources: {} dnsPolicy: ClusterFirst restartPolicy: Never status: {}
delete old nginx pod and recreate one
kubectl delete po nginx -n mynamespace kubectl create -f pod.yaml -n mynamespace
Alternatively, you can run in one line
kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml | kubectl create -n mynamespace -f -
Create a busybox pod (using kubectl command) that runs the command "env". Run it and see the output
$ kubectl run busybox --image=busybox --command --restart=Never -it -- env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=busybox TERM=xterm KUBERNETES_PORT=tcp://10.244.64.1:443 KUBERNETES_PORT_443_TCP=tcp://10.244.64.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_ADDR=10.244.64.1 KUBERNETES_SERVICE_HOST=10.244.64.1 KUBERNETES_SERVICE_PORT=443 KUBERNETES_SERVICE_PORT_HTTPS=443 HOME=/root
$ kubectl run busybox --image=busybox --command --restart=Never -- env
pod/busybox created
$ kubectl logs busybox
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=busybox KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_ADDR=10.244.64.1 KUBERNETES_SERVICE_HOST=10.244.64.1 KUBERNETES_SERVICE_PORT=443 KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT=tcp://10.244.64.1:443 KUBERNETES_PORT_443_TCP=tcp://10.244.64.1:443 HOME=/root
Create a busybox pod (using YAML) that runs the command "env". Run it and see the output
$ kubectl run busybox --image=busybox --restart=Never --dry-run=client -o yaml --command -- env > envpod.yaml $ cat envpod.yam apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: busybox name: busybox spec: containers: - command: - env image: busybox name: busybox resources: {} dnsPolicy: ClusterFirst restartPolicy: Never status: {}
$ kubectl apply -f envpod.yaml
pod/busybox created
# kubectl logs busybox
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=busybox KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT=tcp://10.244.64.1:443 KUBERNETES_PORT_443_TCP=tcp://10.244.64.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_ADDR=10.244.64.1 KUBERNETES_SERVICE_HOST=10.244.64.1 KUBERNETES_SERVICE_PORT=443 HOME=/root
Get the YAML for a new namespace called 'myns' without creating it
# kubectl create namespace myns -o yaml --dry-run=client apiVersion: v1 kind: Namespace metadata: creationTimestamp: null name: myns spec: {} status: {}
Get the YAML for a new ResourceQuota called 'myrq' with hard limits of 1 CPU, 1G memory and 2 pods without creating it
Create a busybox pod (using kubectl command) that runs the command "env". Run it and see the output
showCreate a busybox pod (using YAML) that runs the command "env". Run it and see the output
showGet the YAML for a new namespace called 'myns' without creating it
showGet the YAML for a new ResourceQuota called 'myrq' with hard limits of 1 CPU, 1G memory and 2 pods without creating it
showGet pods on all namespaces
showCreate a pod with image nginx called nginx and expose traffic on port 80
showChange pod's image to nginx:1.7.1. Observe that the container will be restarted as soon as the image gets pulled
showGet nginx pod's ip created in previous step, use a temp busybox image to wget its '/'
showGet pod's YAML
showGet information about the pod, including details about potential issues (e.g. pod hasn't started)
showGet pod logs
showIf pod crashed and restarted, get logs about the previous instance
showExecute a simple shell on the nginx pod
showCreate a busybox pod that echoes 'hello world' and then exits
showDo the same, but have the pod deleted automatically when it's completed
showCreate an nginx pod and set an env value as 'var1=val1'. Check the env value existence within the pod
show- © 2020 GitHub, Inc.
- Terms
- Privacy
- Security
- Status
- Help
- Contact GitHub
- Pricing
- API
- Training
- Blog
- About