创建一个nginx-pod
kubectl run 命令用于一些简单、直观和快速的任务,它适用于 ad-hoc(点对点网络模式)的测试或实验。
现在我们了解了 kubectl create 和 kubectl apply 之间的区别,您可能想知道应该使用哪一个?
在 kubectl create 命令中,我们指定了一个特定行为,也就是 create,因此它是一种更具命令式的方法。在 kubectl apply 命令中,我们指定系统的目标状态,而不指定一个特定的行为,因此它是更具声明性的方法。我们让系统决定采取什么行动。如果资源不存在,它将创建它,如果资源存在,则它将配置应用于现有资源。
简单来说,如果对单个文件运行一个操作来创建资源,create 和 apply 基本是相同的。但是,apply 允许您在一个目录中的多个文件上同时创建和修补。
## 可以创建nginx的pod测试
[root@k8s-master ~]# kubectl run nginx-test --image=nginx:1.20
pod/nginx-test created
[root@k8s-master ~]# kubectl create deployment nginx --image=nginx:1.20
deployment.apps/nginx created
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6768c68f7b-rsxb4 1/1 Running 0 36m 10.2.1.8 k8s-node-02 <none> <none>
nginx-test 1/1 Running 0 70s 10.2.1.9 k8s-node-02 <none> <none>
查看创建的nginx-pod运行信息
[root@k8s-master ~]# kubectl get pod nginx-6768c68f7b-rsxb4 -w
NAME READY STATUS RESTARTS AGE
nginx-6768c68f7b-rsxb4 0/1 ContainerCreating 0 20s
[root@k8s-master ~]# kubectl describe pod nginx-6768c68f7b-rsxb4
Name: nginx-6768c68f7b-rsxb4
Namespace: default
Priority: 0
Service Account: default
Node: k8s-node-02/10.10.10.112
Start Time: Thu, 22 Feb 2024 17:30:58 +0800
Labels: app=nginx
pod-template-hash=6768c68f7b
Annotations: <none>
Status: Running
IP: 10.2.1.8
IPs:
IP: 10.2.1.8
Controlled By: ReplicaSet/nginx-6768c68f7b
Containers:
nginx:
Container ID: docker://89c454091455450e2572b7cabb042e1ec77edf166ec33dbd0ba4efb2a2a6c922
Image: nginx:1.20
Image ID: docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 22 Feb 2024 17:31:29 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-2fd5b (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-2fd5b:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 53s default-scheduler Successfully assigned default/nginx-6768c68f7b-rsxb4 to k8s-node-02
Normal Pulling 52s kubelet Pulling image "nginx:1.20"
Normal Pulled 23s kubelet Successfully pulled image "nginx:1.20" in 29.102262051s
Normal Created 22s kubelet Created container nginx
Normal Started 22s kubelet Started container nginx
# 查看pod 信息
[root@k8s-master ~]# kubectl get pod nginx-6768c68f7b-rsxb4 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6768c68f7b-rsxb4 1/1 Running 0 2m33s 10.2.1.8 k8s-node-02 <none> <none>
如何内部访问这个nginx-pod?
pod的ip是k8s集群中的ip,要想访问这个nginx-pod,只能通过k8s的节点来访问,外部暂时无法访问,如果外部访问,需要更多的网络规则。
# 内部访问
[root@k8s-master ~]# curl 10.2.1.8
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
如何外部访问这个nginx-pod?
需要去把这个nginx的端口给暴露出来
- kubectl expose deployment nginx --port 80 --type=NodePort
# 暴露端口
[root@k8s-master ~]# kubectl expose deployment nginx --port 80 --type=NodePort
service/nginx exposed
[root@k8s-master ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-6768c68f7b-rsxb4 1/1 Running 0 5m49s
pod/nginx-test 1/1 Running 0 5m22s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 2d19h
service/nginx NodePort 10.1.141.159 <none> 80:31656/TCP 11s
# 访问master节点
[root@k8s-master ~]# curl 10.10.10.100:31656
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
# 访问node1节点
[root@k8s-master ~]# curl 10.10.10.177:31656
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
# 访问node2节点
[root@k8s-master ~]# curl 10.10.10.112:31656
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
如何修改这个nginx页面呢?
在使用docker的时候,正常一般都是通过 docker exec -it [容器id] bash 进入到容器修改页面,如何使用容器集群。走管理节点去管理pod。再去修改容器信息?
[root@k8s-master ~]# kubectl exec nginx-6768c68f7b-rsxb4 -- sh -c "echo '这是一个测试页面' > /usr/share/nginx/html/index.html"
[root@k8s-master ~]# curl 10.2.1.8
这是一个测试页面