码迷,mamicode.com
首页 > Web开发 > 详细

006.kubernets之Deployment简单部署

时间:2020-01-14 09:47:38      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:created   dsp   dns   led   yum   back   部署   运维管理   yam   

一 Deployment相对于RC的优势

RS与Deployment主要用于替代RC。RS的全称为Replica Set。相对于RC,RS与Deployment的优势如下:

  • RC只支持基于等式的selector,如env=dev或者environment!=qa。但在RS中,还支持新的基于集合的selector,如version in (v1.0,v2.0)或者env not in (dev,qa)。这给复杂的运维管理带来方便
  • 使用Deployment升级Pod只需要定义Pod的最终状态,k8s会为你执行必要的操作。虽然使用kubectl rolling-update也可以完成滚动升级,但它是在客户端与服务端多次交互控制RC完成的,所以REST API中并没有rolling-update的接口,这为定制自己的管理系统带来了一些麻烦。Deployment拥有更加灵活的升级、回滚功能。

Replica Set目前与RC的区别只是支持的selector不同,后续会加入更多的功能。Deployment使用了Replica Set,是更高一层的概念。除非需要自定义升级功能或者根本不需要升级Pod,否则还是建议使用Deployment而不直接使用Replica Set。

二 Deployment是简单使用

2.1 同时安装三个nginx pod

[root@docker-server1 manifests]# cd /yamls/

[root@docker-server1 yamls]# mkdir deployment

[root@docker-server1 yamls]# cd deployment

[root@docker-server1 deployment]# vim nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
    name: hello-deployment
    namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      name: hello-deployment
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 10%
      maxUnavailable: 0
  template:
    metadata:
      labels:
        name: hello-deployment
    spec:
      containers:
      - name: webserver
        image: nginx:1.4
        ports:
        - containerPort: 80

[root@docker-server1 deployment]# kubectl apply -f nginx-deployment.yaml

2.2 查看

[root@docker-server1 deployment]# kubectl get pods

NAME                               READY   STATUS         RESTARTS   AGE
goproxy                            1/1     Running        0          126m
hello-deployment-c6fb6d858-9g5pq   0/1     ErrImagePull   0          11s
hello-deployment-c6fb6d858-qnnld   0/1     ErrImagePull   0          11s
hello-deployment-c6fb6d858-v549m   0/1     ErrImagePull   0          11s
init-demo                          1/1     Running        0          68m
nginx                              2/2     Running        4          4h13m
nginx-volume                       1/1     Running        0          88m

 [root@docker-server1 deployment]# kubectl get pods -o wide

NAME                               READY   STATUS             RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
goproxy                            1/1     Running            0          126m    10.244.1.8    192.168.132.132   <none>           <none>
hello-deployment-c6fb6d858-9g5pq   0/1     ImagePullBackOff   0          21s     10.244.2.11   192.168.132.133   <none>           <none>
hello-deployment-c6fb6d858-qnnld   0/1     ImagePullBackOff   0          21s     10.244.1.12   192.168.132.132   <none>           <none>
hello-deployment-c6fb6d858-v549m   0/1     ImagePullBackOff   0          21s     10.244.1.13   192.168.132.132   <none>           <none>
init-demo                          1/1     Running            0          68m     10.244.1.10   192.168.132.132   <none>           <none>
nginx                              2/2     Running            4          4h14m   10.244.2.10   192.168.132.133   <none>           <none>
nginx-volume                       1/1     Running            0          89m     10.244.1.9    192.168.132.132   <none>           <none>

[root@docker-server1 deployment]# kubectl describle pods  hello-deployment-c6fb6d858-v549m

Events:
  Type     Reason     Age                    From                      Message
  ----     ------     ----                   ----                      -------
  Normal   Scheduled  <unknown>              default-scheduler         Successfully assigned default/hello-deployment-c6fb6d858-v549m to 192.168.132.132
  Normal   Pulling    4m36s (x4 over 6m20s)  kubelet, 192.168.132.132  Pulling image "nginx:1.4"
  Warning  Failed     4m32s (x4 over 6m12s)  kubelet, 192.168.132.132  Failed to pull image "nginx:1.4": rpc error: code = Unknown desc = Error response from daemon: manifest for nginx:1.4 not found: manifest unknown: manifest unknown
  Warning  Failed     4m32s (x4 over 6m12s)  kubelet, 192.168.132.132  Error: ErrImagePull
  Warning  Failed     4m8s (x7 over 6m11s)   kubelet, 192.168.132.132  Error: ImagePullBackOff
  Normal   BackOff    74s (x19 over 6m11s)   kubelet, 192.168.132.132  Back-off pulling image "nginx:1.4"

是nginx:1.4没有这个镜像

2.3 滚动更新

修改为1.14版本,就会自己更新

滚动更新策略

1 先删后起

2 先起后删

[root@docker-server1 deployment]#  kubectl apply -f nginx-deployment.yaml 

[root@docker-server1 deployment]# kubectl get pods -o wide

hello-deployment-5fdb46d67c-jvvjr   0/1     ContainerCreating   0          4s      <none>        192.168.132.132   <none>           <none>
hello-deployment-c6fb6d858-9g5pq    0/1     ImagePullBackOff    0          8m15s   10.244.2.11   192.168.132.133   <none>           <none>
hello-deployment-c6fb6d858-qnnld    0/1     ImagePullBackOff    0          8m15s   10.244.1.12   192.168.132.132   <none>           <none>
hello-deployment-c6fb6d858-v549m    0/1     ImagePullBackOff    0          8m15s   10.244.1.13   192.168.132.132   <none>           <none>

[root@docker-server1 deployment]# kubectl get pods -o wide

hello-deployment-5fdb46d67c-7ct2z   0/1     ContainerCreating   0          9s      <none>        192.168.132.133   <none>           <none>
hello-deployment-5fdb46d67c-jvvjr   1/1     Running             0          34s     10.244.1.14   192.168.132.132   <none>           <none>
hello-deployment-c6fb6d858-9g5pq    0/1     ImagePullBackOff    0          8m45s   10.244.2.11   192.168.132.133   <none>           <none>
hello-deployment-c6fb6d858-qnnld    0/1     ImagePullBackOff    0          8m45s   10.244.1.12   192.168.132.132   <none>           <none>

已经运行成功更新一个

[root@docker-server1 deployment]# kubectl get pods -o wide

hello-deployment-5fdb46d67c-7ct2z   1/1     Running   0          118s    10.244.2.12   192.168.132.133   <none>           <none>
hello-deployment-5fdb46d67c-jvvjr   1/1     Running   0          2m23s   10.244.1.14   192.168.132.132   <none>           <none>
hello-deployment-5fdb46d67c-qfjht   1/1     Running   0          90s     10.244.1.15   192.168.132.132   <none>           <none

全部更新

二 部署一个word press网站

2.1 部署mysql

[root@docker-server1 deployment]# vim mysql-deployment.yaml

apiVersion:  apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql 
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: RedHat123
            - name: MYSQL_DATABASE
              value: wordpress

[root@docker-server1 deployment]# kubectl apply -f mysql-deployment.yaml 

[root@docker-server1 deployment]# kubectl get pods

mysql-5d4695cd5-kq9cl               0/1     ContainerCreating   0          8s

[root@docker-server1 deployment]# yum -y install mysql

[root@docker-server1 deployment]# kubectl get pods

NAME                                READY   STATUS    RESTARTS   AGE
goproxy                             1/1     Running   0          149m
hello-deployment-5fdb46d67c-7ct2z   1/1     Running   0          15m
hello-deployment-5fdb46d67c-jvvjr   1/1     Running   0          15m
hello-deployment-5fdb46d67c-qfjht   1/1     Running   0          14m
init-demo                           1/1     Running   0          92m
mysql-5d4695cd5-kq9cl               1/1     Running   0          102s
nginx                               2/2     Running   4          4h37m
nginx-volume                        1/1     Running   0          112m

 [root@docker-server1 deployment]# kubectl get pods -o wide

mysql-5d4695cd5-kq9cl               1/1     Running   0          3m22s   10.244.1.16   192.168.132.132

[root@docker-server1 deployment]# mysql -uroot -pRedHat123 -h10.244.1.16

技术图片

2.2 部署wordpress

[root@docker-server1 deployment]# vi wordpress-deployment.yaml 

apiVersion:  apps/v1
kind: Deployment
metadata:
  name: wordpress
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress 
    spec:
      nodeName: 192.168.1.233
      containers:
        - name: wordpress
          image: wordpress:5.3.2-php7.2-apache
          ports:
            - containerPort: 80
          env:
            - name: WORDPRESS_DB_PASSWORD
              value: RedHat123
            - name: WORDPRESS_DB_NAME
              value: wordpress
            - name: WORDPRESS_DB_HOST
              value: 10.244.1.16
            - name: WORDPRESS_DB_USER
              value: root

[root@docker-server1 deployment]# kubectl apply -f wordpress-deployment.yaml 

[root@docker-server1 deployment]# kubectl get pods -o wide

NAME                         READY   STATUS              RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
goproxy                      1/1     Running             0          164m    10.244.1.8    192.168.132.132   <none>           <none>
init-demo                    1/1     Running             0          106m    10.244.1.10   192.168.132.132   <none>           <none>
mysql-5d4695cd5-kq9cl        1/1     Running             0          16m     10.244.1.16   192.168.132.132   <none>           <none>
nginx                        2/2     Running             4          4h51m   10.244.2.10   192.168.132.133   <none>           <none>
nginx-volume                 1/1     Running             0          127m    10.244.1.9    192.168.132.132   <none>           <none>
wordpress-86cbfc4456-zp985   0/1     ContainerCreating   0          2s      <none>        192.168.132.131   <none>           <none>

[root@docker-server1 deployment]# kubectl describe pods wordpress-86cbfc4456-zp985

Events:
  Type    Reason   Age   From                      Message
  ----    ------   ----  ----                      -------
  Normal  Pulling  3m5s  kubelet, 192.168.132.131  Pulling image "wordpress:5.3.2-php7.2-apache"
  Normal  Pulled   97s   kubelet, 192.168.132.131  Successfully pulled image "wordpress:5.3.2-php7.2-apache"
  Normal  Created  94s   kubelet, 192.168.132.131  Created container wordpress
  Normal  Started  94s   kubelet, 192.168.132.131  Started container wordpress

[root@docker-server1 deployment]# kubectl get pods -o wide

NAME                         READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
goproxy                      1/1     Running   0          167m    10.244.1.8    192.168.132.132   <none>           <none>
init-demo                    1/1     Running   0          109m    10.244.1.10   192.168.132.132   <none>           <none>
mysql-5d4695cd5-kq9cl        1/1     Running   0          19m     10.244.1.16   192.168.132.132   <none>           <none>
nginx                        2/2     Running   4          4h55m   10.244.2.10   192.168.132.133   <none>           <none>
nginx-volume                 1/1     Running   0          130m    10.244.1.9    192.168.132.132   <none>           <none>
wordpress-86cbfc4456-zp985   1/1     Running   0          3m9s    10.244.0.4    192.168.132.131   <none>           <none>

2.3 service资源对象创建MySQL的转发

负载均衡器对象,通过创建 service资源对象,会在ku即e- proxy上为这一类资源创建一个負载均衡器,并为其生成一个集群内部可访问的不变的VIP

[root@docker-server1 deployment]# mkdir /yamls/svcs

[root@docker-server1 deployment]# cd /yamls/svcs

[root@docker-server1 svcs]# vi mysql-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
spec:
  selector:        #匹配label
    app: mysql
  type: ClusterIP
  ports:
    - port: 3306    
      targetPort: 3306

[root@docker-server1 svcs]# kubectl apply -f mysql-svc.yaml

[root@docker-server1 svcs]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    10h
mysql-svc    ClusterIP   10.96.240.104   <none>        3306/TCP   18s

[root@docker-server1 svcs]# cat mysql-svc.yam

apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
spec:
  selector:
    app: mysql
  type: ClusterIP
  clusterIP: 10.96.240.104
  ports:
    - port: 3306
      targetPort: 3306

[root@docker-server1 svcs]# kubectl delete -f mysql-svc.yaml

[root@docker-server1 svcs]# kubectl apply -f mysql-svc.yaml 

[root@docker-server1 svcs]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    10h
mysql-svc    ClusterIP   10.96.240.104   <none>        3306/TCP   7s

[root@docker-server1 svcs]# mysql -uroot -pRedHat123 -h10.96.240.104

技术图片

在这里连接很慢

[root@docker-server1 deployment]# kubectl get svc --all-namespaces

NAMESPACE              NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default                kubernetes                  ClusterIP   10.96.0.1       <none>        443/TCP                  11h
default                mysql-svc                   ClusterIP   10.96.240.104   <none>        3306/TCP                 9m37s
kube-system            kube-dns                    ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   11h
kubernetes-dashboard   dashboard-metrics-scraper   ClusterIP   10.96.93.119    <none>        8000/TCP                 10h
kubernetes-dashboard   kubernetes-dashboard        NodePort    10.96.87.98     <none>        443:32443/TCP            10h

2.4 修改wordspress

apiVersion:  apps/v1
kind: Deployment
metadata:
  name: wordpress
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      nodeName: 192.168.132.131
      containers:
        - name: wordpress
          image: wordpress:5.3.2-php7.2-apache
          ports:
            - containerPort: 80
          env:
            - name: WORDPRESS_DB_PASSWORD
              value: RedHat123
            - name: WORDPRESS_DB_NAME
              value: wordpress
            - name: WORDPRESS_DB_HOST
              value: 10.96.240.104    #修改这个IP,为serviceIP
            - name: WORDPRESS_DB_USER
              value: root

2.5 外部访问wordpress

[root@docker-server1 svcs]# vi wordpress-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress
spec:
  selector:
    app: wordpress
  type: NodePort        #为node节点配置端口映射
  ports:
    - port: 80
      targetPort: 80
      nodePort: 32080

[root@docker-server1 svcs]# kubectl apply -f wordpress-svc.yaml

[root@docker-server1 svcs]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        11h
mysql-svc    ClusterIP   10.96.240.104   <none>        3306/TCP       16m
wordpress    NodePort    10.96.72.115    <none>        80:32080/TCP   2s

[root@docker-server1 svcs]# netstat -ntlp|grep 32080

技术图片

2.6 查看详细信息

[root@docker-server1 ~]# kubectl describe  svc mysql-svc

Name:              mysql-svc
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=mysql
Type:              ClusterIP
IP:                10.96.240.104
Port:              <unset>  3306/TCP
TargetPort:        3306/TCP
Endpoints:         10.244.1.22:3306
Session Affinity:  None
Events:            <none>

[root@docker-server1 ~]# kubectl describe  svc wordpress

Name:                     wordpress
Namespace:                default
Labels:                   <none>
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"wordpress","namespace":"default"},"spec":{"ports":[{"nodePort":32...
Selector:                 app=wordpress
Type:                     NodePort
IP:                       10.96.72.115
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  32080/TCP
Endpoints:                10.244.0.6:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

2.7 查看iptables

[root@docker-server1 ~]# iptables -t nat -L|grep 32080

KUBE-MARK-MASQ  tcp  --  anywhere             anywhere             /* default/wordpress: */ tcp dpt:32080
KUBE-SVC-2T26TNVTRAU2MSW5  tcp  --  anywhere             anywhere             /* default/wordpress: */ tcp dpt:32080

[root@docker-server1 ~]# iptables -t nat -L

KUBE-MARK-MASQ  tcp  -- !docker-server1/16    10.96.240.104        /* default/mysql-svc: cluster IP */ tcp dpt:mysql
KUBE-SVC-USVAXDL4J4VMVCQU  tcp  --  anywhere             10.96.240.104        /* default/mysql-svc: cluster IP */ tcp dpt:mysql
KUBE-MARK-MASQ  tcp  -- !docker-server1/16    10.96.72.115         /* default/wordpress: cluster IP */ tcp dpt:http
KUBE-SVC-2T26TNVTRAU2MSW5  tcp  --  anywhere             10.96.72.115         /* default/wordpress: cluster IP */ tcp dpt:http
KUBE-MARK-MASQ  udp  -- !docker-server1/16    10.96.0.10           /* kube-system/kube-dns:dns cluster IP */ udp dpt:domain
KUBE-SVC-TCOU7JCQXEZGVUNU  udp  --  anywhere             10.96.0.10           /* kube-system/kube-dns:dns cluster IP */ udp dpt:domain
KUBE-MARK-MASQ  tcp  -- !docker-server1/16    10.96.0.10           /* kube-system/kube-dns:dns-tcp cluster IP */ tcp dpt:domain
KUBE-SVC-ERIFXISQEP7F7OF4  tcp  --  anywhere             10.96.0.10           /* kube-system/kube-dns:dns-tcp cluster IP */ tcp dpt:domain
KUBE-MARK-MASQ  tcp  -- !docker-server1/16    10.96.0.10           /* kube-system/kube-dns:metrics cluster IP */ tcp dpt:9153
KUBE-SVC-JD5MR3NA4I4DYORP  tcp  --  anywhere             10.96.0.10           /* kube-system/kube-dns:metrics cluster IP */ tcp dpt:9153
KUBE-MARK-MASQ  tcp  -- !docker-server1/16    10.96.93.119         /* kubernetes-dashboard/dashboard-metrics-scraper: cluster IP */ tcp dpt:irdmi
KUBE-SVC-NDSMHFCKXJRPU4FV  tcp  --  anywhere             10.96.93.119         /* kubernetes-dashboard/dashboard-metrics-scraper: cluster IP */ tcp dpt:irdmi
KUBE-MARK-MASQ  tcp  -- !docker-server1/16    10.96.87.98          /* kubernetes-dashboard/kubernetes-dashboard: cluster IP */ tcp dpt:https
KUBE-SVC-4CRUJHTV5RT5YMFY  tcp  --  anywhere             10.96.87.98          /* kubernetes-dashboard/kubernetes-dashboard: cluster IP */ tcp dpt:https
KUBE-MARK-MASQ  tcp  -- !docker-server1/16    10.96.0.1            /* default/kubernetes:https cluster IP */ tcp dpt:https 

2.8 外部配置wordpress

访问:http://192.168.132.131:32080

技术图片

 选择简体中文

技术图片

配置邮件

技术图片

 登录

 技术图片

 输入用户名密码

 技术图片

最终界面

技术图片

配置完成


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

006.kubernets之Deployment简单部署

标签:created   dsp   dns   led   yum   back   部署   运维管理   yam   

原文地址:https://www.cnblogs.com/zyxnhr/p/12183444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!