学习笔记02-部署一个nginx 的Deployment
k8s 生成Deployment的yaml部署配置文件,并能从集群内访问
前面已经成功安装了一个实验集群环境。接下来我们需要来进行应用部署。
由于在上一篇后我又研究了二进制部署的方式, 所以之后的文章均基于一个二进制的2主2从集群,网络组件一九使用了calico。
root@k8s-master01:~# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
192.168.56.11 Ready,SchedulingDisabled master 44h v1.27.5 192.168.56.11 <none> Ubuntu 22.04.3 LTS 5.15.0-91-generic containerd://1.6.23
192.168.56.12 Ready,SchedulingDisabled master 44h v1.27.5 192.168.56.12 <none> Ubuntu 22.04.3 LTS 5.15.0-91-generic containerd://1.6.23
192.168.56.13 Ready node 44h v1.27.5 192.168.56.13 <none> Ubuntu 22.04.3 LTS 5.15.0-91-generic containerd://1.6.23
192.168.56.14 Ready node 44h v1.27.5 192.168.56.14 <none> Ubuntu 22.04.3 LTS 5.15.0-91-generic containerd://1.6.23
root@k8s-master01:~# calicoctl get node
NAME
k8s-master01
k8s-master02
k8s-worker01
k8s-worker02
本篇的目标:能使用一个yaml方式部署一个nginx,并能直接使用pod的ip进行访问nginx的内容
1、创建一个Deployment的YAML
kubectl create deployment nginx-web --image=registry.cn-beijing.aliyuncs.com/an_docker/nginx:latest --dry-run=client -o yaml > nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx-web
name: nginx-web
spec:
replicas: 1
selector:
matchLabels:
app: nginx-web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx-web
spec:
containers:
- image: registry.cn-beijing.aliyuncs.com/an_docker/nginx:latest
name: nginx
resources: {}
status: {}
以上就是自动生成的nginx-deployment.yaml 文件的内容
其中 registry.cn-beijing.aliyuncs.com/an_docker/nginx:latest 是我拉下来的nginx镜像, 防止国内的同学没法从docker官方下载。如果你有自己的资源完全可以替换成你自己的。
root@k8s-master01:~/demo02# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-web created
root@k8s-master01:~/demo02# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-web-7cfd48856f-rncct 1/1 Running 0 56s 172.20.69.194 192.168.56.14 <none> <none>
可以看到现在的IP是172.20.69.194 这个ip是k8s集群随机分配的一个pod IP地址,我们已经可以正常的访问他了
root@k8s-master01:~/demo02# curl 172.20.69.194
<!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>
之后vim nginx-deployment.yaml 修改spec.replicas 的值为 2
重新应用
root@k8s-master01:~/demo02# vim nginx-deployment.yaml
root@k8s-master01:~/demo02# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-web configured
root@k8s-master01:~/demo02# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-web-7cfd48856f-rncct 1/1 Running 0 24m 172.20.69.194 192.168.56.14 <none> <none>
nginx-web-7cfd48856f-zm4hv 1/1 Running 0 39s 172.20.79.69 192.168.56.13 <none> <none>
应用后发现pod副本就变成了两个此时curl 172.20.69.194 和curl 172.20.79.69 效果是完全一样的
至此我们完成了本篇的任务。
现在我们来卸载本文中的所有资源
kubectl delete -f nginx-deployment.yaml
更多推荐
所有评论(0)