kubernetes学习笔记-集群管访问理篇

集群搭建:https://blog.csdn.net/u013271384/article/details/125507588

部署

命名为:deploy1:

kubectl create deployment deploy1 --image=nginx

查看

kubectl describe deployment  deploy1

kubectl get deploy

扩/缩 服务

kubectl scale --replicas=2 deploy/deploy1

kubectl scale --replicas=2 deployment/deploy1

使用上面的方法指定了两个副本
监视器查看

kubectl get rs

kubectl describe rs 

删除(监视器还在的情况下,删除pod,pod还会被重建)

kubectl delete deploy deploy1

配置查看

kubectl create deployment deploy1 --image=nginx --dry-run=client -o yaml

输出yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: deploy1
  name: deploy1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: deploy1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: deploy1
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

更新:

kubectl set image deployments deploy1 nginx=nginx:1.18 --record

查看部署历史:(–record指定后,会有注释显示出来)

kubectl rollout history deploy deploy1

回退

kubectl rollout undo deployment/deploy1

或 回退至指定版本:

kubectl rollout undo daemonset/deploy1 --to-revision=3

滚动更新策略
(默认启动一个pod,关闭一个pod,当要更新很多时,可以修改更新策略)
修改上面生成的yaml中的strategy: {}
创建10%个pod,再删除10%个pod

strategy:
  type: RollingUpdate                      # type值: RollingUpdate(默认,滚动更新),Recreate(重新创建)
  rollingUpdate:
    maxSurge: 10%                          #一次最多创建 10%的节点,可以直接写固定值10,表示一次性创建10个
    maxUnavailable: 0                      #最大不可用数量为0,要求更新的每个pod都可用,即先增加一个再减少一个节点

查看pod集群内ip

kubectl get pods -o wide

集群内pod访问

删除上面的pod,新增yaml,
运行命令输出yaml后修改kubectl create deployment deploy1 --image=nginx --dry-run=client -o=yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: deploy1
  name: deploy1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: deploy1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: deploy1
    spec:
      containers:
      - image: nginx
        name: nginx

上面保存成 deploy1.yaml后运行命令(默认情况下,Master Node不参与工作负载,即pod不能部署到master节点上,只能部署到node节点):

kubectl apply -f deploy1.yaml

kubectl create -f deploy1.yaml

查看pod状态

kubectl get pod -o wide

结果:

NAME                       READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
deploy1-74b8bd969b-6hx5z   1/1     Running   0          61m   10.244.1.8   node1   <none>           <none>
deploy1-74b8bd969b-w9x74   1/1     Running   0          61m   10.244.1.7   node1   <none>           <none>

内部访问成功:

curl http://10.244.1.7
1. 通过service
  1. 集群内部访问四种模式:
 clusterip      Create a ClusterIP service
 externalname   Create an ExternalName service 依赖云服务商
 loadbalancer   Create a LoadBalancer service 依赖云服务商
 nodeport       Create a NodePort service

kubectl create service clusterip deploy1 --tcp=80:80
  1. 集群外部访问
kubectl create service nodeport deploy1 --tcp=80:80 --node-port=30000

--node-port=0 用于把集群内node上的服务暴露出来,有效范围30000-32767。

查看yaml: kubectl create service nodeport deploy1 --tcp=80:80 --node-port=30000 --dry-run=client -o yaml

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: deploy1
  name: deploy1
spec:
  ports:
  - name: 80-80
    nodePort: 30000
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: deploy1
  type: NodePort
status:
  loadBalancer: {}

service跟据selectorlabel值为app: deploy1的deployment关联,默认的selector只能指定app:的标签,其他标签可以打印出yaml修改对应selector的值

2. 通过Ingress

https://blog.csdn.net/u013271384/article/details/126238695


配置 IPv4/IPv6 双协议栈
https://kubernetes.io/zh-cn/docs/concepts/services-networking/dual-stack/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值