先启动k8s
export KUBECONFIG=/etc/kubernetes/admin.conf
或直接导入到.bash_profile
查看
kubectl -n kube-system get pod
运行镜像:nginx,并查看节点信息!
kubectl run nginx --image=nginx:latest
kubectl get pod -o wide ##获取节点和服务版本信息,并查看附加信息
docker load -i myapp.tar
docker tag ikubernetes/myapp:v2 reg.westos.org/library/myapp:v2
docker tag ikubernetes/myapp:v1 reg.westos.org/library/myapp:v1
docker push reg.westos.org/library/myapp:v2
kubectl create deployment webserver --image=myapp:v1
kubectl get pod
kubectl delete pod nginx #删除pod
测试访问pod生成的ip地址,我们可以看到myapp:v1的信息!
[root@server1 ~]# curl 10.244.1.2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@server1 ~]# curl 10.244.1.2/hostname.html
webserver-555478bbb6-bcdg9
kubectl scale deployment webserver --replicas=3 #3个
若是删除pod节点中的任何一个,都会自动生成一个
kubectl delete pod demo-********-*******
并且可以随意添加删除pod
即Pod是可以实现 扩容缩容的
kubectl scale deployment webserver --replicas=6
kubectl get pod -o wide
kubectl scale deployment webserver --replicas=3
测试curl
将指定端口80暴露,使得外部可以访问
kubectl expose deployment webserver --port=80 --target-port=80
查看
kubectl get svc
kubectl describe svc webserver #、后端svc查看svc节点信息.节点Endpoints信息查看
可以看到生成了cluster ip如图的IP
访问这个IP,可以看到已经实现负载均衡
curl 10.105.103.26/hostname.html
刚才使用的镜像是myapp:v1,如果我们需要更新镜像为myapp:v2就可以
kubectl set image deployment webserver myapp=myapp:v2
kubectl get pod
kubectl get all 或
kubectl get rs
更新后以前的rs不会消失,为了回滚更新
我们可以查看版本迭代的历史信息
kubectl rollout history deployment webserver
kubectl rollout undo deployment webserver --to-revision=1
kubectl get pod -o wide
我们也可以将之前的版本删除掉
kubectl get all
kubectl delete rswebserver-779b7fd5d7
kubectl get all
在我们的日常工作中,可能要重复简历或者对pod节点进行操作,如果一直使用命令行来实现的话是不高效的工作方法。
资源清单就好比shell脚本一样,会按照你的清单执行命令,相比较命令行更加方便,也易于修改。
kubectl explain pod
书写时候的查找方法,可以之直接通过explain一步一步查找参数内容用法
编辑清单,文件格式为.yaml
创建名为pod-example的pod,选取镜像为myapp:v1
mkdir pod
cd pod/
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: myapp
image: myapp:v1
此处的清单作用为创建名为pod-example的pod,选取镜像为myapp:v1
执行清单,并查看pod信息!
kubectl apply -f pod.yaml
kubectl get pod
发现之前的webserver 删除
kubectl delete deployments.apps webserver
测试:
kubectl run nginx --image=nginx
kubectl get pod nginx -o yaml
kubectl delete pod nginx #删除
kubectl delete -f pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
kubectl get pod pod-example -o yaml
kubectl delete -f pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
# busybox需要打开输入端口并为其分配伪终端
- name: busybox
image: busyboxplus
tty: true
stdin: true ##交互
kubectl get pod
进入busybox终端,-c指定容器 -i打开输入端口 -t打开伪终端
终端内访问localhost
kubectl attach pod-example -c busybox -i -t
ip addr
curl localhost
退出后可继续进入,pod节点会重启。
ctrl+d退出终端,但是再次查看可以看到还是running状态
默认值,IfNotPresent:本地有则使用本地镜像,不拉取
kubectl delete -f pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent ##如果本地存在镜像,则不拉取;本地不存在镜像,再拉取;默认会一直拉取镜像
- name: busybox
image: busyboxplus
imagePullPolicy: IfNotPresent
tty: true
stdin: true
可以看到详细信息:直接创建,开启容器,没有拉取动作!
kubectl describe pod pod-example
kubectl delete -f pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
hostPort: 80
containerPort: 80
运行清单
可以看到分配到了server3上,访问server3
在server3上查看端口netstat -antlp没有80!
但是在路由策略里面有80
iptables -t nat -nL | grep :80
kubectl delete -f pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
hostPort: 80
containerPort: 80
resources:
limits:
cpu: 0.5
memory: 200Mi
requests:
cpu: 0.2
memory: 100Mi
# 限制内存和cpu使用量,requests为最低限制,limit为最高限制
kubectl describe pod pod-example ##查看限制cpu的信息
kubectl delete -f pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
nodeSelector:
kubernetes.io/hostname: server2
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
hostPort: 80
containerPort: 80
resources:
limits:
cpu: 0.5
memory: 200Mi
requests:
cpu: 0.2
memory: 100Mi
kubectl get pod -o wide
可以看到在节点server2
若在后面server2后面加hostnetwork: true
则 ip变为 虚拟机ip
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
nodeSelector:
kubernetes.io/hostname: server2
hostNetwork: true
nodeSelector 是节点选择约束的最简单推荐形式
kubectl delete -f pod.yaml
vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
nodeSelector:
disktype: ssd
hostNetwork: true
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
hostPort: 80
containerPort: 80
resources:
limits:
cpu: 0.5
memory: 200Mi
requests:
cpu: 0.2
memory: 100Mi
kubectl get pod -o wide #匹配的标签不存在,则容器将不会运行,一直处于Pending 状态。
kubectl label nodes server2 disktype=ssd ##通过label来给节点打上标签
kubectl get pod -o wide #查看这个pod调度在了server2这台服务器
kubectl label nodes server3 disktype=ssd
kubectl get nodes -l disktype
Pod 可以包含多个容器,应用运行在这些容器里面,同时 Pod 也可以有一个或多个先于应用容器启动的 Init 容器。 Init
容器与普通的容器非常像,除了如下两点: 它们总是运行到完成。 Init 容器不支持 Readiness,因为它们必须在 Pod
就绪之前运行完成,每个 Init 容器必须运行成功,下一个才能够运行。 如果 Pod 的 Init 容器失败,Kubernetes
会不断地重启该 Pod,直到 Init 容器成功为止。然而,如果 Pod 对应的 restartPolicy 值为
Never,它不会重新启动。
编写初始化文件
cd pod
vi init.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
initContainers:
- name: init-myservice
image: busyboxplus
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
执行初始化文件
kubectl apply -f init.yaml
初始化未成功
kubectl get svc
再添加服务
vi service.yaml
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
kubectl apply -f service.yaml
kubectl get pod
初始化成功
livenessProbe:存活探针,指示容器是否正在运行。如果为运行会杀掉失败进程,受到重启策略影响重新拉起容器进行检测。默认开启。
readinessProbe:就绪探针,指示容器是否准备好服务请求。
startupProbe: 指示容器中的应用是否已经启动。
kubectl delete pod myapp-pod
先删除刚才的pod
vi liveness.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: nginx
imagePullPolicy: IfNotPresent
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 1
kubectl apply -f liveness.yaml
kubectl delete pod liveness-http
删除刚才做的
vi readiness.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: readiness
name: readiness-http
spec:
containers:
- name: readiness
image: nginx
readinessProbe:
httpGet:
path: /test.html
port: 80
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 1
kubectl apply -f readiness.yaml #运行pod清单
Running但起不来是因为nginx目录里没有test.html
kubectl exec -it readiness-http – bash
root@readiness-http:/# cd /usr/share/nginx/html/
root@readiness-http:/usr/share/nginx/html# ls
50x.html index.html
root@readiness-http:/usr/share/nginx/html# echo 1 > test.html
root@readiness-http:/usr/share/nginx/html# exit
1/1成功
测试: curl 10.244.2.15/test.html
删除test.html后再次回到0/1
kubectl exec -it readiness-http – rm -f /usr/share/nginx/html/test.html
mkdir deployment
cd deployment/
vi replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
kubectl apply -f replicaset.yaml
kubectl get all
修改节点标签,–overwrite:覆盖节点标签
kubectl label pod replicaset-ks8tn app=myapp --overwrite
kubectl get pod --show-labels
deployments控制器:用来创建pod和ReplicaSet,依靠标签显示,相比较rs可更新容器,可扩容,可暂停和恢复
kubectl delete -f replicaset.yaml
cp replicaset.yaml deployment.yaml
vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
kubectl apply -f deployment.yaml
kubectl get all
修改文件deployment.yaml使得版本更新
vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v1
kubectl apply -f deployment.yaml
只需把yaml文件中的myapp 改为nginx 再次启动即可
kubectl delete deployments.apps --all 删除所有清单
vi daemonsets.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
labels:
k8s-app: zabbix-agent
spec:
selector:
matchLabels:
name: zabbix-agent
template:
metadata:
labels:
name: zabbix-agent
spec:
containers:
- name: zabbix-agent
image: nginx
kubectl apply -f daemonsets.yaml
kubectl get pod -o wide
删除节点:
kubectl delete -f daemonset.yaml
vi job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
在server1上拉取镜像perl.tar,并上传到私有仓库中
docker load -i /root/perl.tar
docker tag perl reg.westos.org/library/perl
docker push reg.westos.org/library/perl
执行清单job.yaml
kubectl apply -f job.yaml
kubectl describe pod pi–1-zbd5d 查看进度
我们可以看到pod节点显示完成
我们可以查看日志:
kubectl logs pi-zbd5d
删除掉节点job.yaml
kubectl delete -f job.yaml
vi cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-example
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cronjob
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from k8s cluster
restartPolicy: OnFailure
执行定时清单,并查看节点和日志:
kubectl apply -f cronjob.yaml
kubectl get cronjob
可以看到过60s,就会再次执行,创建一个节点