k8s应用部署

关系图

图片[1]-k8s应用部署-秋风落叶

YAML各个参数解释说明

Deployment参数说明

apiVersion: apps/v1    # API版本
kind: Deployment       # 资源类型
metadata:              # 资源元数据
  name: nginx-3        # 资源名称
spec:                  # 资源规格
  replicas: 3          # 创建Pod的副本数量
  selector:            # 标签选择器,用于选择Pod
    matchLabels:       # 选择标签,选择标签为“app=web3”的Pod
      app: nginx-3
  template:            # 定义Pod模板
    metadata:          # Pod元数据
      labels:          # Pod标签,与上面选择标签定义的要保持一致
        app: nginx-3
    spec:              # Pod规格
      containers:      # Pod容器配置
      - image: nginx   # 容器镜像地址,根据实际情况进行修改
        name: web      # 容器名称

service参数说明

apiVersion: v1        # API版本
kind: Service         # 资源类型
metadata:             # 资源元数据
  name: nginx-3       # 资源名称
spec:                 # 资源规格
  ports:              # 公开的端口列表
  - name: http        # 端口名称,可任意字符串
    port: 80          # 公开的端口
    protocol: TCP     # 端口协议,支持TCP、UDP和SCTP,默认TCP
    targetPort: 80    # 容器中服务运行端口
  selector:           # 标签选择器,用于匹配Pod标签,与上面Pod标签保持一致
    app: nginx-3 
  type: NodePort      # Service类型,表示如何公开,默认是ClusterIP

通过 kubectl 命令行部署应用程序

部署deployment

创建

[root@k8s-master ~]# kubectl create deployment nginx --image=wj-nginx:v1.26.2 --replicas=2
deployment.apps/nginx created

查看

[root@k8s-master ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   2/2     2            2           20s

删除

[root@k8s-master ~]# kubectl delete deployment nginx
deployment.apps "nginx" deleted

部署service服务

创建

[root@k8s-master ~]# kubectl create service nodeport nginx --tcp=80:80
service/nginx created


查看

[root@k8s-master ~]# kubectl get deployment,pod
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   2/2     2            2           2m47s

NAME                         READY   STATUS        RESTARTS   AGE
pod/nginx-5c58b8bc4d-8lxcr   1/1     Running       0          2m47s
pod/nginx-5c58b8bc4d-k47dv   1/1     Running       0          2m47s
pod/nginx-7b7649c8fb-xgl6f   1/1     Terminating   0          6h48m

[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        27h
nginx        NodePort    10.111.211.81   <none>        80:30715/TCP   71
图片[2]-k8s应用部署-秋风落叶

删除

[root@k8s-master ~]# kubectl delete service nginx
service "nginx" deleted

通过定义资源文件部署应用程序

部署deployment

vim deployment-nginx.yaml

apiVersion: apps/v1    
kind: Deployment      
metadata:             
  name: nginx       
spec:                 
  replicas: 2         
  selector:           
    matchLabels:      
      app: nginx
  template:           
    metadata:         
      labels:         
        app: nginx
    spec:             
      containers:     
      - image: wj-nginx:v1.26.2  
        name: web

创建

[root@k8s-master ~]# kubectl create -f deployment-nginx.yaml
deployment.apps/nginx created

查看

[root@k8s-master ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   0/2     2            0           24s

删除

[root@k8s-master ~]# kubectl delete -f deployment-nginx.yaml
deployment.apps "nginx" deleted

部署service服务

[root@k8s-master ~]# vim service-nginx.yaml
apiVersion: v1        # API版本
kind: Service         # 资源类型
metadata:             # 资源元数据
  name: nginx       # 资源名称
spec:                 # 资源规格
  ports:              # 公开的端口列表
  - name: http        # 端口名称,可任意字符串
    port: 80          # 公开的端口
    protocol: TCP     # 端口协议,支持TCP、UDP和SCTP,默认TCP
    targetPort: 80    # 容器中服务运行端口
  selector:           # 标签选择器,用于匹配Pod标签,与上面Pod标签保持一致
    app: nginx
  type: NodePort      # Service类型,表示如何公开,默认是ClusterIP

创建

[root@k8s-master ~]#  kubectl create -f service-nginx.yaml 
service/nginx created

查看

[root@k8s-master ~]# kubectl get deployment,pod
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   2/2     2            2           3m25s

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7b7649c8fb-5dmf4   1/1     Running   0          3m25s
pod/nginx-7b7649c8fb-xgl6f   1/1     Running   0          3m25s


[root@k8s-master ~]#  kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        20h
nginx        NodePort    10.104.168.145   <none>        80:31372/TCP   118s

访问服务地址主要是看service的端口信息

图片[3]-k8s应用部署-秋风落叶

这种方式创建端口是在3000-32767之间变化的,如果想要固定端口需要这样写。

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30002
  selector:
    app: nginx
图片[4]-k8s应用部署-秋风落叶

删除

[root@k8s-master ~]# kubectl delete -f service-nginx.yaml
service "nginx" deleted

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容