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/zhangyaohuang/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.yamlhtml
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
複製代碼
能夠在任何節點上訪問node
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 http://img.golderbrother.cn/deploy.yml
複製代碼
vi deploy.yaml d$mysql
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 鏡像並自動佈署 ingressnginx
kubectl apply -f deploy.yaml
複製代碼
查看 pods 的部署狀態git
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.yamlgithub
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/zhangyaohuang/zhangyaohuang:[鏡像版本號]
//將鏡像推送到Registry
docker login --username=hongqishiq@126.com registry.cn-beijing.aliyuncs.com
docker tag [ImageId] registry.cn-beijing.aliyuncs.com/zhangyaohuang/zhangyaohuang:[鏡像版本號]
docker push registry.cn-beijing.aliyuncs.com/zhangyaohuang/zhangyaohuang:[鏡像版本號]
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"zhangyaohuang" 6764db063e37 registry.cn-beijing.aliyuncs.com/zhangyaohuang/nginx:user-v1
docker container commit -m"nginx-user-v2" -a"zhangyaohuang" 6764db063e37 registry.cn-beijing.aliyuncs.com/zhangyaohuang/nginx:user-v2
docker container commit -m"nginx-pay-v1" -a"zhangyaohuang" 6764db063e37 registry.cn-beijing.aliyuncs.com/zhangyaohuang/nginx:pay-v1
docker push registry.cn-beijing.aliyuncs.com/zhangyaohuang/http-probe:1.0.0
複製代碼
kubectl get ns
複製代碼