kube-apiserver
kube-scheduler
kube-controller-manager
kube-apiserver
会处理集群的持久化数据并保存在etcd
中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 #容器内映射的端口
kubectl apply
代表准备对资源进行配置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
字段 | 说明 |
---|---|
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
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
cookie/header
的值去访问不同的服务ingress
#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
配置生效,拉取ingress镜像并自动布署ingress
kubectl apply -f deploy.yaml
查看pods的部署状态
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
//查看当前的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
//登录阿里云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
kubectl get ns