k8s入门命令(使用官方交互页面)

交互平台:
Interactive Tutorial - Creating a Cluster | Kubernetes
使用以下命令启动k8s

minikube start

使用kubectl get nodes 和 kubectl cluster-info查看节点和集群信息

在这里插入图片描述

基本操作:

部署应用:

kubectl create deployment kubernetes-bootcamp \
     --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8080

暴露端口:

kubectl expose deployment.apps/kubernetes-bootcamp \
     --type="NodePort" --port 8080

查看映射的端口并访问:
在这里插入图片描述
设置副本数:

kubectl scale deployment.apps/kubernetes-bootcamp --replicas=3

版本升级:

kubectl set image deployment.apps/kubernetes-bootcamp \
kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

查看Pod:
在这里插入图片描述

版本回滚:

kubectl rollout undo deployments/kubernetes-bootcamp

访问服务的方法:

使用vi将下面的配置信息粘贴过去即可

配置bootcamp1.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bootcamp1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: bootcamp
  template:
    metadata:
      labels:
        app: bootcamp
    spec:
      containers:
      - name: bootcamp1
        image: docker.io/jocatalin/kubernetes-bootcamp:v1
        ports:
        - containerPort: 8080

配置 bootcamp2.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bootcamp2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: bootcamp
  template:
    metadata:
      labels:
        app: bootcamp
    spec:
      containers:
      - name: bootcamp2
        image: docker.io/jocatalin/kubernetes-bootcamp:v2
        ports:
        - containerPort: 8080

配置svc.yml

apiVersion: v1
kind: Service
metadata:
  name: svc
spec:
  type: NodePort
  selector:
    app: bootcamp
  ports:
  - name: bootcamp
    protocol: TCP
    port: 80
    targetPort: 8080
    nodePort: 30000

启动pod和service:

kubectl create -f bootcamp1.yml
kubectl create -f bootcamp2.yml
kubectl create -f svc.yml

如果要删除对应的pod或者service,只需要kubectl delete 命令即可,例如:

kubectl delete svc/svc
kubectl delete -f svc.yml

获取pod的IP地址:

kubectl get pods -l app=bootcamp -o yaml | grep podIP

在这里插入图片描述
查看svc:
在这里插入图片描述
通过podIP访问服务:
在这里插入图片描述
通过clusterIP访问服务:
在这里插入图片描述
通过NodePort访问服务:
在这里插入图片描述
如果安装kube-dns组件的话,可以通过域名的形式直接访问服务。

数据持久化(PVC):

PV静态供给
使用vi将下面的配置信息粘贴过去即可

pv.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /home/disk
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - minikube

pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc
spec:
  resources:
    requests:
      storage: 1Gi
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage

运行命令:

kubectl apply -f pv.yml
kubectl apply -f pvc.yml

使用命令查看pv和pvc:
在这里插入图片描述
配置pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:   
  containers:
    - name: mypod1
      image: docker.io/jocatalin/kubernetes-bootcamp:v1
      volumeMounts:
        - mountPath: "/mydata"
          name: mydata
  volumes:
    - name: mydata
      persistentVolumeClaim: 
        claimName: pvc

运行命令:

mkidr /home/disk
kubectl apply -f pod.yml

查看Pod状态:
在这里插入图片描述
在挂载目录下写入文件:

kubectl exec mypod1 --  touch  /mydata/test

查看文件:

在这里插入图片描述

PV动态供给

查看StorageClass:

kubectl get storageclass.storage.k8s.io 

minikube有一个默认的StorageClass为standard
在这里插入图片描述
配置pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc
spec:
  resources:
    requests:
      storage: 1Gi
  accessModes:
  - ReadWriteMany
  storageClassName: standard

查看pv,pvc
在这里插入图片描述
配置并启动mypod1,mypod2,同上,其中将mypod2配置文件的metadata的name改为mypod2,再执行下次命令:

kubectl exec mypod1 --  touch  /mydata/test
kubectl exec mypod2 -- ls /mydata

结果如图所示,这样就实现了mypod1和mypod2的数据共享
在这里插入图片描述

Secret & ConfigMap

创建secret:

kubectl create secret generic mysecret --from-literal=password=123456

查看secret:

kubectl describe secret mysecret

在这里插入图片描述
创建configmap:

kubectl create configmap myconfmap --from-literal=user=admin

查看configmap:

kubectl describe configmap myconfmap

在这里插入图片描述
将Secret和ConfigMap应用到容器中:

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:   
  containers:
    - name: mypod1
      image: docker.io/jocatalin/kubernetes-bootcamp:v1
      volumeMounts:
        - mountPath: "/mysecret"
          name: mysecret
        - mountPath: "/myconf"
          name: myconf
  volumes:
    - name: mysecret
      secret:
        secretName: mysecret
        items:
        - key: password
          path: pass
    - name: myconf
      configMap:
        name: myconfmap
        items:
        - key: user
          path: user

从容器中查看Secret和ConfigMap信息:

kubectl exec mypod1 -- cat /mysecret/pass
kubectl exec mypod1 -- cat /myconf/user

在这里插入图片描述

健康检查:

Liveness探测:

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:
  restartPolicy: OnFailure   
  containers:
    - name: mypod1
      image: docker.io/jocatalin/kubernetes-bootcamp:v1
      args:
      - /bin/sh
      - -c
      - touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 100
      livenessProbe:
        exec:
          command:
          - cat
          - /tmp/healthy
        initialDelaySeconds: 5
        periodSeconds: 3

使用kubectl describe pod/mypod1查看Liveness事件
在这里插入图片描述

Readiness探测:

apiVersion: v1
kind: Pod
metadata:
  name: mypod2
spec:
  restartPolicy: OnFailure   
  containers:
    - name: mypod2
      image: docker.io/jocatalin/kubernetes-bootcamp:v1
      args:
      - /bin/sh
      - -c
      - touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 100
      readinessProbe:
        exec:
          command:
          - cat
          - /tmp/healthy
        initialDelaySeconds: 5
        periodSeconds: 3

使用kubectl describe pod/mypod2查看Readiness事件
在这里插入图片描述
参考资料:
k8s–五种控制器类型解析_长恋离亭的博客-CSDN博客_k8s kind 类型
k8s的持久化存储PV&&PVC - benjamin杨 - 博客园
k8s之PV、PVC、StorageClass详解 - 知乎
K8S-PD Pod 处于 Pending 状态 - 简书
Kubernetes K8S之存储PV-PVC详解_踏歌行的专栏-CSDN博客_k8s pv
Minikube体验 - Cocowool - 博客园
1 node(s) had volume node affinity conflict. - KubeSphere 开发者社区

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值