mnubo / kubernetes-py

A python module for Kubernetes.
Apache License 2.0
123 stars 46 forks source link

Authentication error #72

Closed lucascozinheiro closed 7 years ago

lucascozinheiro commented 7 years ago

Hello,

I'm trying to use this python module to control the kubernetes system but I'm having some errors. Someone had the same before? Thank you.

administrator@matchbox:~/lucas$ python graph2.py /usr/lib/python2.7/dist-packages/urllib3/connectionpool.py:794: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html InsecureRequestWarning) Traceback (most recent call last): File "graph2.py", line 29, in deployment.create() File "/usr/local/lib/python2.7/dist-packages/kubernetes/K8sDeployment.py", line 52, in create self._wait_for_desired_replicas() File "/usr/local/lib/python2.7/dist-packages/kubernetes/K8sDeployment.py", line 78, in _wait_for_desired_replicas self.get() File "/usr/local/lib/python2.7/dist-packages/kubernetes/K8sDeployment.py", line 114, in get self.model = Deployment(self.get_model()) File "/usr/local/lib/python2.7/dist-packages/kubernetes/K8sObject.py", line 223, in get_model raise SyntaxError('K8sObject: name: [ {0} ] must be set to fetch the object.'.format(self.name)) SyntaxError: K8sObject: name: [ None ] must be set to fetch the object.

NickG123 commented 7 years ago

Looks to me like you're defining the deployment object without a name. Try something like the example...

from kubernetes import K8sDeployment

deployment = K8sDeployment(
    config=cfg_cert, 
    name='my_deployment',
    replicas=3
)
deployment.add_container(container)
deployment.create()
lucascozinheiro commented 7 years ago

@NickG123 Thank you. My code is similar but doesn't work. Look the beginning.

from kubernetes import K8sConfig from kubernetes import K8sContainer from kubernetes import K8sDeployment

cfg_default = K8sConfig(kubeconfig=None, api_host="console.titan.dev.inmarsat.com")

container = K8sContainer(name='nginx', image='nginx') container.add_port( container_port=80, host_port=80, name='nginx2017' )

deployment = K8sDeployment( config=cfg_default, name='my_deployment', replicas=3 ) deployment.add_container(container) deployment.create()

NickG123 commented 7 years ago

Interesting. When I run your code I get the following error:

kubernetes.K8sExceptions.UnprocessableEntityException: K8sObject: CREATE failed : HTTP 422 : Deployment.extensions "my_deployment" is invalid: metadata.name: Invalid value: "my_deployment": must match the regex [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*

Indicating that the underscore in the deployment name is invalid, which is interesting since that is what they use as a name in the example. But according to the kubernetes docs underscore is not a valid character. I'll make a pull request to update the docs.

I would try again without the underscore in the deployment name, and post again if you still have errors. Nick

lucascozinheiro commented 7 years ago

@NickG123 Doesn't work.. I tried without api_host argument and I had this error:

from kubernetes import K8sConfig from kubernetes import K8sContainer from kubernetes import K8sDeployment

cfg_default = K8sConfig(kubeconfig=None)

container = K8sContainer(name='nginx', image='nginx') container.add_port( container_port=80, host_port=80, name='nginx2017' )

deployment = K8sDeployment( config=cfg_default, name='mydeployment', replicas=3 ) deployment.add_container(container) deployment.create()

NickG123 commented 7 years ago

You will need the api_host argument, that's how the code finds your kubernetes server. Otherwise I believe it defaults to localhost.

Once I add my own api_host your code works fine for me. What version of kubernetes-py and kubernetes server are you using?

Nick

lucascozinheiro commented 7 years ago

I tried by this way now but doesn't still work... I'm using the last version of k-py 1.4.7.13 and about kubernetes 1.5.3

=======================================================

from kubernetes import K8sConfig from kubernetes import K8sContainer from kubernetes import K8sDeployment

cfg_default = K8sConfig(kubeconfig='/home/administrator/.kube/config', api_host="console.titan.dev.inmarsat.com")

container = K8sContainer(name='nginx', image='nginx') container.add_port( container_port=80, host_port=80, name='nginx2017' )

deployment = K8sDeployment( config=cfg_default, name='mydeployment', replicas=3 ) deployment.add_container(container) deployment.create()

The error now was:

Traceback (most recent call last): File "graph2.py", line 31, in deployment.create() File "/usr/local/lib/python2.7/dist-packages/kubernetes/K8sDeployment.py", line 49, in create super(K8sDeployment, self).create() File "/usr/local/lib/python2.7/dist-packages/kubernetes/K8sObject.py", line 266, in create raise UnauthorizedException(message) kubernetes.K8sExceptions.UnauthorizedException: K8sObject: CREATE failed : HTTP 401 : Unauthorized : {'status': {}, 'kind': 'Deployment', 'spec': {'replicas': 3, 'strategy': {'rollingUpdate': {'maxSurge': 1, 'maxUnavailable': 1}}, 'paused': False, 'template': {'spec': {'terminationGracePeriodSeconds': 30, 'dnsPolicy': 'Default', 'restartPolicy': 'Always', 'containers': [{'name': 'nginx', 'image': 'nginx', 'volumeMounts': [], 'env': [], 'imagePullPolicy': 'IfNotPresent', 'ports': [{'protocol': 'TCP', 'containerPort': 80, 'name': 'nginx2017', 'hostPort': 80}], 'resources': {'requests': {'cpu': '100m', 'memory': '32M'}}}]}, 'metadata': {'labels': {'name': 'mydeployment'}}}, 'selector': {'matchLabels': {'name': 'mydeployment'}}, 'minRedySeconds': 0}, 'apiVersion': 'extensions/v1beta1', 'metadata': {'labels': {'name': 'mydeployment'}, 'name': 'mydeployment'}}

NickG123 commented 7 years ago

I'm not a Kubernetes expert, but looks to me like you need to provide the certificates that your Kubernetes server is expecting.

You can alter your K8sConfig line to something like this in order to specify the cert files:

K8sConfig(kubeconfig='/home/administrator/.kube/config', api_host="console.titan.dev.inmarsat.com", certs=('apiserver.pem', 'apiserver-key.pem'))

I'm guessing where you find those files depends on how your Kubernetes cluster was set up. Personally I used Kubeadm, and the files were in '/etc/kubernetes/pki' on the master machine.

Copy them to your local folder (or somewhere else and provide the full path to them) and then try it. Nick

lucascozinheiro commented 7 years ago

But it not supposed those cert files are included on the .kube/conf file ? @NickG123 Thank you.

froch commented 7 years ago

This is not an issue with the kubernetes-py module, it's more of an issue with incorrect use of kubernetes. Closing this issue.