How to do it...

For assigning a name to the Pod, follow the following steps:

  1. The following example is the Pod YAML configuration that assigns the Pod name as my-pod to the container name as my-container; you can successfully create it as follows:
# cat my-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
image: nginx


# kubectl create -f my-pod.yaml
pod "my-pod" created


# kubectl get pods

NAME READY STATUS RESTARTS AGE
my-pod 0/1 Running 0 4s
  1. You can use the kubectl describe command to see the container named my-container as follows:
$ kubectl describe pod my-pod
Name: my-pod
Namespace: default
Node: minikube/192.168.64.12
Start Time: Sat, 16 Dec 2017 10:53:38 -0800
Labels: <none>
Annotations: <none>
Status: Running
IP: 172.17.0.3
Containers:
my-container:
Container ID: docker://fcf36d0a96a49c5a08eb6de1ef27ca761b4ca1c6b4a3a4312df836cb8e0a5304
Image: nginx
Image ID: docker-pullable://nginx@sha256:2ffc60a51c9d658594b63ef5acfac9d92f4e1550f633a3a16d898925c4e7f5a7
Port: <none>
State: Running
Started: Sat, 16 Dec 2017 10:54:43 -0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-lmd62 (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-lmd62:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-lmd62
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 1m default-scheduler Successfully assigned my-pod to minikube
Normal SuccessfulMountVolume 1m kubelet, minikube MountVolume.SetUp succeeded for volume "default-token-lmd62"
Normal Pulling 1m kubelet, minikube pulling image "nginx"
Normal Pulled 50s kubelet, minikube Successfully pulled image "nginx"
Normal Created 50s kubelet, minikube Created container
Normal Started 50s kubelet, minikube Started container
  1. On the other hand, the following example contains two containers, but assigns the same name, my-container; therefore, the kubectl create command returns an error and can't create the Pod:
//delete previous Pod
$ kubectl delete pod --all
pod "my-pod" deleted


$ cat duplicate.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
- name: my-container
image: centos
command: ["/bin/sh", "-c", "while : ;do curl http://localhost:80/; sleep 3; done"]


$ kubectl create -f duplicate.yaml
The Pod "my-pod" is invalid: spec.containers[1].name: Duplicate value: "my-container"
You can add the --validate flag.
For example, the command kubectl create -f duplicate.yaml --validate uses a schema to validate the input before sending it.

In another example, the YAML contains a ReplicationController and Service, both of which are using the same name, my-nginx, but it is successfully created because the Deployment and Service are different objects:

$ cat my-nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3
selector:
matchLabels:
run: my-label
template:
metadata:
labels:
run: my-label
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-nginx
spec:
ports:
- protocol: TCP
port: 80
type: NodePort
selector:
run: my-label


//create Deployment and Service
$ kubectl create -f my-nginx.yaml
deployment.apps "my-nginx" created
service "my-nginx" created


//Deployment "my-nginx" is created
$ kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
my-nginx 3 3 3 3 1m

//Service "my-nginx" is also created
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 13d
my-nginx NodePort 10.0.0.246 <none> 80:31168/TCP 1m
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.221.144