Kubernetes部署“容器化应用”

何为容器化应用?

通俗来说,就是你把一个程序放在docker里部署,这个docker应用就是容器化应用;

比如:在docker里面部署一个springboot,这个docker + springboot一起就是一个容器化应用;

在Docker里面部署一个nginx ,这个docker + nginx一起就是一个容器化应用;

在Docker里面部署一个应用怎么部署?

就是 : 镜像 -> 启动镜像得到一个Docker容器;

在K8S里面怎么部署?

SpringBoot程序 --> 打包一个jar包或war包–>通过Dockerfile文件生成docker镜像–>通过K8s部署这个镜像–>部署完毕;

总结一下Kubernetes部署容器化应用的步骤:

1.制作镜像(自己Dockerfile文件制作,或者从仓库pull)

2.通过控制管理器pod(其实就是把镜像启动得到一个容器, 容器在pod里)

3.暴露应用,以便外界可以访问;

在Kubernetes集群中部署一个Nginx

kubectl create deployment nginx --image=nginx#拉取nginx镜像并启动
kubectl expose deployment nginx --port=80 --type=NodePort #暴露端口(使用了这个命令之后查看service才可以查到)
kubectl get pod,svc

4.访问应用

kubectl get nodes
kubectl get service(s)

kubectl get deployment(deploy)
kubectl get pod(s)

页面访问的时候是node节点的阿里云ip:暴露的端口号

[root@master ~]# netstat -nlpt # 查看端口
#如果上面这个命令找不到要先安装一个
#yum install net-tools -y #安装网络工具
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:10248         0.0.0.0:*               LISTEN      32461/kubelet       
tcp        0      0 127.0.0.1:10249         0.0.0.0:*               LISTEN      311/kube-proxy      
tcp        0      0 172.31.197.181:2379     0.0.0.0:*               LISTEN      32114/etcd          
tcp        0      0 127.0.0.1:2379          0.0.0.0:*               LISTEN      32114/etcd          
tcp        0      0 172.31.197.181:2380     0.0.0.0:*               LISTEN      32114/etcd          
tcp        0      0 127.0.0.1:2381          0.0.0.0:*               LISTEN      32114/etcd          
tcp        0      0 0.0.0.0:31054           0.0.0.0:*               LISTEN      311/kube-proxy      
tcp        0      0 127.0.0.1:10257         0.0.0.0:*               LISTEN      32090/kube-controll 
tcp        0      0 127.0.0.1:10259         0.0.0.0:*               LISTEN      32140/kube-schedule 
tcp        0      0 127.0.0.1:41492         0.0.0.0:*               LISTEN      32461/kubelet       
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1045/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1001/master         
tcp6       0      0 :::10250                :::*                    LISTEN      32461/kubelet       
tcp6       0      0 :::6443                 :::*                    LISTEN      32072/kube-apiserve 
tcp6       0      0 :::10256                :::*                    LISTEN      311/kube-proxy      
tcp6       0      0 ::1:25    
[root@master ~]# kubectl get pod(s s可以省略)
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-rp57r   1/1     Running   0          17h
[root@master ~]# kubectl --help  # 查询有哪些命令
[root@master ~]# kubectl get deployment(deploy 可以简写) # 查控制器
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           17h

[root@master ~]# kubectl get service #查看service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        19h
nginx        NodePort    10.96.186.10   <none>        80:31054/TCP   17h
# 80是集群内部访问  31054是外界访问端口
[root@master ~]# kubectl get services
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        19h
nginx        NodePort    10.96.186.10   <none>        80:31054/TCP   17h

[root@master ~]# kubectl get deploy
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           18h
[root@master ~]# kubectl delete deployment nginx  #删除nginx控制器
deployment.apps "nginx" deleted
[root@master ~]# kubectl get pods
No resources found in default namespace.
#kubectl delete pod pod的名字  #删除Pod
# kubectl delete pod nginx-6799fc88d8-rp57r #删除pod
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        19h
nginx        NodePort    10.96.186.10   <none>        80:31054/TCP   18h
[root@master ~]# kubectl delete service nginx #删除service
service "nginx" deleted
[root@master ~]# systemctl restart docker
[root@master ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
[root@master ~]#  kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
[root@master ~]#  kubectl get pod,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-kqfmm   1/1     Running   0          42s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        22h
service/nginx        NodePort    10.98.160.67   <none>        80:32169/TCP   15s


页面访问是使用 (发现master和node节点的公网ip都可以访问) 阿里云公网ip :暴露的端口 注意暴露的端口需要在阿里云的安全组配置

image-20210606195451877

image-20210606194745701

在Kubernetes集群中部署一个Tomcat

拉取下拉的镜像是部署在node节点上(在node上查看docker images 查看是否把镜像拉取下来),不会部署在master节点上,虽然是在master节点上操作的

kubectl create deployment tomcat --image=tomcat
kubectl expose deployment tomcat --port=8080 --type=NodePort
kubectl get pod,svc

[root@master ~]# kubectl create deployment tomcat --image=tomcat
deployment.apps/tomcat created
[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
tomcat-7d987c7694-vnwv9   1/1     Running   0          2m56s
[root@master ~]# kubectl get deploy
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
tomcat   1/1     1            1           4m15s

[root@master ~]# kubectl get deploy -n kube-system #查看系统控制器有哪些
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
coredns   2/2     2            2           19h
[root@master ~]# kubectl get namespace #查看命名空间
NAME              STATUS   AGE
default           Active   20h
kube-node-lease   Active   20h
kube-public       Active   20h
kube-system       Active   20h

[root@master ~]# kubectl describe pods tomcat-7d987c7694-vnwv9 #查看详情的命令
Name:         tomcat-7d987c7694-vnwv9
Namespace:    default
Priority:     0
Node:         node01/172.31.197.180
Start Time:   Sun, 06 Jun 2021 16:22:39 +0800
Labels:       app=tomcat
              pod-template-hash=7d987c7694
Annotations:  <none>
Status:       Running
IP:           10.244.1.3
IPs:
  IP:           10.244.1.3
Controlled By:  ReplicaSet/tomcat-7d987c7694

#删除tomcat
kubectl delete deploy tomcat
[root@master ~]# kubectl delete deploy tomcat
deployment.apps "tomcat" deleted
[root@master ~]# kubectl get pods
No resources found in default namespace.
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          22h
tomcat       NodePort    10.107.183.58   <none>        8080:30495/TCP   154m
[root@master ~]#  kubectl delete service tomcat
service "tomcat" deleted
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   22h

上面的部署 tomcat在浏览器访问相关端口没有访问成功

在Kubernetes集群中部署SpringBoot应用

1.项目打包(jar, war)–>可以采用一些工具git、maven、jenkinx

2.制作Dockerfile文件,生成镜像

3.kubectl create deployment your-springboot --image=你的镜像

4.你的springboot就部署好了,是以docker容器的方式运行在pod里面的;

Master控制Node --> service --> deployment(控制器) --> pod -->docker

部署Kubernetes Dashboard

Kubernetes仪表盘是Kubernetes集群的基于Web的通用UI,它允许用户管理集群中运行的应用程序并对其进行故障排除,已经管理集群本身;

wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml

root@master ~]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml
--2021-06-06 21:18:19--  https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7552 (7.4K) [text/plain]
Saving to: ‘recommended.yaml’

100%[==========================================================================================================================================>] 7,552       9.79KB/s   in 0.8s   

2021-06-06 21:18:21 (9.79 KB/s) - ‘recommended.yaml’ saved [7552/7552]

[root@master ~]# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml
namespace/kubernetes-dashboard unchanged
serviceaccount/kubernetes-dashboard unchanged
service/kubernetes-dashboard unchanged
secret/kubernetes-dashboard-certs unchanged
secret/kubernetes-dashboard-csrf configured
Warning: resource secrets/kubernetes-dashboard-key-holder is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
secret/kubernetes-dashboard-key-holder configured
configmap/kubernetes-dashboard-settings unchanged
role.rbac.authorization.k8s.io/kubernetes-dashboard unchanged
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard unchanged
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard unchanged
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard unchanged
deployment.apps/kubernetes-dashboard configured
service/dashboard-metrics-scraper unchanged
deployment.apps/dashboard-metrics-scraper configured
[root@master ~]# vim recommended.yaml
[root@master ~]# kubectl get pods -n kubernetes-dashboard
NAME                                         READY   STATUS    RESTARTS   AGE
dashboard-metrics-scraper-7b59f7d4df-67tqb   1/1     Running   0          7m52s
kubernetes-dashboard-665f4c5ff-lt78w         1/1     Running   0          7m52s
#查看service,type类型已经编程为NodePort端口为30001
[root@master ~]# kubectl get service -n kubernetes-dashboard | grep dashboard  
dashboard-metrics-scraper   ClusterIP   10.99.41.254   <none>        8000/TCP        95m
kubernetes-dashboard        NodePort    10.103.73.21   <none>        443:30001/TCP   95m

image-20210606221058957

image-20210606213553776

访问页面:

注意要在阿里云安全组里面把对应的端口开启

 kubectl get service -n kubernetes-dashboard | grep dashboard  # 查看对应的端口

image-20210606221711096

image-20210606221228362

image-20210606221607394

获取token的命令(如果下次再获取token就只用执行最后一个命令就行 因为前两个命令再次执行会报错)

1.#kubectl create serviceaccount dashboard-admin -n kube-system

2.#kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin

3.#kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
 

image-20210606222701188

把生成的token粘贴到上面的仪表盘的token中就有了下面这个页面

image-20210606222813074

安装vim的命令

yum install vim -y

改配置文件要重启docker命令

systemctl restart docker

B站学习网址:Kubernetes_k8s视频教程_带你从基础入门到精通k8s集群_哔哩哔哩_bilibili

Logo

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

更多推荐