<

1. Kubernetes介绍 #

bc263d89a4e90ce3a0902a5181d3baef

6376f226526ca6957a3cd33a4fa9435a

1.1 Pod #

pod

1.2 deployment #

1.3 Service #

2.部署 #

2.1 编写配置文件 #

mkdir deployment && cd deployment
vim deployment-user-v1.yaml
apiVersion: apps/v1  #API 配置版本
kind: Deployment     #资源类型
metadata:
  name: user-v1     #资源名称
spec:
  selector:
    matchLabels:
      app: user-v1 #告诉deployment根据规则匹配相应的Pod进行控制和管理,matchLabels字段匹配Pod的label值
  replicas: 3 #声明一个 Pod,副本的数量
  template:
    metadata:
      labels:
        app: user-v1 #Pod的名称
    spec:   #组内创建的 Pod 信息
      containers:
      - name: nginx #容器的名称
        image: registry.cn-beijing.aliyuncs.com/zhangrenyang/nginx:user-v1 #使用哪个镜像
        ports:
        - containerPort: 80 #容器内映射的端口

2.2 布署Pod #

kubectl apply -f deployment-user-v1.yaml
deployment.apps/user-v1 created
kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
user-8445fbf8d7-6f6d7       0/1     ContainerCreating   0          13s
user-8445fbf8d7-nggzv       0/1     ContainerCreating   0          13s
user-8445fbf8d7-xfn52       0/1     ContainerCreating   0          13s

2.3 Service #

service

2.3.1 配置文件 #

字段 说明
protocol 通信类型(TCP/UDP)
targetPort 原本 Pod 开放的端口
port Kubernetes容器之间互相访问的端口
type NodePort,Service的一种访问方式

user-service-v1.yaml

apiVersion: v1
kind: Service
metadata:
  name: service-user-v1
spec:
  selector:
    app: user-v1
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: NodePort

2.3.2 启动 #

kubectl apply -f user-service-v1.yaml
service/service-user-v1 created
kubectl get svc

可以在任何节点上访问

curl http://172.31.178.169:30859
curl http://172.31.178.170:30859

2.4 ingress #

ec4038a49d0f6eb986be1e5144fc6e41

#wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.34.1/deploy/static/provider/baremetal/deploy.yaml
wget https://img.zhufengpeixun.com/deploy.yaml

vi deploy.yaml d$

  namespace: ingress-nginx
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: http
+     nodePort: 31234
    - name: https
      port: 443
      protocol: TCP
      targetPort: https
+     nodePort: 31235

+ image: registry.cn-hangzhou.aliyuncs.com/bin_x/nginx-ingress:v0.34.1@sha256:80359bdf124d49264fabf136d2aecadac729b54f16618162194356d3c78ce2fe
kubectl -n ingress-nginx get svc
NAME                                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             NodePort    10.108.109.94   <none>        80:31234/TCP,443:31235/TCP   16m
ingress-nginx-controller-admission   ClusterIP   10.106.43.59    <none>        443/TCP                      16m
kubectl -n ingress-nginx get svc
NAME                                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             NodePort    10.108.109.94   <none>        80:31234/TCP,443:31235/TCP   17m
ingress-nginx-controller-admission   ClusterIP   10.106.43.59    <none>        443/TCP                      17m

vi ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - http:
      paths: 
       - path: /user
         backend:
          serviceName: user-service-v1
          servicePort: 80
       - path: /pay
         backend:
          serviceName: pay-service-v1
          servicePort: 80    
  backend:
     serviceName: user-service-v1
     servicePort: 80
kubectl apply -f ./ingress.yaml
curl http://172.16.244.93:31234/user
curl http://172.16.244.94:31234/user
curl http://172.16.244.93:31234/pay
curl http://172.16.244.94:31234/pay
kubectl  describe   ingress

3.参考 #

3.1 查看 #

//查看当前的deployment
kubectl get deploy 
//删除deploy 删除后ReplicateSet和pod也没有了
kubectl delete deploy nginx

//查看Replication Controller
kubectl get rc
//删除Replication Controller,删除后Pod也没有了
kubectl delete rc mysql

//查看pod
kubectl get pod
//删除pod
kubectl delete pod mysql-77w7z

//查看服务
kubectl get svc
//删除服务
kubectl delete service nginx

//查看pod详情
kubectl describe pod fail-1034443984-jerry

3.2 发布镜像 #

//登录阿里云Docker Registry
sudo docker login --username=hongqishiq@126.com registry.cn-beijing.aliyuncs.com
//从Registry中拉取镜像
docker pull registry.cn-beijing.aliyuncs.com/zhangrenyang/zhangrenyang:[镜像版本号]
//将镜像推送到Registry
docker login --username=hongqishiq@126.com registry.cn-beijing.aliyuncs.com
docker tag [ImageId] registry.cn-beijing.aliyuncs.com/zhangrenyang/zhangrenyang:[镜像版本号]
docker push registry.cn-beijing.aliyuncs.com/zhangrenyang/zhangrenyang:[镜像版本号]
docker login --username=hongqishiq@126.com registry.cn-beijing.aliyuncs.com
docker run -d -p 8080:80 nginx
docker exec -it 6764db063e37  bash
/usr/share/nginx/html
docker container commit -m"nginx-user-v1" -a"zhangrenyang" 6764db063e37  registry.cn-beijing.aliyuncs.com/zhangrenyang/nginx:user-v1
docker container commit -m"nginx-user-v2" -a"zhangrenyang" 6764db063e37  registry.cn-beijing.aliyuncs.com/zhangrenyang/nginx:user-v2
docker container commit -m"nginx-pay-v1" -a"zhangrenyang" 6764db063e37  registry.cn-beijing.aliyuncs.com/zhangrenyang/nginx:pay-v1
docker push registry.cn-beijing.aliyuncs.com/zhangrenyang/http-probe:1.0.0

3.3 链接 #

kubectl get ns