k8s从入门到实战(三):使用kubectl创建第一个pod

451 阅读2分钟

在上一篇里面我们搭建好了k8s集群,在K8s中所有的内容都抽象为资源,我们的pod也是资源的一种,至于什么是pod,你现在可以简单理解成docker的容器。后面会专门细说pod

创建资源有命令行跟YAML两种,YAML我们后面说,既然我们上回装好了kubectl,我们就先试用kubectl来进行一些基本的操作

我们创建一个名字叫mynginx的pod 使用nginx这个镜像

kubectl run mynginx --image=nginx

查看pod状态

kubectl describe pod mynginx

这边可以看到有一个Events,就是整个容器的创建过程

首先被分配到k8s-node1节点,然后拉取镜像,拉取成功后创建容器,启动容器

Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  17m    default-scheduler  Successfully assigned default/mynginx to k8s-node1
  Normal  Pulling    17m    kubelet            Pulling image "nginx:latest"
  Normal  Pulled     8m33s  kubelet            Successfully pulled image "nginx:latest" in 9m9.203965433s
  Normal  Created    8m33s  kubelet            Created container mynginx
  Normal  Started    8m33s  kubelet            Started container mynginx

我们可以使用kubectl get pod -owide能看到更详细的信息 比如分配的节点以及ip

[root@k8s-master opt]# kubectl get pod -owide
NAME                                      READY   STATUS    RESTARTS   AGE    IP             NODE        NOMINATED NODE   READINESS GATES
mynginx                                   1/1     Running   0          13m    10.244.36.82   k8s-node1   <none>           <none>
nfs-client-provisioner-6c8cdddb58-kh555   1/1     Running   0          106m   10.244.36.65   k8s-node1   <none> 

在宿主机上可以去访问这个地址,可以看到可以正常访问,但这个是k8s私有ip,只能在宿主机和pod内访问

[root@k8s-master opt]# curl 10.244.36.82
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

至此,我们的一个pod就创建好了,后面我们再说下怎么把这个pod暴露给外部去访问