上一篇博客介绍了如何搭建一套k8s集群
k8s集群搭建部署教程
本篇介绍用k8s部署应用

k8s集群创建pod

master节点创建pod
先创建目录

[root@server1 ~]# mkdir -p k8s_yaml/pod

创建yaml文件

[root@server1 ~]# cd k8s_yaml/pod/
[root@server1 pod]# vi k8s_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: web
spec:
  containers:
    - name: nginx
      image: 192.168.56.132:5000/nginx:1.13
      ports:
        - containerPort: 80

创建nginx pod

[root@server1 pod]# kubectl create -f k8s_pod.yaml
pod/nginx created

查看pod

[root@server1 pod]# kubectl get pod
NAME    READY   STATUS             RESTARTS   AGE
nginx   0/1     ImagePullBackOff   0          3m7s

将master上的nginx镜像打包上传到node1和node2
打包master镜像

[root@server1 pod]# docker save docker.io/pseudecoder/centos-nginx -o /mnt/nginx.tar

上传nginx镜像到node1和node2节点

[root@server1 pod]# scp /mnt/nginx.tar server2:/mnt/
[root@server1 pod]# scp /mnt/nginx.tar server3:/mnt/

node节点加载镜像

[root@server2 ~]# docker load -i /mnt/nginx.tar 
5f70bf18a086: Loading layer [==================================================>] 1.024 kB/1.024 kB
ee1dd2cb6df2: Loading layer [==================================================>] 204.2 MB/204.2 MB
d4f57322e1a5: Loading layer [==================================================>] 3.072 kB/3.072 kB
62c84ccc943b: Loading layer [==================================================>] 74.89 MB/74.89 MB
aafdd9551301: Loading layer [==================================================>] 1.697 MB/1.697 MB
772fd97d8860: Loading layer [==================================================>] 2.048 kB/2.048 kB
385103b6bdaf: Loading layer [==================================================>] 45.57 kB/45.57 kB
Loaded image: docker.io/pseudecoder/centos-nginx:latest
[root@server3 ~]# docker load -i /mnt/nginx.tar 
5f70bf18a086: Loading layer [==================================================>]  1.024kB/1.024kB
ee1dd2cb6df2: Loading layer [==================================================>]  204.2MB/204.2MB
d4f57322e1a5: Loading layer [==================================================>]  3.072kB/3.072kB
62c84ccc943b: Loading layer [==================================================>]  74.89MB/74.89MB
aafdd9551301: Loading layer [==================================================>]  1.697MB/1.697MB
772fd97d8860: Loading layer [==================================================>]  2.048kB/2.048kB
385103b6bdaf: Loading layer [==================================================>]  45.57kB/45.57kB
Loaded image: pseudecoder/centos-nginx:latest

给nginx镜像打标签

[root@server2 ~]# docker tag docker.io/pseudecoder/centos-nginx:latest 192.168.56.132:5000/nginx:1.13
[root@server3 ~]# docker tag docker.io/pseudecoder/centos-nginx:latest 192.168.56.132:5000/nginx:1.13

将node节点上的镜像push到master私有镜像仓库

[root@server2 ~]# docker push 192.168.56.132:5000/nginx:1.13
The push refers to repository [192.168.56.132:5000/nginx]
5f70bf18a086: Pushed 
385103b6bdaf: Pushed 
772fd97d8860: Pushed 
aafdd9551301: Pushed 
62c84ccc943b: Pushed 
d4f57322e1a5: Pushed 
ee1dd2cb6df2: Pushed 
1.13: digest: sha256:3dfe5891f891b3c0af86290e33db9591959acf80cf8067dcfc99e962b2a8d2f6 size: 2604

[root@server3 system]# docker push 192.168.56.132:5000/nginx:1.13
The push refers to repository [192.168.56.132:5000/nginx]
5f70bf18a086: Pushed 
385103b6bdaf: Pushed 
772fd97d8860: Pushed 
aafdd9551301: Pushed 
62c84ccc943b: Pushed 
d4f57322e1a5: Pushed 
ee1dd2cb6df2: Pushed 
1.13: digest: sha256:3dfe5891f891b3c0af86290e33db9591959acf80cf8067dcfc99e962b2a8d2f6 size: 2604

master上创建nginx pod

[root@server1 pod]# kubectl create -f k8s_pod.yaml
pod/nginx created

查看pod

[root@server1 pod]# kubectl get pod
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          118s

查看pod详细信息

[root@server1 pod]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP           NODE                  NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          2m40s   10.244.2.7   server3.example.com   <none>           <none>

查看nginx pod状态及内容
可以看到nginx是正常启动的

[root@server1 pod]# curl -I 10.244.2.7
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 28 Jun 2020 14:50:43 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 26 Jan 2016 15:51:10 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "56a795ee-264"
Accept-Ranges: bytes

在创建一个nginx pod

[root@server1 pod]# cp k8s_pod.yaml k8s_pod2.yaml
[root@server1 pod]# ls
k8s_pod2.yaml  k8s_pod.yaml
[root@server1 pod]# vim k8s_pod2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx2
  labels:
    app: web
spec:
  containers:
    - name: nginx
      image: 192.168.56.132:5000/nginx:1.13
      ports:
        - containerPort: 80
[root@server1 pod]# kubectl create -f k8s_pod2.yaml
pod/nginx2 created

查看nginx2 pod

[root@server1 pod]# kubectl get pod
NAME     READY   STATUS    RESTARTS   AGE
nginx    1/1     Running   0          73s
nginx2   1/1     Running   0          12m

[root@server1 pod]# kubectl get pod -o wide
NAME     READY   STATUS    RESTARTS   AGE    IP           NODE                  NOMINATED NODE   READINESS GATES
nginx    1/1     Running   0          107s   10.244.2.8   server3.example.com   <none>           <none>
nginx2   1/1     Running   0          13m    10.244.1.2   server2.example.com   <none>           <none>

访问nginx2

[root@server1 pod]# curl -I 10.244.1.2
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 29 Jun 2020 14:54:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 26 Jan 2016 15:51:10 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "56a795ee-264"
Accept-Ranges: bytes

创建Replication Controller服务

Replication Controller简称RC,它能够保证Pod持续运行,并且在任何时候都有指定数量的Pod副本,在此基础上提供一些高级特性,比如滚动升级和弹性伸缩

编辑rc配置文件

[root@server1 rc]# vim k8s_rc.yml

apiVersion: v1
kind: ReplicationController
metadata:       #设置rc的元数据
  name: nginx
spec:   #设置rc的具体规格
  replicas: 3   #设置Pod的具体数量
  selector:     #通过selector来匹配相应的Pod的label
    app: myweb
  template:     #设置Pod的模板
    metadata:
      labels:
        app: myweb
    spec:       
      containers:
      - name: myweb
        image: 192.168.56.132:5000/nginx:1.13
        ports:
        - containerPort: 80

创建rc

[root@server1 rc]# kubectl create -f k8s_rc.yml
replicationcontroller/nginx created

查看刚才创建的rc

[root@server1 rc]# kubectl get rc
NAME    DESIRED   CURRENT   READY   AGE
nginx   3         3         3       2m55s

查看pod,以下pod就是刚才创建rc时创建的pod

[root@server1 rc]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
nginx-bg98z   1/1     Running   0          3m52s
nginx-j825s   1/1     Running   0          3m52s
nginx-nzf6v   1/1     Running   0          3m52s

当删除其中一个Pod或者删除全部Pod的时候,RC会自动再次创建Pod,直到满足配置文件中定义的个数

删除一个pod,看是否会自动创建

[root@server1 rc]# kubectl delete pod nginx-bg98z
pod "nginx-bg98z" deleted

查看pod,已经自动创建,数量仍然是3个

[root@server1 rc]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
nginx-9ttf6   1/1     Running   0          98s
nginx-j825s   1/1     Running   0          27m
nginx-nzf6v   1/1     Running   0          27m

弹性伸缩

弹性伸缩就是在现有环境不能满足业务需求的时候,进行的扩容或缩容

1.缩容Pod

[root@server1 rc]# kubectl scale replicationcontroller nginx --replicas=2
replicationcontroller/nginx scaled

查看nginx pod已经缩减至两个

[root@server1 rc]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
nginx-j825s   1/1     Running   0          31m
nginx-nzf6v   1/1     Running   0          31m

2.扩容Pod

[root@server1 rc]# kubectl scale replicationcontroller nginx --replicas=3
replicationcontroller/nginx scaled

查看nginx pod已经扩容至3个了

[root@server1 rc]# kubectl get rc nginx
NAME    DESIRED   CURRENT   READY   AGE
nginx   3         3         3       34m

滚动升级

新建一个rc文件

[root@server1 rc]# cp k8s_rc.yml k8s_rc2.yml 
[root@server1 rc]# vim k8s_rc2.yml
apiVersion: v1
kind: ReplicationController
metadata:       #设置rc的元数据
  name: nginx2
spec:   #设置rc的具体规格
  replicas: 3   #设置Pod的具体数量
  selector:     #通过selector来匹配相应的Pod的label
    app: myweb2
  template:     #设置Pod的模板
    metadata:
      labels:
        app: myweb2
    spec:
      containers:
      - name: myweb
        image: 192.168.56.132:5000/nginx:1.15
        ports:
        - containerPort: 80

本次新建nginx版本为nginx-1.15,在node1和node2上下载nginx-1.15并上传至镜像仓库

[root@server2 ~]# docker load -i /mnt/nginx.tar 
Loaded image: pseudecoder/centos-nginx:latest

[root@server3 ~]# docker load -i /mnt/nginx.tar 
Loaded image: pseudecoder/centos-nginx:latest

将nginx打上1.15的标签

[root@server2 ~]# docker tag pseudecoder/centos-nginx:latest 192.168.56.132:5000/nginx:1.15

[root@server3 ~]# docker tag pseudecoder/centos-nginx:latest 192.168.56.132:5000/nginx:1.15

将nginx镜像推至镜像仓库

[root@server2 ~]# docker push 192.168.56.132:5000/nginx:1.15
The push refers to repository [192.168.56.132:5000/nginx]
1.15: digest: sha256:3dfe5891f891b3c0af86290e33db9591959acf80cf8067dcfc99e962b2a8d2f6 size: 2604

删除原有nginx pod,创建带记录点方式的nginx pod

[root@server1 rc]# kubectl delete ReplicationController nginx
replicationcontroller "nginx" deleted

创建deployment文件

[root@server1 rc]# cp k8s_rc.yml k8s_deploy.yml
[root@server1 rc]# cp k8s_rc2.yml k8s_deploy2.yml
apiVersion: apps/v1
kind: Deployment
metadata:       #设置rc的元数据
  name: nginx-deployment
spec:   #设置rc的具体规格
  replicas: 3   #设置Pod的具体数量
  selector:     #通过selector来匹配相应的Pod的label
   matchLabels:
    app: nginx-deployment
  template:     #设置Pod的模板
    metadata:
      labels:
        app: nginx-deployment
    spec:
      containers:
      - name: nginx-deployment
        image: 192.168.56.132:5000/nginx:1.13
        ports:
        - containerPort: 80

创建带记录点方式的deployment,这条命令将开始记录所有对Depolyment的操作

[root@server1 rc]# kubectl create -f k8s_deploy.yml --record
deployment.apps/nginx-deployment created

查询创建的Depolyment

[root@server1 rc]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           5m38s

查询当前所在的记录点

[root@server1 rc]# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=k8s_deploy.yml --record=true

升级/更新这个Depolyment,这里我们更新images

[root@server1 rc]# vim k8s_deploy.yml 
apiVersion: apps/v1
kind: Deployment
metadata:       #设置rc的元数据
  name: nginx-deployment
spec:   #设置rc的具体规格
  replicas: 3   #设置Pod的具体数量
  selector:     #通过selector来匹配相应的Pod的label
   matchLabels:
    app: nginx-deployment
  template:     #设置Pod的模板
    metadata:
      labels:
        app: nginx-deployment
    spec:
      containers:
      - name: nginx-deployment
        image: docker.io/pseudecoder/centos-nginx       #更换源Images
        ports:
        - containerPort: 80

升级nginx

[root@server1 rc]# kubectl apply -f k8s_deploy.yml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/nginx-deployment configured

看下升级前后pod的变化
升级前:

[root@server1 rc]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5dcf8b988c-2php9   1/1     Running   0          12m
nginx-deployment-5dcf8b988c-8nplm   1/1     Running   0          12m
nginx-deployment-5dcf8b988c-hxb24   1/1     Running   0          12m

升级后:

[root@server1 rc]# kubectl get pod
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-f9548676b-dwwzb   1/1     Running   0          37s
nginx-deployment-f9548676b-wsqx9   1/1     Running   0          87s
nginx-deployment-f9548676b-zgtdp   1/1     Running   0          59s

pod的名字已经变了

如果发现更改有问题,可以进行回滚操作

[root@server1 rc]# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=k8s_deploy.yml --record=true
2         kubectl create --filename=k8s_deploy.yml --record=true

回退到版本1

[root@server1 rc]# kubectl rollout undo deployment nginx-deployment --to-revision=1
deployment.apps/nginx-deployment rolled back

再次查看nginx pod,已经回到升级前

[root@server1 rc]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5dcf8b988c-88w2t   1/1     Running   0          2m48s
nginx-deployment-5dcf8b988c-ctgbk   1/1     Running   0          2m46s
nginx-deployment-5dcf8b988c-vpg89   1/1     Running   0          2m30s

创建service

Kubernetes中一个应用服务会有一个或多个实例(Pod,Pod可以通过rs进行多复本的建立),每个实例(Pod)的IP地址由网络插件动态随机分配(Pod重启后IP地址会改变)。为屏蔽这些后端实例的动态变化和对多实例的负载均衡,引入了Service这个资源对象

创建service目录

[root@server1 k8s_yaml]# mkdir service
[root@server1 k8s_yaml]# cd service
[root@server1 service]# vim k8s_service.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    run: nginx
spec:
  selector:
    app: nginx
  ports:
  - port: 80

假如有3个app: nginx Pod运行在3个不同的Node中,那么此时客户端访问任意一个Node的30001端口都能访问到这个nginx服务。

删掉之前创建的deployment

[root@server1 service]# kubectl delete deployment nginx-deployment
deployment.apps "nginx-deployment" deleted

创建nginx service

[root@server1 service]# kubectl apply -f k8s_service.yml 
service/nginx-service created

查看service信息

[root@server1 service]# kubectl get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP        34d
nginx-service   NodePort    10.103.179.58   <none>        80:30001/TCP   2m16s

创建一个nginx rc服务

[root@server1 rc]# kubectl create -f k8s_rc.yml 
replicationcontroller/nginx created

为nginx创建SVC
(1)使用命令行方式创建

[root@server1 service]# kubectl expose rc nginx --target-port=80 --type=NodePort --port=80
service/nginx exposed
[root@server1 service]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        39d
nginx        NodePort    10.102.191.188   <none>        80:30650/TCP   22s

(2)使用yml文件创建svc

[root@server1 service]# kubectl delete svc nginx
service "nginx" deleted
[root@server1 service]# vim k8s_service.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    run: nginx
spec:
  selector:
    app: nginx  #service通过selector和pod建立关联
  ports:
  - port: 80    #设置Service对外提供服务的端口
    nodePort: 30001     #此宿主机端口可不指定,系统将自动从3万~65535分配
  type: NodePort
[root@server1 service]# kubectl apply -f k8s_service.yml 
service/nginx-service created
[root@server1 service]# kubectl get svc -o wide 
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP        39d   <none>
nginx-service   NodePort    10.105.32.89   <none>        80:30001/TCP   29s   app=nginx

检查svc是否正确的识别到了已存在的pod

[root@server1 service]# kubectl get ep nginx
NAME    ENDPOINTS                                      AGE
nginx   10.244.1.10:80,10.244.1.11:80,10.244.2.22:80   13m

什么是endpoint?

endpoint可以简单理解成路由导向的终点,因为 svc 是将许多个动态的 ip 映射成一个静态的 ip。那么就可以把这些动态的 pod ip 称为 svc 的endpoint。

通过svc访问各nginx pod

[root@server1 service]# kubectl exec nginx-lrpdz -- curl http://10.101.218.89:80
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
100   612  100   612    0     0   7728      0 --:--:-- --:--:-- --:--:--  7746

这里我们采用nodeport的方式创建service可以使容器内的应用暴露出来,外部用户可以直接访问容器内部应用
通过访问宿主机IP+nodeport端口访问容器内部应用
在这里插入图片描述
这样就达到了外部访问容器内部的需求。

Logo

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

更多推荐