创建一个专用的交互式测试客户端:
拉取镜像
kubectl run client-$RANDOM --image=ikubernetes/admin-box:v1.2 --restart=Never -it --rm --command -- /bin/bash
root@client-12383 /#
在默认名称空间下的服务去访问另一个名称空间下的服务
查看另一个名称空间
[root@K8s-master01 ~]#kubectl get svc -n demo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
demoapp NodePort 10.102.165.71 <none> 8080:31248/TCP 178m
进行访问:
root@client-12383 /# curl 10.102.165.71:8080
iKubernetes demoapp v1.0 !! ClientIP: 10.244.3.9, ServerName: demoapp-55c5f88dcb-t7xzh, ServerIP: 10.244.4.6!
root@client-12383 /# curl 10.102.165.71:8080
iKubernetes demoapp v1.0 !! ClientIP: 10.244.3.9, ServerName: demoapp-55c5f88dcb-tqb5j, ServerIP: 10.244.5.6!
不在同一名称空间访问要加上名称空间的名字
root@client-12383 /# curl demoapp.demo:8080 (如果端口是80 后面就不用带端口号)
iKubernetes demoapp v1.0 !! ClientIP: 10.244.3.9, ServerName: demoapp-55c5f88dcb-t7xzh, ServerIP: 10.244.4.6!
持续访问命令
root@client-12383 /# while true; do curl demoapp.demo:8080; .5; done
查看pod的详细信息
[root@K8s-master01 ~]#kubectl get pods -n demo
NAME READY STATUS RESTARTS AGE
demoapp-55c5f88dcb-t7xzh 1/1 Running 0 3h14m
demoapp-55c5f88dcb-tqb5j 1/1 Running 0 3h14m
mysql-787575d954-cdp4x 1/1 Running 0 3h35m
wordpress-6c854887c8-mwp74 1/1 Running 0 172m
[root@K8s-master01 ~]#kubectl get pods demoapp-55c5f88dcb-t7xzh -n demo -o yaml
镜像升级,不影响服务对外使用(滚动更新机制)
[root@K8s-master01 ~]#kubectl set image deployment demoapp demoapp=ikubernetes/demoapp:v1.1 -n demo
deployment.apps/demoapp image updated
持续访问命令
root@client-12383 /# while true; do curl demoapp.demo:8080; .5; done
由1.0逐渐升级为1.1
应用更新历史
[root@K8s-master01 ~]#kubectl rollout history deploy demoapp -n demo
deployment.apps/demoapp
REVISION CHANGE-CAUSE
1 <none>
2 <none>
回滚应用的更新
[root@K8s-master01 ~]#kubectl rollout undo deployment demoapp -n demo
deployment.apps/demoapp rolled back
持续访问命令
root@client-12383 /# while true; do curl demoapp.demo:8080; .5; done
由1.1逐渐升级为1.0
pod手动扩容或缩容
手动扩容或缩容:
kubectl scale <TYPE>/<NAME> --replicas=<NUM> [-n <NAMESPACE>]
只支持这四种类型
<TYPE> Set a new size for a deployment, replica set, replication controller, or stateful set.
如:kubectl scale deployments/demoapp --replicas=6 -n demo (扩容)
kubectl scale deployments/demoapp --replicas=4 -n demo (缩容,不能指定缩哪个)
持续访问命令查看扩容缩容
root@client-12383 /# while true; do curl demoapp.demo:8080; .5; done
使用声明式配置文件-f直接指定目录,会自动把目录下的所有清单文件进行创建
拉取镜像
[root@K8s-master01 tmp]#git clone https://github.com/iKubernetes/learning-k8s.git
[root@K8s-master01 wordpress]#kubectl apply -f mysql-ephemeral/
secret/mysql-user-pass configured
service/mysql unchanged
deployment.apps/mysql configured
没有指定名称空间,就直接配部署在default名称空间下
[root@K8s-master01 wordpress]#kubectl get pods
mysql-787575d954-2l8ns 1/1 Running 0 100s
MySQL的service也被创建出来了
[root@K8s-master01 wordpress]#kubectl get svc
mysql ClusterIP 10.98.211.241 <none> 3306/TCP 120s
[root@K8s-master01 wordpress]#kubectl apply -f wordpress-apache-ephemeral/
service/wordpress created
deployment.apps/wordpress created
[root@K8s-master01 wordpress]#kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d11h
mysql ClusterIP 10.98.211.241 <none> 3306/TCP 2d8h
nginx NodePort 10.99.87.93 <none> 80:31640/TCP 2d10h
wordpress NodePort 10.99.203.255 <none> 80:32671/TCP 4m22s
进行访问
http://10.0.0.100:32671/
卸载wordpress、MySQL(清除该目录下所部署的资源一次性清除)只加载yaml、json结尾的文件,其他不做处理
[root@K8s-master01 wordpress]#kubectl delete -f mysql-ephemeral/
secret "mysql-user-pass" deleted
service "mysql" deleted
deployment.apps "mysql" deleted
[root@K8s-master01 wordpress]#kubectl delete -f wordpress-apache-ephemeral/
service "wordpress" deleted
deployment.apps "wordpress" deleted
查看pods(已经没有了相关pods)
[root@K8s-master01 wordpress]#kubectl get pods
NAME READY STATUS RESTARTS AGE
client-12383 0/1 Error 0 102m
client-5251 1/1 Running 0 103m
nginx-55f494c486-d442z 1/1 Running 0 107m
连续使用-f选项指定多个目录,同时执行部署操作
[root@K8s-master01 wordpress]#kubectl apply -f mysql-ephemeral/ -f wordpress-apache-ephemeral/
secret/mysql-user-pass created
service/mysql created
deployment.apps/mysql created
service/wordpress created
deployment.apps/wordpress created
[root@K8s-master01 wordpress]#kubectl get pods
NAME READY STATUS RESTARTS AGE
client-12383 0/1 Error 0 106m
client-5251 1/1 Running 0 107m
mysql-787575d954-2lv4b 1/1 Running 0 18s
wordpress-6c854887c8-vfp9f 1/1 Running 0 18s
标签:kubectl,回滚,master01,扩缩容,demoapp,wordpress,mysql,pod,root
From: https://blog.51cto.com/lmm01/6901375