k8s部署docker容器

一、环境

需机器已部署好k8s和docker的环境

二、操作步骤

1、将制作好的镜像推送到docker仓库

docker tag nginx:test  harbor:test-nginx
docker push harbor:test-nginx

2、k8s创建命名空间

#nginx-test为本次实验创建的测试命名空间
kubectl create namespace nginx-test

3、创建secret

kubectl create secret docker-registry regcred --docker-server=harbor  --docker-username=yourname --docker-password=yourpassword -n nginx-test

控制台返回“secret/regcred created”表示创建成功

4、创建test-nginx服务的yaml文件

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: nginx-test
  labels:
    app: demo-service
spec:
  type: NodePort
  ports:
  - port: 8800
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: demo-pod
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: nginx-test
spec:
  selector:
    matchLabels:
      app: demo-pod
  replicas: 1
  template:
    metadata:
      labels:
        app: demo-pod
    spec:
      containers:
      - name: demo-container
        image: harbor:test-nginx  #本地私有镜像库的镜像名称+版本
        ports:
        - containerPort: 8800
           hostPort: 8800

5、启动pod

kubectl create -f test-nginx.yaml

6、查看pod

[root@k8s001 test-nginx]# kubectl get pod -n nginx-test
NAME                                READY   STATUS    RESTARTS   AGE
demo-deployment-6b7647579-9p5f7     1/1     Running   0          44m
nginx-deployment-59c577b48f-c5q6q   1/1     Running   0          14m

至此,完成docker容器部署。

k8s部署docker容器

上一篇:配置文件的读取添加webconfig


下一篇:Docker