1. 拉取nginx离线包到本地
sudo docker save nginx:latest -o nginx.tar
2. 导入nginx image到k8s命名空间中
sudo ctr -n k8s.io images import nginx.tar
3. 编辑nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
注意 imagePullPolicy 是 IfNotPresent,使用本地镜像,k8s镜像的拉取策略为:
Always 总是拉取镜像
IfNotPresent 本地有则使用本地镜像,不拉取
Never 只使用本地镜像,从不拉取,即使本地没有
如果省略imagePullPolicy, 策略为always
4. apply yaml启动deployment
kubectl apply -f nginx.yaml
5. kubectl查看部署已经完成
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 5m52s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-54b6f7ddf9-vcl6k 1/1 Running 0 5m58s
nginx-deployment-54b6f7ddf9-x6j7z 1/1 Running 0 5m58s
6. 暴露nginx服务
$ kubectl expose deployment nginx-deployment --type=NodePort --port=80
service/nginx-deployment exposed
$ kubectl get svc nginx-deployment
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-deployment NodePort 10.102.52.246 <none> 80:31177/TCP 105s
访问31177端口即可访问nginx页面,至此部署完成