When you create any Kubernetes objects such as a pod, replication controller and service, you can assign a name to it. The names in Kubernetes are spatially unique, which means you cannot assign the same name in the pods.
Kubernetes allows us to assign a name with the following restrictions:
The following example is the pod definition 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
You can use the kubectl describe
command to see the container name my-container
as follows:
# kubectl describe pod my-pod Name: my-pod Namespace: default Image(s): nginx Node: ip-10-96-219-25/10.96.219.25 Start Time: Wed, 16 Dec 2015 00:46:33 +0000 Labels: <none> Status: Running Reason: Message: IP: 192.168.34.35 Replication Controllers: <none> Containers: my-container: Container ID: docker://5501d115703e334ae44c1541b990a7e22ce4f310226eafea206594e4c85c90d9 Image: nginx Image ID: docker://6ffc02088cb870652eca9ccd4c4fb582f75b29af2879792ed09bb46fd1c898ef State: Running Started: Wed, 16 Dec 2015 00:46:34 +0000 Ready: True Restart Count: 0 Environment Variables:
On the other hand, the following example contains two containers, but assigns the same name as my-container
, therefore the kubectl
command returns an error and can't create the pod.
//delete previous pods # kubectl delete pods --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'
In another example, the YAML contains a replication controller and service, both of which are using the same name my-nginx
, but it is successfully created because the replication controller and service are different:
# cat nginx.yaml apiVersion: v1 kind: ReplicationController metadata: name: my-nginx spec: replicas: 2 selector: sel : my-selector template: metadata: labels: sel : my-selector spec: containers: - name: my-container image: nginx --- apiVersion: v1 kind: Service metadata: name: my-nginx spec: ports: - protocol: TCP port: 80 nodePort: 30080 type: NodePort selector: sel: my-selector # kubectl create -f nginx.yaml replicationcontroller "my-nginx" created service "my-nginx" created # kubectl get rc CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS AGE my-nginx my-container nginx sel=my-selector 2 8s # kubectl get service NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE kubernetes 192.168.0.1 <none> 443/TCP <none> 6d my-nginx 192.168.38.134 nodes 80/TCP sel=my-selector 14s
Name is just a unique identifier, all naming conventions are good, however it is recommended to look up and identify the container image. For example:
memcached-pod1
haproxy.us-west
my-project1.mysql
On the other hand, the following examples do not work because of Kubernetes restrictions:
Note that Kubernetes supports a label that allows to assign a key=value style identifier. It also allows duplication. Therefore, if you want to assign something like the information below, use a label instead:
In addition, Kubernetes also supports namespaces which have isolated namespaces. This means that you can use the same name in different namespaces (for example: nginx). Therefore, if you want to assign just an application name, use namespaces instead.
This section described how to assign and find the name of objects. This is just a basic methodology, but Kubernetes has more powerful naming tools such as namespace and selectors to manage clusters: