Kubernetes练习<2>---Pod管理及资源请单
命令运行容器一、重新开启[wjjk8s@server1 ~]$ kubectl get nodeerror: error loading config file "/etc/kubernetes/admin.conf": open /etc/kubernetes/admin.conf: permission denied[wjjk8s@server1 ~]$ su - root[wjj...
命令运行容器
一、重新开启
[wjjk8s@server1 ~]$ kubectl get node
error: error loading config file "/etc/kubernetes/admin.conf": open /etc/kubernetes/admin.conf: permission denied
[wjjk8s@server1 ~]$ su - root
[wjjk8s@server1 ~]$ su - root
[root@server1 ~]# chmod 666 /etc/kubernetes/admin.conf #权限问题添加即可
[wjjk8s@server1 ~]# kubectl run nginx --image=nginx #命令运行容器
[wjjk8s@server1 ~]# kubectl get pod #创建一个pod
[wjjk8s@server1 ~]# kubectl get pod -n default
[wjjk8s@server1 ~]# kubectl get all -n default
[wjjk8s@server1 ~]$ kubectl get pod -n kube-system #全部running即可
[wjjk8s@server1 ~]$ kubectl describe pod nginx
二、命令运行容器
[wjjk8s@server1 ~]$ kubectl run test --image=busybox
pod/test created
[wjjk8s@server1 ~]$ kubectl delete pod test #删除
集群内部任意节点可以访问pod,但集群外部无法访问。
[wjjk8s@server1 ~]$ kubectl run test -it --image=busyboxplus --restart=Never
curl 10.244.1.4 #集群内部可以访问nginx,
Pod管理
Pod是可以创建和管理Kubernetes计算的最小部署单元,一个Pod代表着2集群运行一个进程,每个Pod都有唯一的ip。
一个Pod类似于一个豌豆荚,包含一个或多个容器(docker),同一个Pod多个容器间共享IPC,Network和UTC namespace。
kubectl命令集官方文档:
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
1、自主式访问:开启两个容器
[wjjk8s@server1 ~]$ kubectl get pod
[wjjk8s@server1 ~]$ kubectl run myapp --image=myapp:v1
[wjjk8s@server1 ~]$ kubectl get pod -o wide #两台node分摊调度
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 0/1 ContainerCreating 0 14s <none> server4 <none> <none>
nginx 1/1 Running 1 5h32m 10.244.1.4 server2 <none> <none>
[wjjk8s@server1 ~]$ kubectl delete pod myapp
pod "myapp" deleted
2、deployment的方式运行
[wjjk8s@server1 ~]$ kubectl create deployment myapp --image=myapp:v1
deployment.apps/myapp created
[wjjk8s@server1 ~]$ kubectl get all
[wjjk8s@server1 ~]$ kubectl delete pod nginx
pod "nginx" deleted
[wjjk8s@server1 ~]$ kubectl delete pod myapp-7449b5b68f-xslkh
pod "myapp-7449b5b68f-xslkh" deleted
[wjjk8s@server1 ~]$ kubectl get pod #删除不干净,会重新自动创建
NAME READY STATUS RESTARTS AGE
myapp-7449b5b68f-d8g9r 0/1 ContainerCreating 0 7s
deployment的方式运行还可以实现拉伸为多个:
[wjjk8s@server1 ~]$ kubectl scale --replicas=2 deployment myapp #创建副本
3、pod间的互相访问:创建service来实现
service是一个抽象概念,定义了一个服务的多个pod逻辑集合和访问pod策略,一般把service称为微服务。
(1)创建service
错误
[wjjk8s@server1 ~]$ kubectl expose deployment nginx --port=80 --target-port=80
Error from server (NotFound): deployments.apps "nginx" not found
#必须找一个已存在的容器
[wjjk8s@server1 ~]$ kubectl expose deployment myapp --port=80 --target-port=80
service/myapp exposed
[wjjk8s@server1 ~]$ kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 0/2 2 0 16m
[wjjk8s@server1 ~]$ kubectl describe svc myapp
[wjjk8s@server1 ~]$ kubectl get svc myapp #集群分给我们myapp的集群内部使用的ip地址
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp ClusterIP 10.102.86.1 <none> 80/TCP 9m56s
此时的pod客户端可以通过service的名称访问后端的两个pod
(2)使用nodeport类型暴露端口,让外部客户端访问pod
[wjjk8s@server1 ~]$ kubectl edit svc myapp #修改
service/myapp edited
kubectl expose deployment myapp --port=80 --target-port=80 --type=NodePort #创建时指定
在ClusterIP基础上为service每台机器绑定一个端口,这样可以通过–type=NodePort访问
4、pod的滚动更新以及回滚
(1)更新pod
[wjjk8s@server1 ~]$ kubectl set image deployments myapp myapp=myapp:v2 --record
deployment.apps/myapp image updated
[wjjk8s@server1 ~]$ kubectl rollout history deployment myapp #查看
(2)回滚
[wjjk8s@server1 ~]$ kubectl rollout undo deployment myapp --to-revision=1
deployment.apps/myapp rolled back
资源请单
[wjjk8s@server1 ~]$ kubectl delete deployments.apps myapp #删除deployments
deployment.apps "myapp" deleted
[wjjk8s@server1 ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
test 1/1 Running 0 169m
(1)创建清单
[wjjk8s@server1 ~]$ vim .vimrc
autocmd filetype yaml setlocal ai ts=2 sw=2 et
[wjjk8s@server1 manifest]$ kubectl api-versions #查看版本
[wjjk8s@server1 ~]$ mkdir manifest
[wjjk8s@server1 manifest]$ kubectl explain pod #编写pod资源设计内容
[wjjk8s@server1 manifest]$kubectl explain pod.spec #期望spec状态
[wjjk8s@server1 manifest]$ kubectl explain pod.spec.containers
[wjjk8s@server1 manifest]$ vim pod.yaml #编写清单
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myapp
image: myapp:v1
imagePullPolicy: IfNotPresent
[wjjk8s@server1 manifest]$ kubectl create -f pod.yaml #创建成功
pod/myapp created
[wjjk8s@server1 manifest]$ kubectl delete -f pod.yaml #删除
pod "myapp" deleted
(2)一个pod创建2个容器
[wjjk8s@server1 manifest]$ vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myapp
image: myapp:v1
imagePullPolicy: IfNotPresent
- name: busybox
image: busyboxplus
imagePullPolicy: IfNotPresent
tty: true
[wjjk8s@server1 manifest]$ kubectl create -f pod.yaml
pod/myapp created
[wjjk8s@server1 manifest]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 2/2 Running 0 7s
test 1/1 Running 0 3h43m
[wjjk8s@server1 ~]$ kubectl exec -it myapp -c busybox -- sh #连接进入容器
Pod管理*
更多推荐
所有评论(0)