本次我们跑一个 nginx 容器,然后使用 kubectl 操作该容器。

kubectl run

可以方便的创建一个容器(实际上创建的是一个由 deployment 来管理的 Pod)。

$ kubectl run --image=nginx:alpine nginx-app --port=80
deployment "nginx-app" created

kubectl get pods

查询资源列表

用下面的命令查询刚刚创建的pod状态。

$ kubectl get pods
NAME                         READY     STATUS              RESTARTS   AGE
nginx-app-2007220645-6f66z   0/1       ContainerCreating   0          15s

这里一般情况下过一会READY状态会变成1/1,STATUS会变成RUNNING。不过我当时是长时间没变。然后我去干其他事情,回来后状态变成了ImagePullBackOff。

NAME                         READY     STATUS             RESTARTS   AGE
nginx-app-2007220645-6f66z   0/1       ImagePullBackOff   0          33m

kubectl describe

输出指定的一个/多个资源的详细信息。

$ kubectl describe pod
Name:		nginx-app-2007220645-6f66z
Namespace:	default
Node:		127.0.0.1/127.0.0.1
Start Time:	Wed, 08 Apr 2020 00:05:03 +0800
Labels:		pod-template-hash=2007220645
		run=nginx-app
Status:		Pending
IP:		172.17.0.2
Controllers:	ReplicaSet/nginx-app-2007220645
Containers:
  nginx-app:
    Container ID:		
    Image:			nginx:alpine
    Image ID:			
    Port:			80/TCP
    State:			Waiting
      Reason:			ImagePullBackOff
    Ready:			False
    Restart Count:		0
    Volume Mounts:		<none>
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True 
  Ready 	False 
  PodScheduled 	True 
No volumes.
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath	Type		Reason		Message
  ---------	--------	-----	----			-------------	--------	------		-------
  33m		33m		1	{default-scheduler }			Normal		Scheduled	Successfully assigned nginx-app-2007220645-6f66z to 127.0.0.1
  23m		23m		1	{kubelet 127.0.0.1}			Warning		FailedSync	Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.  details: (net/http: request canceled)"

  21m	21m	1	{kubelet 127.0.0.1}		Warning	MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  17m	17m	1	{kubelet 127.0.0.1}		Warning	FailedSync		Error syncing pod, skipping: failed to "StartContainer" for "nginx-app" with ErrImagePull: "Get https://registry-1.docker.io/v2/library/nginx/manifests/sha256:ef2b6cd6fdfc6d0502b77710b27f7928a5e29ab5cfae398824e5dcfbbb7a75e2: net/http: TLS handshake timeout"

  17m	17m	1	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Warning	Failed		Failed to pull image "nginx:alpine": Get https://registry-1.docker.io/v2/library/nginx/manifests/sha256:ef2b6cd6fdfc6d0502b77710b27f7928a5e29ab5cfae398824e5dcfbbb7a75e2: net/http: TLS handshake timeout
  15m	15m	1	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Warning	Failed		Failed to pull image "nginx:alpine": Get https://registry-1.docker.io/v2/library/nginx/manifests/alpine: net/http: TLS handshake timeout
  15m	15m	1	{kubelet 127.0.0.1}					Warning	FailedSync	Error syncing pod, skipping: failed to "StartContainer" for "nginx-app" with ErrImagePull: "Get https://registry-1.docker.io/v2/library/nginx/manifests/alpine: net/http: TLS handshake timeout"

  18m	9m	3	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Warning	Failed		Failed to pull image "nginx:alpine": net/http: request canceled
  18m	9m	3	{kubelet 127.0.0.1}					Warning	FailedSync	Error syncing pod, skipping: failed to "StartContainer" for "nginx-app" with ErrImagePull: "net/http: request canceled"

  6m	6m	1	{kubelet 127.0.0.1}		Warning	FailedSync	Error syncing pod, skipping: failed to "StartContainer" for "nginx-app" with ErrImagePull: "Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout"

  6m	6m	1	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Warning	Failed		Failed to pull image "nginx:alpine": Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout
  18m	1m	45	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Normal	BackOff		Back-off pulling image "nginx:alpine"
  18m	1m	45	{kubelet 127.0.0.1}					Warning	FailedSync	Error syncing pod, skipping: failed to "StartContainer" for "nginx-app" with ImagePullBackOff: "Back-off pulling image \\"nginx:alpine\\""

  21m	53s	7	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Normal	Pulling	pulling image "nginx:alpine"

百度搜索k8s Get https://registry-1.docker.io/v2: net/http: TLS handshake timeout",按上面的方法dig @114.114.114.114 registry-1.docker.io

然后把获取到的ip填入hosts文件。

sudo gedit /etc/hosts

然后实验了一下sudo docker pull nginx:alpine是没问题的。

然后看了一下pod状态,Running了。

$ kubectl get pods
NAME                         READY     STATUS    RESTARTS   AGE
nginx-app-2007220645-6f66z   1/1       Running   0          19h

kubectl exec

执行 容器命令

查看容器内进程

$ kubectl exec nginx-app-2007220645-6f66z ps aux
PID   USER     TIME  COMMAND
    1 root      0:00 nginx: master process nginx -g daemon off;
    7 nginx     0:00 nginx: worker process
    8 nginx     0:00 nginx: worker process
    9 nginx     0:00 nginx: worker process
   10 nginx     0:00 nginx: worker process
   11 root      0:00 ps aux

可以看到nginx已经起来了。

查看容器详细信息。

$ kubectl describe pod nginx-app-2007220645-6f66z
Name:		nginx-app-2007220645-6f66z
Namespace:	default
Node:		127.0.0.1/127.0.0.1
Start Time:	Wed, 08 Apr 2020 00:05:03 +0800
Labels:		pod-template-hash=2007220645
		run=nginx-app
Status:		Running
IP:		172.17.0.2
Controllers:	ReplicaSet/nginx-app-2007220645
Containers:
  nginx-app:
    Container ID:		docker://9b3245d69aeb71052bfd95cbd3fbb62942d9e2dd868c6b7a0dee9b7e84831766
    Image:			nginx:alpine
    Image ID:			docker-pullable://docker.io/nginx@sha256:abe5ce652eb78d9c793df34453fddde12bb4d93d9fbf2c363d0992726e4d2cad
    Port:			80/TCP
    State:			Running
      Started:			Wed, 08 Apr 2020 19:49:33 +0800
    Ready:			True
    Restart Count:		0
    Volume Mounts:		<none>
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True 
  Ready 	True 
  PodScheduled 	True 
No volumes.
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath			Type		Reason			Message
  ---------	--------	-----	----			-------------			--------	------			-------
  19h		14m		8	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Normal		Pulling			pulling image "nginx:alpine"
  19h		13m		2	{kubelet 127.0.0.1}					Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  13m		13m		1	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Normal		Pulled			Successfully pulled image "nginx:alpine"
  13m		13m		1	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Normal		Created			Created container with docker id 9b3245d69aeb; Security:[seccomp=unconfined]
  13m		13m		1	{kubelet 127.0.0.1}	spec.containers{nginx-app}	Normal		Started			Started container with docker id 9b3245d69aeb

可以看到pod的IP为172.17.0.2

使用curl或者浏览器访问

image20200408203304993.png

成功出来nginx欢迎页。

kubectl logs

输出pod中一个容器的日志。

$ kubectl logs nginx-app-2007220645-6f66z
172.17.0.1 - - [08/Apr/2020:12:03:44 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
172.17.0.1 - - [08/Apr/2020:12:03:44 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
2020/04/08 12:03:44 [error] 8#8: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "172.17.0.2"

使用yaml创建pod

先创建一个名为nginx.yaml的文件。

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

然后使用下面的命令创建pod

$ kubectl create -f nginx.yaml
pod "nginx" created
$ kubectl get pods
NAME                         READY     STATUS    RESTARTS   AGE
nginx                        1/1       Running   0          3m
nginx-app-2007220645-6f66z   1/1       Running   0          20h

第二个pod也创建好了。

$ kubectl describe pod nginx
...
IP:		172.17.0.3
...

可以看到,这个也是ok的。

image202004082040410681587202809751.png