1、部署一个Tomcat
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8
[root@k8s-node1 k8s]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcat6-5f7ccf4cb9-tft59 1/1 Running 0 77m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 172.96.0.1 <none> 443/TCP 81m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcat6 1/1 1 1 77m
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 1 77m
[root@k8s-node1 k8s]# kubectl get all -o wide #可以获取到tomcat信息
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/tomcat6-5f7ccf4cb9-tft59 1/1 Running 0 79m 172.244.2.2 k8s-node3 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 172.96.0.1 <none> 443/TCP 83m <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/tomcat6 1/1 1 1 79m tomcat tomcat:6.0.53-jre8 app=tomcat6
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 1 79m tomcat tomcat:6.0.53-jre8 app=tomcat6,pod-template-hash=5f7ccf4cb9
[root@k8s-node1 k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat6-5f7ccf4cb9-tft59 1/1 Running 0 83m
[root@k8s-node1 k8s]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default tomcat6-5f7ccf4cb9-tft59 1/1 Running 0 83m
kube-system coredns-7f9c544f75-fkxhb 1/1 Running 0 87m
kube-system coredns-7f9c544f75-j2wfg 1/1 Running 0 87m
kube-system etcd-k8s-node1 1/1 Running 0 87m
kube-system kube-apiserver-k8s-node1 1/1 Running 0 87m
kube-system kube-controller-manager-k8s-node1 1/1 Running 0 87m
kube-system kube-flannel-ds-amd64-89kvs 1/1 Running 0 85m
kube-system kube-flannel-ds-amd64-fh474 1/1 Running 0 86m
kube-system kube-flannel-ds-amd64-rvscb 1/1 Running 0 87m
kube-system kube-proxy-h7wfr 1/1 Running 0 85m
kube-system kube-proxy-mb4l5 1/1 Running 0 86m
kube-system kube-proxy-vsjxb 1/1 Running 0 87m
kube-system kube-scheduler-k8s-node1 1/1 Running 0 87m
tomcat在k8s-node3节点
[root@k8s-node3 k8s]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 6.0.53-jre8 49ab0583115a 3 years ago 290MB
[root@k8s-node3 k8s]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a58c4fc582f6 tomcat "catalina.sh run" About an hour ago Up About an hour k8s_tomcat_tomcat6-5f7ccf4cb9-tft59_default_dd2d9b08-88f6-494c-b922-161a70c9c38b_0
模拟k8s-node3宕机,会在k8s-node2上启动一个tomcat,容灾恢复
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat6-5f7ccf4cb9-8nfq5 1/1 Running 0 23m 172.244.1.4 k8s-node2 <none> <none>
2、暴露nginx访问
kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort
service/tomcat6 exposed
Pod的80映射容器的8080;service会代理Pod的80
[root@k8s-node1 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 172.96.0.1 <none> 443/TCP 137m
tomcat6 NodePort 172.96.156.221 <none> 80:32759/TCP 22s
[root@k8s-node1 ~]# kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 172.96.0.1 <none> 443/TCP 139m <none>
tomcat6 NodePort 172.96.156.221 <none> 80:32759/TCP 98s app=tomcat6
发现80端口对外暴露了32759端口,浏览器访问http://192.168.54.161:32759/ http://192.168.54.162:32759/ http://192.168.54.163:32759/
均可以访问tomcat主页
3、动态扩容测试
kubectl get deployment
应用升级 kubectl set image (--help 查看帮助)
扩容 kubectl scale --replicas=3 deployment tomcat6
扩容了多份,所有无论访问哪个node的指定端口,都可以访问到tomcat6
?
#在k8s-node2 节点有两个 k8s-node3节点有一个
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat6-5f7ccf4cb9-8nfq5 1/1 Running 0 42m 172.244.1.4 k8s-node2 <none> <none>
tomcat6-5f7ccf4cb9-t8sd6 1/1 Running 0 27s 172.244.1.5 k8s-node2 <none> <none>
tomcat6-5f7ccf4cb9-xr56v 1/1 Running 0 27s 172.244.2.5 k8s-node3 <none> <none>
[root@k8s-node1 ~]# kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 172.96.0.1 <none> 443/TCP 148m <none>
tomcat6 NodePort 172.96.156.221 <none> 80:32759/TCP 10m app=tomcat6
#缩容
kubectl scale --replicas=1 deployment tomcat6
#只有k8s-node3节点有一个
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat6-5f7ccf4cb9-xr56v 1/1 Running 0 3m29s 172.244.2.5 k8s-node3 <none> <none>
删除资源
获取所有资源
[root@k8s-node1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcat6-5f7ccf4cb9-xr56v 1/1 Running 0 5m59s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 172.96.0.1 <none> 443/TCP 153m
service/tomcat6 NodePort 172.96.156.221 <none> 80:32759/TCP 15m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcat6 1/1 1 1 149m
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 1 149m
删除部署信息
[root@k8s-node1 ~]# kubectl delete deployment.apps/tomcat6
删除service
[root@k8s-node1 ~]# kubectl delete service/tomcat6
查看命令帮助信息 --help
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --help
输出部署命令的yaml配置
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --dry-run -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: tomcat6
name: tomcat6
spec:
replicas: 1
selector:
matchLabels:
app: tomcat6
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: tomcat6
spec:
containers:
- image: tomcat:6.0.53-jre8
name: tomcat
resources: {}
status: {}
简化部署3分tomcat6,将该配置写在tomcat6.yaml 文件中
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: tomcat6
name: tomcat6
spec:
replicas: 3
selector:
matchLabels:
app: tomcat6
template:
metadata:
labels:
app: tomcat6
spec:
containers:
- image: tomcat:6.0.53-jre8
name: tomcat
执行,
kubectl apply -f tomcat6.yaml
[root@k8s-master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat6-5f7ccf4cb9-m2hbz 1/1 Running 0 17s
tomcat6-5f7ccf4cb9-vv248 1/1 Running 0 17s
tomcat6-5f7ccf4cb9-wbhd7 1/1 Running 0 17s
获取pod的完整信息
kubectl get pod tomcat6-5f7ccf4cb9-m2hbz -o yaml
将一下配置写在 mypod.yaml 文件中
apiVersion: v1
kind: Pod
metadata:
labels:
app: tomcat6-new
name: tomcat6-new
namespace: default
spec:
containers:
- image: tomcat:6.0.53-jre8
imagePullPolicy: IfNotPresent
name: tomcat6-new
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
kubectl apply -f mypod.yaml
获取暴露service yaml配置
kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml
?
apiVersion: v1
kind: Service
metadata:
labels:
app: tomcat6
name: tomcat6
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: tomcat6
type: NodePort
status:
loadBalancer: {}
将部署与暴露yaml合并 --- 分割
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: tomcat6
name: tomcat6
spec:
replicas: 3
selector:
matchLabels:
app: tomcat6
template:
metadata:
labels:
app: tomcat6
spec:
containers:
- image: tomcat:6.0.53-jre8
name: tomcat
---
apiVersion: v1
kind: Service
metadata:
labels:
app: tomcat6
name: tomcat6
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: tomcat6
type: NodePort
status:
loadBalancer: {}
先将以前的部署信息删除
kubectl delete deployment.apps/tomcat6
重新执行部署与暴露
kubectl apply -f tomcat6-deployment.yaml
|