赞
踩
往期回顾:
云端技术驾驭DAY01——云计算底层技术奥秘、云服务器磁盘技术、虚拟化管理、公有云概述
云端技术驾驭DAY02——华为云管理、云主机管理、跳板机配置、制作私有镜像模板
云端技术驾驭DAY03——云主机网站部署、web集群部署、Elasticsearch安装
云端技术驾驭DAY04——Logstash安装部署及插件模块
云端技术驾驭DAY06——容器技术概述、镜像与容器管理、定制简单镜像、容器内安装部署服务
云端技术驾驭DAY07——Dockerfile详解、容器镜像制作、私有仓库
云端技术驾驭DAY08——部署容器服务、Compose微服务管理、harbor仓库部署及管理
云端技术驾驭DAY09——k8s集群安装部署、calico插件部署、计算节点配置管理
云端技术驾驭DAY10——kubectl命令详解、Pod创建过程、Pod的生命周期、定制Pod、资源对象文件
云端技术驾驭DAY11——资源对象文件、Pod自定义命令、多容器Pod、资源监控工具
云端技术驾驭DAY12——Pod调度策略、Pod标签管理、Pod资源配额与限额、全局资源配额与限额策略
云端技术驾驭DAY13——Pod污点、容忍策略、Pod优先级与抢占、容器安全
Pod.spec
下添加volumes
字段,配置外部存储为卷Pod.spec.containers[*]
中添加volumeMounts
字段,声明卷在容器中的挂载位置[root@master ~]# vim web1.yaml --- kind: Pod apiVersion: v1 metadata: name: web1 spec: volumes: # 卷定义 - name: logdata # 卷名称 hostPath: # 资源类型 path: /var/weblog # 宿主机路径 type: DirectoryOrCreate # 目录不存在就创建 containers: - name: nginx image: myos:nginx volumeMounts: # mount 卷 - name: logdata # 卷名称 mountPath: /usr/local/nginx/logs # 容器内路径
[root@master ~]# kubectl apply -f web1.yaml pod/web1 created [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE web1 1/1 Running 0 45m 10.244.2.16 node-0002 [root@master ~]# curl http://10.244.2.16/ Nginx is running ! # 删除Pod ,日志数据也不会丢失 [root@master ~]# kubectl delete pod web1 pod "web1" deleted # 来到 node 上查看日志 [root@node-0002 ~]# cat /var/weblog/access.log 10.244.0.0 - - [27/Jun/2022:02:00:12 +0000] "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0"
type类型 | 解释 |
---|---|
DirectoryOrCreate | 卷映射对象是一个目录,不存在就创建它 |
Directory | 卷映射对象是一个目录,且必须存在 |
FileOrCreate | 卷映射对象是一个文件,不存在就创建它 |
File | 卷映射对象是一个文件,且必须存在 |
Socket | 卷映射对象是一个Socket套接字,且必须存在 |
CharDevice | 卷映射对象是一个字符设备,且必须存在 |
BlockDevice | 卷映射对象是一个块设备,且必须存在 |
主机名 | P地址 | 配置 |
---|---|---|
nfs | 192.168.1.10 | 1CPU,1G内存 |
[root@nfs ~]# mkdir /var/webroot
[root@nfs ~]# echo "nfs server" > /var/webroot/index.html
[root@nfs ~]# dnf -y install nfs-utils
[root@nfs ~]# vim /etc/exports
/var/webroot 192.168.1.0/24(rw,no_root_squash)
[root@nfs ~]# systemctl enable nfs-server.service --now
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
[root@ecs-proxy ~]# mkdir nfs [root@ecs-proxy ~]# cd nfs [root@ecs-proxy nfs]# vim ansible.cfg [defaults] inventory = inventory host_key_checking = False [root@ecs-proxy nfs]# vim inventory [nfs] 192.168.1.[51:55] [root@ecs-proxy nfs]# vim install_nfs.yml --- - hosts: all tasks: - yum: name: nfs-utils - service: name: nfs-server state: started enabled: yes [root@ecs-proxy nfs]# ansible-playbook install_nfs.yml
[root@master ~]# vim web1.yaml --- kind: Pod apiVersion: v1 metadata: name: web1 spec: volumes: - name: logdata hostPath: path: /var/weblog type: DirectoryOrCreate - name: website # 卷名称 nfs: # NFS 资源类型 server: 192.168.1.10 # NFS 服务器地址 path: /var/webroot # NFS 共享目录 containers: - name: nginx image: myos:nginx volumeMounts: - name: logdata mountPath: /usr/local/nginx/logs - name: website # 卷名称 mountPath: /usr/local/nginx/html # 路径 [root@master ~]# kubectl apply -f web1.yaml pod/web1 created [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web1 1/1 Running 0 8s 10.244.147.11 node-0002 <none> <none> [root@master ~]# curl 10.244.147.11 nfs server
cephfs
csi
fc
hostPath
iscsi
nfs
rbd
--- kind: PersistentVolume apiVersion: v1 metadata: name: pv-local spec: volumeMode: Filesystem # 提供资源的类型[Filesystem,Block] accessModes: # 存储卷能提供的访问模式 - ReadWriteOnce # 卷支持的模式[RWO ROX RWX RWOP] capacity: # 存储卷能提供的存储空间 storage: 30Gi # 空间大小 persistentVolumeReclaimPolicy: Retain # 数据回收方式 hostPath: # hostPath配置 path: /var/weblog type: DirectoryOrCreate --- kind: PersistentVolume apiVersion: v1 metadata: name: pv-nfs spec: volumeMode: Filesystem accessModes: - ReadWriteOnce - ReadOnlyMany - ReadWriteMany capacity: storage: 20Gi persistentVolumeReclaimPolicy: Retain mountOptions: # mount的参数 - nolock nfs: # NFS配置 server: 192.168.1.10 # NFS服务器IP地址 path: /var/webroot # 共享目录路径 [root@master ~]# kubectl apply -f pv.yaml persistentvolume/pv-local created persistentvolume/pv-nfs created [root@master ~]# kubectl get persistentvolume NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv-local 30Gi RWO Retain Available 9s pv-nfs 20Gi RWO,ROX,RWX Retain Available 9s
[root@master ~]# vim pvc.yaml --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: pvc1 spec: # 定义需求 volumeMode: Filesystem # 需要使用Filesystem的存储卷 accessModes: - ReadWriteOnce resources: requests: storage: 25Gi # 最小磁盘空间需求 --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: pvc2 spec: volumeMode: Filesystem accessModes: - ReadWriteMany resources: requests: storage: 15Gi [root@master ~]# kubectl apply -f pv.yaml persistentvolume/pv-local created persistentvolume/pv-nfs created [root@master ~]# kubectl get persistentvolume NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv-local 30Gi RWO Retain Available 9s pv-nfs 20Gi RWO,ROX,RWX Retain Available 9s
[root@master ~]# vim web1.yaml --- kind: Pod apiVersion: v1 metadata: name: web1 spec: volumes: # 卷定义 - name: logdata # 卷名称 persistentVolumeClaim: # 通过PVC引用存储资源 claimName: pvc1 # PVC名称 - name: website persistentVolumeClaim: claimName: pvc2 containers: - name: nginx image: myos:nginx volumeMounts: - name: logdata mountPath: /usr/local/nginx/logs - name: website mountPath: /usr/local/nginx/html root@master ~]# kubectl apply -f web1.yaml pod/web1 created [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web1 1/1 Running 0 12s 10.244.240.138 node-0004 <none> <none> [root@master ~]# curl 10.244.240.138 nfs server
kubectl create configmap 名称 [选项/参数]
[root@master ~]# kubectl create configmap tz --from-literal 'TZ=Asia/Shanghai'
configmap/tz created
[root@master ~]# kubectl get configmaps
NAME DATA AGE
kube-root-ca.crt 1 7d2h
tz 1 8s
[root@master ~]# vim timezone.yaml
---
kind: ConfigMap
apiVersion: v1
metadata:
name: timezone
data:
TZ: Asia/Shanghai
[root@master ~]# kubectl apply -f timezone.yaml
configmap/timezone created
# 在 Pod 中增加 php 容器,与 nginx 共享同一块网卡 [root@master ~]# vim web1.yaml --- kind: Pod apiVersion: v1 metadata: name: web1 spec: volumes: - name: logdata persistentVolumeClaim: claimName: pvc1 - name: website persistentVolumeClaim: claimName: pvc2 containers: - name: nginx image: myos:nginx envFrom: - configMapRef: name: timezone volumeMounts: - name: logdata mountPath: /usr/local/nginx/logs - name: website mountPath: /usr/local/nginx/html - name: php # 以下为新增加内容 image: myos:php-fpm envFrom: # 不同容器需要单独配置时区 - configMapRef: name: timezone volumeMounts: - name: website # 不同容器需要单独挂载NFS mountPath: /usr/local/nginx/html [root@master ~]# kubectl delete pod web1 pod "web1" deleted [root@master ~]# kubectl apply -f web1.yaml pod/web1 created [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE web1 2/2 Running 0 5s [root@master ~]# kubectl exec -it web1 -c nginx -- ss -ltun Netid State Recv-Q Send-Q Local Address:Port ... ... tcp LISTEN 0 128 0.0.0.0:80 ... ... tcp LISTEN 0 128 127.0.0.1:9000 ... ...
# 使用 nginx 配置文件创建 configMap
[root@master ~]# kubectl cp -c nginx web1:/usr/local/nginx/conf/nginx.conf nginx.conf
[root@master ~]# vim nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# 使用命令创建 configMap
[root@master ~]# kubectl create configmap nginx-php --from-file=nginx.conf
configmap/nginx-php created
[root@master ~]# vim web1.yaml --- kind: Pod apiVersion: v1 metadata: name: web1 spec: volumes: - name: logdata persistentVolumeClaim: claimName: pvc1 - name: website persistentVolumeClaim: claimName: pvc2 - name: nginx-php # 卷名称 configMap: # 引用资源对象 name: nginx-php # 资源对象名称 containers: - name: nginx image: myos:nginx envFrom: - configMapRef: name: timezone volumeMounts: - name: nginx-php # 卷名称 subPath: nginx.conf # 键值(文件名称) mountPath: /usr/local/nginx/conf/nginx.conf # 路径 - name: logdata mountPath: /usr/local/nginx/logs - name: website mountPath: /usr/local/nginx/html - name: php image: myos:php-fpm envFrom: - configMapRef: name: timezone volumeMounts: - name: website mountPath: /usr/local/nginx/html
# 拷贝测试页面 s4/public/info.php [root@ecs-proxy s4]# rsync -av public/info.php 192.168.1.10:/var/webroot/info.php #------------------------------------------------------------ [root@master ~]# kubectl delete pod web1 pod "web1" deleted [root@master ~]# kubectl apply -f web1.yaml pod/web1 created [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE web1 2/2 Running 0 18s 10.244.3.17 node-0003 [root@master ~]# curl http://10.244.3.17/info.php <pre> Array ( [REMOTE_ADDR] => 10.244.0.0 [REQUEST_METHOD] => GET [HTTP_USER_AGENT] => curl/7.29.0 [REQUEST_URI] => /info.php ) php_host: web1 1229
Secret.data
字段时,所有键值对必须是经过base64编码的字符串kubectl create secret 子类型 名称 [选项/参数]
kubectl create secret generic 名称 [选项/参数]
kubectl create secret docker-registry 名称 [选项/参数]
kubectl create secret tls 名称 [选项/参数]
[root@master ~]# kubectl create secret generic tz --from-literal 'TZ=Asia/Shanghai'
secret/tz created
[root@master ~]# kubectl get secrets tz -o yaml
apiVersion: v1
data:
TZ: QXNpYS9TaGFuZ2hhaQ== # 经加密过的数据
kind: Secret
metadata:
creationTimestamp: "2024-02-28T11:16:50Z"
name: tz
namespace: default
resourceVersion: "648195"
uid: 21097fce-1632-4b00-b45e-0d257ec599d5
type: Opaque
[root@master ~]# kubectl create secret docker-registry harbor-auth --docker-server=harbor:443 --docker-username="用户名" --docker-password="密码"
secret/harbor-auth created
[root@master ~]# vim web2.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: web2
spec:
containers:
- name: apache
image: myos:httpd
imagePullSecrets:
- name: harbor-auth
[root@master ~]# kubectl apply -f web2.yaml
[root@master ~]# vim web2.yaml --- kind: Pod apiVersion: v1 metadata: name: web2 spec: imagePullSecrets: - name: harbor-auth volumes: # 卷配置 - name: cache # 卷名称 emptyDir: {} # 资源类型 containers: - name: apache image: harbor:443/myimg/httpd:latest volumeMounts: # 挂载卷 - name: cache # 卷名称 mountPath: /var/cache # 路径 [root@master ~]# kubectl delete pod web2 pod "web2" deleted [root@master ~]# kubectl apply -f web2.yaml pod/web2 created [root@master ~]# kubectl exec -it web2 -- bash [root@web2 html]# mount -l |grep cache /dev/vda1 on /var/cache type xfs (rw,relatime,attr2)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。