k8s部署nginx示例
k8s-api参考文档,不同版本调整链接后的版本号即可
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23
- 提前安装nfs(处于安全性考虑,不使用hostPath)
- kuboard中,推荐将同一个app中的对象name设置为同名
创建命名空间
kubectl create namespace test
构建对象yaml文件(nfs挂载方式)
可以使用---
创建多个对象,也可以拆成多个文件来创建
apiVersion: v1 # 创建Service使用的api版本
kind: Service
metadata:
namespace: test
name: web-nginx
labels:
app: nginx-service
spec:
selector:
app: nginx-pod
type: NodePort
ports:
- nodePort: 30013 # 节点上的端口,接收集群外部的请求
port: 80 # service端口
protocol: TCP
targetPort: 80 # pod端口,service端口上接收到的网络请求转发到这里,与containerPort对应
---
apiVersion: apps/v1 # 创建Deployment使用的api版本
kind: Deployment
metadata: # 定义Deployment的元数据
namespace: test
name: web-nginx
labels:
app: nginx-deploy
spec: # Deployment的期望状态
selector:
matchLabels: # 选择包含标签app: web-nginx的Pod
app: nginx-pod
replicas: 1
template: # 定义Pod模板
metadata:
labels:
app: nginx-pod
spec: # Pod的期望状态
containers: # 定义Pod中的容器
- name: nginx
image: nginx:1.23.3
ports:
- containerPort: 80 # pod内部容器的80端口
volumeMounts: # 描述容器的挂载情况
- name: nginx-dist # 数据卷名字,与volumes[*].name对应
mountPath: /usr/share/nginx/html # 挂载路径
- name: nginx-config # 数据卷名字,与volumes[*].name对应
mountPath: /etc/nginx # 挂载路径
volumes: # 定义Pod中的数据卷
# - name: nginx-dist
# hostPath:
# path: /home/nfs/nginx/dist
# - name: nginx-config
# hostPath:
# path: /home/nfs/nginx/config
- name: nginx-dist
nfs:
server: 172.16.13.111
path: /home/nfs/nginx/dist
- name: nginx-config
nfs:
server: 172.16.13.111
path: /home/nfs/nginx/config
不挂载的情况,文件目录情况,这个根据镜像决定
配置文件:
/etc/nginx/conf.d/default.conf
静态文件:
/usr/share/nginx/html
构建对象yaml文件(configmap挂载方式)
- configmap挂载的方式,key值为文件名
- configmap非挂载的方式,其中所有的键值对作为容器组的环境变量
apiVersion: v1 # 创建Service使用的api版本
kind: Service
metadata:
namespace: test
name: web-nginx
labels:
app: nginx-service
spec:
selector:
app: nginx-pod
type: NodePort
ports:
- nodePort: 30013 # 节点上的端口,接收集群外部的请求
port: 80 # service端口
protocol: TCP
targetPort: 80 # pod端口,service端口上接收到的网络请求转发到这里,与containerPort对应
---
apiVersion: apps/v1 # 创建Deployment使用的api版本
kind: Deployment
metadata: # 定义Deployment的元数据
namespace: test
name: web-nginx
labels:
app: nginx-deploy
spec: # Deployment的期望状态
selector:
matchLabels: # 选择包含标签app: web-nginx的Pod
app: nginx-pod
replicas: 1
template: # 定义Pod模板
metadata:
labels:
app: nginx-pod
spec: # Pod的期望状态
containers: # 定义Pod中的容器
- name: nginx
image: nginx:1.23.3
ports:
- containerPort: 80 # pod内部容器的80端口
volumeMounts: # 描述容器的挂载情况
- name: nginx-dist # 数据卷名字,与volumes[*].name对应
mountPath: /usr/share/nginx/html # 挂载路径
- name: nginx-config # 数据卷名字,与volumes[*].name对应
mountPath: /etc/nginx # 挂载路径
volumes: # 定义Pod中的数据卷
- name: nginx-dist
nfs:
server: 172.16.13.111
path: /home/nfs/nginx/dist
- name: nginx-config
configMap:
name: web-nginx
---
apiVersion: v1
kind: ConfigMap
metadata:
namespace: test
name: web-nginx
data:
nginx.conf: |
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
应用资源
kubectl apply -f nginx-service-deployment.yaml
删除资源
kubectl delete deploy -n test nginx-deploy
kubectl delete svc -n test nginx-service
常用命令
命令 | 描述 |
---|---|
kubectl apply -f res.yaml kubectl apply -f | 应用资源 |
kubectl delete -f res.yaml kubectl delete -f | 删除资源 |
kubectl delete ns nsName | 删除命名空间 |
kubectl delete deploy/pod/svc -n nsName deployName/podName/svcName | 删除指定名称资源 |
kubectl delete pod -n nsName podName --now | 强制删除 |
kubectl describe deploy/pod/svc -n nsName deployName/podName/svcName | 查看指定名称资源描述信息 |