kubernets-1.14.3 demo 实验
创建运行一个demo
- Examples
Examples:
# Start a single instance of nginx.
kubectl run nginx --image=nginx
#nginx 表示控制器的名称
#--images=nginx 创建运行一个nginx镜像容器
# Start a single instance of hazelcast and let the container expose port 5701 .
kubectl run hazelcast --image=hazelcast --port=5701
# Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container.
kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
# Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the container.
kubectl run hazelcast --image=hazelcast --labels="app=hazelcast,env=prod"
# Start a replicated instance of nginx.
kubectl run nginx --image=nginx --replicas=5
# Dry run. Print the corresponding API objects without creating them.
kubectl run nginx --image=nginx --dry-run
# Start a single instance of nginx, but overload the spec of the deployment with a partial set of values parsed from JSON.
kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
# Start a pod of busybox and keep it in the foreground, don't restart it if it exits.
kubectl run -i -t busybox --image=busybox --restart=Never
# Start the nginx container using the default command, but use custom arguments (arg1 .. argN) for that command.
kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>
# Start the nginx container using a different command and custom arguments.
kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>
# Start the perl container to compute π to 2000 places and print it out.
kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
# Start the cron job to compute π to 2000 places and print it out every 5 minutes.
kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
- demo-1 nginx
kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true
nginx-deploy 是我们创建 deployment 的名字
–image : 指定使用的镜像
–port=80 :容器暴露的端口,该端口只能在 K8S 集群中访问
-dry-run=true : 检测语法是否错误,不会正在创建
检查deployment状态
# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 1/1 1 1 17s
检查pods状态
# kubectl get ns
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deploy-84cbfc56b6-zwzxr 1/1 Running 0 4m26s 10.244.1.2 master01.example.com <none> <none>
docker 服务暴露固定端口
kubectl expose
端口固定暴露给集群,但是kubelet集群外部无法访问
#kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
这一步说是将服务暴露出去,实际上是在服务前面加一个负载均衡,因为pod可能分布在不同的结点上。
–port:暴露出去的端口
–type=NodePort:使用结点+端口方式访问服务
–target-port:容器的端口
–name:创建service指定的名称
查看服务状态
kubectl get service
端口暴露给非kube集群
端口ClusterIP改为NodePort
#kubectl edit svc nginx
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21h
nginx NodePort 10.104.82.167 <none> 80:32356/TCP 3h9m
验证发现访问任意node所在ip的32356端口,都可以访问到nginx服务
集群服务是用bube-dns解析
# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 18h
验证dns服务器解析
dig -t A nginx.default.svc.cluster.local @10.96.0.10
kubectl run clent --image=busybox --replicas=1 --restart=Never -it
kubectl edit
kubectl edit svc nginx
或者
kubectl delete svc nginx
kubelet 副本数
生成myapp pods
kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2
kubectl get deployment -w
kubectl delete deployment myapp
扩展myapp 副本数
kubectl scale --replicas=5 deployment myapp
kubectl get pods -o wide
滚动更新
滚动更新image版本
kubectl describe pods
kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
查看滚动更新状态
kubectl rollout status deployment myapp
回滚
kubectl rollout undo deployment myapp