章节练习:
• 命名空间:aliang-cka
• 名称:web
• 镜像:nginx
• 命名空间名称:kube-system
• 标签:k8s-app=kube-dns
解答:
1,
集群搭建链接: https://blog.csdn.net/MrFDd.
用此命令查看集群 READY 1/1 STATUS Runing 就说明集群正常了。
kubectl get pods -n kube-system
kubectl create namespace aliang-cka
kubectl create deployment web --image=nginx -n aliang-cka
kubectl expose deployment web --port=80 --target-port=80 --type=NodePort -n aliang-cka
(service不指定--name=web默认和deployment名字一样)
kubectl expose deployment web2 --port=80 --target-port=80 --type=NodePort --name=web2 -n aliang-cka
kubectl get service
3,
kubectl get pod -n kube-system -l k8s-app=kube-dns
章节练习:
• pod名称:web
• 文件:/opt/web
kubectl get pods -n aliang-cka
kubectl logs web-674477549d-7vrj9 -n aliang-cka |grep error > /opt/error-erb.txt
• 标签:app=web
• 文件:/opt/cpu
kubectl top pods -l app=web --use-protocol-buffers |awk -F ' ' 'NR==2{print $2}' > /opt/cpu
kubectl top pod -n aliang-cka -l app=web --sort-by="cpu"|sed -n "2p" > /opt/cpu
vim pod1
apiVersion: v1
kind: Pod
metadata:
name: pod2
spec:
containers:
- name: container1
image: nginx
- name: container2
image: redis
- name: container3
image: memcached
- name: container4
image: consul
• 节点:k8s-node1
• pod名称:web
• 镜像:nginx
在官网复制一个deployment,修改添加镜像
vim /etc/kubernetes/manifests
apiVersion: v1
kind: Pod
metadata:
labels:
app: nginx
name: web
spec:
containers:
- image: nginx
name: ngin
Kubelet apply -f IVcontainer.yaml
• 文件路径:/tmp/test.sock
apiVersion: v1
kind: Pod
metadata:
name: healthcheck
spec:
containers:
- name: healthcheck
image: nginx
livenessProbe: #存活检查,重启容器
exec:
command:
- ls
- /tmp/test.sock
initialDelaySeconds: 8 #启动容器后多少秒健康检查
periodSeconds: 5 #以后每间隔多少秒检查一次
kubectl apply -f healthcheck.yaml
章节练习:
• 镜像:nginx
• node标签:disk=ssd
1,给节点添加标签并验证。
kubectl label nodes k8s-node1 disk=ssd
kubectl get nodes --show-labels
2,创建一个nodeselector.yaml 文件,并验证。
apiVersion: v1
kind: Pod
metadata:
name: nodeselector
spec:
nodeSelector:
disk: "ssd"
containers:
- name: nginx
image: nginx:1.19
kubectl apply -f nodeselector
kubectl get pods -o wide
• pod名称:nginx
• 镜像:nginx
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx
spec:
selector:
matchLabels:
name: filebeat
template:
metadata:
labels:
name: filebeat
spec:
tolerations:
- effect: NoSchedule
operator: Exists
containers:
- name: log
image: nginx
创建pod分配到每一个节点,并查看节点信息。
kubectl apply -f daemontnginx.yaml
kubectl get pods -o wide
kubectl get nodes |grep "\" |wc -l > /tmp/nodeQuantity.txt
cat /tmp/nodeQuantity.txt
kubectl get nodes
1、给一个pod创建service,并可以通过ClusterIP/NodePort访问
• 名称:web-service
• pod名称:web
• 容器端口:80
创建一个pod。
vim web-service.yaml
piVersion: v1
kind: Service
metadata:
name: web
spec:
type: NodePort # 服务类型
ports:
- name: http
port: 80 # Service端口
protocol: TCP # 协议
targetPort: 80 # 容器端口(应用程序监听端口)
- name: https
port: 443 # Service端口
protocol: TCP # 协议
targetPort: 443 # 容器端口(应用程序监听端口)
nodePort: 30521
selector:
app: web # 指定关联Pod的标签
暴露service。
vim webPod.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
labels:
app: web
spec:
containers:
- image: nginx
name: nginx
2、 任意名称创建deployment和service,使用busybox容器nslookup解析service
3、列出命名空间下某个service关联的所有pod,并将pod名称写到/opt/pod.txt文件中(使用标签筛选)
• 命名空间:default
• service名称:web
创建一个pod并暴露service。
kubectl create deployment web --image=nginx
kubectl expose deployment web --port=80 --target-port=80
写到/opt/pod.txt文件中。
kubectl get pods -l app=web | awk -F ' ' 'NR=2{print $1}' > /opt/pot.txt
4、使用Ingress将美女示例应用暴露到外部访问
• 镜像:lizhenliang/java-demo
创建pod并暴露service
kubectl create deployment beauty --image=lizhenliang/java-demo
kubectl expose deployment beauty --port=80 --target-port=8080
ingress暴露应用。
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-beauty
annotations:
kubernetes.io/ingress.class: "nginx" # 指定ingress控制器
spec:
rules:
- host: "beauty.aliangedu.cn"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: beauty
port:
number: 80
1,创建一个secret,并创建2个pod,pod1挂载该secret,路径为/secret,pod2使用环境变量引用该
secret,该变量的环境变量名为ABC
• secret名称:my-secret
• pod1名称:pod-volume-secret
• pod2名称:pod-env-secret
2、 创建一个pv,再创建一个pod使用该pv
• 容量:5Gi
• 访问模式:ReadWriteOnce
vim pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv-task
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
nfs:
path: /ifs/kubernetes
server: 192.168.30.4
vim web-pvc-task
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-pvc-task
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: wwwroot
mountPath: /usr/share/nginx/html
volumes:
- name: wwwroot
persistentVolumeClaim:
claimName: my-pv
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc-task
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
3、创建一个pod并挂载数据卷,不可以用持久卷
• 卷来源:emptyDir、hostPath任意
• 挂载路径:/data
4、将pv按照名称、容量排序,并保存到/opt/pv文件
持续更新