命令部署举例

kubectl create deployment web --image=nginx --replicas=3 -n default

kubectl expose deployment web --port=80 --target-port=80 --type=NodePort -n default

说明:port,target-port 和 nodeport

(1)port是service的port,集群内其它pod访问nginx的pod先访问其service,就是到这个端口
(2)targetport是pod暴露出来的端口,当请求到达service的80端口(port)时,service就会根据selector中的标签,把该请求转发到nginx这个pod的80端口(targetport)上
(3)nodeport是集群外客户端访问集群内服务时访问的端口,比如访问集群中的nginx,网页中URL的端口就是分配或者指定的nodeport

使用yaml部署

@如何写yaml?

1.使用命令导出yaml,命令最后加上 --dry-run=client -o yaml > somefilename.yaml

说明:--dry-run 不会实际运行命令,可以用来检查语法

--dry-run='none': Must be "none", "server", or "client". If client strategy, only print the
object that would be sent, without sending it. If server strategy, submit server-side request
without persisting the resource.

如果有个别字段不记得,可以使用 kubectl explain命令查看,例如

kubectl explain deployment
kubectl explain deployment.spec
kubectl explain deployment.spec.selector
kubectl explain deployment.spec.selector.matchLabels

2.查看官方文档

https://kubernetes.io/zh/docs/home/   -> 文档 的搜索框中 输入deployment
https://kubernetes.io/zh/docs/concepts/workloads/controllers/deployment/

 @使用命令导出yaml创建deployment

1.创建deployment

执行命令 kubectl create deployment web --image=nginx --replicas=3 -n default --dry-run=client -o yaml > test-deploy.yaml

编辑生成的yaml文件,去掉creationTimestamp和status

[root@k8s-master ~]# kubectl create deployment web --image=nginx --replicas=3 -n default --dry-run=client -o yaml > test-deploy.yaml
[root@k8s-master ~]# cat test-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
[root@k8s-master ~]# vim test-deploy.yaml

编辑后的内容如下,可以根据需要修改其它字段

[root@k8s-master ~]# cat test-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}

执行kubectl apply -f test-deploy.yaml,部署deployment

[root@k8s-master ~]# kubectl apply -f test-deploy.yaml
deployment.apps/web created
[root@k8s-master ~]# kubectl get deployment -o wide
NAME   READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES   SELECTOR
web    3/3     3            3           5m45s   nginx        nginx    app=web
[root@k8s-master ~]#

2.创建对应的service

执行命令 kubectl expose deployment web --port=80 --target-port=80 --type=NodePort -n default --dry-run=client -o yaml > test-svc.yaml

[root@k8s-master ~]# kubectl expose deployment web --port=80 --target-port=8080 --type=NodePort -n default --dry-run=client -o yaml > test-svc.yaml
[root@k8s-master ~]# cat test-svc.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: NodePort
status:
  loadBalancer: {}
[root@k8s-master ~]# vim test-svc.yaml

编辑生成的yaml文件,去掉status,可以根据需要修改其它字段。编辑后的文件如下

[root@k8s-master ~]# cat test-svc.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: web
  name: web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: NodePort

执行kubectl apply -f test-svc.yaml,暴露端口

[root@k8s-master ~]# kubectl apply -f test-svc.yaml
service/web created
[root@k8s-master ~]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        19d   <none>
web          NodePort    10.99.129.160   <none>        80:32009/TCP   4s    app=web
[root@k8s-master ~]#

3.deployment和service也可以放在一个yaml文件中,使用“---”分隔资源

首先删除刚才创建的资源

[root@k8s-master ~]# kubectl delete -f test-svc.yaml
service "web" deleted
[root@k8s-master ~]# kubectl delete -f test-deploy.yaml
deployment.apps "web" deleted
[root@k8s-master ~]#

创建一个新的yaml文件,包含deployment和service

[root@k8s-master ~]# cat test-deploy-svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: web
  name: web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: NodePort
[root@k8s-master ~]#

执行kubectl apply命令创建资源

[root@k8s-master ~]# kubectl get deploy -o wide
NAME   READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES   SELECTOR
web    3/3     3            3           44m   nginx        nginx    app=web
[root@k8s-master ~]#
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        19d   <none>
web          NodePort    10.98.67.244   <none>        80:31133/TCP   44m   app=web
[root@k8s-master ~]#
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -o wide
NAME                  READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
web-96d5df5c8-2x868   1/1     Running   0          44m   10.244.36.78     k8s-node1   <none>           <none>
web-96d5df5c8-5gvzp   1/1     Running   0          44m   10.244.36.77     k8s-node1   <none>           <none>
web-96d5df5c8-gfvvb   1/1     Running   0          44m   10.244.169.152   k8s-node2   <none>           <none>
[root@k8s-master ~]#

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐