ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

精华 k8s 入门安装配置并部署nginx

2022-05-31 18:00:11  阅读:142  来源: 互联网

标签:kubectl 入门 deployment nginx controller ubuntu k8s root


k8s 搭建

1,关闭 swap 内存 确保禁止掉swap分区

K8s的要求,在每个宿主机上执行:

sudo swapoff -a

#修改/etc/fstab,注释掉swap那行,持久化生效
sudo vi /etc/fstab

安装Docker

apt update && apt install docker.io && systemctl start docker && systemctl enable docker
sudo apt-get update && sudo apt-get install -y ca-certificates curl software-properties-common apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF 
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

安装Kubelet kubeadm kubectl

apt-get update && apt-get install -y kubelet kubeadm kubectl
# 它是用来锁住这几个apt包的更新的,如果一旦手误更新了这些包,K8s集群就会因为版本不兼容挂了:
apt-mark hold kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

设置主机hostname

hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2
hostnamectl set-hostname k8s-node3

vi /etc/hosts

service-cidr和pod-network-cidr介绍

在用kubadm安装k8s时出现一个疑问,service-cidr和pod-network-cidr这个地址如何配置

参数说明

  • --apiserver-advertise-address=192.168.181.131 这个参数就是master主机的IP地址,例如我的Master主机的IP是:192.168.181.131

  • --image-repository=registry.aliyuncs.com/google_containers 这个是镜像地址,由于国外地址无法访问,故使用的阿里云仓库地址:registry.aliyuncs.com/google_containers

  • --kubernetes-version=v1.17.4 这个参数是下载的k8s软件版本号

  • -service-cidr=10.96.0.0/12 这个参数后的IP地址直接就套用10.96.0.0/12 ,以后安装时也套用即可,不要更改

  • --pod-network-cidr=10.244.0.0/16 k8s内部的pod节点之间网络可以使用的IP段,不能和service-cidr写一样,如果不知道怎么配,就先用这个10.244.0.0/16

service-cidr 的选取不能和PodCIDR及本机网络有重叠或者冲突。 一般可以选择一个本机网络和PodCIDR都没有用到的私网地址段,比如PODCIDR使用192.168.0.1/16, 那么service cidr可以选择172.16.0.1/20. 主机网段可以选10.1.0.1/8. 三者之间网络无重叠冲突即可。

Docker 和 kubelet 驱动不一致处理:https://www.cnblogs.com/hongdada/p/9771857.html

# 报错 docker 驱动 跟 kubelet 驱动不一致 解决方案; 
# kubelet cgroup driver: \"systemd\" is different from docker cgroup driver

sudo docker info|grep Cgroup
journalctl -f -u kubelet

初始化k8s 集群

kubeadm reset && kubeadm init \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
--kubernetes-version=v1.22.2 \
--pod-network-cidr=192.168.3.0/8 \
--service-cidr=10.96.0.0/16  \
--apiserver-advertise-address=192.168.2.129 \
--v=6 
# 查看节点状态
kubectl get node
NAME         STATUS     ROLES                  AGE   VERSION
k8s-master   NotReady   control-plane,master   10m   v1.22.2

# 注意复制下下列两项内容:
sudo mkdir -p $HOME/.kube && sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config && sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubeadm join 10.206.0.6:6443 --token jgzjsx.rpgeo2xsi4g88qvv --discovery-token-ca-cert-hash sha256:7c02cc3d7a3a4d881101129087a41876bd0dd3135e261b7624fc70f655db78a4

安装CNI

https://docs.projectcalico.org/getting-started/kubernetes/self-managed-onprem/onpremises

installing a pod network add-on
下面就是要安装Container Network Interface(CNI),这是必须的,不然Master Node 会处于NotReady状态,无法部署任何应用。也就是要先安装CNI才能部署pod.

kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml

各个节点加入Master:

kubeadm join 10.206.0.6:6443 --token z8b9l2.9etm9mskhzzrlfya \
	--discovery-token-ca-cert-hash sha256:2c07adb82773e53e0fc243cda29b165666c25d8cf4255eab7009d2c625bc3603 

查看各节点状态

root@k8s-master:/home/timeless# kubectl get nodes
NAME         STATUS   ROLES                  AGE   VERSION
k8s-master   Ready    control-plane,master   42m   v1.22.2
k8s-node1    Ready    <none>                 39m   v1.22.2
k8s-node2    Ready    <none>                 39m   v1.22.2
k8s-node3    Ready    <none>                 39m   v1.22.2

查看 namesace pod 系统应用 日志

kubectl -n kube-system logs  calico-node-2wkzd

部署 nginx 服务

1、创建nginx-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-controller
spec:
  replicas: 2
  selector:
    name: nginx
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80

2、创建 nginx-service-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service-nodeport
spec:
  ports:
    - port: 8000
      targetPort: 80
      protocol: TCP
  type: NodePort
  selector:
    name: nginx

3、创建pod

kubectl create -f nginx-rc.yaml

4、创建service

kubectl create -f nginx-service-nodeport.yaml

5、查看pod

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nginx-controller-7kt4z   0/1     ContainerCreating   0          18s
nginx-controller-wllwc   0/1     ContainerCreating   0          18s

pull image failed

root@k8s-master:/home/timeless# kubectl get pod
NAME                     READY   STATUS             RESTARTS   AGE
nginx-controller-cttqr   0/1     ImagePullBackOff   0          4m1s
nginx-controller-z69jv   0/1     ImagePullBackOff   0          4m1s
root@VM-0-6-ubuntu:/home/ubuntu# kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
nginx-controller-7kt4z   1/1       Running   1          1h
nginx-controller-wllwc    1/1       Running   1          1h
查看 pod 的共享命名空间的IP
root@k8s-master:/home/timeless# kubectl get pod nginx-deployment-748755bf57-778d2 --template={{.status.podIP}}
192.109.131.23

每个node 节点都可访问 每一个Pod 都拥有一个扁平化的共享空间IP

root@k8s-master:/home/timeless# curl 192.109.131.23
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
''''''''
</html>

查看集群节点状态:

# 查看集群状态
kubectl get cs
Get "http://127.0.0.1:10251/healthz": dial tcp 127.0.0.1:10251: connect: connection refused

自學k8s-kubeadm部署過程中遇到的dial tcp 127.0.0.1:10251: connect: connection refused錯誤

查看nginx 状态:

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get svc
NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes               ClusterIP   10.96.0.1       <none>        443/TCP          135m
nginx-service-nodeport   NodePort    10.96.132.209   <none>        8000:31857/TCP   36m

image-20211017172844238

kubectl rc 扩缩容:

root@VM-0-6-ubuntu:/home/ubuntu# kubectl scale rc nginx-controller --replicas=5
replicationcontroller/nginx-controller scaled

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get svc,pod,node
NAME                             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/kubernetes               ClusterIP   10.96.0.1       <none>        443/TCP          20h
service/nginx-service-nodeport   NodePort    10.96.132.209   <none>        8000:31857/TCP   18h

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-controller-9n5k6   1/1     Running   0          3m53s
pod/nginx-controller-dqvnf   1/1     Running   0          3m53s
pod/nginx-controller-qrnwc   1/1     Running   0          3m53s
pod/nginx-controller-w4wnh   1/1     Running   0          18h
pod/nginx-controller-wlngk   1/1     Running   0          18h

NAME                 STATUS   ROLES                  AGE   VERSION
node/vm-0-6-ubuntu   Ready    control-plane,master   20h   v1.22.2
node/vm-0-9-ubuntu   Ready    <none>                 20h   v1.22.2

查看集群详细信息

root@VM-0-6-ubuntu:/home/ubuntu# kubectl  get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP              NODE            NOMINATED NODE   READINESS GATES
nginx-controller-9n5k6   1/1     Running   0          23h   192.168.202.4   vm-0-9-ubuntu   <none>           <none>
nginx-controller-dqvnf   1/1     Running   0          23h   192.168.202.5   vm-0-9-ubuntu   <none>           <none>
nginx-controller-qrnwc   1/1     Running   0          23h   192.168.202.6   vm-0-9-ubuntu   <none>           <none>
nginx-controller-w4wnh   1/1     Running   0          41h   192.168.202.3   vm-0-9-ubuntu   <none>           <none>
nginx-controller-wlngk   1/1     Running   0          41h   192.168.202.2   vm-0-9-ubuntu   <none>           <none>

查看pod 的 label

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get pod --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx-controller-9n5k6   1/1     Running   0          27h   name=nginx
nginx-controller-dqvnf   1/1     Running   0          27h   name=nginx
nginx-controller-qrnwc   1/1     Running   0          27h   name=nginx
nginx-controller-w4wnh   1/1     Running   0          46h   name=nginx
nginx-controller-wlngk   1/1     Running   0          46h   name=nginx

根据label 查找 Pod

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get pod -l name=nginx
NAME                     READY   STATUS    RESTARTS   AGE
nginx-controller-9n5k6   1/1     Running   0          27h
nginx-controller-dqvnf   1/1     Running   0          27h
nginx-controller-qrnwc   1/1     Running   0          27h
nginx-controller-w4wnh   1/1     Running   0          46h
nginx-controller-wlngk   1/1     Running   0          46h

创建deployment

查看 deployment yaml语法格式

root@VM-0-6-ubuntu:/home/ubuntu# kubectl explain deployment.apiVersion
KIND:     Deployment
VERSION:  apps/v1

FIELD:    apiVersion <string>

DESCRIPTION:
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
root@VM-0-6-ubuntu:/home/ubuntu# vi nginx-deployment.yaml 
root@VM-0-6-ubuntu:/home/ubuntu# kubectl explain deployment.apiVersion
KIND:     Deployment
VERSION:  apps/v1

FIELD:    apiVersion <string>

DESCRIPTION:
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

创建deployment:

#test-pod 
apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中   
kind: Pod #指定创建资源的角色/类型   
metadata: #资源的元数据/属性   
  name: test-pod #资源的名字,在同一个namespace中必须唯一   
  labels: #设定资源的标签 
    k8s-app: apache   
    version: v1   
    kubernetes.io/cluster-service: "true"   
  annotations:            #自定义注解列表   
    - name: String        #自定义注解名字   
spec: #specification of the resource content 指定该资源的内容   
  restartPolicy: Always #表明该容器一直运行,默认k8s的策略,在此容器退出后,会立即创建一个相同的容器   
  nodeSelector:     #节点选择,先给主机打标签kubectl label nodes kube-node1 zone=node1   
    zone: node1   
  containers:   
  - name: test-pod #容器的名字   
    image: 10.192.21.18:5000/test/chat:latest #容器使用的镜像地址   
    imagePullPolicy: Never #三个选择Always、Never、IfNotPresent,每次启动时检查和更新(从registery)images的策略, 
                           # Always,每次都检查 
                           # Never,每次都不检查(不管本地是否有) 
                           # IfNotPresent,如果本地有就不检查,如果没有就拉取 
    command: ['sh'] #启动容器的运行命令,将覆盖容器中的Entrypoint,对应Dockefile中的ENTRYPOINT   
    args: ["$(str)"] #启动容器的命令参数,对应Dockerfile中CMD参数   
    env: #指定容器中的环境变量   
    - name: str #变量的名字   
      value: "/etc/run.sh" #变量的值   
    resources: #资源管理 
      requests: #容器运行时,最低资源需求,也就是说最少需要多少资源容器才能正常运行   
        cpu: 0.1 #CPU资源(核数),两种方式,浮点数或者是整数+m,0.1=100m,最少值为0.001核(1m) 
        memory: 32Mi #内存使用量   
      limits: #资源限制   
        cpu: 0.5   
        memory: 1000Mi   
    ports:   
    - containerPort: 80 #容器开发对外的端口 
      name: httpd  #名称 
      protocol: TCP   
    livenessProbe: #pod内容器健康检查的设置 
      httpGet: #通过httpget检查健康,返回200-399之间,则认为容器正常   
        path: / #URI地址   
        port: 80   
        #host: 127.0.0.1 #主机地址   
        scheme: HTTP   
      initialDelaySeconds: 180 #表明第一次检测在容器启动后多长时间后开始   
      timeoutSeconds: 5 #检测的超时时间   
      periodSeconds: 15  #检查间隔时间   
      #也可以用这种方法   
      #exec: 执行命令的方法进行监测,如果其退出码不为0,则认为容器正常   
      #  command:   
      #    - cat   
      #    - /tmp/health   
      #也可以用这种方法   
      #tcpSocket: //通过tcpSocket检查健康    
      #  port: number    
    lifecycle: #生命周期管理   
      postStart: #容器运行之前运行的任务   
        exec:   
          command:   
            - 'sh'   
            - 'yum upgrade -y'   
      preStop:#容器关闭之前运行的任务   
        exec:   
          command: ['service httpd stop']   
    volumeMounts:  #挂载持久存储卷 
    - name: volume #挂载设备的名字,与volumes[*].name 需要对应     
      mountPath: /data #挂载到容器的某个路径下   
      readOnly: True   
  volumes: #定义一组挂载设备   
  - name: volume #定义一个挂载设备的名字   
    #meptyDir: {}   
    hostPath:   
      path: /opt #挂载设备类型为hostPath,路径为宿主机下的/opt,这里设备类型支持很多种 
    #nfs

创建一个yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.10
        ports:
        - containerPort: 80

执行创建deployment

root@VM-0-6-ubuntu:/home/ubuntu# kubectl create -f nginx-deployment.yaml
deployment.apps "nginx-deployment" created
root@VM-0-6-ubuntu:/home/ubuntu# kubectl get deployment

查看pod 标签:

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get pod --show-labels
NAME                               READY   STATUS    RESTARTS   AGE   LABELS
nginx-controller-9n5k6             1/1     Running   0          28h   name=nginx
nginx-controller-dqvnf             1/1     Running   0          28h   name=nginx
nginx-controller-qrnwc             1/1     Running   0          28h   name=nginx
nginx-controller-w4wnh             1/1     Running   0          46h   name=nginx
nginx-controller-wlngk             1/1     Running   0          46h   name=nginx
nginx-deployment-897f8f586-htc7n   1/1     Running   0          21m   app=nginx,pod-template-hash=897f8f586
nginx-deployment-897f8f586-lt5zr   1/1     Running   0          21m   app=nginx,pod-template-hash=897f8f586
nginx-deployment-897f8f586-p2npp   1/1     Running   0          21m   app=nginx,pod-template-hash=897f8f586

通过标签查找 Pod

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get pod -l app=nginx
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-897f8f586-htc7n   1/1     Running   0          23m
nginx-deployment-897f8f586-lt5zr   1/1     Running   0          23m
nginx-deployment-897f8f586-p2npp   1/1     Running   0          24m

查看 deployment 创建过程:

Deployment 管理的是replicaset-controller,RC会创建Pod。Pod自身会下载镜像并启动镜像

root@VM-0-6-ubuntu:/home/ubuntu# kubectl describe rs nginx-deployment
Name:           nginx-deployment-748755bf57
Namespace:      default
Selector:       app=nginx,pod-template-hash=748755bf57
Labels:         app=nginx
                pod-template-hash=748755bf57
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 4
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/nginx-deployment
Replicas:       0 current / 0 desired
Pods Status:    0 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
           pod-template-hash=748755bf57
  Containers:
   nginx:
    Image:        nginx:1.10
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:           <none>

Name:           nginx-deployment-897f8f586
Namespace:      default
Selector:       app=nginx,pod-template-hash=897f8f586
Labels:         app=nginx
                pod-template-hash=897f8f586
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 4
                deployment.kubernetes.io/revision: 2
Controlled By:  Deployment/nginx-deployment
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
           pod-template-hash=897f8f586
  Containers:
   nginx:
    Image:        nginx:1.11
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:           <none>

升级nginx镜像

root@VM-0-6-ubuntu:/home/ubuntu#  kubectl set image deploy/nginx-deployment nginx=nginx:1.11
deployment.apps "nginx-deployment" image updated
root@VM-0-6-ubuntu:/home/ubuntu# kubectl exec -it nginx-deployment-897f8f586-htc7n bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-897f8f586-htc7n:/# nginx -v
nginx version: nginx/1.11.13

ctrl+ D 退出

升级镜像的过程是逐步进行的,pod不会一下子全部关闭,而是一个一个升级

查看发布过程

root@nginx-deployment-897f8f586-htc7n:/#  kubectl rollout status deploy/nginx-deployment
deployment "nginx-deployment" successfully rolled out

查看Deployment 历史版本

root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout history deploy/nginx-deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
# 显示历史有两个版本 查看两个版本

root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout history deploy/nginx-deployment --revision=1
deployment.apps/nginx-deployment with revision #1
Pod Template:
  Labels:	app=nginx
	pod-template-hash=748755bf57
  Containers:
   nginx:
    Image:	nginx:1.10
    Port:	80/TCP
    Host Port:	0/TCP
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout history deploy/nginx-deployment --revision=2
deployment.apps/nginx-deployment with revision #2
Pod Template:
  Labels:	app=nginx
	pod-template-hash=897f8f586
  Containers:
   nginx:
    Image:	nginx:1.11
    Port:	80/TCP
    Host Port:	0/TCP
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>


编辑deployment

修改nginx 版本为 1.12

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "2"
  creationTimestamp: "2021-10-19T07:08:12Z"
  generation: 2
  name: nginx-deployment
  namespace: default
  resourceVersion: "226930"
  uid: 008b5b22-ceeb-454d-bed3-d3bd7f17476d
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.12
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}

查看升级过程

root@VM-0-6-ubuntu:/home/ubuntu# kubectl edit deploy/nginx-deployment
deployment.apps/nginx-deployment edited
root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout status deploy/nginx-deployment
deployment "nginx-deployment" successfully rolled out
root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout history deploy/nginx-deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>
root@VM-0-6-ubuntu:/home/ubuntu# kubectl  exec -it  nginx-deployment-f77774fc5-2b7f9 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-f77774fc5-2b7f9:/# nginx -v
nginx version: nginx/1.12.2

扩容/缩容(指定--replicas的数量)

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get pod -l app=nginx
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-f77774fc5-2b7f9   1/1     Running   0          6m38s
nginx-deployment-f77774fc5-hh8kj   1/1     Running   0          6m59s
nginx-deployment-f77774fc5-xmzrk   1/1     Running   0          6m36s
root@VM-0-6-ubuntu:/home/ubuntu# kubectl scale deploy/nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled
root@VM-0-6-ubuntu:/home/ubuntu#  kubectl rollout status deploy/nginx-deployment
deployment "nginx-deployment" successfully rolled out
root@VM-0-6-ubuntu:/home/ubuntu# kubectl get pod -l app=nginx
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-f77774fc5-26nm6   1/1     Running   0          19s
nginx-deployment-f77774fc5-2b7f9   1/1     Running   0          7m41s
nginx-deployment-f77774fc5-hh8kj   1/1     Running   0          8m2s
nginx-deployment-f77774fc5-hklrh   1/1     Running   0          19s
nginx-deployment-f77774fc5-xmzrk   1/1     Running   0          7m39s

创建Service 提供对外访问接口

修改nodePort 端口范围

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  ports:
  - port: 88
    targetPort: 80
  selector:
    app: nginx


####
apiVersion: 指定版本
kind: 类型
name: 指定服务名称
labels: 标签
port: Service 服务暴露的端口
targetPort: 容器暴露的端口
seletor: 关联的Pod的标签

创建service

root@VM-0-6-ubuntu:/home/ubuntu# kubectl create -f nginx-service.yaml
service/nginx-service created
root@VM-0-6-ubuntu:/home/ubuntu# kubectl get svc/nginx-service
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
nginx-service   ClusterIP   10.96.36.156   <none>        88/TCP    16s

查看service

访问nginx 服务,访问Pod是有负载均衡的

root@VM-0-6-ubuntu:/home/ubuntu# kubectl get svc/nginx-service
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
nginx-service   ClusterIP   10.96.36.156   <none>        88/TCP    64s
root@VM-0-6-ubuntu:/home/ubuntu# curl 10.96.36.156:88
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

对service的描述

root@VM-0-6-ubuntu:/home/ubuntu# kubectl describe svc/nginx-service
Name:              nginx-service
Namespace:         default
Labels:            app=nginx
Annotations:       <none>
Selector:          app=nginx
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.96.36.156
IPs:               10.96.36.156
Port:              <unset>  88/TCP
TargetPort:        80/TCP
Endpoints:         192.168.202.13:80,192.168.202.14:80,192.168.202.15:80 + 2 more...
Session Affinity:  None
Events:            <none>

进入容器查看:

root@VM-0-6-ubuntu:/home/ubuntu# kubectl exec nginx-deployment-897f8f586-htc7n  -it  bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-897f8f586-htc7n:/# nginx -v
nginx version: nginx/1.11.13

回滚到之前的版本:

root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout history deploy/nginx-deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>

查看前一个版本 版本 和 配置

kubectl rollout history --help
View previous rollout revisions and configurations.
root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout history deploy/nginx-deployment --revision=3
deployment.apps/nginx-deployment with revision #3
Pod Template:
  Labels:	app=nginx
	pod-template-hash=f77774fc5
  Containers:
   nginx:
    Image:	nginx:1.12
    Port:	80/TCP
    Host Port:	0/TCP
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

回滚到上个版本:

root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout undo deploy/nginx-deployment
deployment.apps/nginx-deployment rolled back
root@VM-0-6-ubuntu:/home/ubuntu# kubectl describe deploy/nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 19 Oct 2021 15:08:12 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 4
Selector:               app=nginx
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.11
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-897f8f586 (5/5 replicas created)
Events:
  Type    Reason             Age                 From                   Message
  ----    ------             ----                ----                   -------
  Normal  ScalingReplicaSet  42m                 deployment-controller  Scaled up replica set nginx-deployment-f77774fc5 to 1
  Normal  ScalingReplicaSet  42m                 deployment-controller  Scaled down replica set nginx-deployment-897f8f586 to 2
  Normal  ScalingReplicaSet  42m                 deployment-controller  Scaled up replica set nginx-deployment-f77774fc5 to 2
  Normal  ScalingReplicaSet  42m                 deployment-controller  Scaled up replica set nginx-deployment-f77774fc5 to 3
  Normal  ScalingReplicaSet  42m                 deployment-controller  Scaled down replica set nginx-deployment-897f8f586 to 1
  Normal  ScalingReplicaSet  42m                 deployment-controller  Scaled down replica set nginx-deployment-897f8f586 to 0
  Normal  ScalingReplicaSet  35m                 deployment-controller  Scaled up replica set nginx-deployment-f77774fc5 to 5
  Normal  ScalingReplicaSet  80s                 deployment-controller  Scaled down replica set nginx-deployment-f77774fc5 to 4
  Normal  ScalingReplicaSet  80s (x2 over 118m)  deployment-controller  Scaled up replica set nginx-deployment-897f8f586 to 3
  Normal  ScalingReplicaSet  80s (x2 over 118m)  deployment-controller  Scaled up replica set nginx-deployment-897f8f586 to 2
  Normal  ScalingReplicaSet  78s                 deployment-controller  Scaled down replica set nginx-deployment-f77774fc5 to 3
  Normal  ScalingReplicaSet  78s                 deployment-controller  Scaled up replica set nginx-deployment-897f8f586 to 4
  Normal  ScalingReplicaSet  78s                 deployment-controller  Scaled down replica set nginx-deployment-f77774fc5 to 2
  Normal  ScalingReplicaSet  78s                 deployment-controller  Scaled up replica set nginx-deployment-897f8f586 to 5
  Normal  ScalingReplicaSet  78s                 deployment-controller  Scaled down replica set nginx-deployment-f77774fc5 to 1
  Normal  ScalingReplicaSet  74s                 deployment-controller  Scaled down replica set nginx-deployment-f77774fc5 to 0

回滚到指定版本:

root@VM-0-6-ubuntu:/home/ubuntu# kubectl rollout undo deploy/nginx-deployment --to-revision=1
deployment.apps/nginx-deployment rolled back
root@VM-0-6-ubuntu:/home/ubuntu# kubectl describe deploy/nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 19 Oct 2021 15:08:12 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 5
Selector:               app=nginx
Replicas:               5 desired | 5 updated | 6 total | 4 available | 2 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.10

参考文档:

https://zhuanlan.zhihu.com/p/138554103

https://www.cnblogs.com/fisherbook/p/14277388.html

kubeadm创建集群 https://blog.csdn.net/qq_44851613/article/details/120589180

https://www.jianshu.com/p/f2d4dd4d1fb1

https://www.m690.com/archives/1160/

Docker中的Cgroup Driver:Cgroupfs 与 Systemd

官方文档:

https://kubernetes.io/zh/docs/reference/command-line-tools-reference/kubelet/

较好文档:

https://blog.51cto.com/u_13760351/2504882

Kubernetes 高可用集群

kubectl get 报错:possibly because of "crypto/rsa: verification error" wkubernetes"

标签:kubectl,入门,deployment,nginx,controller,ubuntu,k8s,root
来源: https://www.cnblogs.com/timelesszhuang/p/k8s-install.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有